Skip to content

Commit

Permalink
Repalce all sqlx with wrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
qbart committed Oct 30, 2021
1 parent 5d4edfb commit a4887be
Show file tree
Hide file tree
Showing 11 changed files with 41 additions and 38 deletions.
5 changes: 5 additions & 0 deletions krabdb/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ type ExecerContext interface {

type QueryerContext interface {
SelectContext(ctx context.Context, dest interface{}, query string, args ...interface{}) error
QueryContext(ctx context.Context, query string, args ...interface{}) (*sqlx.Rows, error)
}

type DB struct {
Expand All @@ -36,6 +37,10 @@ func (d *DB) SelectContext(ctx context.Context, dest interface{}, query string,
return sqlx.SelectContext(ctx, d.db, dest, query, args...)
}

func (d *DB) QueryContext(ctx context.Context, query string, args ...interface{}) (*sqlx.Rows, error) {
return d.db.QueryxContext(ctx, query, args...)
}

func (d *DB) ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error) {
return d.db.ExecContext(ctx, query, args...)
}
4 changes: 2 additions & 2 deletions spec/action_migrate_down_arguments_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ package spec
import (
"testing"

"github.com/jmoiron/sqlx"
"github.com/ohkrab/krab/krabdb"
)

func TestActionMigrateDownArugments(t *testing.T) {
withPg(t, func(db *sqlx.DB) {
withPg(t, func(db *krabdb.DB) {
c := mockCli(mockConfig(`
migration "create_animals" {
version = "v1"
Expand Down
4 changes: 2 additions & 2 deletions spec/action_migrate_down_hooks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ package spec
import (
"testing"

"github.com/jmoiron/sqlx"
"github.com/ohkrab/krab/krabdb"
)

func TestActionMigrateDownHooks(t *testing.T) {
withPg(t, func(db *sqlx.DB) {
withPg(t, func(db *krabdb.DB) {
c := mockCli(mockConfig(`
migration "create_animals" {
version = "v1"
Expand Down
8 changes: 4 additions & 4 deletions spec/action_migrate_down_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ package spec
import (
"testing"

"github.com/jmoiron/sqlx"
"github.com/ohkrab/krab/krabdb"
"github.com/stretchr/testify/assert"
)

func TestActionMigrateDown(t *testing.T) {
assert := assert.New(t)

withPg(t, func(db *sqlx.DB) {
withPg(t, func(db *krabdb.DB) {
c := mockCli(mockConfig(`
migration "create_animals" {
version = "v1"
Expand Down Expand Up @@ -65,7 +65,7 @@ Done
func TestActionMigrateDownOnError(t *testing.T) {
assert := assert.New(t)

withPg(t, func(db *sqlx.DB) {
withPg(t, func(db *krabdb.DB) {
c := mockCli(mockConfig(`
migration "create_animals" {
version = "v1"
Expand Down Expand Up @@ -118,7 +118,7 @@ Done
func TestActionMigrateDownWhenSchemaDoesNotExist(t *testing.T) {
assert := assert.New(t)

withPg(t, func(db *sqlx.DB) {
withPg(t, func(db *krabdb.DB) {
c := mockCli(mockConfig(`
migration "create_animals" {
version = "v1"
Expand Down
4 changes: 2 additions & 2 deletions spec/action_migrate_down_transaction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ package spec
import (
"testing"

"github.com/jmoiron/sqlx"
"github.com/ohkrab/krab/krabdb"
)

func TestActionMigrateDownTransactions(t *testing.T) {
withPg(t, func(db *sqlx.DB) {
withPg(t, func(db *krabdb.DB) {
c := mockCli(mockConfig(`
migration "create_animals" {
version = "v1"
Expand Down
4 changes: 2 additions & 2 deletions spec/action_migrate_up_arguments_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ package spec
import (
"testing"

"github.com/jmoiron/sqlx"
"github.com/ohkrab/krab/krabdb"
)

func TestActionMigrateUpArguments(t *testing.T) {
withPg(t, func(db *sqlx.DB) {
withPg(t, func(db *krabdb.DB) {
c := mockCli(mockConfig(`
migration "create_animals" {
version = "v1"
Expand Down
4 changes: 2 additions & 2 deletions spec/action_migrate_up_dsl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ package spec
import (
"testing"

"github.com/jmoiron/sqlx"
"github.com/ohkrab/krab/krabdb"
)

func TestActionMigrateUpDsl(t *testing.T) {
withPg(t, func(db *sqlx.DB) {
withPg(t, func(db *krabdb.DB) {
c := mockCli(mockConfig(`
migration "create_categories" {
version = "v1"
Expand Down
4 changes: 2 additions & 2 deletions spec/action_migrate_up_hooks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ package spec
import (
"testing"

"github.com/jmoiron/sqlx"
"github.com/ohkrab/krab/krabdb"
)

func TestActionMigrateUpHooks(t *testing.T) {
withPg(t, func(db *sqlx.DB) {
withPg(t, func(db *krabdb.DB) {
c := mockCli(mockConfig(`
migration "create_animals" {
version = "v1"
Expand Down
8 changes: 3 additions & 5 deletions spec/action_migrate_up_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ import (
"context"
"testing"

"github.com/jmoiron/sqlx"
"github.com/ohkrab/krab/krab"
"github.com/ohkrab/krab/krabdb"
)

func TestActionMigrateUp(t *testing.T) {
withPg(t, func(db *sqlx.DB) {
withPg(t, func(db *krabdb.DB) {
c := mockCli(mockConfig(`
migration "do_nothing" {
version = "v1"
Expand All @@ -34,9 +34,7 @@ Done
}

func TestActionMigrateUpWithError(t *testing.T) {
// assert := assert.New(t)

withPg(t, func(db *sqlx.DB) {
withPg(t, func(db *krabdb.DB) {
krab.NewSchemaMigrationTable("public").Init(context.TODO(), db)

c := mockCli(mockConfig(`
Expand Down
4 changes: 2 additions & 2 deletions spec/action_migrate_up_transaction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ package spec
import (
"testing"

"github.com/jmoiron/sqlx"
"github.com/ohkrab/krab/krabdb"
)

func TestActionMigrateUpTransactions(t *testing.T) {
withPg(t, func(db *sqlx.DB) {
withPg(t, func(db *krabdb.DB) {
c := mockCli(mockConfig(`
migration "create_animals" {
version = "v1"
Expand Down
30 changes: 15 additions & 15 deletions spec/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"bytes"
"context"
"fmt"
"os"
"strings"
"testing"

Expand All @@ -14,6 +13,8 @@ import (
"github.com/ohkrab/krab/cli"
"github.com/ohkrab/krab/krab"
"github.com/ohkrab/krab/krabcli"
"github.com/ohkrab/krab/krabdb"
"github.com/ohkrab/krab/krabenv"
"github.com/spf13/afero"
"github.com/stretchr/testify/assert"
)
Expand Down Expand Up @@ -75,7 +76,7 @@ func (m *cliMock) AssertUiErrorOutputContains(t *testing.T, output string) bool
)
}

func (m *cliMock) AssertSchemaMigrationTableMissing(t *testing.T, db *sqlx.DB, schema string) bool {
func (m *cliMock) AssertSchemaMigrationTableMissing(t *testing.T, db *krabdb.DB, schema string) bool {
_, err := krab.NewSchemaMigrationTable(schema).SelectAll(context.TODO(), db)
if assert.Error(t, err) {
return assert.Contains(
Expand All @@ -88,7 +89,7 @@ func (m *cliMock) AssertSchemaMigrationTableMissing(t *testing.T, db *sqlx.DB, s
return false
}

func (m *cliMock) AssertSchemaMigrationTable(t *testing.T, db *sqlx.DB, schema string, expectedVersions ...string) bool {
func (m *cliMock) AssertSchemaMigrationTable(t *testing.T, db *krabdb.DB, schema string, expectedVersions ...string) bool {
versions, err := krab.NewSchemaMigrationTable(schema).SelectAll(context.TODO(), db)
if assert.NoError(t, err) {
if assert.Equal(t, len(versions), len(expectedVersions), "Scheme versions count mismatch") {
Expand All @@ -107,8 +108,8 @@ func (m *cliMock) AssertSchemaMigrationTable(t *testing.T, db *sqlx.DB, schema s
return false
}

func (m *cliMock) Query(t *testing.T, db *sqlx.DB, query string) ([]string, []map[string]interface{}) {
rows, err := db.QueryxContext(context.TODO(), query)
func (m *cliMock) Query(t *testing.T, db *krabdb.DB, query string) ([]string, []map[string]interface{}) {
rows, err := db.QueryContext(context.TODO(), query)
assert.NoError(t, err, fmt.Sprint("Query ", query, " must execute successfully"))
defer rows.Close()

Expand All @@ -118,7 +119,7 @@ func (m *cliMock) Query(t *testing.T, db *sqlx.DB, query string) ([]string, []ma
return cols, vals
}

func (m *cliMock) Insert(t *testing.T, db *sqlx.DB, table string, cols string, vals string) bool {
func (m *cliMock) Insert(t *testing.T, db *krabdb.DB, table string, cols string, vals string) bool {
_, err := db.ExecContext(
context.TODO(),
fmt.Sprintf(
Expand Down Expand Up @@ -170,24 +171,19 @@ func mockParser(pathContentPair ...string) *krab.Parser {
return p
}

func withPg(t *testing.T, f func(db *sqlx.DB)) {
db, err := sqlx.Connect("pgx", os.Getenv("DATABASE_URL"))
func withPg(t *testing.T, f func(db *krabdb.DB)) {
db, err := krabdb.Connect(krabenv.DatabaseURL())
if err != nil {
t.Fatal(err)
}
defer db.Close()

err = db.Ping()
if err != nil {
t.Fatalf("Failed to ping db: %v", err)
}
defer cleanDb(db)

f(db)
}

func cleanDb(db *sqlx.DB) {
db.MustExec(`
func cleanDb(db *krabdb.DB) {
_, err := db.ExecContext(context.TODO(), `
DO
$$
DECLARE
Expand All @@ -204,6 +200,10 @@ BEGIN
END LOOP;
END
$$`)

if err != nil {
panic(err)
}
}

func sqlxRowsMapScan(rows *sqlx.Rows) []map[string]interface{} {
Expand Down

0 comments on commit a4887be

Please sign in to comment.