Skip to content

Instantly share code, notes, and snippets.

@islishude
Created August 20, 2019 08:25
Show Gist options
  • Save islishude/c069acf8189bf4f9359fea365891bdb2 to your computer and use it in GitHub Desktop.
Save islishude/c069acf8189bf4f9359fea365891bdb2 to your computer and use it in GitHub Desktop.
golang sql migrate tools

下载:

go get -tags 'mysql' -u -v github.com/golang-migrate/migrate/cmd/migrate

创建名称为 test 的数据库:

create database test;

创建第一个 migration

mkdir migrations
migrate create -ext=sql -dir=migrations -seq init

编辑 migration/000001_init.up.sql

CREATE TABLE IF NOT EXISTS `users`(
   `user_id` serial PRIMARY KEY,
   `username` VARCHAR (50) UNIQUE NOT NULL,
   `password` VARCHAR (50) NOT NULL,
   `email` VARCHAR (300) UNIQUE NOT NULL
);

编辑 migration/000001_init.down.sql

drop table if exists `users`;

migrate

migrate -path=./migration -database="mysql://root:root@tcp(127.0.0.1:3306)/test" up

ok!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment