Skip to content

Commit

Permalink
refactor: move db package to be a shared package
Browse files Browse the repository at this point in the history
  • Loading branch information
CaioTeixeira95 committed Nov 1, 2023
1 parent d8b8669 commit d0d5ca7
Show file tree
Hide file tree
Showing 224 changed files with 1,418 additions and 1,846 deletions.
2 changes: 1 addition & 1 deletion cmd/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import (
"github.com/stellar/go/support/config"
"github.com/stellar/go/support/log"
cmdUtils "github.com/stellar/stellar-disbursement-platform-backend/cmd/utils"
"github.com/stellar/stellar-disbursement-platform-backend/db"
"github.com/stellar/stellar-disbursement-platform-backend/internal/data"
"github.com/stellar/stellar-disbursement-platform-backend/internal/db"
di "github.com/stellar/stellar-disbursement-platform-backend/internal/dependencyinjection"
"github.com/stellar/stellar-disbursement-platform-backend/internal/htmltemplate"
"github.com/stellar/stellar-disbursement-platform-backend/internal/message"
Expand Down
2 changes: 1 addition & 1 deletion cmd/auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (

"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/stellar/stellar-disbursement-platform-backend/internal/db/dbtest"
"github.com/stellar/stellar-disbursement-platform-backend/db/dbtest"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
Expand Down
2 changes: 1 addition & 1 deletion cmd/channel_accounts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/stellar/stellar-disbursement-platform-backend/db/dbtest"
"github.com/stellar/stellar-disbursement-platform-backend/internal/crashtracker"
"github.com/stellar/stellar-disbursement-platform-backend/internal/db/dbtest"
txSubSvc "github.com/stellar/stellar-disbursement-platform-backend/internal/transactionsubmission/services"
)

Expand Down
5 changes: 3 additions & 2 deletions cmd/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import (
migrate "github.com/rubenv/sql-migrate"
"github.com/spf13/cobra"
"github.com/stellar/go/support/log"
"github.com/stellar/stellar-disbursement-platform-backend/internal/db"
"github.com/stellar/stellar-disbursement-platform-backend/db"
migrations "github.com/stellar/stellar-disbursement-platform-backend/db/migrations/sdp-migrations"
"github.com/stellar/stellar-disbursement-platform-backend/internal/services"
"github.com/stellar/stellar-disbursement-platform-backend/internal/utils"
"github.com/stellar/stellar-disbursement-platform-backend/stellar-auth/pkg/cli"
Expand Down Expand Up @@ -127,7 +128,7 @@ func (c *DatabaseCommand) Command() *cobra.Command {
}

func (c *DatabaseCommand) migrate(dir migrate.MigrationDirection, count int) error {
numMigrationsRun, err := db.Migrate(globalOptions.databaseURL, dir, count)
numMigrationsRun, err := db.Migrate(globalOptions.databaseURL, dir, count, migrations.FS, db.StellarAuthMigrationsTableName)
if err != nil {
return fmt.Errorf("migrating database: %w", err)
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ import (
"github.com/stellar/go/keypair"
"github.com/stellar/go/network"
"github.com/stellar/go/support/log"
"github.com/stellar/stellar-disbursement-platform-backend/db"
"github.com/stellar/stellar-disbursement-platform-backend/db/dbtest"
"github.com/stellar/stellar-disbursement-platform-backend/internal/data"
"github.com/stellar/stellar-disbursement-platform-backend/internal/db"
"github.com/stellar/stellar-disbursement-platform-backend/internal/db/dbtest"
"github.com/stellar/stellar-disbursement-platform-backend/internal/services"
"github.com/stellar/stellar-disbursement-platform-backend/internal/utils"
"github.com/stretchr/testify/assert"
Expand Down
2 changes: 1 addition & 1 deletion cmd/integration_tests_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"testing"

"github.com/spf13/cobra"
"github.com/stellar/stellar-disbursement-platform-backend/internal/db/dbtest"
"github.com/stellar/stellar-disbursement-platform-backend/db/dbtest"
"github.com/stellar/stellar-disbursement-platform-backend/internal/integrationtests"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
Expand Down
2 changes: 1 addition & 1 deletion cmd/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ import (
"go/types"

cmdUtils "github.com/stellar/stellar-disbursement-platform-backend/cmd/utils"
"github.com/stellar/stellar-disbursement-platform-backend/db"
"github.com/stellar/stellar-disbursement-platform-backend/internal/anchorplatform"
"github.com/stellar/stellar-disbursement-platform-backend/internal/crashtracker"
"github.com/stellar/stellar-disbursement-platform-backend/internal/data"
"github.com/stellar/stellar-disbursement-platform-backend/internal/db"
di "github.com/stellar/stellar-disbursement-platform-backend/internal/dependencyinjection"
"github.com/stellar/stellar-disbursement-platform-backend/internal/message"
"github.com/stellar/stellar-disbursement-platform-backend/internal/scheduler"
Expand Down
2 changes: 1 addition & 1 deletion cmd/serve_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ import (
"github.com/stellar/go/clients/horizonclient"
"github.com/stellar/go/network"
cmdUtils "github.com/stellar/stellar-disbursement-platform-backend/cmd/utils"
"github.com/stellar/stellar-disbursement-platform-backend/db/dbtest"
"github.com/stellar/stellar-disbursement-platform-backend/internal/anchorplatform"
"github.com/stellar/stellar-disbursement-platform-backend/internal/crashtracker"
"github.com/stellar/stellar-disbursement-platform-backend/internal/db/dbtest"
di "github.com/stellar/stellar-disbursement-platform-backend/internal/dependencyinjection"
"github.com/stellar/stellar-disbursement-platform-backend/internal/message"
"github.com/stellar/stellar-disbursement-platform-backend/internal/monitor"
Expand Down
16 changes: 15 additions & 1 deletion internal/db/db.go → db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"time"

"github.com/jmoiron/sqlx"
_ "github.com/lib/pq"
"github.com/stellar/go/support/log"
"github.com/stellar/stellar-disbursement-platform-backend/internal/monitor"
)
Expand All @@ -16,6 +17,13 @@ const (
MaxOpenDBConns = 30
)

// DBConnectionPoolFromSqlDB returns a new DBConnectionPool wrapper for a PRE-EXISTING *sql.DB. The driverName of the
// original database is required for named query support. ATTENTION: this will not start a new connection pool, just
// create a wrap aroung the pre-existing connection pool.
func DBConnectionPoolFromSqlDB(sqlDB *sql.DB, driverName string) DBConnectionPool {
return &DBConnectionPoolImplementation{DB: sqlx.NewDb(sqlDB, driverName)}
}

// DBConnectionPool is an interface that wraps the sqlx.DB structs methods and includes the RunInTransaction helper.
type DBConnectionPool interface {
SQLExecuter
Expand All @@ -24,11 +32,13 @@ type DBConnectionPool interface {
Ping() error
SqlDB() *sql.DB
SqlxDB() *sqlx.DB
DSN() string
}

// DBConnectionPoolImplementation is a wrapper around sqlx.DB that implements DBConnectionPool.
type DBConnectionPoolImplementation struct {
*sqlx.DB
dataSourceName string
}

func (db *DBConnectionPoolImplementation) BeginTxx(ctx context.Context, opts *sql.TxOptions) (DBTransaction, error) {
Expand All @@ -43,6 +53,10 @@ func (db *DBConnectionPoolImplementation) SqlxDB() *sqlx.DB {
return db.DB
}

func (db *DBConnectionPoolImplementation) DSN() string {
return db.dataSourceName
}

// RunInTransactionWithResult runs the given atomic function in an atomic database transaction and returns a result and
// an error. Boilerplate code for database transactions.
func RunInTransactionWithResult[T any](ctx context.Context, dbConnectionPool DBConnectionPool, opts *sql.TxOptions, atomicFunction func(dbTx DBTransaction) (T, error)) (result T, err error) {
Expand Down Expand Up @@ -141,7 +155,7 @@ func OpenDBConnectionPool(dataSourceName string) (DBConnectionPool, error) {
return nil, fmt.Errorf("error pinging app DB connection pool: %w", err)
}

return &DBConnectionPoolImplementation{DB: sqlxDB}, nil
return &DBConnectionPoolImplementation{DB: sqlxDB, dataSourceName: dataSourceName}, nil
}

// OpenDBConnectionPoolWithMetrics opens a new database connection pool with the monitor service. It returns an error if it can't connect to the database.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,7 @@ func (dbc *DBConnectionPoolWithMetrics) SqlDB() *sql.DB {
func (dbc *DBConnectionPoolWithMetrics) SqlxDB() *sqlx.DB {
return dbc.dbConnectionPool.SqlxDB()
}

func (dbc *DBConnectionPoolWithMetrics) DSN() string {
return dbc.dbConnectionPool.DSN()
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"testing"

"github.com/jmoiron/sqlx"
"github.com/stellar/stellar-disbursement-platform-backend/internal/db/dbtest"
"github.com/stellar/stellar-disbursement-platform-backend/db/dbtest"
"github.com/stellar/stellar-disbursement-platform-backend/internal/monitor"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
Expand Down
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"context"
"testing"

"github.com/stellar/stellar-disbursement-platform-backend/internal/db/dbtest"
"github.com/stellar/stellar-disbursement-platform-backend/db/dbtest"
"github.com/stellar/stellar-disbursement-platform-backend/internal/monitor"
"github.com/stretchr/testify/require"
)
Expand Down
96 changes: 96 additions & 0 deletions db/dbtest/dbtest.go
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
}
33 changes: 33 additions & 0 deletions db/dbtest/dbtest_test.go
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 stellar-multitenant/internal/db/migrate.go → db/migrate.go
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)
}
Loading

0 comments on commit d0d5ca7

Please sign in to comment.