Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions executor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@ type userRow struct {

func TestExecutor(t *testing.T) {
lDB, lError := ormshift.OpenDatabase(sqlite.Driver(), ormshift.ConnectionParams{InMemory: true})
if lError != nil {
t.Errorf("ormshift.OpenDatabase failed: %v", lError)
if !testutils.AssertNotNilResultAndNilError(t, lDB, lError, "ormshift.OpenDatabase") {
return
}
defer func() { _ = lDB.Close() }()
Expand Down
12 changes: 4 additions & 8 deletions migrations/migrations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ import (

func TestMigrate(t *testing.T) {
lDB, lError := ormshift.OpenDatabase(sqlite.Driver(), ormshift.ConnectionParams{InMemory: true})
if lError != nil {
t.Errorf("ormshift.OpenDatabase failed: %v", lError)
if !testutils.AssertNilError(t, lError, "ormshift.OpenDatabase") {
return
}
defer func() { _ = lDB.Close() }()
Expand Down Expand Up @@ -41,8 +40,7 @@ func TestMigrate(t *testing.T) {

func TestMigrateTwice(t *testing.T) {
lDB, lError := ormshift.OpenDatabase(sqlite.Driver(), ormshift.ConnectionParams{InMemory: true})
if lError != nil {
t.Errorf("ormshift.OpenDatabase failed: %v", lError)
if !testutils.AssertNilError(t, lError, "ormshift.OpenDatabase") {
return
}
defer func() { _ = lDB.Close() }()
Expand Down Expand Up @@ -81,8 +79,7 @@ func TestMigrateTwice(t *testing.T) {

func TestMigrateFailsWhenDatabaseIsClosed(t *testing.T) {
lDB, lError := ormshift.OpenDatabase(sqlite.Driver(), ormshift.ConnectionParams{InMemory: true})
if lError != nil {
t.Errorf("ormshift.OpenDatabase failed: %v", lError)
if !testutils.AssertNilError(t, lError, "ormshift.OpenDatabase") {
return
}
_ = lDB.Close()
Expand All @@ -101,8 +98,7 @@ func TestMigrateFailsWhenDatabaseIsClosed(t *testing.T) {

func TestMigrateFailsWhenMigrationUpFails(t *testing.T) {
lDB, lError := ormshift.OpenDatabase(sqlite.Driver(), ormshift.ConnectionParams{InMemory: true})
if lError != nil {
t.Errorf("ormshift.OpenDatabase failed: %v", lError)
if !testutils.AssertNilError(t, lError, "ormshift.OpenDatabase") {
return
}
defer func() { _ = lDB.Close() }()
Expand Down
28 changes: 13 additions & 15 deletions migrations/migrator.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,12 +128,10 @@ func (m Migrator) deleteAppliedMigration(pMigrationName string) error {
return lError
}

func getAppliedMigrationNames(pDatabase ormshift.Database, pConfig MigratorConfig) ([]string, error) {
var lAppliedMigrationNames []string

lError := ensureMigrationsTableExists(pDatabase, pConfig)
if lError != nil {
return nil, lError
func getAppliedMigrationNames(pDatabase ormshift.Database, pConfig MigratorConfig) (rMigrationNames []string, rError error) {
rError = ensureMigrationsTableExists(pDatabase, pConfig)
if rError != nil {
return
}

q, p := pDatabase.SQLBuilder().InteroperateSQLCommandWithNamedArgs(
Expand All @@ -144,24 +142,24 @@ func getAppliedMigrationNames(pDatabase ormshift.Database, pConfig MigratorConfi
pConfig.migrationNameColumn,
),
)
lMigrationsRows, lError := pDatabase.SQLExecutor().Query(q, p...)
if lError != nil {
return nil, lError
lMigrationsRows, rError := pDatabase.SQLExecutor().Query(q, p...)
if rError != nil {
return
}
defer func() {
if err := lMigrationsRows.Close(); err != nil && lError == nil {
lError = err
if err := lMigrationsRows.Close(); err != nil && rError == nil {
rError = err
}
}()
for lMigrationsRows.Next() {
var lMigrationName string
lError = lMigrationsRows.Scan(&lMigrationName)
if lError != nil {
rError = lMigrationsRows.Scan(&lMigrationName)
if rError != nil {
break
}
lAppliedMigrationNames = append(lAppliedMigrationNames, lMigrationName)
rMigrationNames = append(rMigrationNames, lMigrationName)
}
return lAppliedMigrationNames, lError
return
}

func ensureMigrationsTableExists(pDatabase ormshift.Database, pConfig MigratorConfig) error {
Expand Down
16 changes: 6 additions & 10 deletions migrations/migrator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ func TestNewMigratorWhenDatabaseIsInvalid(t *testing.T) {

func TestApplyAllMigrationsFailsWhenRecordingFails(t *testing.T) {
lDB, lError := ormshift.OpenDatabase(sqlite.Driver(), ormshift.ConnectionParams{InMemory: true})
if lError != nil {
t.Errorf("ormshift.OpenDatabase failed: %v", lError)
if !testutils.AssertNotNilResultAndNilError(t, lDB, lError, "ormshift.OpenDatabase") {
return
}
defer func() { _ = lDB.Close() }()

lMigrator, lError := migrations.NewMigrator(lDB, migrations.NewMigratorConfig())
if !testutils.AssertNotNilResultAndNilError(t, lMigrator, lError, "migrations.NewMigrator") {
Expand All @@ -54,8 +54,7 @@ func TestApplyAllMigrationsFailsWhenRecordingFails(t *testing.T) {

func TestRevertLastAppliedMigration(t *testing.T) {
lDB, lError := ormshift.OpenDatabase(sqlite.Driver(), ormshift.ConnectionParams{InMemory: true})
if lError != nil {
t.Errorf("ormshift.OpenDatabase failed: %v", lError)
if !testutils.AssertNotNilResultAndNilError(t, lDB, lError, "ormshift.OpenDatabase") {
return
}
defer func() { _ = lDB.Close() }()
Expand Down Expand Up @@ -89,8 +88,7 @@ func TestRevertLastAppliedMigration(t *testing.T) {

func TestRevertLastAppliedMigrationWhenNoMigrationsApplied(t *testing.T) {
lDB, lError := ormshift.OpenDatabase(sqlite.Driver(), ormshift.ConnectionParams{InMemory: true})
if lError != nil {
t.Errorf("ormshift.OpenDatabase failed: %v", lError)
if !testutils.AssertNotNilResultAndNilError(t, lDB, lError, "ormshift.OpenDatabase") {
return
}
defer func() { _ = lDB.Close() }()
Expand All @@ -107,8 +105,7 @@ func TestRevertLastAppliedMigrationWhenNoMigrationsApplied(t *testing.T) {

func TestRevertLastAppliedMigrationFailsWhenDownFails(t *testing.T) {
lDB, lError := ormshift.OpenDatabase(sqlite.Driver(), ormshift.ConnectionParams{InMemory: true})
if lError != nil {
t.Errorf("ormshift.OpenDatabase failed: %v", lError)
if !testutils.AssertNotNilResultAndNilError(t, lDB, lError, "ormshift.OpenDatabase") {
return
}
defer func() { _ = lDB.Close() }()
Expand All @@ -131,8 +128,7 @@ func TestRevertLastAppliedMigrationFailsWhenDownFails(t *testing.T) {

func TestRevertLastAppliedMigrationFailsWhenDeletingFails(t *testing.T) {
lDB, lError := ormshift.OpenDatabase(sqlite.Driver(), ormshift.ConnectionParams{InMemory: true})
if lError != nil {
t.Errorf("ormshift.OpenDatabase failed: %v", lError)
if !testutils.AssertNotNilResultAndNilError(t, lDB, lError, "ormshift.OpenDatabase") {
return
}
defer func() { _ = lDB.Close() }()
Expand Down
38 changes: 19 additions & 19 deletions schema/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,26 +30,25 @@ func (s DBSchema) HasTable(pTableName TableName) bool {
})
}

func (s DBSchema) fetchTableNames() ([]string, error) {
lRows, lError := s.db.Query(s.tableNamesQuery)
if lError != nil {
return nil, lError
func (s DBSchema) fetchTableNames() (rTableNames []string, rError error) {
lRows, rError := s.db.Query(s.tableNamesQuery)
if rError != nil {
return
}
defer func() {
if err := lRows.Close(); err != nil && lError == nil {
lError = err
if err := lRows.Close(); err != nil && rError == nil {
rError = err
}
}()
var lTableNames []string
lTableName := ""
for lRows.Next() {
lError = lRows.Scan(&lTableName)
if lError != nil {
return nil, lError
rError = lRows.Scan(&lTableName)
if rError != nil {
return
}
lTableNames = append(lTableNames, lTableName)
rTableNames = append(rTableNames, lTableName)
}
return lTableNames, lError
return
}

func (s DBSchema) HasColumn(pTableName TableName, pColumnName ColumnName) bool {
Expand All @@ -62,15 +61,16 @@ func (s DBSchema) HasColumn(pTableName TableName, pColumnName ColumnName) bool {
})
}

func (s DBSchema) fetchColumnTypes(pTableName TableName) ([]*sql.ColumnType, error) {
lRows, lError := s.db.Query(fmt.Sprintf("SELECT * FROM %s WHERE 1=0", pTableName.String())) // NOSONAR go:S2077 - Dynamic SQL is controlled and sanitized internally
if lError != nil {
return nil, lError
func (s DBSchema) fetchColumnTypes(pTableName TableName) (rColumnTypes []*sql.ColumnType, rError error) {
lRows, rError := s.db.Query(fmt.Sprintf("SELECT * FROM %s WHERE 1=0", pTableName.String())) // NOSONAR go:S2077 - Dynamic SQL is controlled and sanitized internally
if rError != nil {
return
}
defer func() {
if err := lRows.Close(); err != nil && lError == nil {
lError = err
if err := lRows.Close(); err != nil && rError == nil {
rError = err
}
}()
return lRows.ColumnTypes()
rColumnTypes, rError = lRows.ColumnTypes()
return
}
16 changes: 8 additions & 8 deletions schema/schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (

func TestNewDBSchema(t *testing.T) {
lDB, lError := ormshift.OpenDatabase(sqlite.Driver(), ormshift.ConnectionParams{InMemory: true})
if !testutils.AssertNilError(t, lError, "ormshift.OpenDatabase") {
if !testutils.AssertNotNilResultAndNilError(t, lDB, lError, "ormshift.OpenDatabase") {
return
}
defer func() { _ = lDB.Close() }()
Expand All @@ -32,8 +32,7 @@ func TestNewDBSchemaFailsWhenDBIsNil(t *testing.T) {

func TestHasColumn(t *testing.T) {
lDB, lError := ormshift.OpenDatabase(sqlite.Driver(), ormshift.ConnectionParams{InMemory: true})
if lError != nil {
t.Errorf("ormshift.OpenDatabase failed: %v", lError)
if !testutils.AssertNotNilResultAndNilError(t, lDB, lError, "ormshift.OpenDatabase") {
return
}
defer func() { _ = lDB.Close() }()
Expand Down Expand Up @@ -68,24 +67,25 @@ func TestHasColumn(t *testing.T) {

func TestHasTableReturnsFalseWhenDatabaseIsInvalid(t *testing.T) {
lDB, lError := ormshift.OpenDatabase(sqlite.Driver(), ormshift.ConnectionParams{InMemory: true})
if lError != nil {
t.Errorf("ormshift.OpenDatabase failed: %v", lError)
if !testutils.AssertNotNilResultAndNilError(t, lDB, lError, "ormshift.OpenDatabase") {
return
}
defer func() { _ = lDB.Close() }()

lProductAttributeTable := testutils.FakeProductAttributeTable(t)
if lProductAttributeTable == nil {
_ = lDB.Close()
return
}

_, lError = lDB.SQLExecutor().Exec(sqlite.Driver().SQLBuilder().CreateTable(*lProductAttributeTable))
if !testutils.AssertNilError(t, lError, "DB.Exec") {
_ = lDB.Close()
return
}
_ = lDB.Close()

lError = lDB.Close()
if !testutils.AssertNilError(t, lError, "DB.Close") {
return
}
lDBSchema := lDB.DBSchema()
testutils.AssertEqualWithLabel(t, false, lDBSchema.HasTable(lProductAttributeTable.Name()), "DBSchema.HasTable")
}