migrator package - github.com/larapulse/migrator - Go Packages (original) (raw)

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.

This section is empty.

View Source

var (

ErrTableNotExists = [errors](/errors).[New](/errors#New)("Migration table does not exist")


ErrNoMigrationDefined = [errors](/errors).[New](/errors#New)("No migrations defined")


ErrEmptyRollbackStack = [errors](/errors).[New](/errors#New)("Nothing to rollback, there are no migration executed")


ErrMissingMigrationName = [errors](/errors).[New](/errors#New)("Missing migration name")


ErrNoSQLCommandsToRun = [errors](/errors).[New](/errors#New)("There are no commands to be executed")

)

BuildForeignNameOnTable builds a name for the foreign key on the table

BuildUniqueKeyNameOnTable builds a name for the foreign key on the table

type AddColumnCommand

type AddColumnCommand struct { Name string Column columnType After string First bool }

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 AddUniqueIndexCommand

type AddUniqueIndexCommand struct { Key string Columns []string }

AddUniqueIndexCommand is a command to add a unique key to the table on some columns.

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

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

type ChangeColumnCommand struct { From string To string Column columnType }

ChangeColumnCommand is a default command to change column. Warning ⚠️ BC incompatible!

type DropColumnCommand

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 DropPrimaryIndexCommand

type DropPrimaryIndexCommand struct{}

DropPrimaryIndexCommand is a command to remove the primary key from the table.

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'

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

Foreign represents an instance to handle foreign key interactions

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

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 {}

Key represents an instance to handle key (index) interactions

type Migration struct { Name string Up func() Schema Down func() Schema Transaction bool }

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 struct {

TableName [string](/builtin#string)

Pool [][Migration](#Migration)

}

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.

Migrate runs all migrations from pool and stores in migration table executed migration.

Revert reverts all executed migration from the pool.

Rollback reverts last executed batch of migrations.

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.

Schema allows adding commands on the schema. It should be used within migration to add migration commands.

AlterTable makes changes on the table level.

Example:

var s migrator.Schema var c TableCommands s.AlterTable("test", c)

func (s *Schema) CreateTable(t Table)

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)

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 (s *Schema) DropTableIfExists(name string)

DropTableIfExists removes table if exists from the schema. Warning ⚠️ BC incompatible!

Example:

var s migrator.Schema s.DropTableIfExists("test")

RenameTable executes a command to rename the table. Warning ⚠️ BC incompatible!

Example:

var s migrator.Schema s.RenameTable("old", "new")

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'

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

BigInt adds bigint(precision) column to the table

Binary adds binary(precision) column to the table

BinaryID adds unique binary id column (represented as UUID) that is the primary key

Blob adds blob column to the table

Boolean represented in DB as tinyint

Char adds char(precision) column to the table

func (*Table) Column

func (t *Table) Column(name string, c columnType)

Column adds a column to the table

Date adds date column to the table

Decimal adds decimal(precision,scale) column to the table

FixedFloat is an alias to decimal(precision,scale) column

Float adds float(precision,scale) column to the table

Foreign adds foreign key constraints

ID adds bigint `id` column that is the primary key

Index adds index (key) on selected columns

Int adds int(precision) column to the table

JSON adds json column to the table

PreciseTimestamp adds timestamp column with precision to the table

func (t *Table) Primary(columns ...string)

Primary adds primary key

Text adds text column to the table

Time adds time column to the table

Timestamp adds timestamp column to the table

func (t *Table) Timestamps()

Timestamps adds default timestamps: `created_at` and `updated_at`

UUID adds char(36) column

func (t *Table) Unique(columns ...string)

Unique adds unique key on selected columns

UniqueID adds unique id column (represented as UUID) that is the primary key

Varbinary adds varbinary(precision) column to the table

Varchar adds varchar(precision) column to the table

Year adds year column to the table

Text represents long text column type represented in DB as:

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

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