migu

package module
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Nov 29, 2018 License: MIT Imports: 14 Imported by: 0

README

Migu CircleCI

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/astronoka/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)
IGNORE
Body string `migu:"-"` // Ignore during migration
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

type Index added in v0.1.1

type Index struct {
	Name        string
	Unique      bool
	ColumnNames []string
}

Index is table index

func (*Index) AsASTField added in v0.1.1

func (index *Index) AsASTField(indexNo int) (*ast.Field, error)

func (*Index) AsCreateTableDefinition added in v0.1.1

func (index *Index) AsCreateTableDefinition(d dialect.Dialect) string

type Table added in v0.1.1

type Table struct {
	Columns []*columnSchema
	Indexes []*Index
}

Table is table definitions

func (Table) ColumnMap added in v0.1.1

func (t Table) ColumnMap() map[string]*columnSchema

func (Table) HasDatetimeColumn added in v0.1.1

func (t Table) HasDatetimeColumn() bool

func (Table) IndexMap added in v0.1.1

func (t Table) IndexMap() map[string]*Index

type TableAST added in v0.1.1

type TableAST struct {
	Name        string
	Schema      *ast.StructType
	IndexSchema *ast.StructType
}

func (*TableAST) AlterTableQueries added in v0.1.1

func (t *TableAST) AlterTableQueries(d dialect.Dialect, currentTable *Table) ([]string, error)

func (*TableAST) ColumnMap added in v0.1.1

func (t *TableAST) ColumnMap() map[string]*field

func (*TableAST) Columns added in v0.1.1

func (t *TableAST) Columns() ([]*field, error)

func (*TableAST) CreateTableQuery added in v0.1.1

func (t *TableAST) CreateTableQuery(d dialect.Dialect) ([]string, error)

see: https://dev.mysql.com/doc/refman/5.6/en/create-table.html

func (*TableAST) GenerateAddFieldSQLs added in v0.1.1

func (t *TableAST) GenerateAddFieldSQLs(d dialect.Dialect, currentColumMap map[string]*columnSchema) ([]string, error)

func (*TableAST) GenerateAddIndexSQLs added in v0.1.1

func (t *TableAST) GenerateAddIndexSQLs(d dialect.Dialect, currentIndexMap map[string]*Index) ([]string, error)

func (*TableAST) GenerateDropFieldSQLs added in v0.1.1

func (t *TableAST) GenerateDropFieldSQLs(d dialect.Dialect, currentColumMap map[string]*columnSchema) ([]string, error)

func (*TableAST) GenerateDropIndexSQLs added in v0.1.1

func (t *TableAST) GenerateDropIndexSQLs(d dialect.Dialect, currentIndexMap map[string]*Index) ([]string, error)

func (*TableAST) GenerateModifyFieldSQLs added in v0.1.1

func (t *TableAST) GenerateModifyFieldSQLs(d dialect.Dialect, currentColumMap map[string]*columnSchema) ([]string, error)

func (*TableAST) HasSchema added in v0.1.1

func (t *TableAST) HasSchema() bool

func (*TableAST) IndexMap added in v0.1.1

func (t *TableAST) IndexMap() map[string]*Index

func (*TableAST) Indexes added in v0.1.1

func (t *TableAST) Indexes() ([]*Index, error)

func (*TableAST) MustColumns added in v0.1.1

func (t *TableAST) MustColumns() []*field

func (*TableAST) MustIndexes added in v0.1.1

func (t *TableAST) MustIndexes() []*Index

Directories

Path Synopsis
_example
cmd

Jump to

Keyboard shortcuts

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