Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] - v4-alpha.1 #502

Closed
wants to merge 22 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:

strategy:
matrix:
go-version: ["1.18", "1.19", "1.20"]
go-version: ["1.19", "1.20"]
os: [ubuntu-latest]

runs-on: ${{ matrix.os }}
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@
# Local testing
.envrc
*.FAIL
/scripts
1 change: 0 additions & 1 deletion .goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,3 @@ changelog:
exclude:
- "^docs:"
- "^test:"
# TODO(mf): add docker support?
8 changes: 0 additions & 8 deletions Dockerfile.local

This file was deleted.

26 changes: 18 additions & 8 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ dist:
GOOS=windows GOARCH=amd64 go build -o ./bin/goose-windows64.exe ./cmd/goose
GOOS=windows GOARCH=386 go build -o ./bin/goose-windows386.exe ./cmd/goose

.PHONY: build
build:
go build -o $$GOBIN/goose ./cmd/goose

.PHONY: clean
clean:
@find . -type f -name '*.FAIL' -delete
Expand All @@ -28,25 +32,31 @@ test-packages:
test-e2e: test-e2e-postgres test-e2e-mysql test-e2e-clickhouse test-e2e-vertica

test-e2e-postgres:
go test $(GO_TEST_FLAGS) ./tests/e2e -dialect=postgres
go test $(GO_TEST_FLAGS) ./tests/e2e/postgres

test-e2e-mysql:
go test $(GO_TEST_FLAGS) ./tests/e2e -dialect=mysql
go test $(GO_TEST_FLAGS) ./tests/e2e/mysql

test-e2e-clickhouse:
go test $(GO_TEST_FLAGS) ./tests/clickhouse -test.short
go test $(GO_TEST_FLAGS) ./tests/e2e/clickhouse -test.short

test-e2e-vertica:
go test $(GO_TEST_FLAGS) ./tests/vertica
go test $(GO_TEST_FLAGS) ./tests/e2e/vertica

docker-cleanup:
docker stop -t=0 $$(docker ps --filter="label=goose_test" -aq)

docker-start-postgres:
docker run --rm -d \
-e POSTGRES_USER=${GOOSE_POSTGRES_DB_USER} \
-e POSTGRES_PASSWORD=${GOOSE_POSTGRES_PASSWORD} \
-e POSTGRES_DB=${GOOSE_POSTGRES_DBNAME} \
-p ${GOOSE_POSTGRES_PORT}:5432 \
-e POSTGRES_USER=dbuser \
-e POSTGRES_PASSWORD=password1 \
-e POSTGRES_DB=goosedb \
-p 5433:5432 \
-l goose_test \
postgres:14-alpine -c log_statement=all

todo:
rg --type go --ignore-case '//.*(todo|feat)\('

gh-links:
rg --type go --ignore-case '//.*github.com/.*/(issues|pull)/[0-9]+'
29 changes: 16 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,22 @@ Goose supports [embedding SQL migrations](#embedded-sql-migrations), which means
### Goals of this fork

`github.com/pressly/goose` is a fork of `bitbucket.org/liamstask/goose` with the following changes:

- No config files
- [Default goose binary](./cmd/goose/main.go) can migrate SQL files only
- Go migrations:
- We don't `go build` Go migrations functions on-the-fly
from within the goose binary
- Instead, we let you
[create your own custom goose binary](examples/go-migrations),
register your Go migration functions explicitly and run complex
migrations with your own `*sql.DB` connection
- Go migration functions let you run your code within
an SQL transaction, if you use the `*sql.Tx` argument
- We don't `go build` Go migrations functions on-the-fly
from within the goose binary
- Instead, we let you
[create your own custom goose binary](examples/go-migrations),
register your Go migration functions explicitly and run complex
migrations with your own `*sql.DB` connection
- Go migration functions let you run your code within
an SQL transaction, if you use the `*sql.Tx` argument
- The goose pkg is decoupled from the binary:
- goose pkg doesn't register any SQL drivers anymore,
thus no driver `panic()` conflict within your codebase!
- goose pkg doesn't have any vendor dependencies anymore
- goose pkg doesn't register any SQL drivers anymore,
thus no driver `panic()` conflict within your codebase!
- goose pkg doesn't have any vendor dependencies anymore
- We use timestamped migrations by default but recommend a hybrid approach of using timestamps in the development process and sequential versions in production.
- Supports missing (out-of-order) migrations with the `-allow-missing` flag, or if using as a library supply the functional option `goose.WithAllowMissing()` to Up, UpTo or UpByOne.
- Supports applying ad-hoc migrations without tracking them in the schema table. Useful for seeding a database after migrations have been applied. Use `-no-versioning` flag or the functional option `goose.WithNoVersioning()`.
Expand Down Expand Up @@ -255,6 +256,7 @@ language plpgsql;
```

## Embedded sql migrations

Go 1.16 introduced new feature: [compile-time embedding](https://pkg.go.dev/embed/) files into binary and
corresponding [filesystem abstraction](https://pkg.go.dev/io/fs/).

Expand Down Expand Up @@ -314,14 +316,14 @@ package migrations
import (
"database/sql"

"github.com/pressly/goose/v3"
"github.com/pressly/goose/v4"
)

func init() {
goose.AddMigration(Up, Down)
}

func Up(tx *sql.Tx) error {
func Up(ctx context.Context,tx *sql.Tx) error {
_, err := tx.Exec("UPDATE users SET username='admin' WHERE username='root';")
if err != nil {
return err
Expand Down Expand Up @@ -350,6 +352,7 @@ DOCKER_BUILDKIT=1 docker build -f Dockerfile.local --output bin .
```

# Hybrid Versioning

Please, read the [versioning problem](https://github.com/pressly/goose/issues/63#issuecomment-428681694) first.

By default, if you attempt to apply missing (out-of-order) migrations `goose` will raise an error. However, If you want to apply these missing migrations pass goose the `-allow-missing` flag, or if using as a library supply the functional option `goose.WithAllowMissing()` to Up, UpTo or UpByOne.
Expand Down
57 changes: 57 additions & 0 deletions apply_version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package goose

import (
"context"
"database/sql"
"errors"
"fmt"

"github.com/pressly/goose/v4/internal/sqlparser"
"go.uber.org/multierr"
)

// ApplyVersion applies exactly one migration at the specified version. If a migration cannot be
// found for the specified version, this method returns ErrNoCurrentVersion. If the migration has
// been applied already, this method returns ErrAlreadyApplied.
//
// If the direction is true, the migration will be applied. If the direction is false, the migration
// will be rolled back.
func (p *Provider) ApplyVersion(ctx context.Context, version int64, direction bool) (_ *MigrationResult, retErr error) {
if version < 1 {
return nil, fmt.Errorf("invalid version: %d", version)
}

m, err := p.getMigration(version)
if err != nil {
return nil, err
}

conn, cleanup, err := p.initialize(ctx)
if err != nil {
return nil, err
}
defer func() {
retErr = multierr.Append(retErr, cleanup())
}()

result, err := p.store.GetMigration(ctx, conn, version)
if err != nil && !errors.Is(err, sql.ErrNoRows) {
return nil, err
}
if result != nil {
return nil, ErrAlreadyApplied
}

d := sqlparser.DirectionDown
if direction {
d = sqlparser.DirectionUp
}
results, err := p.runMigrations(ctx, conn, []*migration{m}, d, true)
if err != nil {
return nil, err
}
if len(results) == 0 {
return nil, ErrAlreadyApplied
}
return results[0], nil
}
Loading