ikeikeikeike's unk blog.

http://github-awards.com/users/ikeikeikeike

beego migration

なんかbee new で新しくprojectを作るとmigrationフォルダができるけどなんか使いづらかったので gooseを替わりに使用した

Download

$ go get bitbucket.org/liamstask/goose/cmd/goose    

db/dbconf.ymlを作成&編集

$ cat db/dbconf.yml
development:
    driver: postgres
    open: postgres://user:password@develop.local:5432/tablename

production:
    driver: postgres
    open: postgres://user:password@127.0.0.1:5432/tablename

environment_variable_config:
    driver: $DB_DRIVER
    open: $DATABASE_URL

'entry'.q カラムにginインデックスをはる

$ goose create InstallPgExtension sql
goose: created /path/to/gopath/src/example.com/ikeikeikeike/example/db/migrations/20150130234510_InstallPgExtension.sql
$ goose create AddGinIndexToQOnEntry sql
goose: created /path/to/gopath/src/example.com/ikeikeikeike/example/db/migrations/20150130234522_AddGinIndexToQOnEntry.sql

20150130234510_InstallPgExtension.sql の中身

-- +goose Up
-- SQL in section 'Up' is executed when this migration is applied
CREATE EXTENSION IF NOT EXISTS pg_trgm;


-- +goose Down
-- SQL section 'Down' is executed when this migration is rolled back
DROP EXTENSION IF EXISTS pg_trgm;

20150130234522_AddGinIndexToQOnEntry.sql の中身

-- +goose Up
-- SQL in section 'Up' is executed when this migration is applied
CREATE INDEX index_entry_on_q ON entry USING gin (q gin_trgm_ops);


-- +goose Down
-- SQL section 'Down' is executed when this migration is rolled back
DROP INDEX index_entry_on_q;

migration実行 -env オプションでenvironmentの変更ができる

up

$ goose up
goose: migrating db environment 'development', current version: 0, target: 20150130174127
OK    20150130234510_InstallPgExtension.sql
OK    20150130234522_AddGinIndexToQOnEntry.sql

down

$ goose down
goose: migrating db environment 'development', current version: 20150130174127, target: 20150130174043
OK    20150130174127_AddGinIndexToQOnEntry.sql

$ goose down
goose: migrating db environment 'development', current version: 20150130174043, target: 0
OK    20150130174043_InstallPgExtension.sql

よさげ