diff --git a/executor_test.go b/executor_test.go index 2c2f8c4..bc0d4f9 100644 --- a/executor_test.go +++ b/executor_test.go @@ -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() }() diff --git a/migrations/migrations_test.go b/migrations/migrations_test.go index f19f974..e3422be 100644 --- a/migrations/migrations_test.go +++ b/migrations/migrations_test.go @@ -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() }() @@ -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() }() @@ -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() @@ -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() }() diff --git a/migrations/migrator.go b/migrations/migrator.go index 0972f7d..e9a13cf 100644 --- a/migrations/migrator.go +++ b/migrations/migrator.go @@ -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( @@ -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 { diff --git a/migrations/migrator_test.go b/migrations/migrator_test.go index f52d5bc..9e207ba 100644 --- a/migrations/migrator_test.go +++ b/migrations/migrator_test.go @@ -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") { @@ -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() }() @@ -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() }() @@ -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() }() @@ -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() }() diff --git a/schema/schema.go b/schema/schema.go index 2d95994..87857c3 100644 --- a/schema/schema.go +++ b/schema/schema.go @@ -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 { @@ -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 } diff --git a/schema/schema_test.go b/schema/schema_test.go index 2a30e66..fc3bdfe 100644 --- a/schema/schema_test.go +++ b/schema/schema_test.go @@ -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() }() @@ -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() }() @@ -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") }