# goose
<img align="right" width="125" src="assets/goose_logo.png">
[![Goose
CI](https://github.com/pressly/goose/actions/workflows/ci.yaml/badge.svg)](https://github.com/pressly/goose/actions/workflows/ci.yaml)
[![Go
Reference](https://pkg.go.dev/badge/github.com/pressly/goose/v3.svg)](https://pkg.go.dev/github.com/pressly/goose/v3)
[![Go Report
Card](https://goreportcard.com/badge/github.com/pressly/goose/v3)](https://goreportcard.com/report/github.com/pressly/goose/v3)
Goose is a database migration tool. Both a CLI and a library.
Manage your **database schema** by creating incremental SQL changes or Go functions.
#### Features
- Works against multiple databases:
- Postgres, MySQL, SQLite, YDB, ClickHouse, MSSQL, Vertica, and
more.
- Supports Go migrations written as plain functions.
- Supports [embedded](https://pkg.go.dev/embed/) migrations.
- Out-of-order migrations.
- Seeding data.
- Environment variable substitution in SQL migrations.
- ... and more.
# Install
```shell
go install github.com/pressly/goose/v3/cmd/goose@latest
```
This will install the `goose` binary to your `$GOPATH/bin` directory.
Binary too big? Build a lite version by excluding the drivers you don't need:
```shell
go build -tags='no_postgres no_mysql no_sqlite3 no_ydb' -o goose ./cmd/goose
# Available build tags:
# no_clickhouse no_libsql no_mssql no_mysql
# no_postgres no_sqlite3 no_vertica no_ydb
```
For macOS users `goose` is available as a [Homebrew
Formulae](https://formulae.brew.sh/formula/goose#default):
```shell
brew install goose
```
See [installation documentation](https://pressly.github.io/goose/installation/) for more details.
# Usage
<details>
<summary>Click to show <code>goose help</code> output</summary>
```
Usage: goose [OPTIONS] DRIVER DBSTRING COMMAND
or
Set environment key
GOOSE_DRIVER=DRIVER
GOOSE_DBSTRING=DBSTRING
GOOSE_MIGRATION_DIR=MIGRATION_DIR
Usage: goose [OPTIONS] COMMAND
Drivers:
postgres
mysql
sqlite3
mssql
redshift
tidb
clickhouse
vertica
ydb
starrocks
Examples:
goose sqlite3 ./foo.db status
goose sqlite3 ./foo.db create init sql
goose sqlite3 ./foo.db create add_some_column sql
goose sqlite3 ./foo.db create fetch_user_data go
goose sqlite3 ./foo.db up
goose postgres "user=postgres dbname=postgres sslmode=disable" status
goose mysql "user:password@/dbname?parseTime=true" status
goose redshift "postgres://user:password@qwerty.us-east-1.redshift.amazonaws.com:5439/db" status
goose tidb "user:password@/dbname?parseTime=true" status
goose mssql "sqlserver://user:password@dbname:1433?database=master" status
goose clickhouse "tcp://127.0.0.1:9000" status
goose vertica "vertica://user:password@localhost:5433/dbname?connection_load_balance=1" status
goose ydb "grpcs://localhost:2135/local?go_query_mode=scripting&go_fake_tx=scripting&go_query_bind=declare,numeric" status
goose starrocks "user:password@/dbname?parseTime=true&interpolateParams=true" status
GOOSE_DRIVER=sqlite3 GOOSE_DBSTRING=./foo.db goose status
GOOSE_DRIVER=sqlite3 GOOSE_DBSTRING=./foo.db goose create init sql
GOOSE_DRIVER=postgres GOOSE_DBSTRING="user=postgres dbname=postgres sslmode=disable" goose status
GOOSE_DRIVER=mysql GOOSE_DBSTRING="user:password@/dbname" goose status
GOOSE_DRIVER=redshift GOOSE_DBSTRING="postgres://user:password@qwerty.us-east-1.redshift.amazonaws.com:5439/db" goose status
GOOSE_DRIVER=clickhouse GOOSE_DBSTRING="clickhouse://user:password@qwerty.clickhouse.cloud:9440/dbname?secure=true&skip_verify=false" goose status
Options:
-allow-missing
applies missing (out-of-order) migrations
-certfile string
file path to root CA's certificates in pem format (only support on mysql)
-dir string
directory with migration files (default ".", can be set via the GOOSE_MIGRATION_DIR env variable).
-h print help
-no-color
disable color output (NO_COLOR env variable supported)
-no-versioning
apply migration commands with no versioning, in file order, from directory pointed to
-s use sequential numbering for new migrations
-ssl-cert string
file path to SSL certificates in pem format (only support on mysql)
-ssl-key string
file path to SSL key in pem format (only support on mysql)
-table string
migrations table name (default "goose_db_version")
-timeout duration
maximum allowed duration for queries to run; e.g., 1h13m
-v enable verbose mode
-version
print version
Commands:
up Migrate the DB to the most recent version available
up-by-one Migrate the DB up by 1
up-to VERSION Migrate the DB to a specific VERSION
down Roll back the version by 1
down-to VERSION Roll back to a specific VERSION
redo Re-run the latest migration
reset Roll back all migrations
status Dump the migration status for the current DB
version Print the current version of the database
create NAME [sql|go] Creates new migration file with the current timestamp
fix Apply sequential ordering to migrations
validate Check migration files without running them
```
</details>
Commonly used commands:
[create](#create)<span> • </span> [up](#up)<span> • </span> [up-to](#up-to)<span> • </span> [down](#down)<span> • </span> [down-to](#down-to)<span> • </span> [status](#status)<span> • </span> [version](#version)
## create
Create a new SQL migration.
$ goose create add_some_column sql
$ Created new file: 20170506082420_add_some_column.sql
$ goose -s create add_some_column sql
$ Created new file: 00001_add_some_column.sql
Edit the newly created file to define the behavior of your migration.
You can also create a Go migration, if you then invoke it with [your own goose
binary](#go-migrations):
$ goose create fetch_user_data go
$ Created new file: 20170506082421_fetch_user_data.go
## up
Apply all available migrations.
$ goose up
$ OK 001_basics.sql
$ OK 002_next.sql
$ OK 003_and_again.go
## up-to
Migrate up to a specific version.
$ goose up-to 20170506082420
$ OK 20170506082420_create_table.sql
## up-by-one
Migrate up a single migration from the current version
$ goose up-by-one
$ OK 20170614145246_change_type.sql
## down
Roll back a single migration from the current version.
$ goose down
$ OK 003_and_again.go
## down-to
Roll back migrations to a specific version.
$ goose down-to 20170506082527
$ OK 20170506082527_alter_column.sql
Or, roll back all migrations (careful!):
$ goose down-to 0
## status
Print the status of all migrations:
$ goose status
$ Applied At Migration
$ =======================================
$ Sun Jan 6 11:25:03 2013 -- 001_basics.sql
$ Sun Jan 6 11:25:03 2013 -- 002_next.sql
$ Pending -- 003_and_again.go
Note: for MySQL [parseTime flag](https://github.com/go-sql-driver/mysql#parsetime) must be enabled.
Note: for MySQL
[`multiStatements`](https://github.com/go-sql-driver/mysql?tab=readme-ov-file#multistatements) must
be enabled. This is required when writing multiple queries separated by ';' characters in a single
sql file.
## version
Print the current version of the database:
$ goose version
$ goose: version 002
# Migrations
goose supports migrations written in SQL or in Go.
## SQL Migrations
A sample SQL migration looks like:
```sql
-- +goose Up
CREATE TABLE post (
id int NOT NULL,
title text,
body text,
PRIMARY KEY(id)
);
-- +goose Down
DROP TABLE post;
```
Each migration file must have exactly one `-- +goose Up` annotation. The `-- +goose Down`
没有合适的资源?快使用搜索试试~ 我知道了~
数据库迁移工具 支持 SQL 迁移和 Go 函数 .zip
共252个文件
go:126个
sql:100个
yaml:7个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 26 浏览量
2024-12-03
03:29:53
上传
评论
收藏 376KB ZIP 举报
温馨提示
鹅 Goose 是一个数据库迁移工具。既是一个 CLI,又是一个库。通过创建增量 SQL 更改或 Go 函数来管理数据库模式。特征适用于多个数据库Postgres、MySQL、SQLite、YDB、ClickHouse、MSSQL、Vertica 等。支持以普通函数形式编写的 Go 迁移。支持嵌入式迁移。无序迁移。播种数据。SQL 迁移中的环境变量替换。...还有更多。安装go install github.com/pressly/goose/v3/cmd/goose@latest这会将goose二进制文件安装到您的$GOPATH/bin目录中。二进制文件太大?通过排除不需要的驱动程序来构建精简版go build -tags='no_postgres no_mysql no_sqlite3 no_ydb' -o goose ./cmd/goose# Available build tags:# no_clickhouse no_libsql no_mssql no_mysql# no_postgres
资源推荐
资源详情
资源评论
收起资源包目录
数据库迁移工具 支持 SQL 迁移和 Go 函数 .zip (252个子文件)
taxi_zone_lookup.csv 12KB
.gitignore 144B
provider_run_test.go 39KB
provider.go 21KB
postgres_locking_test.go 16KB
provider_run.go 15KB
parser_test.go 14KB
main.go 12KB
migration.go 11KB
globals_test.go 11KB
parser.go 11KB
migrate.go 11KB
provider_collect_test.go 11KB
migrate_test.go 9KB
resolve_test.go 7KB
store_test.go 7KB
up.go 6KB
goose_cli_test.go 6KB
provider_collect.go 6KB
provider_options.go 6KB
database_test.go 6KB
store.go 5KB
migrationstats_test.go 5KB
dialect.go 5KB
register.go 4KB
postgres.go 4KB
goose.go 4KB
resolve.go 4KB
ydb.go 4KB
migration_go.go 3KB
globals.go 3KB
clickhouse.go 3KB
session_locker_options.go 3KB
register_test.go 3KB
down.go 3KB
migration_sql.go 3KB
vertica.go 3KB
create.go 3KB
store.go 3KB
starrocks.go 3KB
mariadb.go 3KB
turso.go 3KB
postgres.go 2KB
integration.go 2KB
gomigrations_error_test.go 2KB
provider_types.go 2KB
gomigrations_success_test.go 2KB
parse_test.go 2KB
provider_options_test.go 2KB
fix_test.go 2KB
provider_test.go 2KB
status.go 2KB
migrationstats.go 2KB
helpers.go 2KB
postgres.go 2KB
dialectquery.go 2KB
driver_mysql.go 2KB
provider_errors.go 2KB
goose_embed_test.go 2KB
db.go 2KB
dialect.go 2KB
reset.go 2KB
store_extended.go 1KB
parse.go 1KB
testdb.go 1KB
starrocks.go 1KB
clickhouse.go 1KB
controller.go 1KB
ydb.go 1KB
version.go 1KB
create_test.go 1KB
sqlserver.go 1KB
redshift.go 1KB
mysql.go 1KB
redo.go 1KB
vertica.go 1KB
sqlite3.go 1KB
tidb.go 1KB
fix.go 1KB
00003_add_user_no_tx.go 950B
migration_sql.go 943B
main.go 898B
container_healthcheck.go 884B
log.go 874B
migrationstats_walker.go 863B
cfg.go 818B
session_locker.go 811B
up_test.go 808B
osfs.go 791B
helpers_test.go 688B
sql_extended.go 660B
001_up_down.go 657B
doc.go 615B
002_ERROR_insert_no_tx.go 476B
00002_rename_root.go 470B
004_ERROR_insert.go 469B
dialects.go 442B
013_up_down_no_tx_ctx.go 330B
009_up_down_ctx.go 326B
005_up_down_no_tx.go 276B
共 252 条
- 1
- 2
- 3
资源评论
赵闪闪168
- 粉丝: 1647
- 资源: 4872
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功