Skip to content

Commit 5c49f1f

Browse files
authored
fix: Remove required Close from database.Migrate (#25)
* fix: Remove required Close from database.Migrate * Remove dbName from Migrate function arguments * Fix func call
1 parent a461bc1 commit 5c49f1f

File tree

5 files changed

+13
-16
lines changed

5 files changed

+13
-16
lines changed

database/dump/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ func main() {
2525
if err != nil {
2626
panic(err)
2727
}
28-
err = database.Migrate(context.Background(), "postgres", db)
28+
err = database.Migrate(context.Background(), db)
2929
if err != nil {
3030
panic(err)
3131
}

database/migrate.go

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import (
1616
var migrations embed.FS
1717

1818
// Migrate runs SQL migrations to ensure the database schema is up-to-date.
19-
func Migrate(ctx context.Context, dbName string, db *sql.DB) error {
19+
func Migrate(ctx context.Context, db *sql.DB) error {
2020
sourceDriver, err := iofs.New(migrations, "migrations")
2121
if err != nil {
2222
return xerrors.Errorf("create iofs: %w", err)
@@ -25,7 +25,7 @@ func Migrate(ctx context.Context, dbName string, db *sql.DB) error {
2525
if err != nil {
2626
return xerrors.Errorf("wrap postgres connection: %w", err)
2727
}
28-
m, err := migrate.NewWithInstance("", sourceDriver, dbName, dbDriver)
28+
m, err := migrate.NewWithInstance("", sourceDriver, "", dbDriver)
2929
if err != nil {
3030
return xerrors.Errorf("migrate: %w", err)
3131
}
@@ -37,12 +37,5 @@ func Migrate(ctx context.Context, dbName string, db *sql.DB) error {
3737
}
3838
return xerrors.Errorf("up: %w", err)
3939
}
40-
srcErr, dbErr := m.Close()
41-
if srcErr != nil {
42-
return xerrors.Errorf("close source: %w", err)
43-
}
44-
if dbErr != nil {
45-
return xerrors.Errorf("close database: %w", err)
46-
}
4740
return nil
4841
}

database/migrate_test.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,11 @@ import (
77
"database/sql"
88
"testing"
99

10-
"github.com/coder/coder/database"
11-
"github.com/coder/coder/database/postgres"
1210
"github.com/stretchr/testify/require"
1311
"go.uber.org/goleak"
12+
13+
"github.com/coder/coder/database"
14+
"github.com/coder/coder/database/postgres"
1415
)
1516

1617
func TestMain(m *testing.M) {
@@ -25,6 +26,7 @@ func TestMigrate(t *testing.T) {
2526
defer closeFn()
2627
db, err := sql.Open("postgres", connection)
2728
require.NoError(t, err)
28-
err = database.Migrate(context.Background(), "postgres", db)
29+
defer db.Close()
30+
err = database.Migrate(context.Background(), db)
2931
require.NoError(t, err)
3032
}

database/pubsub_memory_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@ import (
44
"context"
55
"testing"
66

7-
"github.com/coder/coder/database"
87
"github.com/stretchr/testify/assert"
98
"github.com/stretchr/testify/require"
9+
10+
"github.com/coder/coder/database"
1011
)
1112

1213
func TestPubsubMemory(t *testing.T) {

database/pubsub_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,11 @@ import (
77
"database/sql"
88
"testing"
99

10-
"github.com/coder/coder/database"
11-
"github.com/coder/coder/database/postgres"
1210
"github.com/stretchr/testify/assert"
1311
"github.com/stretchr/testify/require"
12+
13+
"github.com/coder/coder/database"
14+
"github.com/coder/coder/database/postgres"
1415
)
1516

1617
func TestPubsub(t *testing.T) {

0 commit comments

Comments
 (0)