golang数据库迁移管理插件godfish支持Cassandra/MySQL/PostgreSQL/SQLite3原生查询
Golang数据库迁移管理插件godfish支持Cassandra/MySQL/PostgreSQL/SQLite3原生查询
godfish是一个用Golang编写的数据库迁移管理工具,类似于优秀的dogfish,但专注于Golang生态。
主要特点
- 在迁移文件中使用数据库原生查询语言,不需要高级DSL
- 支持多种数据库
- 依赖轻量
- 提供清晰的错误信息
支持的数据库
godfish支持以下数据库的原生查询:
- Cassandra
- MySQL
- PostgreSQL
- SQLite3
- SQL Server
安装与构建
预构建版本
GitHub仓库的Releases页面提供了预构建的二进制文件,每个归档文件包含针对不同数据库的可执行文件。
自行构建
可以为特定数据库构建CLI二进制文件:
make build-cassandra
make build-mysql
make build-postgres
make build-sqlite3
make build-sqlserver
构建完成后可以将二进制文件移动到$GOPATH/bin
或项目目录中。
使用示例
基本命令
# 查看帮助
godfish help
godfish -h
godfish <command> -h
数据库连接
数据库连接参数从环境变量读取:
export DB_DSN="your_database_connection_string"
配置文件
创建配置文件:
godfish init
这会创建.godfish.json
配置文件,内容类似:
{
"path_to_files": "db/migrations"
}
可以指定不同的配置文件路径:
godfish -conf custom_config.json
创建迁移
# 创建可逆迁移
godfish create-migration -name alpha
# 输出:
# db/migrations/forward-20200128070010-alpha.sql
# db/migrations/reverse-20200128070010-alpha.sql
# 创建不可逆迁移
godfish create-migration -name bravo -reversible=false
# 输出:
# db/migrations/forward-20200128070106-bravo.sql
应用迁移
# 应用所有迁移
godfish migrate
# 应用到特定版本
godfish migrate -version 20060102150405
# 查看迁移状态
godfish info
# 回滚迁移
godfish rollback
# 重新应用最后一个迁移
godfish remigrate
# 查看版本信息
godfish version
godfish version -json
迁移文件示例
MySQL迁移示例
forward-20200128070010-create_users.sql
:
CREATE TABLE users (
id INT AUTO_INCREMENT PRIMARY KEY,
username VARCHAR(255) NOT NULL UNIQUE,
email VARCHAR(255) NOT NULL UNIQUE,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
reverse-20200128070010-create_users.sql
:
DROP TABLE users;
PostgreSQL迁移示例
forward-20200128070106-add_roles.sql
:
CREATE TYPE user_role AS ENUM ('admin', 'editor', 'viewer');
ALTER TABLE users ADD COLUMN role user_role NOT NULL DEFAULT 'viewer';
reverse-20200128070106-add_roles.sql
:
ALTER TABLE users DROP COLUMN role;
DROP TYPE user_role;
SQLite3迁移示例
forward-20200128070203-add_indexes.sql
:
CREATE INDEX idx_users_email ON users(email);
CREATE INDEX idx_users_created_at ON users(created_at);
reverse-20200128070203-add_indexes.sql
:
DROP INDEX idx_users_email;
DROP INDEX idx_users_created_at;
Cassandra迁移示例
forward-20200128070304-create_keyspace.sql
:
CREATE KEYSPACE IF NOT EXISTS myapp
WITH replication = {
'class': 'SimpleStrategy',
'replication_factor': 1
};
reverse-20200128070304-create_keyspace.sql
:
DROP KEYSPACE myapp;
文件命名规范
godfish使用以下命名规范:
- 正向迁移:
forward-${date}-${name}.sql
- 反向迁移:
reverse-${date}-${name}.sql
例如:
forward-20191112050547-init_foos.sql
forward-20191127051242-add_bars.sql
reverse-20191112050547-init_foos.sql
reverse-20191127051242-add_bars.sql
测试
使用Docker创建测试环境并运行测试:
# 启动测试环境
make -f ci.Makefile ci-postgres14-up
make -f ci.Makefile ci-mysql-up
# 运行测试
# ...
# 关闭测试环境
make -f ci.Makefile ci-postgres14-down
make -f ci.Makefile ci-mysql-down
godfish是一个功能强大且灵活的数据库迁移工具,特别适合需要在多种数据库环境中工作的Golang项目。
更多关于golang数据库迁移管理插件godfish支持Cassandra/MySQL/PostgreSQL/SQLite3原生查询的实战教程也可以访问 https://www.itying.com/category-94-b0.html
更多关于golang数据库迁移管理插件godfish支持Cassandra/MySQL/PostgreSQL/SQLite3原生查询的实战系列教程也可以访问 https://www.itying.com/category-94-b0.html
Godfish - 多数据库迁移管理工具
Godfish 是一个用 Go 语言编写的数据库迁移管理工具,支持多种数据库系统,包括 Cassandra、MySQL、PostgreSQL 和 SQLite3。它提供了原生查询支持,使得开发者可以针对不同数据库编写特定的迁移脚本。
主要特性
- 支持多种数据库系统
- 原生查询支持
- 版本控制
- 迁移回滚功能
- 命令行工具集成
安装
go get github.com/rafaelespinoza/godfish
基本用法
初始化迁移项目
package main
import (
"github.com/rafaelespinoza/godfish"
"github.com/rafaelespinoza/godfish/drivers/mysql"
)
func main() {
// 选择数据库驱动
driver := mysql.NewDriver()
// 初始化迁移目录
err := godfish.Init(driver, "migrations")
if err != nil {
panic(err)
}
}
创建新迁移
package main
import (
"github.com/rafaelespinoza/godfish"
"github.com/rafaelespinoza/godfish/drivers/postgres"
)
func main() {
driver := postgres.NewDriver()
// 创建新的迁移文件
err := godfish.Create(driver, "migrations", "create_users_table")
if err != nil {
panic(err)
}
}
编写迁移脚本
Godfish 会为每个迁移创建两个文件:<timestamp>_<name>_up.sql
和 <timestamp>_<name>_down.sql
。
示例 MySQL 迁移文件 (up):
CREATE TABLE users (
id INT AUTO_INCREMENT PRIMARY KEY,
username VARCHAR(255) NOT NULL,
email VARCHAR(255) NOT NULL UNIQUE,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
对应的回滚文件 (down):
DROP TABLE users;
执行迁移
package main
import (
"database/sql"
"fmt"
"github.com/rafaelespinoza/godfish"
"github.com/rafaelespinoza/godfish/drivers/mysql"
_ "github.com/go-sql-driver/mysql"
)
func main() {
// 建立数据库连接
db, err := sql.Open("mysql", "user:password@tcp(127.0.0.1:3306)/dbname")
if err != nil {
panic(err)
}
defer db.Close()
// 初始化驱动
driver := mysql.NewDriverWithDB(db)
// 执行所有待处理的迁移
err = godfish.Migrate(driver, "migrations", godfish.Forward)
if err != nil {
panic(err)
}
fmt.Println("Migrations applied successfully!")
}
回滚迁移
// 回滚最近一次迁移
err = godfish.Migrate(driver, "migrations", godfish.Backward)
if err != nil {
panic(err)
}
多数据库支持示例
PostgreSQL 示例
package main
import (
"database/sql"
"github.com/rafaelespinoza/godfish"
"github.com/rafaelespinoza/godfish/drivers/postgres"
_ "github.com/lib/pq"
)
func main() {
db, err := sql.Open("postgres", "postgres://user:password@localhost/dbname?sslmode=disable")
if err != nil {
panic(err)
}
defer db.Close()
driver := postgres.NewDriverWithDB(db)
// 执行迁移
err = godfish.Migrate(driver, "migrations", godfish.Forward)
if err != nil {
panic(err)
}
}
SQLite 示例
package main
import (
"database/sql"
"github.com/rafaelespinoza/godfish"
"github.com/rafaelespinoza/godfish/drivers/sqlite"
_ "github.com/mattn/go-sqlite3"
)
func main() {
db, err := sql.Open("sqlite3", "./test.db")
if err != nil {
panic(err)
}
defer db.Close()
driver := sqlite.NewDriverWithDB(db)
// 执行迁移
err = godfish.Migrate(driver, "migrations", godfish.Forward)
if err != nil {
panic(err)
}
}
Cassandra 示例
package main
import (
"github.com/gocql/gocql"
"github.com/rafaelespinoza/godfish"
"github.com/rafaelespinoza/godfish/drivers/cassandra"
)
func main() {
cluster := gocql.NewCluster("127.0.0.1")
cluster.Keyspace = "mykeyspace"
session, err := cluster.CreateSession()
if err != nil {
panic(err)
}
defer session.Close()
driver := cassandra.NewDriverWithSession(session)
// 执行迁移
err = godfish.Migrate(driver, "migrations", godfish.Forward)
if err != nil {
panic(err)
}
}
高级功能
条件迁移
Godfish 允许你在迁移脚本中使用数据库特定的语法:
-- PostgreSQL 示例
CREATE TABLE IF NOT EXISTS users (
id SERIAL PRIMARY KEY,
name VARCHAR(100) NOT NULL
);
-- MySQL 示例
CREATE TABLE IF NOT EXISTS users (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(100) NOT NULL
);
事务支持
Godfish 支持事务性迁移,确保迁移要么完全成功,要么完全失败:
// 在迁移时启用事务
err = godfish.Migrate(driver, "migrations", godfish.Forward, godfish.WithTransaction(true))
if err != nil {
panic(err)
}
总结
Godfish 是一个功能强大的多数据库迁移工具,它支持多种流行的数据库系统,并提供原生查询支持。通过简单的 API 和清晰的迁移文件结构,Godfish 使得数据库版本控制和迁移管理变得简单可靠。
无论是小型项目还是大型企业应用,Godfish 都能提供灵活的数据库迁移解决方案,帮助开发者更好地管理数据库结构变更。