数据迁移
软件的维护是很繁重的工作,你可能不时的需要给你的数据库打补丁,比如新加、修改或者删除一些字段。用户Pop来管理数据迁移会比较简单一些。
我们可以用fizz创建一个数据迁移,fizz是一种描述数据库变动的语言。
描述迁移
soda 是嵌入在buffalo中的,可以独立使用,来代替使用 buffalo pop。
Fizz
运行fizz创建数据迁移的时候,会产生up和down两个文件。
$ soda generate fizz name_of_migration
运行上面的命令,将产生两个空的文件:
/migrations/20160815134952_name_of_migration.up.fizz
./migrations/20160815134952_name_of_migration.down.fizz
关于用法,可以使用help查看
$ soda g migration --help
Generates Up/Down migrations for your database using fizz.
Usage:
soda generate fizz [name] [flags]
Aliases:
fizz, migration
Flags:
-h, --help help for fizz
Global Flags:
-c, --config string The configuration file you would like to use.
-d, --debug Use debug/verbose mode
-e, --env string The environment you want to run migrations against. Will use $GO_ENV if set. (default "development")
-p, --path string Path to the migrations folder (default "./migrations")
默认会产生三个字段,UUID格式的id主键字段,datetime格式的created_at和updated_at字段。当然如果需要,你可以覆盖他。
SQL
如果你不想使用fizz,或者你有负责的语句要执行,也可以使用sql语句。
$ soda generate sql name_of_migration
将生成两个文件
./migrations/20160815134952_name_of_migration.up.sql
./migrations/20160815134952_name_of_migration.down.sql
可以通过help查看sql命令的用法
$ soda g sql --help
Generates Up/Down migrations for your database using SQL.
Usage:
soda generate sql [name] [flags]
Flags:
-h, --help help for sql
Global Flags:
-c, --config string The configuration file you would like to use.
-d, --debug Use debug/verbose mode
-e, --env string The environment you want to run migrations against. Will use $GO_ENV if set. (default "development")
-p, --path string Path to the migrations folder (default "./migrations")
运行迁移
迁移
一旦迁移语句写完了,就可以执行迁移了。
$ soda migrate
$ soda migrate up
这两个命令是一样的,第一个是第二个的缩写。
回滚
当迁移完成发现一些问题的话,可以回滚。
soda migrate down
该命令的更多用户可以通过help查看
$ soda migrate --help
Runs migrations against your database.
Usage:
soda migrate [flags]
soda migrate [command]
Aliases:
migrate, m
Available Commands:
down Apply one or more of the 'down' migrations.
reset The equivalent of running `migrate down` and then `migrate up`
status Displays the status of all migrations.
up Apply all of the 'up' migrations.
Flags:
-h, --help help for migrate
Global Flags:
-c, --config string The configuration file you would like to use.
-d, --debug Use debug/verbose mode
-e, --env string The environment you want to run migrations against. Will use $GO_ENV if set. (default "development")
-p, --path string Path to the migrations folder (default "./migrations")
Use "soda migrate [command] --help" for more information about a command.
自定义迁移记录表
默认情况下,应用的迁移记录记录在schema_migration表中。如果数据库中没有这个表,pop会自动创建他。
有时候我们需要修改跟踪迁移记录的表名。在pop v4.5.0版本之后是可以自定义的。
development:
dialect: "postgres"
url: "your_db_development"
options:
migration_table_name: migrations
部署的时候使用迁移
buffalo build后的二进制文件是包括了迁移文件的。所以在部署的时候,可以直接迁移。
$ ./myapp migrate
DEBU[2018-01-12T06:14:20Z] select count(*) as row_count from (SELECT schema_migration.* FROM schema_migration AS schema_migration WHERE version = ?) a $1=20171213171622
DEBU[2018-01-12T06:14:20Z] select count(*) as row_count from (SELECT schema_migration.* FROM schema_migration AS schema_migration WHERE version = ?) a $1=20171213172104
DEBU[2018-01-12T06:14:20Z] select count(*) as row_count from (SELECT schema_migration.* FROM schema_migration AS schema_migration WHERE version = ?) a $1=20171213172249
DEBU[2018-01-12T06:14:20Z] select count(*) as row_count from (SELECT schema_migration.* FROM schema_migration AS schema_migration WHERE version = ?) a $1=20171213173148
DEBU[2018-01-12T06:14:20Z] select count(*) as row_count from (SELECT schema_migration.* FROM schema_migration AS schema_migration WHERE version = ?) a $1=20171219070903
DEBU[2018-01-12T06:14:20Z] select count(*) as row_count from (SELECT schema_migration.* FROM schema_migration AS schema_migration WHERE version = ?) a $1=20171219071524
0.0010 seconds