Documentation ¶
Overview ¶
Package migrator represents MySQL database migrator
MySQL database migrator designed to run migrations to your features and manage database schema update with intuitive go code. It is compatible with the latest MySQL v8.
Index ¶
- Variables
- func BuildForeignNameOnTable(table string, column string) string
- func BuildUniqueKeyNameOnTable(table string, columns ...string) string
- type AddColumnCommand
- type AddForeignCommand
- type AddIndexCommand
- type AddPrimaryIndexCommand
- type AddUniqueIndexCommand
- type Binary
- type Bit
- type ChangeColumnCommand
- type DropColumnCommand
- type DropForeignCommand
- type DropIndexCommand
- type DropPrimaryIndexCommand
- type Enum
- type Floatable
- type Foreign
- type Integer
- type JSON
- type Key
- type Migration
- type Migrator
- type ModifyColumnCommand
- type RenameColumnCommand
- type Schema
- func (s *Schema) AlterTable(name string, c TableCommands)
- func (s *Schema) CreateTable(t Table)
- func (s *Schema) CustomCommand(c command)
- func (s *Schema) DropTable(name string, soft bool, option string)
- func (s *Schema) DropTableIfExists(name string)
- func (s *Schema) RenameTable(old string, new string)
- type String
- type Table
- func (t *Table) BigInt(name string, precision uint16, unsigned bool)
- func (t *Table) Binary(name string, precision uint16, nullable bool)
- func (t *Table) BinaryID(name string)
- func (t *Table) Blob(name string, nullable bool)
- func (t *Table) Boolean(name string, def string)
- func (t *Table) Char(name string, precision uint16)
- func (t *Table) Column(name string, c columnType)
- func (t *Table) Date(name string, nullable bool, def string)
- func (t *Table) Decimal(name string, precision uint16, scale uint16)
- func (t *Table) FixedFloat(name string, precision uint16, scale uint16)
- func (t *Table) Float(name string, precision uint16, scale uint16)
- func (t *Table) Foreign(column string, reference string, on string, onUpdate string, onDelete string)
- func (t *Table) ID(name string)
- func (t *Table) Index(name string, columns ...string)
- func (t *Table) Int(name string, precision uint16, unsigned bool)
- func (t *Table) JSON(name string)
- func (t *Table) PreciseTimestamp(name string, precision uint16, nullable bool, def string)
- func (t *Table) Primary(columns ...string)
- func (t *Table) Text(name string, nullable bool)
- func (t *Table) Time(name string, nullable bool, def string)
- func (t *Table) Timestamp(name string, nullable bool, def string)
- func (t *Table) Timestamps()
- func (t *Table) UUID(name string, def string, nullable bool)
- func (t *Table) Unique(columns ...string)
- func (t *Table) UniqueID(name string)
- func (t *Table) Varbinary(name string, precision uint16, nullable bool)
- func (t *Table) Varchar(name string, precision uint16)
- func (t *Table) Year(name string, nullable bool, def string)
- type TableCommands
- type Text
- type Timable
Constants ¶
This section is empty.
Variables ¶
var ( // ErrTableNotExists returns when migration table not found ErrTableNotExists = errors.New("Migration table does not exist") // ErrNoMigrationDefined returns when no migrations defined in the migrations pool ErrNoMigrationDefined = errors.New("No migrations defined") // ErrEmptyRollbackStack returns when nothing can be reverted ErrEmptyRollbackStack = errors.New("Nothing to rollback, there are no migration executed") // ErrMissingMigrationName returns when migration name is missing ErrMissingMigrationName = errors.New("Missing migration name") // ErrNoSQLCommandsToRun returns when migration is invalid and has no commands in the pool ErrNoSQLCommandsToRun = errors.New("There are no commands to be executed") )
Functions ¶
func BuildForeignNameOnTable ¶ added in v1.2.0
BuildForeignNameOnTable builds a name for the foreign key on the table
func BuildUniqueKeyNameOnTable ¶ added in v1.2.0
BuildUniqueKeyNameOnTable builds a name for the foreign key on the table
Types ¶
type AddColumnCommand ¶
AddColumnCommand is a command to add the column to the table.
type AddForeignCommand ¶
type AddForeignCommand struct {
Foreign Foreign
}
AddForeignCommand adds the foreign key constraint to the table.
type AddIndexCommand ¶
AddIndexCommand adds a key to the table.
type AddPrimaryIndexCommand ¶
type AddPrimaryIndexCommand string
AddPrimaryIndexCommand is a command to add a primary key.
type AddUniqueIndexCommand ¶
AddUniqueIndexCommand is a command to add a unique key to the table on some columns.
type Binary ¶
type Binary struct { Default string Nullable bool Comment string OnUpdate string Fixed bool // binary for fixed, otherwise varbinary Precision uint16 }
Binary represents binary column type: `binary` or `varbinary`
Default migrator.Binary will build a sql row: `varbinary NOT NULL`
Examples:
binary ➡️ migrator.Binary{Fixed: true, Precision: 36, Default: "1", Comment: "uuid"} ↪️ binary(36) NOT NULL DEFAULT 1 COMMENT 'uuid' varbinary ➡️ migrator.Binary{Precision: 255, Nullable: true, OnUpdate: "set null"} ↪️ varbinary(255) NULL ON UPDATE set null
type Bit ¶
Bit represents default `bit` column type
Default migrator.Bit will build a sql row: `bit NOT NULL`
Examples:
➡️ migrator.Bit{Precision: 8, Default: "1", Comment: "mario game code"} ↪️ bit(8) NOT NULL DEFAULT 1 COMMENT 'mario game code' ➡️ migrator.Bit{Precision: 64, Nullable: true, OnUpdate: "set null"} ↪️ bit(64) NULL ON UPDATE set null
type ChangeColumnCommand ¶
ChangeColumnCommand is a default command to change column. Warning ⚠️ BC incompatible!
type DropColumnCommand ¶
type DropColumnCommand string
DropColumnCommand is a command to drop a column from the table. Warning ⚠️ BC incompatible!
type DropForeignCommand ¶
type DropForeignCommand string
DropForeignCommand is a command to remove a foreign key constraint.
type DropIndexCommand ¶
type DropIndexCommand string
DropIndexCommand removes the key from the table.
type DropPrimaryIndexCommand ¶
type DropPrimaryIndexCommand struct{}
DropPrimaryIndexCommand is a command to remove the primary key from the table.
type Enum ¶
type Enum struct { Default string Nullable bool Comment string OnUpdate string Values []string Multiple bool // "set", otherwise "enum" }
Enum represents choosable value. In the database represented by: `enum` or `set`
Default migrator.Enum will build a sql row: `enum(”) NOT NULL`
Examples:
enum ➡️ migrator.Enum{Values: []string{"on", "off"}, Default: "off", Nullable: true, OnUpdate: "set null"} ↪️ enum('on', 'off') NULL DEFAULT 'off' ON UPDATE set null set ➡️ migrator.Enum{Values: []string{"1", "2", "3"}, Comment: "options"} ↪️ set('1', '2', '3') NOT NULL COMMENT 'options'
type Floatable ¶
type Floatable struct { Default string Nullable bool Comment string OnUpdate string Type string // float, real, double, decimal, numeric Unsigned bool Precision uint16 Scale uint16 }
Floatable represents a number with a floating point in DB: `float`, `double` or `decimal`
Default migrator.Floatable will build a sql row: `float NOT NULL`
Examples:
float ➡️ migrator.Floatable{Precision: 2, Nullable: true} ↪️ float(2) NULL real ➡️ migrator.Floatable{Type: "real", Precision: 5, Scale: 2} ↪️ real(5,2) NOT NULL double ➡️ migrator.Floatable{Type: "double", Scale: 2, Unsigned: true} ↪️ double(0,2) unsigned NOT NULL decimal ➡️ migrator.Floatable{Type: "decimal", Precision: 15, Scale: 2, OnUpdate: "0.0", Comment: "money"} ↪️ decimal(15,2) NOT NULL ON UPDATE 0.0 COMMENT 'money' numeric ➡️ migrator.Floatable{Type: "numeric", Default: "0.0"} ↪️ numeric NOT NULL DEFAULT 0.0
type Foreign ¶ added in v1.2.0
type Foreign struct { Key string Column string Reference string // reference field On string // reference table OnUpdate string OnDelete string }
Foreign represents an instance to handle foreign key interactions
type Integer ¶
type Integer struct { Default string Nullable bool Comment string OnUpdate string Prefix string // tiny, small, medium, big Unsigned bool Precision uint16 Autoincrement bool }
Integer represents an integer value in DB: {tiny,small,medium,big}int
Default migrator.Integer will build a sql row: `int NOT NULL`
Examples:
tinyint ➡️ migrator.Integer{Prefix: "tiny", Unsigned: true, Precision: 1, Default: "0"} ↪️ tinyint(1) unsigned NOT NULL DEFAULT 0 int ➡️ migrator.Integer{Nullable: true, OnUpdate: "set null", Comment: "nullable counter"} ↪️ int NULL ON UPDATE set null COMMENT 'nullable counter' mediumint ➡️ migrator.Integer{Prefix: "medium", Precision: "255"} ↪️ mediumint(255) NOT NULL bigint ➡️ migrator.Integer{Prefix: "big", Unsigned: true, Precision: "255", Autoincrement: true} ↪️ bigint(255) unsigned NOT NULL AUTO_INCREMENT
type JSON ¶
JSON represents DB column type `json`
Default migrator.JSON will build a sql row: `json NOT NULL`
Examples:
➡️ migrator.JSON{Nullable: true, Comment: "user data"} ↪️ json NULL COMMENT 'user data' ➡️ migrator.JSON{Default: "{}", OnUpdate: "{}"} ↪️ json NOT NULL DEFAULT '{}' ON UPDATE {}
type Migration ¶
Migration represents migration entity
Name should be a unique name to specify migration. It is up to you to choose the name you like Up() should return Schema with prepared commands to be migrated Down() should return Schema with prepared commands to be reverted Transaction optinal flag to enable transaction for migration
Example:
var migration = migrator.Migration{ Name: "19700101_0001_create_posts_table", Up: func() migrator.Schema { var s migrator.Schema posts := migrator.Table{Name: "posts"} posts.UniqueID("id") posts.Column("title", migrator.String{Precision: 64}) posts.Column("content", migrator.Text{}) posts.Timestamps() s.CreateTable(posts) return s }, Down: func() migrator.Schema { var s migrator.Schema s.DropTableIfExists("posts") return s }, }
type Migrator ¶
type Migrator struct { // Name of the table to track executed migrations TableName string // stack of migrations Pool []Migration // contains filtered or unexported fields }
Migrator represents a struct with migrations, that should be executed.
Default migration table name is `migrations`, but it can be re-defined. Pool is a list of migrations that should be migrated.
func (Migrator) Migrate ¶
Migrate runs all migrations from pool and stores in migration table executed migration.
type ModifyColumnCommand ¶
type ModifyColumnCommand struct { Name string Column columnType }
ModifyColumnCommand is a command to modify column type. Warning ⚠️ BC incompatible!
Info ℹ️ extension for Oracle compatibility.
type RenameColumnCommand ¶
RenameColumnCommand is a command to rename a column in the table. Warning ⚠️ BC incompatible!
Info ℹ️ extension for Oracle compatibility.
type Schema ¶
type Schema struct {
// contains filtered or unexported fields
}
Schema allows adding commands on the schema. It should be used within migration to add migration commands.
func (*Schema) AlterTable ¶
func (s *Schema) AlterTable(name string, c TableCommands)
AlterTable makes changes on the table level.
Example:
var s migrator.Schema var c TableCommands s.AlterTable("test", c)
func (*Schema) CreateTable ¶
CreateTable allows creating the table in the schema.
Example:
var s migrator.Schema t := migrator.Table{Name: "test"} s.CreateTable(t)
func (*Schema) CustomCommand ¶
func (s *Schema) CustomCommand(c command)
CustomCommand allows adding the custom command to the Schema.
Example:
type customCommand string func (c customCommand) toSQL() string { return string(c) } c := customCommand("DROP PROCEDURE abc") var s migrator.Schema s.CustomCommand(c)
func (*Schema) DropTable ¶
DropTable removes a table from the schema. Warning ⚠️ BC incompatible!
Example:
var s migrator.Schema s.DropTable("test", false, "")
Soft delete (drop if exists)
s.DropTable("test", true, "")
func (*Schema) DropTableIfExists ¶
DropTableIfExists removes table if exists from the schema. Warning ⚠️ BC incompatible!
Example:
var s migrator.Schema s.DropTableIfExists("test")
func (*Schema) RenameTable ¶
RenameTable executes a command to rename the table. Warning ⚠️ BC incompatible!
Example:
var s migrator.Schema s.RenameTable("old", "new")
type String ¶
type String struct { Default string Nullable bool Comment string OnUpdate string Charset string Collate string Fixed bool // char for fixed, otherwise varchar Precision uint16 }
String represents basic DB string column type: `char` or `varchar`
Default migrator.String will build a sql row: `varchar COLLATE utf8mb4_unicode_ci NOT NULL`
Examples:
char ➡️ migrator.String{Fixed: true, Precision: 36, Nullable: true, OnUpdate: "set null", Comment: "uuid"} ↪️ char(36) COLLATE utf8mb4_unicode_ci NULL ON UPDATE set null COMMENT 'uuid' varchar ➡️ migrator.String{Precision: 255, Default: "active", Charset: "utf8mb4", Collate: "utf8mb4_general_ci"} ↪️ varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL DEFAULT 'active'
type Table ¶
type Table struct { Name string Engine string Charset string Collation string Comment string // contains filtered or unexported fields }
Table is an entity to create a table.
- Name table name - Engine default: InnoDB - Charset default: utf8mb4 or first part of collation (if set) - Collation default: utf8mb4_unicode_ci or charset with `_unicode_ci` suffix - Comment optional comment on table
func (*Table) BinaryID ¶
BinaryID adds unique binary id column (represented as UUID) that is the primary key
func (*Table) FixedFloat ¶
FixedFloat is an alias to decimal(precision,scale) column
func (*Table) Foreign ¶
func (t *Table) Foreign(column string, reference string, on string, onUpdate string, onDelete string)
Foreign adds foreign key constraints
func (*Table) PreciseTimestamp ¶ added in v1.2.0
PreciseTimestamp adds timestamp column with precision to the table
func (*Table) Timestamps ¶
func (t *Table) Timestamps()
Timestamps adds default timestamps: `created_at` and `updated_at`
func (*Table) UniqueID ¶
UniqueID adds unique id column (represented as UUID) that is the primary key
type TableCommands ¶
type TableCommands []command
TableCommands is a pool of commands to be executed on the table. https://dev.mysql.com/doc/refman/8.0/en/alter-table.html
type Text ¶
type Text struct { Default string Nullable bool Comment string OnUpdate string Charset string Collate string Prefix string // tiny, medium, long Blob bool // for binary }
Text represents long text column type represented in DB as:
- {tiny,medium,long}text
- {tiny,medium,long}blob
Default migrator.Text will build a sql row: `text COLLATE utf8mb4_unicode_ci NOT NULL`
Examples:
tinytext ➡️ migrator.Text{Prefix: "tiny"} ↪️ tinytext COLLATE utf8mb4_unicode_ci NOT NULL text ➡️ migrator.Text{Nullable: true, OnUpdate: "set null", Comment: "write your comment here"} ↪️ text COLLATE utf8mb4_unicode_ci NULL ON UPDATE set null COMMENT 'write your comment here' mediumtext ➡️ migrator.Text{Prefix: "medium"} ↪️ mediumtext COLLATE utf8mb4_unicode_ci NOT NULL longtext ➡️ migrator.Text{Prefix: "long", Default: "write you text", Charset: "utf8mb4", Collate: "utf8mb4_general_ci"} ↪️ longtext CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL DEFAULT 'write you text' tinyblob ➡️ migrator.Text{Prefix: "tiny", Blob: true} ↪️ tinyblob NOT NULL blob ➡️ migrator.Text{Blob: true} ↪️ blob NOT NULL mediumblob ➡️ migrator.Text{Prefix: "medium", Blob: true} ↪️ mediumblob NOT NULL longblob ➡️ migrator.Text{Prefix: "long", Blob: true} ↪️ longblob NOT NULL
type Timable ¶
type Timable struct { Default string Nullable bool Comment string OnUpdate string Type string // date, time, datetime, timestamp, year Precision uint16 }
Timable represents DB representation of timable column type: `date`, `datetime`, `timestamp`, `time` or `year`
Default migrator.Timable will build a sql row: `timestamp NOT NULL`. Precision from 0 to 6 can be set for `datetime`, `timestamp`, `time`.
Examples:
date ➡️ migrator.Timable{Type: "date", Nullable: true} ↪️ date NULL datetime ➡️ migrator.Timable{Type: "datetime", Precision: 3, Default: "CURRENT_TIMESTAMP"} ↪️ datetime(3) NOT NULL DEFAULT CURRENT_TIMESTAMP timestamp ➡️ migrator.Timable{Default: "CURRENT_TIMESTAMP(6)", OnUpdate: "CURRENT_TIMESTAMP(6)"} ↪️ timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6) time ➡️ migrator.Timable{Type: "time", Comment: "meeting time"} ↪️ time NOT NULL COMMENT 'meeting time' year ➡️ migrator.Timable{Type: "year", Nullable: true} ↪️ year NULL