Skip to content

Commit

Permalink
🎨 linter fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
lawzava committed Nov 30, 2021
1 parent 8aa0ea6 commit 188d910
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 24 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/golang.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ jobs:
- name: golangci-lint
uses: golangci/golangci-lint-action@v2
with:
version: v1.41.1
version: v1.43.0
19 changes: 18 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
)
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down Expand Up @@ -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=
Expand Down
45 changes: 27 additions & 18 deletions migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
Expand All @@ -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)
Expand Down
4 changes: 3 additions & 1 deletion repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down

0 comments on commit 188d910

Please sign in to comment.