migu

package module
v0.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 25, 2015 License: MIT Imports: 14 Imported by: 0

README

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

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Diff

func Diff(db *sql.DB, filename string, src interface{}) ([]string, error)

Diff returns SQLs for schema synchronous between database and Go's struct.

func Fprint

func Fprint(output io.Writer, db *sql.DB) error

Fprint generates Go's structs from database schema and writes to output.

func Sync

func Sync(db *sql.DB, filename string, src interface{}) error

Sync synchronizes the schema between Go's struct and the database. Go's struct may be provided via the filename of the source file, or via the src parameter.

If src != nil, Sync parses the source from src and filename is not used. The type of the argument for the src parameter must be string, []byte, or io.Reader. If src == nil, Sync parses the file specified by filename.

All query for synchronization will be performed within the transaction if storage engine supports the transaction. (e.g. MySQL's MyISAM engine does NOT support the transaction)

Types

This section is empty.

Directories

Path Synopsis
_example
cmd

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL