/migu

Database schema migration tool for Go

Primary LanguageGoMIT LicenseMIT

Migu Build Status

Database schema migration tool for Go.

Migu has idempotence like Chef or Puppet.

This tool is inspired by Ridgepole.

Installation

go get -u github.com/naoina/migu/cmd/migu

Basic usage

Save the following Go code as schema.go:

package yourownpackagename

type User struct {
	Name string
	Age  int
}

Then type the following commands to execute the first migration:

% mysqladmin -u root create migu_test
% migu -u root sync migu_test schema.go
% mysql -u root migu_test -e 'desc user'
+-------+--------------+------+-----+---------+-------+
| Field | Type         | Null | Key | Default | Extra |
+-------+--------------+------+-----+---------+-------+
| name  | varchar(255) | NO   |     | NULL    |       |
| age   | int(11)      | NO   |     | NULL    |       |
+-------+--------------+------+-----+---------+-------+

migu sync command will create the table user into database migu_test because it still not exist.

Next, modify and save schema.go as follows:

package yourownpackagename

type User struct {
	Name string
	Age  uint
}

Then type the following commands again:

% migu -u root sync migu_test schema.go
% mysql -u root migu_test -e 'desc user'
+-------+------------------+------+-----+---------+-------+
| Field | Type             | Null | Key | Default | Extra |
+-------+------------------+------+-----+---------+-------+
| name  | varchar(255)     | NO   |     | NULL    |       |
| age   | int(10) unsigned | NO   |     | NULL    |       |
+-------+------------------+------+-----+---------+-------+

A type of field age on user table has been changed because type of Age in schema.go was changed from int to uint.

See migu --help for more options.

Detailed definition of the column by struct field's tag

You can specify the detailed definition of the column by some struct field's tags.

PRIMARY KEY

ID int64 `migu:"pk"`

AUTOINCREMENT

ID int64 `migu:"autoincrement"`

UNIQUE

Email string `migu:"unique"`

DEFAULT

Active bool `migu:"default:true"`

If the field type is string, the value doesn't need to be quoted because the value type will be guess by Migu.

Active string `migu:"default:yes"`

SIZE

Body string `migu:"size:512"` // VARCHAR(512)

Specify the multiple struct field's tags

To specify the multiple struct field's tags to the single column, join with commas.

Email string `migu:"unique,size:512"`

Supported database

  • MySQL

TODO

  • Struct Tag support for some ORM
  • PostgreSQL and SQLite3 support

License

MIT