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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章