从遮罩/迁移分叉
数据库驱动程序运行迁移。添加新数据库?
数据库连接字符串通过 URL 指定。URL 格式取决于驱动程序,但通常具有以下格式:
dbdriver://username:password@host:port/dbname?param1=true¶m2=false
任何保留的 URL 字符都需要转义。注意,字符也需要转义
%
显式地,需要对以下字符进行转义:、、
!
#
$
%
&
'
(
)
*
+
,
/
:
;
=
?
@
[
]
最简单的方法是始终通过 URL 编码器运行数据库连接 URL 的 URL 部分(例如用户名、密码等)。请参阅下面的示例 Python 片段:
$ python3 -c 'import urllib.parse; print(urllib.parse.quote(input("String to encode: "), ""))'
String to encode: FAKEpassword!#$%&'()*+,/:;=?@[]
FAKEpassword%21%23%24%25%26%27%28%29%2A%2B%2C%2F%3A%3B%3D%3F%40%5B%5D
$ python2 -c 'import urllib; print urllib.quote(raw_input("String to encode: "), "")'
String to encode: FAKEpassword!#$%&'()*+,/:;=?@[]
FAKEpassword%21%23%24%25%26%27%28%29%2A%2B%2C%2F%3A%3B%3D%3F%40%5B%5D
$
源驱动程序从本地或远程源读取迁移。添加新源?
$ migrate -source file://path/to/migrations -database postgres://localhost:5432/database up 2
$ docker run -v {{ migration dir }}:/migrations --network host migrate/migrate
-path=/migrations/ -database postgres://localhost:5432/database up 2
GracefulStop chan bool
io.Reader
import (
"github.com/golang-migrate/migrate/v4"
_ "github.com/golang-migrate/migrate/v4/database/postgres"
_ "github.com/golang-migrate/migrate/v4/source/github"
)
func main() {
m, err := migrate.New(
"github://mattes:personal-access-token@mattes/migrate_test",
"postgres://localhost:5432/database?sslmode=enable")
m.Steps(2)
}
想要使用现有的数据库客户端?
import (
"database/sql"
_ "github.com/lib/pq"
"github.com/golang-migrate/migrate/v4"
"github.com/golang-migrate/migrate/v4/database/postgres"
_ "github.com/golang-migrate/migrate/v4/source/file"
)
func main() {
db, err := sql.Open("postgres", "postgres://localhost:5432/database?sslmode=enable")
driver, err := postgres.WithInstance(db, &postgres.Config{})
m, err := migrate.NewWithDatabaseInstance(
"file:///migrations",
"postgres", driver)
m.Up() // or m.Step(2) if you want to explicitly set the number of migrations to run
}
转到入门
(更多教程即将推出)
每个迁移都有一个向上和向下的迁移。为什么?
1481574547_create_users_table.up.sql
1481574547_create_users_table.down.sql
版本 | 支持? | 进口 | 笔记 |
---|---|---|---|
主人 | import "github.com/golang-migrate/migrate/v4" |
新功能和错误修复首先到达此处 | |
v4 | import "github.com/golang-migrate/migrate/v4" |
用于稳定版本 | |
v3 |
import "github.com/golang-migrate/migrate"(使用包管理器)或(不推荐) import "gopkg.in/golang-migrate/migrate.v3" |
不使用 - 不再支持 |
另请查看常见问题解答。