|
| 1 | +# MySQL database migrator |
| 2 | + |
| 3 | +<img align="right" width="159px" src="./logo.png"> |
| 4 | + |
| 5 | +[](https://travis-ci.org/larapulse/migrator) |
| 6 | +[](LICENSE.md) |
| 7 | +[](https://codecov.io/gh/larapulse/migrator) |
| 8 | +[](https://goreportcard.com/report/github.com/larapulse/migrator) |
| 9 | +[](https://pkg.go.dev/github.com/larapulse/migrator?tab=doc) |
| 10 | +[](https://github.com/larapulse/migrator/releases) |
| 11 | +[](https://www.tickgit.com/browse?repo=github.com/larapulse/migrator) |
| 12 | + |
| 13 | +MySQL database migrator designed to run migrations to your features and manage database schema update with intuitive go code. It is compatible with latest MySQL v8. |
| 14 | + |
| 15 | +## Installation |
| 16 | + |
| 17 | +To install `migrator` package, you need to install Go and set your Go workspace first. |
| 18 | + |
| 19 | +1. The first need [Go](https://golang.org/) installed (**version 1.13+ is required**), then you can use the below Go command to install `migrator`. |
| 20 | + |
| 21 | +```sh |
| 22 | +$ go get -u github.com/larapulse/migrator |
| 23 | +``` |
| 24 | + |
| 25 | +2. Import it in your code: |
| 26 | + |
| 27 | +```go |
| 28 | +import "github.com/larapulse/migrator" |
| 29 | +``` |
| 30 | + |
| 31 | +## Quick start |
| 32 | + |
| 33 | +Initialize migrator with migration entries: |
| 34 | + |
| 35 | +```go |
| 36 | +var migrations = []migrator.Migration{ |
| 37 | + { |
| 38 | + Name: "19700101_0001_create_posts_table", |
| 39 | + Up: func() migrator.Schema { |
| 40 | + var s migrator.Schema |
| 41 | + posts := migrator.Table{Name: "posts"} |
| 42 | + |
| 43 | + posts.UniqueID("id") |
| 44 | + posts.Column("title", migrator.String{Precision: 64}) |
| 45 | + posts.Column("content", migrator.Text{}) |
| 46 | + posts.Timestamps() |
| 47 | + |
| 48 | + s.CreateTable(posts) |
| 49 | + |
| 50 | + return s |
| 51 | + }, |
| 52 | + Down: func() migrator.Schema { |
| 53 | + var s migrator.Schema |
| 54 | + |
| 55 | + s.DropTable("posts") |
| 56 | + |
| 57 | + return s |
| 58 | + }, |
| 59 | + }, |
| 60 | + { |
| 61 | + Name: "19700101_0002_create_comments_table", |
| 62 | + Up: func() migrator.Schema { |
| 63 | + var s migrator.Schema |
| 64 | + comments := migrator.Table{Name: "comments"} |
| 65 | + |
| 66 | + comments.UniqueID("id") |
| 67 | + comments.UUID("post_id", "", false) |
| 68 | + comments.Column("title", migrator.String{Precision: 64}) |
| 69 | + comments.Column("content", migrator.Text{}) |
| 70 | + comments.Timestamps() |
| 71 | + |
| 72 | + comments.Foreign("post_id", "id", "posts", "RESTRICT", "RESTRICT") |
| 73 | + |
| 74 | + s.CreateTable(comments) |
| 75 | + |
| 76 | + return s |
| 77 | + }, |
| 78 | + Down: func() migrator.Schema { |
| 79 | + var s migrator.Schema |
| 80 | + |
| 81 | + s.DropTable("comments") |
| 82 | + |
| 83 | + return s |
| 84 | + }, |
| 85 | + }, |
| 86 | +} |
| 87 | + |
| 88 | +m := migrator.Migrator{Pool: migrations} |
| 89 | +migrated, err = m.Migrate(db) |
| 90 | + |
| 91 | +if err != nil { |
| 92 | + fmt.Errorf("Could not migrate: %v", err) |
| 93 | + os.Exit(1) |
| 94 | +} |
| 95 | + |
| 96 | +if len(migrated) == 0 { |
| 97 | + fmt.Println("Nothing were migrated.") |
| 98 | +} |
| 99 | + |
| 100 | +for _, m := range migrated { |
| 101 | + fmt.Println("Migration: "+m+" was migrated ✅") |
| 102 | +} |
| 103 | + |
| 104 | +fmt.Println("Migration did run successfully") |
| 105 | +``` |
| 106 | + |
| 107 | +After first migration run, `migrations` table will be created: |
| 108 | + |
| 109 | +``` |
| 110 | ++----+-------------------------------------+-------+---------------------+ |
| 111 | +| id | name | batch | applied_at | |
| 112 | ++----+-------------------------------------+-------+---------------------+ |
| 113 | +| 1 | 19700101_0001_create_posts_table | 1 | 2020-06-27 00:00:00 | |
| 114 | +| 2 | 19700101_0002_create_comments_table | 1 | 2020-06-27 00:00:00 | |
| 115 | ++----+-------------------------------------+-------+---------------------+ |
| 116 | +``` |
| 117 | + |
| 118 | +If you want to use another name for migration table, change it `Migrator` before running migrations: |
| 119 | + |
| 120 | +```go |
| 121 | +m := migrator.Migrator{TableName: "_my_app_migrations"} |
| 122 | +``` |
| 123 | + |
| 124 | +### Transactional migration |
| 125 | + |
| 126 | +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: |
| 127 | + |
| 128 | +```go |
| 129 | +var migration = migrator.Migration{ |
| 130 | + Name: "19700101_0001_create_posts_and_users_tables", |
| 131 | + Up: func() migrator.Schema { |
| 132 | + var s migrator.Schema |
| 133 | + posts := migrator.Table{Name: "posts"} |
| 134 | + posts.UniqueID("id") |
| 135 | + posts.Timestamps() |
| 136 | + |
| 137 | + users := migrator.Table{Name: "users"} |
| 138 | + users.UniqueID("id") |
| 139 | + users.Timestamps() |
| 140 | + |
| 141 | + s.CreateTable(posts) |
| 142 | + s.CreateTable(users) |
| 143 | + |
| 144 | + return s |
| 145 | + }, |
| 146 | + Down: func() migrator.Schema { |
| 147 | + var s migrator.Schema |
| 148 | + |
| 149 | + s.DropTable("users") |
| 150 | + s.DropTable("posts") |
| 151 | + |
| 152 | + return s |
| 153 | + }, |
| 154 | + Transaction: true, |
| 155 | +} |
| 156 | +``` |
| 157 | + |
| 158 | +### Rollback and revert |
| 159 | + |
| 160 | +In case you need to revert your deploy and DB, you can revert last migrated batch: |
| 161 | + |
| 162 | +```go |
| 163 | +m := migrator.Migrator{Pool: migrations} |
| 164 | +reverted, err := m.Rollback(db) |
| 165 | + |
| 166 | +if err != nil { |
| 167 | + fmt.Errorf("Could not roll back migrations: %v", err) |
| 168 | + os.Exit(1) |
| 169 | +} |
| 170 | + |
| 171 | +if len(reverted) == 0 { |
| 172 | + fmt.Println("Nothing were rolled back.") |
| 173 | +} |
| 174 | + |
| 175 | +for _, m := range reverted { |
| 176 | + fmt.Println("Migration: "+m+" was rolled back ✅") |
| 177 | +} |
| 178 | +``` |
| 179 | + |
| 180 | +To revert all migrated items back, you have to call `Revert()` on your `migrator`: |
| 181 | + |
| 182 | +```go |
| 183 | +m := migrator.Migrator{Pool: migrations} |
| 184 | +reverted, err := m.Revert(db) |
| 185 | +``` |
| 186 | + |
| 187 | +## Customize queries |
| 188 | + |
| 189 | +You may add any column definition to the database on your own, just be sure you implement `columnType` interface: |
| 190 | + |
| 191 | +```go |
| 192 | +type customType string |
| 193 | + |
| 194 | +func (ct customType) buildRow() string { |
| 195 | + return string(ct) |
| 196 | +} |
| 197 | + |
| 198 | +posts := migrator.Table{Name: "posts"} |
| 199 | +posts.UniqueID("id") |
| 200 | +posts.Column("data", customType("json not null")) |
| 201 | +posts.Timestamps() |
| 202 | +``` |
| 203 | + |
| 204 | +Same logic is for adding custom commands to the Schema to be migrated or reverted, just be sure you implement `command` interface: |
| 205 | + |
| 206 | +```go |
| 207 | +type customCommand string |
| 208 | + |
| 209 | +func (cc customCommand) toSQL() string { |
| 210 | + return string(cc) |
| 211 | +} |
| 212 | + |
| 213 | +var s migrator.Schema |
| 214 | + |
| 215 | +c := customCommand("DROP PROCEDURE abc") |
| 216 | +s.CustomCommand(c) |
| 217 | +``` |
0 commit comments