-- +goose Up -- SQL in section 'Up' is executed when this migration is applied CREATETABLE IF NOTEXISTS test ( id serial, con text );
-- +goose Down -- SQL section 'Down' is executed when this migration is rolled back DROPTABLE test;
注 sql语句以分号结束,不然可能无法识别。
Up
执行goose up就是执行脚本Up区域的内容,所有可用的迁移脚本都将被执行。
在上面的例子中,执行goose up得到结果:
1 2 3 4
goose: migrating db environment 'development', current version: 0, target: 20161130232902 Up Out ... OK 20161130232822_basic_go.go OK 20161130232902_basic_sql.sql
# 结果 goose: migrating db environment 'production', current version: 0, target: 20161130232902 Up Out ... OK 20161130232822_basic_go.go OK 20161130232902_basic_sql.sql
# 结果 goose: created /Users/trustasia/dev/go/src/demo_mi/goose/db/migrations/20161130235504_add_cloumn.sql
# 写上迁移语句 -- +goose Up -- SQL in section 'Up' is executed when this migration is applied ALTER TABLE test ADD new_con text;
-- +goose Down -- SQL section 'Down' is executed when this migration is rolled back ALTER TABLE test DROP COLUMN new_con;
# 指定脚本迁移 goose goose up add_cloumn # 结果 goose: migrating db environment 'development', current version: 20161130232902, target: 20161130235504 OK 20161130235504_add_cloumn.sql
Down
执行goose down 向前回滚一个版本。
在上面的例子中,我们回滚。
1 2 3 4
goose down # 结果 goose: migrating db environment 'development', current version: 20161130235504, target: 20161130232902 OK 20161130235504_add_cloumn.sql
我们也可以指定环境,或指定脚本来回滚。
1 2 3 4 5 6 7 8 9 10 11
# 指定生产环境回滚 goose -env production down # 结果 goose: migrating db environment 'production', current version: 20161130232902, target: 20161130232822 OK 20161130232902_basic_sql.sql
# 制定版本回滚 goose down basic_sql # 结果 goose: migrating db environment 'development', current version: 20161130232902, target: 20161130232822 OK 20161130232902_basic_sql.sql
redo
向上回滚一个版本,然后重新执行一次上次的操作。
1 2 3 4 5 6 7 8
goose redo
goose: migrating db environment 'development', current version: 20161130232822, target: 0 Down Out ... OK 20161130232822_basic_go.go goose: migrating db environment 'development', current version: 0, target: 20161130232822 Up Out ... OK 20161130232822_basic_go.go