LoginSignup
55
40

More than 5 years have passed since last update.

Golang製DBマイグレーションツールgoose + MySQLを試してみた

Last updated at Posted at 2016-11-04

gooseとは

Golang製のDBマイグレーションツール
https://bitbucket.org/liamstask/goose/

なんでgoose?

プロジェクトのプログラムとは離した状態でマイグレーションを導入したかった。プロジェクトごとにマイグレーション手段が変わるのが嫌だったから。
SQLをそのまま書けるので学習コストが高くなさそう。
Golang製のツールを使ってみたかった。

導入まで

参考: http://enomotodev.github.io/post/use-goose/

goは導入済みとして

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

goose is a database migration management system for Go projects.

Usage:
    goose [options] <subcommand> [subcommand options]

Options:
  -env string
        which DB environment to use (default "development")
  -path string

▽
        folder containing db info (default "db")
  -pgschema string
        which postgres-schema to migrate (default = none)

Commands:
    up         Migrate the DB to the most recent version available
    down       Roll back the version by 1
    redo       Re-run the latest migration
    status     dump the migration status for the current DB
    create     Create the scaffolding for a new migration
    dbversion  Print the current version of the database

疎通確認する

設定ファイルを作って疎通確認まで
設定ファイルはサンプルをコピーしてくる

$ mkdir db
$ cd db/
$ cp $GOPATH/src/bitbucket.org/liamstask/goose/db-sample/dbconf.yml ./

とりあえずdevelopmentだけ書き換えてみる
MySQLを使うのでdriverを変える
接続設定を書き換える。 [db_name], [user_name], [password] を適宜書き換えてください。

dbconf.yml
development:
    driver: mymysql
    open: tcp:localhost:3306*[db_name]/[user_name]/[password]

下記のように返ってくればとりあえず疎通はOK。

$ goose status
goose: status for environment 'development'
    Applied At                  Migration
    =======================================

使ってみる

上記で作ったconf入りのdbディレクトリの一階層上で実行します。(./db/dbconf.ymlを読むため)

Usersテーブルを試しに作ってみます。

$ goose create createUsers sql
goose: created /srv/qiitarank/db/migrations/20161104023332_createUsers.sql

goose create [マイグレーション名] sql でマイグレーション用SQLファイルを作ってくれます。
sqlを外すとgoファイルを作ります(SQLで書きたいので今回は省略)。


-- +goose Up
-- SQL in section 'Up' is executed when this migration is applied


-- +goose Down
-- SQL section 'Down' is executed when this migration is rolled back

上記のようなファイルができますので、あとはSQLを書くだけです。
+goose Up 以下にUp時に実行するSQL
+goose Down 以下にDown時に実行するSQL
を記述します。


-- +goose Up
-- SQL in section 'Up' is executed when this migration is applied
CREATE TABLE users (
    id varchar(255) NOT NULL,
    name varchar(255) NOT NULL,
    PRIMARY KEY(id)
);

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

テーブルを作成する処理をUpに、削除する処理をDownに記述しました。
goose upを叩いて、Upの処理を実行します。

$ goose up
goose: migrating db environment 'development', current version: 20161027112322, target: 20161104023332
OK    20161104023332_createUsers.sql

これで +goose Up に記述されたSQLが実行されてテーブルが作成されます。

goose down を叩くと、Downの処理が実行されます。

$ goose down
goose: migrating db environment 'development', current version: 20161104023332, target: 20161027112322
OK    20161104023332_createTable.sql

まとめ

  • gooseの利用は簡単
  • SQLを書けばいいので想定通り学習コストは低い
  • 今回試してないが環境ごとの設定記述も可能なので十分使えそう。
55
40
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
55
40