From d426afcec1fe8bf900166cbf0e32d84163de301a Mon Sep 17 00:00:00 2001 From: Jhoni Conzatti Date: Mon, 26 Jan 2026 20:26:19 -0300 Subject: [PATCH 1/5] fix(migrations): throw deferred error --- migrations/migrator.go | 28 +++++++++++++--------------- 1 file changed, 13 insertions(+), 15 deletions(-) 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 { From dbd3737e82ddc7f2bfd2b14e7b47062b1577302d Mon Sep 17 00:00:00 2001 From: Jhoni Conzatti Date: Mon, 26 Jan 2026 20:26:44 -0300 Subject: [PATCH 2/5] fix:(schema): throw deferred error --- schema/schema.go | 38 +++++++++++++++++++------------------- 1 file changed, 19 insertions(+), 19 deletions(-) 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 } From b7974bd23d98ea14ec59bf0927dac3dab1c6616c Mon Sep 17 00:00:00 2001 From: Jhoni Conzatti Date: Mon, 26 Jan 2026 20:34:11 -0300 Subject: [PATCH 3/5] test(schema): use testutils.AssertNilError instead --- schema/schema_test.go | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/schema/schema_test.go b/schema/schema_test.go index 2a30e66..ca7d020 100644 --- a/schema/schema_test.go +++ b/schema/schema_test.go @@ -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.AssertNilError(t, lError, "ormshift.OpenDatabase") { return } defer func() { _ = lDB.Close() }() @@ -68,8 +67,7 @@ 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.AssertNilError(t, lError, "ormshift.OpenDatabase") { return } defer func() { _ = lDB.Close() }() From f122970cdd21f489ebb07fff0d03a079a9a19c37 Mon Sep 17 00:00:00 2001 From: Jhoni Conzatti Date: Mon, 26 Jan 2026 20:35:17 -0300 Subject: [PATCH 4/5] tests(schema): already deferred DB.Close() --- schema/schema_test.go | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/schema/schema_test.go b/schema/schema_test.go index ca7d020..8b9cc7f 100644 --- a/schema/schema_test.go +++ b/schema/schema_test.go @@ -74,16 +74,18 @@ func TestHasTableReturnsFalseWhenDatabaseIsInvalid(t *testing.T) { 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") } From 30ee4df777869aade4ea4d8e4f7538b7899a9798 Mon Sep 17 00:00:00 2001 From: Jhoni Conzatti Date: Mon, 26 Jan 2026 20:41:25 -0300 Subject: [PATCH 5/5] tests: use testutils.AssertNotNilResultAndNilError instead --- executor_test.go | 3 +-- migrations/migrations_test.go | 12 ++++-------- migrations/migrator_test.go | 16 ++++++---------- schema/schema_test.go | 6 +++--- 4 files changed, 14 insertions(+), 23 deletions(-) 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_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_test.go b/schema/schema_test.go index 8b9cc7f..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,7 +32,7 @@ func TestNewDBSchemaFailsWhenDBIsNil(t *testing.T) { func TestHasColumn(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() }() @@ -67,7 +67,7 @@ func TestHasColumn(t *testing.T) { func TestHasTableReturnsFalseWhenDatabaseIsInvalid(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() }()