ProductPromotion
Logo

Go.Lang

made by https://0x3d.site

GitHub - larapulse/migrator: MySQL database migrator
MySQL database migrator. Contribute to larapulse/migrator development by creating an account on GitHub.
Visit Site

GitHub - larapulse/migrator: MySQL database migrator

GitHub - larapulse/migrator: MySQL database migrator

MySQL database migrator

Build Status Software License codecov Go Report Card GoDoc Mentioned in Awesome Go 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)

Articles
to learn more about the golang concepts.

Resources
which are currently available to browse on.

mail [email protected] to add your project or resources here ๐Ÿ”ฅ.

FAQ's
to know more about the topic.

mail [email protected] to add your project or resources here ๐Ÿ”ฅ.

Queries
or most google FAQ's about GoLang.

mail [email protected] to add more queries here ๐Ÿ”.

More Sites
to check out once you're finished browsing here.

0x3d
https://www.0x3d.site/
0x3d is designed for aggregating information.
NodeJS
https://nodejs.0x3d.site/
NodeJS Online Directory
Cross Platform
https://cross-platform.0x3d.site/
Cross Platform Online Directory
Open Source
https://open-source.0x3d.site/
Open Source Online Directory
Analytics
https://analytics.0x3d.site/
Analytics Online Directory
JavaScript
https://javascript.0x3d.site/
JavaScript Online Directory
GoLang
https://golang.0x3d.site/
GoLang Online Directory
Python
https://python.0x3d.site/
Python Online Directory
Swift
https://swift.0x3d.site/
Swift Online Directory
Rust
https://rust.0x3d.site/
Rust Online Directory
Scala
https://scala.0x3d.site/
Scala Online Directory
Ruby
https://ruby.0x3d.site/
Ruby Online Directory
Clojure
https://clojure.0x3d.site/
Clojure Online Directory
Elixir
https://elixir.0x3d.site/
Elixir Online Directory
Elm
https://elm.0x3d.site/
Elm Online Directory
Lua
https://lua.0x3d.site/
Lua Online Directory
C Programming
https://c-programming.0x3d.site/
C Programming Online Directory
C++ Programming
https://cpp-programming.0x3d.site/
C++ Programming Online Directory
R Programming
https://r-programming.0x3d.site/
R Programming Online Directory
Perl
https://perl.0x3d.site/
Perl Online Directory
Java
https://java.0x3d.site/
Java Online Directory
Kotlin
https://kotlin.0x3d.site/
Kotlin Online Directory
PHP
https://php.0x3d.site/
PHP Online Directory
React JS
https://react.0x3d.site/
React JS Online Directory
Angular
https://angular.0x3d.site/
Angular JS Online Directory