From df7b259e41ddf322253daf2e5e5c11511e841838 Mon Sep 17 00:00:00 2001 From: David Sharnoff Date: Tue, 29 Mar 2022 22:23:22 -0700 Subject: [PATCH] add code coverage (#12) * add code coverage * lint --- .github/workflows/codecov.yml | 18 ++++++++++++++++ .github/workflows/golangci-lint.yml | 33 +++++++++++++++++++++++++++++ .github/workflows/mysql.yml | 9 ++++++++ .github/workflows/pg.yml | 8 +++++++ api.go | 1 - apply.go | 14 ++++++++---- lsmysql/check.go | 4 ++-- lsmysql/mysql.go | 5 ++--- lsmysql/mysql_test.go | 4 ---- lspostgres/postgres.go | 5 ++--- 10 files changed, 84 insertions(+), 17 deletions(-) create mode 100644 .github/workflows/codecov.yml create mode 100644 .github/workflows/golangci-lint.yml diff --git a/.github/workflows/codecov.yml b/.github/workflows/codecov.yml new file mode 100644 index 0000000..2eb8d89 --- /dev/null +++ b/.github/workflows/codecov.yml @@ -0,0 +1,18 @@ +name: Test and coverage + +on: [push, pull_request] + +jobs: + build: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + with: + fetch-depth: 2 + - uses: actions/setup-go@v2 + with: + go-version: '1.17' + - name: Run coverage + run: go test ./... -race -coverprofile=coverage.txt -covermode=atomic + - name: Upload coverage to Codecov + run: bash <(curl -s https://codecov.io/bash) diff --git a/.github/workflows/golangci-lint.yml b/.github/workflows/golangci-lint.yml new file mode 100644 index 0000000..067e1a5 --- /dev/null +++ b/.github/workflows/golangci-lint.yml @@ -0,0 +1,33 @@ +name: golangci-lint +on: [push ] +jobs: + golangci: + name: lint + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - name: golangci-lint + uses: golangci/golangci-lint-action@v2 + with: + # Optional: version of golangci-lint to use in form of v1.2 or v1.2.3 or `latest` to use the latest version + version: latest + + # Optional: working directory, useful for monorepos + # working-directory: somedir + output: checkstyle + + # Optional: golangci-lint command line arguments. + # args: --issues-exit-code=0 + # args: --out-format checkstyle + + # Optional: show only new issues if it's a pull request. The default value is `false`. + # only-new-issues: true + + # Optional: if set to true then the action will use pre-installed Go. + # skip-go-installation: true + + # Optional: if set to true then the action don't cache or restore ~/go/pkg. + # skip-pkg-cache: true + + # Optional: if set to true then the action don't cache or restore ~/.cache/go-build. + # skip-build-cache: true diff --git a/.github/workflows/mysql.yml b/.github/workflows/mysql.yml index 831e891..f6cff38 100644 --- a/.github/workflows/mysql.yml +++ b/.github/workflows/mysql.yml @@ -36,3 +36,12 @@ jobs: LIBSCHEMA_MYSQL_TEST_DSN: "root:mysql@tcp(127.0.0.1:3306)/libschematest?tls=false" run: go test ./lsmysql/... -v + - name: Run Coverage + env: + LIBSCHEMA_MYSQL_TEST_DSN: "root:mysql@tcp(127.0.0.1:3306)/libschematest?tls=false" + run: go test -coverprofile=coverage.txt -covermode=atomic ./lsmysql/... + + - name: Upload coverage to Codecov + run: bash <(curl -s https://codecov.io/bash) + + diff --git a/.github/workflows/pg.yml b/.github/workflows/pg.yml index 0c44ad0..5c3188a 100644 --- a/.github/workflows/pg.yml +++ b/.github/workflows/pg.yml @@ -35,3 +35,11 @@ jobs: LIBSCHEMA_POSTGRES_TEST_DSN: "postgres://postgres:postgres@localhost?sslmode=disable" run: go test ./lspostgres/... -v + - name: Run Coverage + env: + LIBSCHEMA_POSTGRES_TEST_DSN: "postgres://postgres:postgres@localhost?sslmode=disable" + run: go test -coverprofile=coverage.txt -covermode=atomic ./lspostgres/... + + - name: Upload coverage to Codecov + run: bash <(curl -s https://codecov.io/bash) + diff --git a/api.go b/api.go index 92bf794..d8c652c 100644 --- a/api.go +++ b/api.go @@ -130,7 +130,6 @@ type Schema struct { databases map[string]*Database databaseOrder []*Database options Options - count int context context.Context } diff --git a/apply.go b/apply.go index 678c7c6..f785ca4 100644 --- a/apply.go +++ b/apply.go @@ -64,7 +64,7 @@ func (s *Schema) Migrate(ctx context.Context) (err error) { return errors.Errorf("--migrate-dsn can only be used when there is only one database to migrate") } for _, d := range todo { - err := func(d *Database) error { + err := func(d *Database) (finalErr error) { if *MigrateDSN != "" { var err error d.db, err = OpenAnyDB(*MigrateDSN) @@ -76,7 +76,12 @@ func (s *Schema) Migrate(ctx context.Context) (err error) { if err != nil { return err } - defer d.unlock() + defer func() { + err := d.unlock() + if err != nil && finalErr == nil { + finalErr = err + } + }() if *ExitIfMigrateNeeded && !d.done() { return errors.Errorf("Migrations required for %s", d.name) } @@ -306,8 +311,9 @@ func (d *Database) asyncMigrate(ctx context.Context) { } } -func (d *Database) unlock() { +func (d *Database) unlock() error { if !d.asyncInProgress { - d.driver.UnlockMigrationsTable(d.log) + return d.driver.UnlockMigrationsTable(d.log) } + return nil } diff --git a/lsmysql/check.go b/lsmysql/check.go index 0b79ac4..426d0bc 100644 --- a/lsmysql/check.go +++ b/lsmysql/check.go @@ -11,8 +11,8 @@ type CheckResult string const ( Safe CheckResult = "safe" - DataAndDDL = "dataAndDDL" - NonIdempotentDDL = "nonIdempotentDDL" + DataAndDDL CheckResult = "dataAndDDL" + NonIdempotentDDL CheckResult = "nonIdempotentDDL" ) var ifExistsRE = regexp.MustCompile(`(?i)\bIF (?:NOT )?EXISTS\b`) diff --git a/lsmysql/mysql.go b/lsmysql/mysql.go index 7c3a1d3..3b4c62a 100644 --- a/lsmysql/mysql.go +++ b/lsmysql/mysql.go @@ -126,11 +126,10 @@ func (p *MySQL) DoOneMigration(ctx context.Context, log libschema.MyLogger, d *l } defer func() { if err != nil { - tx.Rollback() + _ = tx.Rollback() } else { err = errors.Wrapf(tx.Commit(), "Commit migration %s", m.Base().Name) } - return }() pm := m.(*mmigration) if pm.script != nil { @@ -153,7 +152,7 @@ func (p *MySQL) DoOneMigration(ctx context.Context, log libschema.MyLogger, d *l } if err != nil { err = errors.Wrapf(err, "Problem with migration %s", m.Base().Name) - tx.Rollback() + _ = tx.Rollback() ntx, txerr := d.DB().BeginTx(ctx, d.Options.MigrationTxOptions) if txerr != nil { return errors.Wrapf(err, "Tx for saving status for %s also failed with %s", m.Base().Name, txerr) diff --git a/lsmysql/mysql_test.go b/lsmysql/mysql_test.go index 2ba9ded..b786c2d 100644 --- a/lsmysql/mysql_test.go +++ b/lsmysql/mysql_test.go @@ -252,7 +252,3 @@ func TestSkipFunctions(t *testing.T) { assert.True(t, enf, "users hi_level constraint") } } - -func pointerToString(s string) *string { - return &s -} diff --git a/lspostgres/postgres.go b/lspostgres/postgres.go index bd64321..2faee7d 100644 --- a/lspostgres/postgres.go +++ b/lspostgres/postgres.go @@ -111,11 +111,10 @@ func (p *Postgres) DoOneMigration(ctx context.Context, log libschema.MyLogger, d } defer func() { if err != nil { - tx.Rollback() + _ = tx.Rollback() } else { err = errors.Wrapf(tx.Commit(), "Commit migration %s", m.Base().Name) } - return }() pm := m.(*pmigration) if pm.script != nil { @@ -127,7 +126,7 @@ func (p *Postgres) DoOneMigration(ctx context.Context, log libschema.MyLogger, d } if err != nil { err = errors.Wrapf(err, "Problem with migration %s", m.Base().Name) - tx.Rollback() + _ = tx.Rollback() ntx, txerr := d.DB().BeginTx(ctx, d.Options.MigrationTxOptions) if txerr != nil { return errors.Wrapf(err, "Tx for saving status for %s also failed with %s", m.Base().Name, txerr)