diff --git a/.github/workflows/golang.yml b/.github/workflows/golang.yml index eea3175..e4d73cf 100644 --- a/.github/workflows/golang.yml +++ b/.github/workflows/golang.yml @@ -9,7 +9,7 @@ jobs: - name: Setup Go for use with actions uses: actions/setup-go@v2 with: - go-version: 1.15 + go-version: 1.17 - uses: actions/checkout@v2 - uses: actions/cache@v2 with: diff --git a/.github/workflows/golangci.yml b/.github/workflows/golangci.yml index ebfbe9a..e7077fa 100644 --- a/.github/workflows/golangci.yml +++ b/.github/workflows/golangci.yml @@ -10,4 +10,4 @@ jobs: - name: golangci-lint uses: golangci/golangci-lint-action@v2 with: - version: v1.41.1 + version: v1.43.0 diff --git a/go.mod b/go.mod index cfbd9c7..46e156c 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ module github.com/lawzava/go-pg-migrate -go 1.15 +go 1.17 require ( github.com/fergusstrange/embedded-postgres v1.2.0 @@ -18,3 +18,20 @@ require ( golang.org/x/crypto v0.0.0-20211117183948-ae814b36b871 // indirect golang.org/x/sys v0.0.0-20211124211545-fe61309f8881 // indirect ) + +require ( + github.com/davecgh/go-spew v1.1.1 // indirect + github.com/dsnet/compress v0.0.1 // indirect + github.com/go-pg/zerochecker v0.2.0 // indirect + github.com/jinzhu/inflection v1.0.0 // indirect + github.com/mholt/archiver v3.1.1+incompatible // indirect + github.com/pmezard/go-difflib v1.0.0 // indirect + github.com/stretchr/objx v0.1.0 // indirect + github.com/tmthrgd/go-hex v0.0.0-20190904060850-447a3041c3bc // indirect + github.com/vmihailenco/bufpool v0.1.11 // indirect + github.com/vmihailenco/tagparser v0.1.2 // indirect + github.com/vmihailenco/tagparser/v2 v2.0.0 // indirect + github.com/xi2/xz v0.0.0-20171230120015-48954b6210f8 // indirect + gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c // indirect + mellium.im/sasl v0.2.1 // indirect +) diff --git a/go.sum b/go.sum index 6fc923a..78140cd 100644 --- a/go.sum +++ b/go.sum @@ -34,7 +34,6 @@ github.com/golang/protobuf v1.4.0-rc.4.0.20200313231945-b860323f09d0/go.mod h1:W github.com/golang/protobuf v1.4.0/go.mod h1:jodUvKwWbYaEsadDk5Fwe5c77LiNKVO9IDvqG2KuDX0= github.com/golang/protobuf v1.4.1/go.mod h1:U8fpvMrcmy5pZrNK1lt4xCsGvpyWQ/VVv6QDs8UjoX8= github.com/golang/protobuf v1.4.2/go.mod h1:oDoupMAO8OvCJWAcko0GGGIgR6R6ocIYbsSw735rRwI= -github.com/golang/protobuf v1.4.3 h1:JjCZWpVbqXDqFVmTfYWEVTMIYrL/NPdPSCHPJ0T/raM= github.com/golang/protobuf v1.4.3/go.mod h1:oDoupMAO8OvCJWAcko0GGGIgR6R6ocIYbsSw735rRwI= github.com/golang/snappy v0.0.1/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q= github.com/golang/snappy v0.0.2 h1:aeE13tS0IiQgFjYdoL8qN3K1N2bXXtI6Vi51/y7BpMw= @@ -194,7 +193,6 @@ google.golang.org/protobuf v1.21.0/go.mod h1:47Nbq4nVaFHyn7ilMalzfO3qCViNmqZ2kzi google.golang.org/protobuf v1.22.0/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU= google.golang.org/protobuf v1.23.0/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU= google.golang.org/protobuf v1.23.1-0.20200526195155-81db48ad09cc/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU= -google.golang.org/protobuf v1.25.0 h1:Ejskq+SyPohKW+1uil0JJMtmHCgJPJ/qWTxr8qp+R4c= google.golang.org/protobuf v1.25.0/go.mod h1:9JNX74DMeImyA3h4bdi1ymwjUzf21/xIlbajtzgsN7c= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= diff --git a/migrate.go b/migrate.go index 9928dc6..bcb3e27 100644 --- a/migrate.go +++ b/migrate.go @@ -78,22 +78,9 @@ func New(db *pg.DB, opt Options, migrations ...*Migration) (Migrate, error) { } // migrate applies actual migrations based on the specified options. -func (m migrationTask) migrate() error { - if m.opt.RefreshSchema { - if err := m.refreshSchema("public"); err != nil { - return fmt.Errorf("refreshing schema 'public': %w", err) - } - } else if len(m.opt.SchemasToRefresh) > 0 { - for _, schemaName := range m.opt.SchemasToRefresh { - if err := m.refreshSchema(schemaName); err != nil { - return fmt.Errorf("refreshing schema %s: %w", schemaName, err) - } - } - } else { - err := m.repo.EnsureMigrationTable() - if err != nil { - return fmt.Errorf("failed to automatically migrate migrations table: %w", err) - } +func (m *migrationTask) migrate() error { + if err := m.performPreMigrationTask(); err != nil { + return fmt.Errorf("failed to perform pre-migration task: %w", err) } if m.opt.ForceVersionWithoutMigrations { @@ -118,7 +105,29 @@ func (m migrationTask) migrate() error { return nil } -func (m migrationTask) handleForceVersionWithoutMigrations() error { +func (m *migrationTask) performPreMigrationTask() error { + switch { + case m.opt.RefreshSchema: + if err := m.refreshSchema("public"); err != nil { + return fmt.Errorf("refreshing schema 'public': %w", err) + } + case len(m.opt.SchemasToRefresh) > 0: + for _, schemaName := range m.opt.SchemasToRefresh { + if err := m.refreshSchema(schemaName); err != nil { + return fmt.Errorf("refreshing schema %s: %w", schemaName, err) + } + } + default: + err := m.repo.EnsureMigrationTable() + if err != nil { + return fmt.Errorf("failed to automatically migrate migrations table: %w", err) + } + } + + return nil +} + +func (m *migrationTask) handleForceVersionWithoutMigrations() error { for _, migration := range m.migrations { if migration.Number != m.opt.VersionNumberToApply { continue @@ -138,7 +147,7 @@ func (m migrationTask) handleForceVersionWithoutMigrations() error { return errNoMigrationVersion } -func (m migrationTask) refreshSchema(schemaName string) error { +func (m *migrationTask) refreshSchema(schemaName string) error { m.opt.LogInfo("refreshing database") err := m.repo.DropSchema(schemaName) diff --git a/repository.go b/repository.go index a38ff7c..fed81b5 100644 --- a/repository.go +++ b/repository.go @@ -105,7 +105,9 @@ func (r *repo) EnsureMigrationTable() error { } func (r *repo) DropSchema(schemaName string) error { - _, err := r.db.Exec(fmt.Sprintf(`DROP SCHEMA IF EXISTS "%s" CASCADE; CREATE SCHEMA IF NOT EXISTS "%s";`, schemaName, schemaName)) + _, err := r.db.Exec( + fmt.Sprintf(`DROP SCHEMA IF EXISTS %q CASCADE; CREATE SCHEMA IF NOT EXISTS %q;`, + schemaName, schemaName)) if err != nil { return fmt.Errorf("failed to drop schema: %w", err) }