教程集 www.jiaochengji.com
教程集 >  Golang编程  >  golang教程  >  正文 golang版本的数据库迁移工具(数据库升级回滚)

golang版本的数据库迁移工具(数据库升级回滚)

发布时间:2023-03-09   编辑:jiaochengji.com
教程集为您提供golang版本的数据库迁移工具(数据库升级回滚)等资源,欢迎您收藏本站,我们将为您提供最新的golang版本的数据库迁移工具(数据库升级回滚)资源

我们在工作中,总会用到数据库升级回滚、版本管理用具,最近迷恋go语言的工具,所有就开始使用golang版本的数据库迁移工具

1、数据库迁移

  • darwin - Go 实现的数据库 schema 演进库
  • goose - 数据库迁移工具。可通过创建增量 SQL 或 Go 脚本来管理数据库的演变
  • gormigrate - Gorm ORM 的数据库迁移助手
  • migrate - Go 实现的数据库迁移处理,支持 MySQL, PostgreSQL, Cassandra, 和 SQLite
  • pravasan - 简单的迁移工具,目前支持 MySQL,PostgreSQL,但计划很快支持 SQLite, MongoDB 等
  • soda - 具有数据库迁移、创建和 ORM 等功能,适用于 MySQL, PostgreSQL, 和 SQLite
  • sql-migrate - 数据库 schema 迁移工具。允许使用 go-bindata 将迁移嵌入到应用程序中

在众多的迁移工具中,我们利用工具活跃度,以前升级回滚的操作方便性,我们选择了migrate最为我们项目组的数据库升级工具

2、migrate编译

在开发项目中,我们是用的公司自己的数据库,对migrate依赖的库pq需要进行替换,所以我们要自己编译出包,编译方法如下:

#!/bin/bash

#gopath
gopath=`go env GOPATH`
if [ -z $gopath ]; then
	echo "gopath is null"
	exit 1
fi

#创建目录
migrate_path=${gopath}/src/github.com/golang-migrate/migrate/v4
mkdir -p ${migrate_path}
tar -zxvf migrate-4.3.1.tar.gz
#由于我们go版本还没用go mod特性,所以我们自己下载依赖包进行编译
cd migrate
go mod vendor -v
rm -fr go.mod go.sum
cd ..
cp -fr ./migrate/* ${migrate_path}

rm -fr ./migrate

#替换pg库,开源的pg连接数据库会报错

cd ${migrate_path}

#编译出包
#DATABASES="postgres mysql redshift cassandra spanner cockroachdb clickhouse mongodb"
#我们只需要支持gaussdb以及mysql
DATABASES="postgres mysql"
#我们只需要支持gaussdb以及mysql
#SOURCES="file go_bindata github aws_s3 google_cloud_storage godoc_vfs gitlab"
#我们只需要支持file形式
SOURCES="file"
VERSION=4.3.1
go build -a -o build/dbmigrate -ldflags="-X main.Version=${VERSION}" -tags "$DATABASES $SOURCES" ./cmd/migrate

我们还没用到go mod管理依赖包,可以参考https://blog.csdn.net/grace_yi/article/details/90232800 下载依赖包

3、migrate的使用方法

migrate --help
Usage: migrate OPTIONS COMMAND [arg...]
migrate [ -version | -help ]

Options:
-source Location of the migrations (driver://url)
-path Shorthand for -source=file://path
-database Run migrations against this database (driver://url)
-prefetch N Number of migrations to load in advance before executing (default 10)
-lock-timeout N Allow N seconds to acquire database lock (default 15)
-verbose Print verbose logging
-version Print version
-help Print usage

Commands:
create [-ext E] [-dir D] [-seq] [-digits N] [-format] NAME
Create a set of timestamped up/down migrations titled NAME, in directory D with extension E.
Use -seq option to generate sequential up/down migrations with N digits.
Use -format option to specify a Go time format string.
goto V Migrate to version V
up [N] Apply all or N up migrations
down [N] Apply all or N down migrations
drop Drop everything inside database
force V Set version V but don't run migration (ignores dirty state)
version Print current migration version

Source drivers: file
Database drivers: mysql, postgres, postgresql, stub

比如:

清空数据库

migrate -path /tmp/dbmigrate/source -database postgres://csdn:密码@192.168.120.213:33018/CSDN drop

升级数据库到最新的版本数据库

migrate -path /tmp/dbmigrate/source -database postgres://csdn:密码@192.168.120.213:33018/CSDN up

回滚数据库

migrate -path /tmp/dbmigrate/source -database postgres://csdn:密码@192.168.120.213:33018/CSDN down

指定数据库升级或者回滚到某个版本

migrate -path /tmp/dbmigrate/source -database postgres://csdn:密码@192.168.120.213:33018/CSDN goto N

 

参考文档:

https://studygolang.com/articles/9434

https://github.com/golang-migrate/migrate

到此这篇关于“golang版本的数据库迁移工具(数据库升级回滚)”的文章就介绍到这了,更多文章或继续浏览下面的相关文章,希望大家以后多多支持JQ教程网!

您可能感兴趣的文章:
不停机状态下使用Django创建索引
Drupal配置迁移详细讨论
使用存储过程sp_dbcmptlevel对SQL Server 2005调整兼容级别
flask数据库迁移是什么意思
mha是python写的吗
手工升级mysql数据库方法
如何获取DB2 V9.5版本中的DPF许可证
golang 大数据平台_从数据仓库到大数据平台再到数据中台
Yii 2 创建 migration 给表添加字段的例子
如何搭建django虚拟环境

[关闭]
~ ~