Skip to content

Commit

Permalink
continue - v4-beta.1
Browse files Browse the repository at this point in the history
  • Loading branch information
mfridman committed May 25, 2023
1 parent 3d345c4 commit a36956d
Show file tree
Hide file tree
Showing 171 changed files with 6,632 additions and 3,808 deletions.
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.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.

28 changes: 19 additions & 9 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-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=testdb \
-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]+'
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -317,14 +317,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
59 changes: 59 additions & 0 deletions apply_version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package goose

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

"github.com/pressly/goose/v4/internal/migration"
"github.com/pressly/goose/v4/internal/sqlparser"
)

// 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.
//
// It is safe for concurrent use.
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 = errors.Join(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.Migration{m}, d, true)
if err != nil {
return nil, err
}
if len(results) == 0 {
return nil, ErrAlreadyApplied
}
return results[0], nil
}
67 changes: 1 addition & 66 deletions cmd/goose/driver_mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,70 +4,5 @@
package main

import (
"crypto/tls"
"crypto/x509"
"fmt"
"log"
"os"

"github.com/go-sql-driver/mysql"
_ "github.com/ziutek/mymysql/godrv"
_ "github.com/go-sql-driver/mysql"
)

// normalizeMySQLDSN parses the dsn used with the mysql driver to always have
// the parameter `parseTime` set to true. This allows internal goose logic
// to assume that DATETIME/DATE/TIMESTAMP can be scanned into the time.Time
// type.
func normalizeDBString(driver string, str string, certfile string, sslcert string, sslkey string) string {
if driver == "mysql" {
isTLS := certfile != ""
if isTLS {
if err := registerTLSConfig(certfile, sslcert, sslkey); err != nil {
log.Fatalf("goose run: %v", err)
}
}
var err error
str, err = normalizeMySQLDSN(str, isTLS)
if err != nil {
log.Fatalf("failed to normalize MySQL connection string: %v", err)
}
}
return str
}

const tlsConfigKey = "custom"

func normalizeMySQLDSN(dsn string, tls bool) (string, error) {
config, err := mysql.ParseDSN(dsn)
if err != nil {
return "", err
}
config.ParseTime = true
if tls {
config.TLSConfig = tlsConfigKey
}
return config.FormatDSN(), nil
}

func registerTLSConfig(pemfile string, sslcert string, sslkey string) error {
rootCertPool := x509.NewCertPool()
pem, err := os.ReadFile(pemfile)
if err != nil {
return err
}
if ok := rootCertPool.AppendCertsFromPEM(pem); !ok {
return fmt.Errorf("failed to append PEM: %q", pemfile)
}

tlsConfig := &tls.Config{
RootCAs: rootCertPool,
}
if sslcert != "" && sslkey != "" {
cert, err := tls.LoadX509KeyPair(sslcert, sslkey)
if err != nil {
return fmt.Errorf("failed to load x509 keypair: %w", err)
}
tlsConfig.Certificates = append(tlsConfig.Certificates, cert)
}
return mysql.RegisterTLSConfig(tlsConfigKey, tlsConfig)
}
8 changes: 0 additions & 8 deletions cmd/goose/driver_no_mysql.go

This file was deleted.

Loading

0 comments on commit a36956d

Please sign in to comment.