Skip to content

Commit

Permalink
add code coverage (#12)
Browse files Browse the repository at this point in the history
* add code coverage

* lint
  • Loading branch information
muir authored Mar 30, 2022
1 parent 83aa21d commit df7b259
Show file tree
Hide file tree
Showing 10 changed files with 84 additions and 17 deletions.
18 changes: 18 additions & 0 deletions .github/workflows/codecov.yml
Original file line number Diff line number Diff line change
@@ -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)
33 changes: 33 additions & 0 deletions .github/workflows/golangci-lint.yml
Original file line number Diff line number Diff line change
@@ -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
9 changes: 9 additions & 0 deletions .github/workflows/mysql.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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)


8 changes: 8 additions & 0 deletions .github/workflows/pg.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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)

1 change: 0 additions & 1 deletion api.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,6 @@ type Schema struct {
databases map[string]*Database
databaseOrder []*Database
options Options
count int
context context.Context
}

Expand Down
14 changes: 10 additions & 4 deletions apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
}
Expand Down Expand Up @@ -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
}
4 changes: 2 additions & 2 deletions lsmysql/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -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`)
Expand Down
5 changes: 2 additions & 3 deletions lsmysql/mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)
Expand Down
4 changes: 0 additions & 4 deletions lsmysql/mysql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,3 @@ func TestSkipFunctions(t *testing.T) {
assert.True(t, enf, "users hi_level constraint")
}
}

func pointerToString(s string) *string {
return &s
}
5 changes: 2 additions & 3 deletions lspostgres/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)
Expand Down

0 comments on commit df7b259

Please sign in to comment.