You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
db2go is a command to export database table structure to go or proto file
Usage
导出数据表为go模型文件
[~@devhost] db2go go -h ok 15:04:28
NAME:
db2go go - Generate Go model files from database tables
USAGE:
db2go go [command options] [arguments...]
OPTIONS:
--url value, -u value data source name of database
--out value, -o value output directory (default: ".")
--table value, -t value database tables to export (prefix with - to exclude)
--package value, -P value package name
--without value exclude columns split by comma
--ssh value ssh tunnel e.g ssh://root:123456@192.168.1.23:22
--debug, -d open debug mode (default: false)
--tag value, -T value export tags for golang
--readonly value, -R value readonly columns split by comma
--spec-type value, -S value specify column as customized types, e.g 'user.detail=UserDetail, user.data=UserData'
--enable-decimal, -D decimal as sqlca.Decimal type (default: false)
--ddl value export database DDL to file
--help, -h show help (default: false)
导出数据表为protobuf文件
[~@devhost] db2go proto -h
NAME:
db2go proto - Generate protobuf files from database tables
USAGE:
db2go proto [command options] [arguments...]
OPTIONS:
--url value, -u value data source name of database
--out value, -o value output directory (default: ".")
--table value, -t value database tables to export (prefix with - to exclude)
--package value, -P value package name
--without value exclude columns split by comma
--ssh value ssh tunnel e.g ssh://root:123456@192.168.1.23:22
--debug, -d open debug mode (default: false)
--proto-options value, --po value set protobuf options, multiple options separated by ';'
--gogo-options value, --gogo value gogo proto options
--field-style value, --style value protobuf message field camel style (small or big)
--help, -h show help (default: false)
1. 编译安装
Ubuntu 20.04 or later
$ sudo apt update && sudo apt install -y make gcc
$ go env -w CGO_ENABLED=1
$ make
2. 数据库表导出到go文件
Windows batch 脚本
@echooffrem 输出文件根目录setOUT_DIR=.
rem 数据模型文件包名setPACK_NAME="models"rem 只读字段(不更新)setREAD_ONLY="created_at, updated_at"rem 指定或排除表名(不指定则整个数据库全部导出, 排除表名在表名前面加-)setTABLE_NAME="-user_roles"rem 忽略字段名(逗号分隔)setWITHOUT=""rem 添加标签setTAGS="gorm"rem 数据库连接源DSNsetDSN_URL="mysql://root:123456@127.0.0.1:3306/test?charset=utf8"rem 指定具体表对应字段类型(不指定表则全局生效)setSPEC_TYPES="users.is_deleted=bool"rem 数据库DDL文件setDDL_FILE="deploy/test.sql"rem 判断本地系统是否已安装db2go工具,没有则进行安装where db2go.exe
IF"%errorlevel%"=="0" (
echo db2go already installed.
) ELSE (
echo"db2go not found in system %%PATH%%, installing..."
CGO_ENABLED=1 go install github.com/civet148/db2go@latest
If"%errorlevel%"=="0" (
echo db2go install succeeded
) ELSE (
rem 安装失败,Linux/Mac请安装gcc工具链,Windows系统可以通过链接直接下载二进制(https://github.com/civet148/release/tree/master/db2go)echo"error: db2go install failed, Linux/Mac please install gcc tool-chain, Windows download from https://github.com/civet148/release/tree/master/db2go"
)
)
db2go go --url "%DSN_URL%" --out "%OUT_DIR%" --table "%TABLE_NAME%" --enable-decimal --spec-type "%SPEC_TYPES%"^
--package "%PACK_NAME%" --readonly "%READ_ONLY%" --without "%WITHOUT%" --tag "%TAGS%" --ddl "%DDL_FILE%"echo"generate go file ok, formatting..."
gofmt -w %OUT_DIR%/%PACK_NAME%pause