-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: move db package to be a shared package
- Loading branch information
1 parent
d8b8669
commit d0d5ca7
Showing
224 changed files
with
1,418 additions
and
1,846 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
package dbtest | ||
|
||
import ( | ||
"net/http" | ||
"testing" | ||
|
||
migrate "github.com/rubenv/sql-migrate" | ||
"github.com/stellar/go/support/db/dbtest" | ||
"github.com/stellar/go/support/db/schema" | ||
authmigrations "github.com/stellar/stellar-disbursement-platform-backend/db/migrations/auth-migrations" | ||
sdpmigrations "github.com/stellar/stellar-disbursement-platform-backend/db/migrations/sdp-migrations" | ||
tenantmigrations "github.com/stellar/stellar-disbursement-platform-backend/db/migrations/tenant-migrations" | ||
) | ||
|
||
func OpenWithoutMigrations(t *testing.T) *dbtest.DB { | ||
db := dbtest.Postgres(t) | ||
return db | ||
} | ||
|
||
func Open(t *testing.T) *dbtest.DB { | ||
db := OpenWithoutMigrations(t) | ||
|
||
conn := db.Open() | ||
defer conn.Close() | ||
|
||
// Tenant migrations | ||
ms := migrate.MigrationSet{TableName: "migrations"} | ||
migrateDirection := migrate.Up | ||
m := migrate.HttpFileSystemMigrationSource{FileSystem: http.FS(tenantmigrations.FS)} | ||
_, err := ms.ExecMax(conn.DB, "postgres", m, migrateDirection, 0) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
// SDP migrations | ||
ms = migrate.MigrationSet{} | ||
m = migrate.HttpFileSystemMigrationSource{FileSystem: http.FS(sdpmigrations.FS)} | ||
_, err = ms.ExecMax(conn.DB, "postgres", m, migrateDirection, 0) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
// Auth migrations | ||
ms = migrate.MigrationSet{TableName: "auth_migrations"} | ||
m = migrate.HttpFileSystemMigrationSource{FileSystem: http.FS(authmigrations.FS)} | ||
_, err = ms.ExecMax(conn.DB, "postgres", m, migrateDirection, 0) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
return db | ||
} | ||
|
||
func OpenWithTenantMigrationsOnly(t *testing.T) *dbtest.DB { | ||
db := OpenWithoutMigrations(t) | ||
|
||
conn := db.Open() | ||
defer conn.Close() | ||
|
||
migrateDirection := schema.MigrateUp | ||
m := migrate.HttpFileSystemMigrationSource{FileSystem: http.FS(tenantmigrations.FS)} | ||
_, err := schema.Migrate(conn.DB, m, migrateDirection, 0) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
return db | ||
} | ||
|
||
func OpenWithSDPMigrationsOnly(t *testing.T) *dbtest.DB { | ||
db := OpenWithoutMigrations(t) | ||
|
||
conn := db.Open() | ||
defer conn.Close() | ||
|
||
migrateDirection := schema.MigrateUp | ||
m := migrate.HttpFileSystemMigrationSource{FileSystem: http.FS(sdpmigrations.FS)} | ||
_, err := schema.Migrate(conn.DB, m, migrateDirection, 0) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
return db | ||
} | ||
|
||
func OpenWithAuthMigrationsOnly(t *testing.T) *dbtest.DB { | ||
db := OpenWithoutMigrations(t) | ||
|
||
conn := db.Open() | ||
defer conn.Close() | ||
|
||
migrateDirection := schema.MigrateUp | ||
m := migrate.HttpFileSystemMigrationSource{FileSystem: http.FS(authmigrations.FS)} | ||
_, err := schema.Migrate(conn.DB, m, migrateDirection, 0) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
return db | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package dbtest | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestOpen(t *testing.T) { | ||
db := Open(t) | ||
defer db.Close() | ||
|
||
session := db.Open() | ||
defer session.Close() | ||
|
||
count := 0 | ||
|
||
// Tenant migrations | ||
err := session.Get(&count, `SELECT COUNT(*) FROM migrations`) | ||
require.NoError(t, err) | ||
assert.Greater(t, count, 0) | ||
|
||
// SDP migrations | ||
err = session.Get(&count, `SELECT COUNT(*) FROM gorp_migrations`) | ||
require.NoError(t, err) | ||
assert.Greater(t, count, 0) | ||
|
||
// Auth Migrations | ||
err = session.Get(&count, `SELECT COUNT(*) FROM auth_migrations`) | ||
require.NoError(t, err) | ||
assert.Greater(t, count, 0) | ||
} |
17 changes: 11 additions & 6 deletions
17
stellar-multitenant/internal/db/migrate.go → db/migrate.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,33 @@ | ||
package db | ||
|
||
import ( | ||
"embed" | ||
"fmt" | ||
"net/http" | ||
|
||
"github.com/stellar/stellar-disbursement-platform-backend/stellar-multitenant/internal/db/migrations" | ||
|
||
migrate "github.com/rubenv/sql-migrate" | ||
"github.com/stellar/stellar-disbursement-platform-backend/stellar-auth/pkg/utils" | ||
) | ||
|
||
const StellarMultitenantMigrationsTableName = "migrations" | ||
type MigrationTableName string | ||
|
||
const ( | ||
StellarMultitenantMigrationsTableName = "migrations" | ||
StellarSDPMigrationsTableName = "gorp_migrations" | ||
StellarAuthMigrationsTableName = "auth_migrations" | ||
) | ||
|
||
func Migrate(dbURL string, dir migrate.MigrationDirection, count int) (int, error) { | ||
func Migrate(dbURL string, dir migrate.MigrationDirection, count int, migrationFiles embed.FS, tableName MigrationTableName) (int, error) { | ||
dbConnectionPool, err := OpenDBConnectionPool(dbURL) | ||
if err != nil { | ||
return 0, fmt.Errorf("database URL '%s': %w", utils.TruncateString(dbURL, len(dbURL)/4), err) | ||
} | ||
defer dbConnectionPool.Close() | ||
|
||
ms := migrate.MigrationSet{ | ||
TableName: StellarMultitenantMigrationsTableName, | ||
TableName: string(tableName), | ||
} | ||
|
||
m := migrate.HttpFileSystemMigrationSource{FileSystem: http.FS(migrations.FS)} | ||
m := migrate.HttpFileSystemMigrationSource{FileSystem: http.FS(migrationFiles)} | ||
return ms.ExecMax(dbConnectionPool.SqlDB(), dbConnectionPool.DriverName(), m, dir, count) | ||
} |
Oops, something went wrong.