mysql migrate
Fork from sql-migrate
- Installed golang
- 定義 env 變數, 其中
Environment.Dir
為 sql folder - 建立 sql folder, 將 sql files 放置該目錄底下, 檔名格式為
yyyyMMddHHmmss-簡述.sql
import (
command "github.com/chiehting/db-migrate/command"
)
func main() {
env := &command.Environment{
Dialect: "mysql",
DataSource: "root:123456@tcp(localhost:3306)/bac?parseTime=true",
Dir: "./sql"}
command.SetEnvironment(env)
command.SetIgnoreUnknown(true)
Upcommand := command.UpCommand{}
if err := Upcommand.RunProcess([]string{}); err != nil {
panic(err.Error())
}
}
$ go build -o db-migrate ./main
$ cat dbconfig.yml
development:
dialect: mysql
datasource: root:123456@tcp(localhost:3306)/bac?parseTime=true
dir: sql
$ ./db-migrate status
+---------------+---------+
| MIGRATION | APPLIED |
+---------------+---------+
| 1-initial.sql | no |
| 2-record.sql | no |
+---------------+---------+
$ ./db-migrate up
Applied 2 migrations
$ ./db-migrate status
+---------------+-------------------------------+
| MIGRATION | APPLIED |
+---------------+-------------------------------+
| 1-initial.sql | 2020-08-07 12:44:20 +0000 UTC |
| 2-record.sql | 2020-08-07 12:44:20 +0000 UTC |
+---------------+-------------------------------+
$ ./db-migrate down
Applied 1 migration
$ ./db-migrate status
+---------------+-------------------------------+
| MIGRATION | APPLIED |
+---------------+-------------------------------+
| 1-initial.sql | 2020-08-07 12:44:20 +0000 UTC |
| 2-record.sql | no |
+---------------+-------------------------------+