go-fast-migrate

command module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: May 12, 2021 License: Apache-2.0 Imports: 10 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

go-fast-migrate

Like PHP Lavarel Framework, Use go-fast-migrate to Manage your database

简介

所谓迁移就像是数据库的版本控制,这种机制允许团队简单轻松的编辑并共享应用的数据库表结构。

迁移可以很容易地构建应用的数据库表结构。如果你曾经频繁告知团队成员需要手动添加列到本地数据库表结构以维护本地开发环境,那么这正是数据库迁移所致力于解决的问题。

编译
go build -o migrate main.go
新建迁移文件
命令

命令:./migrate new xxxxx

示例
  • 创建迁移文件
./migrate new create_user_table

执行以上命令后,将会在migrations目录生成迁移文件:

devops@localhost migrate % ls -al migrations 
total 16
drwxr-xr-x   4 zhoufei  staff   128  5 10 20:30 .
drwxr-xr-x  14 zhoufei  staff   448  5 10 20:38 ..
-rw-r--r--   1 zhoufei  staff  1338  5 10 20:30 2021_05_10_182112_create_user_table.go
-rw-r--r--   1 zhoufei  staff   113  5  9 22:40 init.go

生成的迁移文件如下:

// UP 迁移操作
func up()  {
	fmt.Println("CreateUserTable2021_05_12_210511 Up")
	// Write your migrate action here
	// 创建用户表
	schema.NewSchema().CreateTable("user", func(builder *schema.Builder) {
		builder.BigIncrements("id")
		builder.String("open_id", 32).Unique().Default("").Comment("平台用户标识")
		builder.String("nickname", 64).Default("").Comment("用户在平台昵称")
		builder.String("avatar").Default("").Comment("用户在平台头像")
		builder.UnsignedInteger("last_login_time").Default(0).Comment("最新登录时间")
		builder.UnsignedInteger("login_times").Default(0).Comment("登陆次数")
		builder.UnsignedInteger("continue_login_days").Default(0).Comment("连续登陆天数")
		builder.UnsignedInteger("date").Default(0).Comment("注册日期")
		builder.String("qq", 20).Unique().Default("").Comment("用户QQ号")
		builder.UnsignedTinyInteger("status").Default(0).Comment("用户状态,0:正常;1:可疑;2:封禁")

		builder.Timestamps()
	})
}

// Down 迁移回滚
func down()  {
	fmt.Println("CreateUserTable2021_05_12_210511 Down")
	// Write your rollback action here
	// 创建用户表
	schema.NewSchema().DropTable("user")
}

其中,我们需要在up()中补充创建表/修改表等操作,在down()方法中实现up()操作的回退。

以创建用户表操作为例:

func up() {
	// 创建用户表
	schema.NewSchema().CreateTable("user", func(builder *schema.Builder) {
		builder.BigIncrements("id")
		builder.String("open_id", 32).Unique().Default("").Comment("平台用户标识")
		builder.String("nickname", 64).Default("").Comment("用户在平台昵称")
		builder.String("avatar").Default("").Comment("用户在平台头像")
		builder.UnsignedInteger("last_login_time").Default(0).Comment("最新登录时间")
		builder.UnsignedInteger("login_times").Default(0).Comment("登陆次数")
		builder.UnsignedInteger("continue_login_days").Default(0).Comment("连续登陆天数")
		builder.UnsignedInteger("date").Default(0).Comment("注册日期")
		builder.String("qq", 20).Unique().Default("").Comment("用户QQ号")
		builder.UnsignedTinyInteger("status").Default(0).Comment("用户状态,0:正常;1:可疑;2:封禁")

		builder.Timestamps()
	})
}

func down() {
	// 删除用户表
	schema.NewSchema().DropTableIfExists("user")
}
执行迁移
./migrate start

执行迁移后,可以看到数据库中增加了user表:

mysql> desc user;
+---------------------+------------------+------+-----+---------+----------------+
| Field               | Type             | Null | Key | Default | Extra          |
+---------------------+------------------+------+-----+---------+----------------+
| id                  | bigint unsigned  | NO   | PRI | NULL    | auto_increment |
| open_id             | varchar(32)      | NO   | UNI |         |                |
| nickname            | varchar(64)      | NO   |     |         |                |
| avatar              | varchar(255)     | NO   |     |         |                |
| last_login_time     | int unsigned     | NO   |     | 0       |                |
| login_times         | int unsigned     | NO   |     | 0       |                |
| continue_login_days | int unsigned     | NO   |     | 0       |                |
| date                | int unsigned     | NO   |     | 0       |                |
| qq                  | varchar(20)      | NO   | UNI |         |                |
| status              | tinyint unsigned | NO   |     | 0       |                |
| created_at          | timestamp        | YES  |     | NULL    |                |
| updated_at          | timestamp        | YES  |     | NULL    |                |
+---------------------+------------------+------+-----+---------+----------------+
12 rows in set (0.01 sec)

执行回滚
./migrate rollback

执行回滚后,user表将被删除。

重新迁移
./migrate refresh

其他

  • 支持多数据库源

Documentation

The Go Gopher

There is no documentation for this package.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
⌂ 换网址