diff --git a/krabdb/db.go b/krabdb/db.go index e5c68db..054a0d0 100644 --- a/krabdb/db.go +++ b/krabdb/db.go @@ -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 { @@ -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...) } diff --git a/spec/action_migrate_down_arguments_test.go b/spec/action_migrate_down_arguments_test.go index ed4d08b..27a314a 100644 --- a/spec/action_migrate_down_arguments_test.go +++ b/spec/action_migrate_down_arguments_test.go @@ -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" diff --git a/spec/action_migrate_down_hooks_test.go b/spec/action_migrate_down_hooks_test.go index f67aa1a..3186228 100644 --- a/spec/action_migrate_down_hooks_test.go +++ b/spec/action_migrate_down_hooks_test.go @@ -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" diff --git a/spec/action_migrate_down_test.go b/spec/action_migrate_down_test.go index 34654a4..b1dba52 100644 --- a/spec/action_migrate_down_test.go +++ b/spec/action_migrate_down_test.go @@ -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" @@ -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" @@ -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" diff --git a/spec/action_migrate_down_transaction_test.go b/spec/action_migrate_down_transaction_test.go index 12493ea..ea96418 100644 --- a/spec/action_migrate_down_transaction_test.go +++ b/spec/action_migrate_down_transaction_test.go @@ -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" diff --git a/spec/action_migrate_up_arguments_test.go b/spec/action_migrate_up_arguments_test.go index f851a4d..0e7e6d2 100644 --- a/spec/action_migrate_up_arguments_test.go +++ b/spec/action_migrate_up_arguments_test.go @@ -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" diff --git a/spec/action_migrate_up_dsl_test.go b/spec/action_migrate_up_dsl_test.go index 3688298..78ff341 100644 --- a/spec/action_migrate_up_dsl_test.go +++ b/spec/action_migrate_up_dsl_test.go @@ -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" diff --git a/spec/action_migrate_up_hooks_test.go b/spec/action_migrate_up_hooks_test.go index 44c62f9..a00fcd6 100644 --- a/spec/action_migrate_up_hooks_test.go +++ b/spec/action_migrate_up_hooks_test.go @@ -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" diff --git a/spec/action_migrate_up_test.go b/spec/action_migrate_up_test.go index 8a331f5..15d98e9 100644 --- a/spec/action_migrate_up_test.go +++ b/spec/action_migrate_up_test.go @@ -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" @@ -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(` diff --git a/spec/action_migrate_up_transaction_test.go b/spec/action_migrate_up_transaction_test.go index a3fcf5d..de61c3b 100644 --- a/spec/action_migrate_up_transaction_test.go +++ b/spec/action_migrate_up_transaction_test.go @@ -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" diff --git a/spec/helpers_test.go b/spec/helpers_test.go index 0304349..2445e2d 100644 --- a/spec/helpers_test.go +++ b/spec/helpers_test.go @@ -4,7 +4,6 @@ import ( "bytes" "context" "fmt" - "os" "strings" "testing" @@ -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" ) @@ -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( @@ -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") { @@ -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() @@ -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( @@ -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 @@ -204,6 +200,10 @@ BEGIN END LOOP; END $$`) + + if err != nil { + panic(err) + } } func sqlxRowsMapScan(rows *sqlx.Rows) []map[string]interface{} {