Goose
Go 語言下,好用的資料庫遷移工具。特別是跟同事協同開發時,做好資料庫版本管理的好幫手。除了用 SQL 外,你也可以自己產生對映的 Go 程式。
Goose is a database migration tool. Manage your database schema by creating incremental SQL changes or Go functions.
github repo. (程式庫連結)
https://github.com/pressly/goose
Install (安裝)
$ go get -u github.com/pressly/goose/cmd/goose
Usage (語法)
kz@KZ ~/Blogger/bloggerfiles (master) $ goose Usage: goose [OPTIONS] DRIVER DBSTRING COMMAND Drivers: postgres mysql sqlite3 redshift Examples: goose sqlite3 ./foo.db status goose sqlite3 ./foo.db create init sql goose sqlite3 ./foo.db create add_some_column sql goose sqlite3 ./foo.db create fetch_user_data go goose sqlite3 ./foo.db up goose postgres "user=postgres dbname=postgres sslmode=disable" status goose mysql "user:password@/dbname" status goose redshift "postgres://user:password@qwerty.us-east-1.redshift.amazonaws.com:5439/db" status Options: -dir string directory with migration files (default ".") Commands: up Migrate the DB to the most recent version available up-to VERSION Migrate the DB to a specific VERSION down Roll back the version by 1 down-to VERSION Roll back to a specific VERSION redo Re-run the latest migration status Dump the migration status for the current DB version Print the current version of the database create NAME [sql|go] Creates new migration file with next version
Create (建立遷移)
Create a new SQL migration.如果是第一次使用 goose 版本應為 0.
If this is your first time running goose, the following version number of goose should be 0.
$ goose mysql "username:password@tcp(127.0.0.1:3306)/testdb?parseTime=true" version goose: version 0
建立遷移
Create a new SQL migration.
$ goose create add_some_column sql Created new file: 00001_add_some_column.sql
下指令可以看到目錄下 goose 產生的檔案
kz@KZ ~/Blogger/bloggerfiles/migration (master %) $ ls -alh total 8 drwxr-xr-x 3 kz staff 96B Nov 4 00:22 . drwxr-xr-x 6 kz staff 192B Nov 4 00:13 .. -rw-r--r-- 1 kz staff 165B Nov 4 00:22 00001_add_some_column.sql
編輯剛產生的 00001_add_some_column.sql
可以看到 goose 幫我們產生的 template
加一些我們想要的語法進去
Up (套用遷移)
Apply all available migrations.成功的話,可以看到回傳 OK & 版本號
$ goose mysql "username:password@tcp(127.0.0.1:3306)/testdb?parseTime=true" up OK 00001_add_some_column.sql goose: no migrations to run. current version: 1
Status - After Up command (顯示 Up 後的遷移狀態)
Print the status of all migrations.$ goose mysql "username:password@tcp(127.0.0.1:3306)/testdb?parseTime=true" status Applied At Migration ======================================= Sat Nov 4 00:52:16 2017 -- 00001_add_some_column.sql
Version - After Up command (顯示 Up 後的遷移版本)
Print the current version of the database$ goose mysql "username:password@tcp(127.0.0.1:3306)/testdb?parseTime=true" version goose: version 1
down (覆原遷移)
Roll back a single migration from the current version.成功的話,可以看到回傳 OK & 版本號
$ goose mysql "username:password@tcp(127.0.0.1:3306)/testdb?parseTime=true" down OK 00001_add_some_column.sql
Status - After Down command (顯示 Down 後的遷移狀態)
Print the status of all migrations.$ goose mysql "username:password@tcp(127.0.0.1:3306)/testdb?parseTime=true" status Applied At Migration ======================================= Pending -- 00001_add_some_column.sql
Version - After Down command (顯示 Down 後的遷移版本)
Print the current version of the database$ goose mysql "username:password@tcp(127.0.0.1:3306)/testdb?parseTime=true" version goose: version 0
就這樣,是不是很方便~
That's it.
留言