• Home
  • History
  • Annotate
Name Date Size #Lines LOC

..03-May-2022-

cmd/goose/H08-Dec-2020-277215

examples/H08-Dec-2020-202142

vendor/H03-May-2022-337,892233,807

.gitignoreH A D08-Dec-2020158 1410

.travis.ymlH A D08-Dec-202079 97

LICENSEH A D08-Dec-20201.1 KiB2217

MakefileH A D08-Dec-2020654 1917

README.mdH A D08-Dec-20208.6 KiB277201

_go.modH A D08-Dec-2020333 1411

_go.sumH A D08-Dec-202014.1 KiB148147

create.goH A D08-Dec-20202.6 KiB12193

create_test.goH A D08-Dec-20201.2 KiB5646

db.goH A D08-Dec-2020662 3124

dialect.goH A D08-Dec-20209.1 KiB325231

down.goH A D08-Dec-20201.1 KiB5744

fix.goH A D08-Dec-20201,012 5441

fix_test.goH A D08-Dec-20202 KiB8670

goose.goH A D08-Dec-20202.1 KiB9988

goose_test.goH A D08-Dec-20202.4 KiB9679

helpers.goH A D08-Dec-20201.8 KiB8575

helpers_test.goH A D08-Dec-2020688 2823

log.goH A D08-Dec-2020892 3121

migrate.goH A D08-Dec-20207.7 KiB320224

migrate_test.goH A D08-Dec-20201.2 KiB5943

migration.goH A D08-Dec-20203.6 KiB149116

migration_sql.goH A D08-Dec-20202.4 KiB9468

redo.goH A D08-Dec-2020553 3425

reset.goH A D08-Dec-20201.3 KiB6145

sql_parser.goH A D08-Dec-20206.4 KiB225172

sql_parser_test.goH A D08-Dec-20207.4 KiB345294

status.goH A D08-Dec-20201.4 KiB5540

up.goH A D08-Dec-20201.2 KiB6651

version.goH A D08-Dec-2020497 2919

README.md

1# goose
2
3Goose is a database migration tool. Manage your database schema by creating incremental SQL changes or Go functions.
4
5[![GoDoc Widget]][GoDoc] [![Travis Widget]][Travis]
6
7### Goals of this fork
8
9`github.com/pressly/goose` is a fork of `bitbucket.org/liamstask/goose` with the following changes:
10- No config files
11- [Default goose binary](./cmd/goose/main.go) can migrate SQL files only
12- Go migrations:
13    - We don't `go build` Go migrations functions on-the-fly
14      from within the goose binary
15    - Instead, we let you
16      [create your own custom goose binary](examples/go-migrations),
17      register your Go migration functions explicitly and run complex
18      migrations with your own `*sql.DB` connection
19    - Go migration functions let you run your code within
20      an SQL transaction, if you use the `*sql.Tx` argument
21- The goose pkg is decoupled from the binary:
22    - goose pkg doesn't register any SQL drivers anymore,
23      thus no driver `panic()` conflict within your codebase!
24    - goose pkg doesn't have any vendor dependencies anymore
25- We use timestamped migrations by default but recommend a hybrid approach of using timestamps in the development process and sequential versions in production.
26
27# Install
28
29    $ go get -u github.com/pressly/goose/cmd/goose
30
31This will install the `goose` binary to your `$GOPATH/bin` directory.
32
33For a lite version of the binary without DB connection dependent commands, use the exclusive build tags:
34
35    $ go build -tags='no_postgres no_mysql no_sqlite3' -i -o goose ./cmd/goose
36
37
38# Usage
39
40```
41Usage: goose [OPTIONS] DRIVER DBSTRING COMMAND
42
43Drivers:
44    postgres
45    mysql
46    sqlite3
47    mssql
48    redshift
49
50Examples:
51    goose sqlite3 ./foo.db status
52    goose sqlite3 ./foo.db create init sql
53    goose sqlite3 ./foo.db create add_some_column sql
54    goose sqlite3 ./foo.db create fetch_user_data go
55    goose sqlite3 ./foo.db up
56
57    goose postgres "user=postgres dbname=postgres sslmode=disable" status
58    goose mysql "user:password@/dbname?parseTime=true" status
59    goose redshift "postgres://user:password@qwerty.us-east-1.redshift.amazonaws.com:5439/db" status
60    goose tidb "user:password@/dbname?parseTime=true" status
61    goose mssql "sqlserver://user:password@dbname:1433?database=master" status
62
63Options:
64
65  -dir string
66    	directory with migration files (default ".")
67  -table string
68    	migrations table name (default "goose_db_version")
69  -h	print help
70  -v	enable verbose mode
71  -version
72    	print version
73
74Commands:
75    up                   Migrate the DB to the most recent version available
76    up-by-one            Migrate the DB up by 1
77    up-to VERSION        Migrate the DB to a specific VERSION
78    down                 Roll back the version by 1
79    down-to VERSION      Roll back to a specific VERSION
80    redo                 Re-run the latest migration
81    reset                Roll back all migrations
82    status               Dump the migration status for the current DB
83    version              Print the current version of the database
84    create NAME [sql|go] Creates new migration file with the current timestamp
85    fix                  Apply sequential ordering to migrations
86```
87
88## create
89
90Create a new SQL migration.
91
92    $ goose create add_some_column sql
93    $ Created new file: 20170506082420_add_some_column.sql
94
95Edit the newly created file to define the behavior of your migration.
96
97You can also create a Go migration, if you then invoke it with [your own goose binary](#go-migrations):
98
99    $ goose create fetch_user_data go
100    $ Created new file: 20170506082421_fetch_user_data.go
101
102## up
103
104Apply all available migrations.
105
106    $ goose up
107    $ OK    001_basics.sql
108    $ OK    002_next.sql
109    $ OK    003_and_again.go
110
111## up-to
112
113Migrate up to a specific version.
114
115    $ goose up-to 20170506082420
116    $ OK    20170506082420_create_table.sql
117
118## up-by-one
119
120Migrate up a single migration from the current version
121
122    $ goose up-by-one
123    $ OK    20170614145246_change_type.sql
124
125## down
126
127Roll back a single migration from the current version.
128
129    $ goose down
130    $ OK    003_and_again.go
131
132## down-to
133
134Roll back migrations to a specific version.
135
136    $ goose down-to 20170506082527
137    $ OK    20170506082527_alter_column.sql
138
139## redo
140
141Roll back the most recently applied migration, then run it again.
142
143    $ goose redo
144    $ OK    003_and_again.go
145    $ OK    003_and_again.go
146
147## status
148
149Print the status of all migrations:
150
151    $ goose status
152    $   Applied At                  Migration
153    $   =======================================
154    $   Sun Jan  6 11:25:03 2013 -- 001_basics.sql
155    $   Sun Jan  6 11:25:03 2013 -- 002_next.sql
156    $   Pending                  -- 003_and_again.go
157
158Note: for MySQL [parseTime flag](https://github.com/go-sql-driver/mysql#parsetime) must be enabled.
159
160## version
161
162Print the current version of the database:
163
164    $ goose version
165    $ goose: version 002
166
167# Migrations
168
169goose supports migrations written in SQL or in Go.
170
171## SQL Migrations
172
173A sample SQL migration looks like:
174
175```sql
176-- +goose Up
177CREATE TABLE post (
178    id int NOT NULL,
179    title text,
180    body text,
181    PRIMARY KEY(id)
182);
183
184-- +goose Down
185DROP TABLE post;
186```
187
188Notice the annotations in the comments. Any statements following `-- +goose Up` will be executed as part of a forward migration, and any statements following `-- +goose Down` will be executed as part of a rollback.
189
190By default, all migrations are run within a transaction. Some statements like `CREATE DATABASE`, however, cannot be run within a transaction. You may optionally add `-- +goose NO TRANSACTION` to the top of your migration
191file in order to skip transactions within that specific migration file. Both Up and Down migrations within this file will be run without transactions.
192
193By default, SQL statements are delimited by semicolons - in fact, query statements must end with a semicolon to be properly recognized by goose.
194
195More complex statements (PL/pgSQL) that have semicolons within them must be annotated with `-- +goose StatementBegin` and `-- +goose StatementEnd` to be properly recognized. For example:
196
197```sql
198-- +goose Up
199-- +goose StatementBegin
200CREATE OR REPLACE FUNCTION histories_partition_creation( DATE, DATE )
201returns void AS $$
202DECLARE
203  create_query text;
204BEGIN
205  FOR create_query IN SELECT
206      'CREATE TABLE IF NOT EXISTS histories_'
207      || TO_CHAR( d, 'YYYY_MM' )
208      || ' ( CHECK( created_at >= timestamp '''
209      || TO_CHAR( d, 'YYYY-MM-DD 00:00:00' )
210      || ''' AND created_at < timestamp '''
211      || TO_CHAR( d + INTERVAL '1 month', 'YYYY-MM-DD 00:00:00' )
212      || ''' ) ) inherits ( histories );'
213    FROM generate_series( $1, $2, '1 month' ) AS d
214  LOOP
215    EXECUTE create_query;
216  END LOOP;  -- LOOP END
217END;         -- FUNCTION END
218$$
219language plpgsql;
220-- +goose StatementEnd
221```
222
223## Go Migrations
224
2251. Create your own goose binary, see [example](./examples/go-migrations)
2262. Import `github.com/pressly/goose`
2273. Register your migration functions
2284. Run goose command, ie. `goose.Up(db *sql.DB, dir string)`
229
230A [sample Go migration 00002_users_add_email.go file](./examples/go-migrations/00002_rename_root.go) looks like:
231
232```go
233package migrations
234
235import (
236	"database/sql"
237
238	"github.com/pressly/goose"
239)
240
241func init() {
242	goose.AddMigration(Up, Down)
243}
244
245func Up(tx *sql.Tx) error {
246	_, err := tx.Exec("UPDATE users SET username='admin' WHERE username='root';")
247	if err != nil {
248		return err
249	}
250	return nil
251}
252
253func Down(tx *sql.Tx) error {
254	_, err := tx.Exec("UPDATE users SET username='root' WHERE username='admin';")
255	if err != nil {
256		return err
257	}
258	return nil
259}
260```
261
262# Hybrid Versioning
263Please, read the [versioning problem](https://github.com/pressly/goose/issues/63#issuecomment-428681694) first.
264
265We strongly recommend adopting a hybrid versioning approach, using both timestamps and sequential numbers. Migrations created during the development process are timestamped and sequential versions are ran on production. We believe this method will prevent the problem of conflicting versions when writing software in a team environment.
266
267To help you adopt this approach, `create` will use the current timestamp as the migration version. When you're ready to deploy your migrations in a production environment, we also provide a helpful `fix` command to convert your migrations into sequential order, while preserving the timestamp ordering. We recommend running `fix` in the CI pipeline, and only when the migrations are ready for production.
268
269## License
270
271Licensed under [MIT License](./LICENSE)
272
273[GoDoc]: https://godoc.org/github.com/pressly/goose
274[GoDoc Widget]: https://godoc.org/github.com/pressly/goose?status.svg
275[Travis]: https://travis-ci.org/pressly/goose
276[Travis Widget]: https://travis-ci.org/pressly/goose.svg?branch=master
277