golang-migrate/migrate
GitHub: golang-migrate/migrate
一个用 Go 编写的、支持 CLI 与库模式的数据库 Schema 迁移工具,旨在以版本化、可回滚的方式管理多种数据库的结构变更。
Stars: 18253 | Forks: 1559
[](https://github.com/golang-migrate/migrate/actions/workflows/ci.yaml?query=branch%3Amaster)
[](https://pkg.go.dev/github.com/golang-migrate/migrate/v4)
[](https://coveralls.io/github/golang-migrate/migrate?branch=master)
[](https://packagecloud.io/golang-migrate/migrate?filter=debs)
[](https://hub.docker.com/r/migrate/migrate/)

[](https://github.com/golang-migrate/migrate/releases)
[](https://goreportcard.com/report/github.com/golang-migrate/migrate/v4)
# 迁移
__使用 Go 编写的数据库迁移工具。可作为 [CLI](#cli-usage) 使用或作为 [库](#use-in-your-go-project) 导入。__
* Migrate 从[源](#migration-sources)读取迁移内容
并按正确的顺序将其应用到[数据库](#databases)。
* 驱动程序是“笨”的,migrate 将所有内容粘合在一起并确保逻辑无懈可击。
(这也保持了驱动程序的轻量化。)
* 数据库驱动程序不假设任何情况,也不尝试纠正用户输入。如有疑问,直接失败。
Forked from [mattes/migrate](https://github.com/mattes/migrate)
## 数据库
数据库驱动程序用于运行迁移。[添加新数据库?](database/driver.go)
* [PostgreSQL](database/postgres)
* [PGX v4](database/pgx)
* [PGX v5](database/pgx/v5)
* [Redshift](database/redshift)
* [Ql](database/ql)
* [Cassandra / ScyllaDB](database/cassandra)
* [SQLite](database/sqlite)
* [SQLite3](database/sqlite3) ([todo #165](https://github.com/mattes/migrate/issues/165))
* [SQLCipher](database/sqlcipher)
* [MySQL / MariaDB](database/mysql)
* [Neo4j](database/neo4j)
* [MongoDB](database/mongodb)
* [CrateDB](database/crate) ([todo #170](https://github.com/mattes/migrate/issues/170))
* [Shell](database/shell) ([todo #171](https://github.com/mattes/migrate/issues/171))
* [Google Cloud Spanner](database/spanner)
* [CockroachDB](database/cockroachdb)
* [YugabyteDB](database/yugabytedb)
* [ClickHouse](database/clickhouse)
* [Firebird](database/firebird)
* [MS SQL Server](database/sqlserver)
* [rqlite](database/rqlite)
### 数据库 URL
数据库连接字符串通过 URL 指定。URL 格式取决于驱动程序,但通常采用以下形式:`dbdriver://username:password@host:port/dbname?param1=true¶m2=false`
任何[保留的 URL 字符](https://en.wikipedia.org/wiki/Percent-encoding#Percent-encoding_reserved_characters)都需要进行转义。注意,`%` 字符也[需要转义](https://en.wikipedia.org/wiki/Percent-encoding#Percent-encoding_the_percent_character)
明确地说,以下字符需要转义:
`!`, `#`, `$`, `%`, `&`, `'`, `(`, `)`, `*`, `+`, `,`, `/`, `:`, `;`, `=`, `?`, `@`, `[`, `]`
最简单的方法是始终将数据库连接 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
$
```
## 迁移源
源驱动程序从本地或远程源读取迁移。[添加新源?](source/driver.go)
* [Filesystem](source/file) - 从文件系统读取
* [io/fs](source/iofs) - 从 Go [io/fs](https://pkg.go.dev/io/fs#FS) 读取
* [Go-Bindata](source/go_bindata) - 从嵌入的二进制数据读取 ([jteeuwen/go-bindata](https://github.com/jteeuwen/go-bindata))
* [pkger](source/pkger) - 从嵌入的二进制数据读取 ([markbates/pkger](https://github.com/markbates/pkger))
* [GitHub](source/github) - 从远程 GitHub 仓库读取
* [GitHub Enterprise](source/github_ee) - 从远程 GitHub Enterprise 仓库读取
* [Bitbucket](source/bitbucket) - 从远程 Bitbucket 仓库读取
* [Gitlab](source/gitlab) - 从远程 Gitlab 仓库读取
* [AWS S3](source/aws_s3) - 从 Amazon Web Services S3 读取
* [Google Cloud Storage](source/google_cloud_storage) - 从 Google Cloud Platform Storage 读取
## CLI 用法
* 此库的简单封装。
* 优雅地处理 ctrl+c (SIGINT)。
* 没有配置搜索路径,没有配置文件,没有神奇的环境变量注入。
[CLI 文档](cmd/migrate)(包含 CLI 安装说明)
### 基本用法
```
$ migrate -source file://path/to/migrations -database postgres://localhost:5432/database up 2
```
### Docker 用法
```
$ docker run -v {{ migration dir }}:/migrations --network host migrate/migrate
-path=/migrations/ -database postgres://localhost:5432/database up 2
```
## 在你的 Go 项目中使用
* 此版本 (v3 & v4) 的 API 已稳定并冻结。
* 使用 [Go modules](https://golang.org/cmd/go/#hdr-Modules__module_versions__and_more) 管理依赖。
* 为了帮助防止数据库损坏,它支持通过 `GracefulStop chan bool` 进行优雅停止。
* 自带日志记录器。
* 内部使用 `io.Reader` 流以保持低内存占用。
* 线程安全且无 goroutine 泄漏。
__[Go 文档](https://pkg.go.dev/github.com/golang-migrate/migrate/v4)__
```
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.Steps(2) if you want to explicitly set the number of migrations to run
}
```
## 入门指南
前往[入门指南](GETTING_STARTED.md)
## 教程
* [CockroachDB](database/cockroachdb/TUTORIAL.md)
* [PostgreSQL](database/postgres/TUTORIAL.md)
(更多教程即将推出)
## 迁移文件
每个迁移都有一个 up 和 down 迁移。[为什么?](FAQ.md#why-two-separate-files-up-and-down-for-a-migration)
```
1481574547_create_users_table.up.sql
1481574547_create_users_table.down.sql
```
[最佳实践:如何编写迁移。](MIGRATIONS.md)
## 来自其他数据库迁移工具?
查看 [migradaptor](https://github.com/musinit/migradaptor/)。
*注意:migradaptor 与本项目无关,也不受本项目支持*
## 版本
Version | Supported? | Import | Notes
--------|------------|--------|------
**master** | :white_check_mark: | `import "github.com/golang-migrate/migrate/v4"` | 新功能和错误修复首先到达此处 |
**v4** | :white_check_mark: | `import "github.com/golang-migrate/migrate/v4"` | 用于稳定版本 |
**v3** | :x: | `import "github.com/golang-migrate/migrate"` (使用包管理器) 或 `import "gopkg.in/golang-migrate/migrate.v3"` (不推荐) | **请勿使用** - 不再受支持 |
## 开发与贡献
是的,请 是你的朋友,
请阅读[开发指南](CONTRIBUTING.md)。
也请看看 [FAQ](FAQ.md)。
寻找替代方案?[https://awesome-go.com/#database](https://awesome-go.com/#database)。
标签:DNS解析, Docker, EVTX分析, Golang, Go语言, LNA, MongoDB, PostgreSQL, SQLite, 基础设施工具, 安全可观测性, 安全编程, 安全防御评估, 开发库, 开源框架, 开源项目, 持续集成, 数据库工具, 数据库迁移, 文档结构分析, 日志审计, 模式迁移, 测试用例, 版本控制, 程序破解, 请求拦截