migrator

package module
Version: v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Aug 7, 2020 License: MIT Imports: 6 Imported by: 0

README

MySQL database migrator

Build Status Software License codecov Go Report Card GoDoc Release TODOs

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.

Installation

To install migrator package, you need to install Go and set your Go workspace first.

  1. The first need Go installed (version 1.13+ is required), then you can use the below Go command to install migrator.
$ go get -u github.com/larapulse/migrator
  1. Import it in your code:
import "github.com/larapulse/migrator"

Quick start

Initialize migrator with migration entries:

var migrations = []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.Varchar("title", 64)
			posts.Text("content", false)
			posts.Timestamps()

			s.CreateTable(posts)

			return s
		},
		Down: func() migrator.Schema {
			var s migrator.Schema

			s.DropTableIfExists("posts")

			return s
		},
	},
	{
		Name: "19700101_0002_create_comments_table",
		Up: func() migrator.Schema {
			var s migrator.Schema
			comments := migrator.Table{Name: "comments"}

			comments.UniqueID("id")
			comments.UUID("post_id", "", false)
			comments.Varchar("name", 64)
			comments.Column("email", migrator.String{Default: "<nil>"})
			comments.Text("content", false)
			comments.Timestamps()

			comments.Foreign("post_id", "id", "posts", "RESTRICT", "RESTRICT")

			s.CreateTable(comments)

			return s
		},
		Down: func() migrator.Schema {
			var s migrator.Schema

			s.DropTableIfExists("comments")

			return s
		},
	},
	{
		Name: "19700101_0003_rename_foreign_key",
		Up: func() migrator.Schema {
			var s migrator.Schema

			keyName := migrator.BuildForeignNameOnTable("comments", "post_id")
			newKeyName := migrator.BuildForeignNameOnTable("comments", "article_id")

			s.AlterTable("comments", migrator.TableCommands{
				migrator.DropForeignCommand(keyName),
				migrator.DropIndexCommand(keyName),
				migrator.RenameColumnCommand{"post_id", "article_id"},
				migrator.AddIndexCommand{newKeyName, []string{"article_id"}},
				migrator.AddForeignCommand{migrator.Foreign{
					Key:       newKeyName,
					Column:    "article_id",
					Reference: "id",
					On:        "posts",
				}},
			})

			return s
		},
		Down: func() migrator.Schema {
			var s migrator.Schema

			keyName := migrator.BuildForeignNameOnTable("comments", "article_id")
			newKeyName := migrator.BuildForeignNameOnTable("comments", "post_id")

			s.AlterTable("comments", migrator.TableCommands{
				migrator.DropForeignCommand(keyName),
				migrator.DropIndexCommand(keyName),
				migrator.RenameColumnCommand{"article_id", "post_id"},
				migrator.AddIndexCommand{newKeyName, []string{"post_id"}},
				migrator.AddForeignCommand{migrator.Foreign{
					Key:       newKeyName,
					Column:    "post_id",
					Reference: "id",
					On:        "posts",
				}},
			})

			return s
	},
}

m := migrator.Migrator{Pool: migrations}
migrated, err = m.Migrate(db)

if err != nil {
	log.Errorf("Could not migrate: %v", err)
	os.Exit(1)
}

if len(migrated) == 0 {
	log.Print("Nothing were migrated.")
}

for _, m := range migrated {
	log.Printf("Migration: %s was migrated ✅", m)
}

log.Print("Migration did run successfully")

After the first migration run, migrations table will be created:

+----+-------------------------------------+-------+----------------------------+
| id | name                                | batch | applied_at                 |
+----+-------------------------------------+-------+----------------------------+
|  1 | 19700101_0001_create_posts_table    |     1 | 2020-06-27 00:00:00.000000 |
|  2 | 19700101_0002_create_comments_table |     1 | 2020-06-27 00:00:00.000000 |
|  3 | 19700101_0003_rename_foreign_key    |     1 | 2020-06-27 00:00:00.000000 |
+----+-------------------------------------+-------+----------------------------+

If you want to use another name for migration table, change it Migrator before running migrations:

m := migrator.Migrator{TableName: "_my_app_migrations"}
Transactional migration

In case you have multiple commands within one migration and you want to be sure it is migrated properly, you might enable transactional execution per migration:

var migration = migrator.Migration{
	Name: "19700101_0001_create_posts_and_users_tables",
	Up: func() migrator.Schema {
		var s migrator.Schema
		posts := migrator.Table{Name: "posts"}
		posts.UniqueID("id")
		posts.Timestamps()

		users := migrator.Table{Name: "users"}
		users.UniqueID("id")
		users.Timestamps()

		s.CreateTable(posts)
		s.CreateTable(users)

		return s
	},
	Down: func() migrator.Schema {
		var s migrator.Schema

		s.DropTableIfExists("users")
		s.DropTableIfExists("posts")

		return s
	},
	Transaction: true,
}
Rollback and revert

In case you need to revert your deploy and DB, you can revert last migrated batch:

m := migrator.Migrator{Pool: migrations}
reverted, err := m.Rollback(db)

if err != nil {
	log.Errorf("Could not roll back migrations: %v", err)
	os.Exit(1)
}

if len(reverted) == 0 {
	log.Print("Nothing were rolled back.")
}

for _, m := range reverted {
	log.Printf("Migration: %s was rolled back ✅", m)
}

To revert all migrated items back, you have to call Revert() on your migrator:

m := migrator.Migrator{Pool: migrations}
reverted, err := m.Revert(db)

Customize queries

You may add any column definition to the database on your own, just be sure you implement columnType interface:

type customType string

func (ct customType) buildRow() string {
	return string(ct)
}

posts := migrator.Table{Name: "posts"}
posts.UniqueID("id")
posts.Column("data", customType("json not null"))
posts.Timestamps()

The same logic is for adding custom commands to the Schema to be migrated or reverted, just be sure you implement command interface:

type customCommand string

func (cc customCommand) toSQL() string {
	return string(cc)
}

var s migrator.Schema

c := customCommand("DROP PROCEDURE abc")
s.CustomCommand(c)

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

Constants

This section is empty.

Variables

View Source
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

func BuildForeignNameOnTable(table string, column string) string

BuildForeignNameOnTable builds a name for the foreign key on the table

func BuildUniqueKeyNameOnTable added in v1.2.0

func BuildUniqueKeyNameOnTable(table string, columns ...string) string

BuildUniqueKeyNameOnTable builds a name for the foreign key on the table

Types

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 AddIndexCommand

type AddIndexCommand struct {
	Name    string
	Columns []string
}

AddIndexCommand adds a key to the table.

type AddPrimaryIndexCommand

type AddPrimaryIndexCommand string

AddPrimaryIndexCommand is a command to add a primary key.

type AddUniqueIndexCommand

type AddUniqueIndexCommand struct {
	Key     string
	Columns []string
}

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

type Bit struct {
	Default  string
	Nullable bool
	Comment  string
	OnUpdate string

	Precision uint16
}

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

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

type JSON struct {
	Default  string
	Nullable bool
	Comment  string
	OnUpdate string
}

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 Key added in v1.2.0

type Key struct {
	Name    string
	Type    string // primary, unique
	Columns []string
}

Key represents an instance to handle key (index) interactions

type Migration

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

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

func (m Migrator) Migrate(db *sql.DB) (migrated []string, err error)

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

func (Migrator) Revert

func (m Migrator) Revert(db *sql.DB) (reverted []string, err error)

Revert reverts all executed migration from the pool.

func (Migrator) Rollback

func (m Migrator) Rollback(db *sql.DB) (reverted []string, err error)

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

type RenameColumnCommand struct {
	Old string
	New string
}

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

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)

func (*Schema) DropTable

func (s *Schema) DropTable(name string, soft bool, option string)

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

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")

func (*Schema) RenameTable

func (s *Schema) RenameTable(old string, new string)

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) BigInt

func (t *Table) BigInt(name string, precision uint16, unsigned bool)

BigInt adds bigint(precision) column to the table

func (*Table) Binary

func (t *Table) Binary(name string, precision uint16, nullable bool)

Binary adds binary(precision) column to the table

func (*Table) BinaryID

func (t *Table) BinaryID(name string)

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

func (*Table) Blob

func (t *Table) Blob(name string, nullable bool)

Blob adds blob column to the table

func (*Table) Boolean

func (t *Table) Boolean(name string, def string)

Boolean represented in DB as tinyint

func (*Table) Char

func (t *Table) Char(name string, precision uint16)

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

func (*Table) Date

func (t *Table) Date(name string, nullable bool, def string)

Date adds date column to the table

func (*Table) Decimal

func (t *Table) Decimal(name string, precision uint16, scale uint16)

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

func (*Table) FixedFloat

func (t *Table) FixedFloat(name string, precision uint16, scale uint16)

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

func (*Table) Float

func (t *Table) Float(name string, precision uint16, scale uint16)

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

func (*Table) Foreign

func (t *Table) Foreign(column string, reference string, on string, onUpdate string, onDelete string)

Foreign adds foreign key constraints

func (*Table) ID

func (t *Table) ID(name string)

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

func (*Table) Index

func (t *Table) Index(name string, columns ...string)

Index adds index (key) on selected columns

func (*Table) Int

func (t *Table) Int(name string, precision uint16, unsigned bool)

Int adds int(precision) column to the table

func (*Table) JSON

func (t *Table) JSON(name string)

JSON adds json column to the table

func (*Table) PreciseTimestamp added in v1.2.0

func (t *Table) PreciseTimestamp(name string, precision uint16, nullable bool, def string)

PreciseTimestamp adds timestamp column with precision to the table

func (*Table) Primary

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

Primary adds primary key

func (*Table) Text

func (t *Table) Text(name string, nullable bool)

Text adds text column to the table

func (*Table) Time

func (t *Table) Time(name string, nullable bool, def string)

Time adds time column to the table

func (*Table) Timestamp

func (t *Table) Timestamp(name string, nullable bool, def string)

Timestamp adds timestamp column to the table

func (*Table) Timestamps

func (t *Table) Timestamps()

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

func (*Table) UUID

func (t *Table) UUID(name string, def string, nullable bool)

UUID adds char(36) column

func (*Table) Unique

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

Unique adds unique key on selected columns

func (*Table) UniqueID

func (t *Table) UniqueID(name string)

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

func (*Table) Varbinary

func (t *Table) Varbinary(name string, precision uint16, nullable bool)

Varbinary adds varbinary(precision) column to the table

func (*Table) Varchar

func (t *Table) Varchar(name string, precision uint16)

Varchar adds varchar(precision) column to the table

func (*Table) Year

func (t *Table) Year(name string, nullable bool, def string)

Year adds year column to the table

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 COLLATE utf8mb4_unicode_ci NOT NULL
blob		➡️ migrator.Text{Blob: true}
	↪️ blob COLLATE utf8mb4_unicode_ci NOT NULL
mediumblob	➡️ migrator.Text{Prefix: "medium", Blob: true}
	↪️ mediumblob COLLATE utf8mb4_unicode_ci NOT NULL
longblob	➡️ migrator.Text{Prefix: "long", Blob: true}
	↪️ longblob COLLATE utf8mb4_unicode_ci 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

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
t or T : Toggle theme light dark auto
y or Y : Canonical URL