diff --git a/cmd/db.go b/cmd/db.go index bdbeda2a9..455bf7c8a 100644 --- a/cmd/db.go +++ b/cmd/db.go @@ -11,11 +11,12 @@ import ( "github.com/spf13/cobra" "github.com/stellar/go/support/config" "github.com/stellar/go/support/log" + "github.com/stellar/stellar-disbursement-platform-backend/cmd/utils" "github.com/stellar/stellar-disbursement-platform-backend/db" authmigrations "github.com/stellar/stellar-disbursement-platform-backend/db/migrations/auth-migrations" sdpmigrations "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" + sdpUtils "github.com/stellar/stellar-disbursement-platform-backend/internal/utils" tenantcli "github.com/stellar/stellar-disbursement-platform-backend/stellar-multitenant/pkg/cli" "github.com/stellar/stellar-disbursement-platform-backend/stellar-multitenant/pkg/tenant" ) @@ -24,16 +25,15 @@ type databaseCommandConfigOptions struct { All bool TenantID string } - type DatabaseCommand struct{} func (c *DatabaseCommand) Command() *cobra.Command { opts := databaseCommandConfigOptions{} - + // TODO: tie these configs only where needed configOptions := config.ConfigOptions{ { Name: "all", - Usage: "Apply the migrations to all tenants.", + Usage: "Apply the migrations to all tenants. Either --tenant-id or --all must be set, but the --all option will be ignored if --tenant-id is set.", OptType: types.Bool, FlagDefault: false, ConfigKey: &opts.All, @@ -41,7 +41,7 @@ func (c *DatabaseCommand) Command() *cobra.Command { }, { Name: "tenant-id", - Usage: "The tenant ID where the migrations will be applied.", + Usage: "The tenant ID where the migrations will be applied. Either --tenant-id or --all must be set, but the --all option will be ignored if --tenant-id is set.", OptType: types.String, ConfigKey: &opts.TenantID, Required: false, @@ -52,43 +52,39 @@ func (c *DatabaseCommand) Command() *cobra.Command { Use: "db", Short: "Database related commands", PersistentPreRun: func(cmd *cobra.Command, args []string) { - if cmd.Parent().PersistentPreRun != nil { - cmd.Parent().PersistentPreRun(cmd.Parent(), args) - } + utils.PropagatePersistentPreRun(cmd, args) configOptions.Require() if err := configOptions.SetValues(); err != nil { - log.Fatalf("Error setting values of config options: %s", err.Error()) - } - }, - Run: func(cmd *cobra.Command, _ []string) { - err := cmd.Help() - if err != nil { - log.Fatalf("Error calling help command: %s", err.Error()) + log.Ctx(cmd.Context()).Fatalf("Error setting values of config options: %s", err.Error()) } }, + RunE: utils.CallHelpCommand, } - // migrate CMD - sdpMigrateCmd := c.migrateCmd() - authMigrateCmd := c.migrateCmd() - cmd.AddCommand(sdpMigrateCmd) + // ADD COMMANDs: + cmd.AddCommand(c.setupForNetworkCmd(cmd.Context(), &opts)) // 'setup-for-network' + cmd.AddCommand(c.sdpPerTenantMigrationsCmd(cmd.Context(), &opts)) // 'sdp migrate up|down' + cmd.AddCommand(c.authPerTenantMigrationsCmd(cmd.Context(), &opts)) // 'auth migrate up|down' + cmd.AddCommand(c.adminMigrationsCmd(cmd.Context(), &opts)) // 'admin migrate up|down' - // migrate Up CMD - sdpMigrateCmd.AddCommand(c.migrateUpCmd(&opts)) - authMigrateCmd.AddCommand(c.migrateUpCmd(&opts)) + if err := configOptions.Init(cmd); err != nil { + log.Ctx(cmd.Context()).Fatalf("initializing config options: %v", err) + } - // migrate Down CMD - sdpMigrateCmd.AddCommand(c.migrateDownCmd(&opts)) - authMigrateCmd.AddCommand(c.migrateDownCmd(&opts)) + return cmd +} - setupForNetwork := &cobra.Command{ +// setupForNetworkCmd returns a cobra.Command responsible for setting up the assets and wallets registered in the +// database based on the network passphrase. +func (c *DatabaseCommand) setupForNetworkCmd(ctx context.Context, opts *databaseCommandConfigOptions) *cobra.Command { + return &cobra.Command{ Use: "setup-for-network", Short: "Set up the assets and wallets registered in the database based on the network passphrase.", Long: "Set up the assets and wallets registered in the database based on the network passphrase. It inserts or updates the entries of these tables according with the configured Network Passphrase.", Run: func(cmd *cobra.Command, args []string) { ctx := cmd.Context() - if err := c.validateFlags(&opts); err != nil { + if err := c.validateFlags(opts); err != nil { log.Ctx(ctx).Fatal(err.Error()) } @@ -101,19 +97,19 @@ func (c *DatabaseCommand) Command() *cobra.Command { if dsn, ok := tenantsDSNMap[opts.TenantID]; ok { tenantsDSNMap = map[string]string{opts.TenantID: dsn} } else { - log.Fatalf("tenant ID %s does not exist", opts.TenantID) + log.Ctx(ctx).Fatalf("tenant ID %s does not exist", opts.TenantID) } } for tenantID, dsn := range tenantsDSNMap { - log.Infof("running for tenant ID %s", tenantID) + log.Ctx(ctx).Infof("running for tenant ID %s", tenantID) dbConnectionPool, err := db.OpenDBConnectionPool(dsn) if err != nil { log.Ctx(ctx).Fatalf("error connection to the database: %s", err.Error()) } defer dbConnectionPool.Close() - networkType, err := utils.GetNetworkTypeFromNetworkPassphrase(globalOptions.networkPassphrase) + networkType, err := sdpUtils.GetNetworkTypeFromNetworkPassphrase(globalOptions.networkPassphrase) if err != nil { log.Ctx(ctx).Fatalf("error getting network type: %s", err.Error()) } @@ -128,142 +124,118 @@ func (c *DatabaseCommand) Command() *cobra.Command { } }, } - cmd.AddCommand(setupForNetwork) +} - stellarAuthMigrateCmd := &cobra.Command{ - Use: "auth", - Short: "Stellar Auth schema migration helpers", - PersistentPreRun: func(cmd *cobra.Command, args []string) { - if cmd.Parent().PersistentPreRun != nil { - cmd.Parent().PersistentPreRun(cmd.Parent(), args) - } - }, - Run: func(cmd *cobra.Command, args []string) { - if err := cmd.Help(); err != nil { - log.Fatalf("Error calling help command: %s", err.Error()) - } - }, +// sdpPerTenantMigrationsCmd returns a cobra.Command responsible for running the migrations of the `sdp-migrations` +// folder on the desired tenant(s). +func (c *DatabaseCommand) sdpPerTenantMigrationsCmd(ctx context.Context, opts *databaseCommandConfigOptions) *cobra.Command { + sdpCmd := &cobra.Command{ + Use: "sdp", + Short: "Stellar Disbursement Platform's per-tenant schema migration helpers. Will execute the migrations of the `sdp-migrations` folder on the desired tenant, according with the --all or --tenant-id configs. The migrations are tracked in the table `sdp_migrations`.", + PersistentPreRun: utils.PropagatePersistentPreRun, + RunE: utils.CallHelpCommand, } - stellarAuthMigrateCmd.AddCommand(authMigrateCmd) + sdpCmd.AddCommand(c.migrateCmd(ctx, opts)) + return sdpCmd +} - tenantMigrateCmd := &cobra.Command{ - Use: "tenant", - Short: "Stellar Multi-Tenant migration helpers", - PersistentPreRun: func(cmd *cobra.Command, args []string) { - if cmd.Parent().PersistentPreRun != nil { - cmd.Parent().PersistentPreRun(cmd.Parent(), args) - } - }, - Run: func(cmd *cobra.Command, args []string) { - if err := cmd.Help(); err != nil { - log.Fatalf("Error calling help command: %s", err.Error()) - } - }, +// authPerTenantMigrationsCmd returns a cobra.Command responsible for running the migrations of the `auth-migrations` +// folder on the desired tenant(s). +func (c *DatabaseCommand) authPerTenantMigrationsCmd(ctx context.Context, opts *databaseCommandConfigOptions) *cobra.Command { + authCmd := &cobra.Command{ + Use: "auth", + Short: "Authentication's per-tenant schema migration helpers. Will execute the migrations of the `auth-migrations` folder on the desired tenant, according with the --all or --tenant-id configs. The migrations are tracked in the table `auth_migrations`.", + PersistentPreRun: utils.PropagatePersistentPreRun, + RunE: utils.CallHelpCommand, } - tenantMigrateCmd.AddCommand(tenantcli.MigrateCmd(dbConfigOptionFlagName)) - - // Add `auth` as a sub-command to `db`. Usage: db auth migrate up - cmd.AddCommand(stellarAuthMigrateCmd) - - // Add `tenant` as a sub-command to `db`. Usage: db tenant migrate up - cmd.AddCommand(tenantMigrateCmd) + authCmd.AddCommand(c.migrateCmd(ctx, opts)) + return authCmd +} - if err := configOptions.Init(cmd); err != nil { - log.Fatalf("initializing config options: %v", err) +// adminMigrationsCmd returns a cobra.Command responsible for running the migrations of the `admin-migrations` +// folder, that are used to configure the multi-tenant module that manages the tenants. +func (c *DatabaseCommand) adminMigrationsCmd(ctx context.Context, opts *databaseCommandConfigOptions) *cobra.Command { + adminCmd := &cobra.Command{ + Use: "admin", + Short: "Admin migrations used to configure the multi-tenant module that manages the tenants. Will execute the migrations of the `admin-migrations` and the migrations are tracked in the table `admin_migrations`.", + PersistentPreRun: utils.PropagatePersistentPreRun, + RunE: utils.CallHelpCommand, } - - return cmd + adminCmd.AddCommand(tenantcli.MigrateCmd(dbConfigOptionFlagName)) + return adminCmd } -func (c *DatabaseCommand) migrateCmd() *cobra.Command { - return &cobra.Command{ - Use: "migrate", - Short: "Schema migration helpers", - PersistentPreRun: func(cmd *cobra.Command, args []string) { - if cmd.Parent().PersistentPreRun != nil { - cmd.Parent().PersistentPreRun(cmd.Parent(), args) - } - }, - Run: func(cmd *cobra.Command, _ []string) { - err := cmd.Help() - if err != nil { - log.Fatalf("Error calling help command: %s", err.Error()) - } - }, +// migrateCmd returns a cobra.Command responsible for running the database migrations. +func (c *DatabaseCommand) migrateCmd(ctx context.Context, opts *databaseCommandConfigOptions) *cobra.Command { + migrateCmd := &cobra.Command{ + Use: "migrate", + Short: "Schema migration helpers", + PersistentPreRun: utils.PropagatePersistentPreRun, + RunE: utils.CallHelpCommand, } -} -func (c *DatabaseCommand) migrateUpCmd(opts *databaseCommandConfigOptions) *cobra.Command { - return &cobra.Command{ - Use: "up", - Short: "Migrates database up [count]", - Args: cobra.MaximumNArgs(1), - PersistentPreRun: func(cmd *cobra.Command, args []string) { - if cmd.Parent().PersistentPreRun != nil { - cmd.Parent().PersistentPreRun(cmd.Parent(), args) - } - }, + migrateUpCmd := cobra.Command{ + Use: "up", + Short: "Migrates database up [count] migrations", + Args: cobra.MaximumNArgs(1), + PersistentPreRun: utils.PropagatePersistentPreRun, Run: func(cmd *cobra.Command, args []string) { var count int if len(args) > 0 { var err error count, err = strconv.Atoi(args[0]) if err != nil { - log.Fatalf("Invalid [count] argument: %s", args[0]) + log.Ctx(ctx).Fatalf("Invalid [count] argument: %s", args[0]) } } migrationFiles := sdpmigrations.FS - migrationTableName := db.StellarSDPMigrationsTableName + migrationTableName := db.StellarPerTenantSDPMigrationsTableName - migrateCmd := cmd.Parent() - if migrateCmd.Parent().Name() == "auth" { + if cmd.Parent().Parent().Name() == "auth" { migrationFiles = authmigrations.FS - migrationTableName = db.StellarAuthMigrationsTableName + migrationTableName = db.StellarPerTenantAuthMigrationsTableName } if err := c.executeMigrate(cmd.Context(), opts, migrate.Up, count, migrationFiles, migrationTableName); err != nil { - log.Fatalf("Error executing migrate up: %v", err) + log.Ctx(ctx).Fatalf("Error executing migrate up: %v", err) } }, } -} -func (c *DatabaseCommand) migrateDownCmd(opts *databaseCommandConfigOptions) *cobra.Command { - return &cobra.Command{ - Use: "down [count]", - Short: "Migrates database down [count] migrations", - Args: cobra.ExactArgs(1), - PersistentPreRun: func(cmd *cobra.Command, args []string) { - if cmd.Parent().PersistentPreRun != nil { - cmd.Parent().PersistentPreRun(cmd.Parent(), args) - } - }, + migrateDownCmd := &cobra.Command{ + Use: "down [count]", + Short: "Migrates database down [count] migrations", + Args: cobra.ExactArgs(1), + PersistentPreRun: utils.PropagatePersistentPreRun, Run: func(cmd *cobra.Command, args []string) { count, err := strconv.Atoi(args[0]) if err != nil { - log.Fatalf("Invalid [count] argument: %s", args[0]) + log.Ctx(ctx).Fatalf("Invalid [count] argument: %s", args[0]) } migrationFiles := sdpmigrations.FS - migrationTableName := db.StellarSDPMigrationsTableName + migrationTableName := db.StellarPerTenantSDPMigrationsTableName - migrateCmd := cmd.Parent() - if migrateCmd.Parent().Name() == "auth" { + if cmd.Parent().Parent().Name() == "auth" { migrationFiles = authmigrations.FS - migrationTableName = db.StellarAuthMigrationsTableName + migrationTableName = db.StellarPerTenantAuthMigrationsTableName } if err := c.executeMigrate(cmd.Context(), opts, migrate.Down, count, migrationFiles, migrationTableName); err != nil { - log.Fatalf("Error executing migrate down: %v", err) + log.Ctx(ctx).Fatalf("Error executing migrate down: %v", err) } }, } + + migrateCmd.AddCommand(&migrateUpCmd) + migrateCmd.AddCommand(migrateDownCmd) + return migrateCmd } func (c *DatabaseCommand) executeMigrate(ctx context.Context, opts *databaseCommandConfigOptions, dir migrate.MigrationDirection, count int, migrationFiles embed.FS, tableName db.MigrationTableName) error { if err := c.validateFlags(opts); err != nil { - log.Fatal(err.Error()) + log.Ctx(ctx).Fatal(err.Error()) } tenantsDSNMap, err := c.getTenantsDSN(ctx, globalOptions.databaseURL) @@ -275,15 +247,15 @@ func (c *DatabaseCommand) executeMigrate(ctx context.Context, opts *databaseComm if dsn, ok := tenantsDSNMap[opts.TenantID]; ok { tenantsDSNMap = map[string]string{opts.TenantID: dsn} } else { - log.Fatalf("tenant ID %s does not exist", opts.TenantID) + log.Ctx(ctx).Fatalf("tenant ID %s does not exist", opts.TenantID) } } for tenantID, dsn := range tenantsDSNMap { - log.Infof("applying migrations on tenant ID %s", tenantID) + log.Ctx(ctx).Infof("Applying migrations on tenant ID %s", tenantID) err = c.applyMigrations(dsn, dir, count, migrationFiles, tableName) if err != nil { - log.Fatalf("Error migrating database Up: %s", err.Error()) + log.Ctx(ctx).Fatalf("Error migrating database Up: %s", err.Error()) } } @@ -299,11 +271,19 @@ func (c *DatabaseCommand) applyMigrations(dbURL string, dir migrate.MigrationDir if numMigrationsRun == 0 { log.Info("No migrations applied.") } else { - log.Infof("Successfully applied %d migrations.", numMigrationsRun) + log.Infof("Successfully applied %d migrations %s.", numMigrationsRun, migrationDirectionStr(dir)) } return nil } +// migrationDirectionStr returns a string representation of the migration direction (up or down). +func migrationDirectionStr(dir migrate.MigrationDirection) string { + if dir == migrate.Up { + return "up" + } + return "down" +} + func (c *DatabaseCommand) validateFlags(opts *databaseCommandConfigOptions) error { if !opts.All && opts.TenantID == "" { return fmt.Errorf( diff --git a/cmd/db_test.go b/cmd/db_test.go index fde292825..08bd8cacc 100644 --- a/cmd/db_test.go +++ b/cmd/db_test.go @@ -22,7 +22,7 @@ import ( func getSDPMigrationsApplied(t *testing.T, ctx context.Context, db db.DBConnectionPool) []string { t.Helper() - rows, err := db.QueryContext(ctx, "SELECT id FROM gorp_migrations") + rows, err := db.QueryContext(ctx, "SELECT id FROM sdp_migrations") require.NoError(t, err) defer rows.Close() @@ -74,12 +74,13 @@ func Test_DatabaseCommand_db_help(t *testing.T) { "Database related commands", "stellar-disbursement-platform db [flags]", "stellar-disbursement-platform db [command]", - "auth Stellar Auth schema migration helpers", - "migrate Schema migration helpers", + "admin Admin migrations used to configure the multi-tenant module that manages the tenants.", + "auth Authentication's per-tenant schema migration helpers. Will execute the migrations of the `auth-migrations` folder on the desired tenant, according with the --all or --tenant-id configs. The migrations are tracked in the table `auth_migrations`.", + "sdp Stellar Disbursement Platform's per-tenant schema migration helpers.", "setup-for-network Set up the assets and wallets registered in the database based on the network passphrase.", - "--all Apply the migrations to all tenants. (ALL)", + "--all Apply the migrations to all tenants. Either --tenant-id or --all must be set, but the --all option will be ignored if --tenant-id is set.", "-h, --help help for db", - "--tenant-id string The tenant ID where the migrations will be applied. (TENANT_ID)", + "--tenant-id string The tenant ID where the migrations will be applied. Either --tenant-id or --all must be set, but the --all option will be ignored if --tenant-id is set. (TENANT_ID)", `--base-url string The SDP backend server's base URL. (BASE_URL) (default "http://localhost:8000")`, `--database-url string Postgres DB URL (DATABASE_URL) (default "postgres://localhost:5432/sdp?sslmode=disable")`, `--environment string The environment where the application is running. Example: "development", "staging", "production". (ENVIRONMENT) (default "development")`, @@ -104,7 +105,7 @@ func Test_DatabaseCommand_db_help(t *testing.T) { } } -func Test_DatabaseCommand_db_migrate(t *testing.T) { +func Test_DatabaseCommand_db_sdp_migrate(t *testing.T) { dbt := dbtest.OpenWithTenantMigrationsOnly(t) defer dbt.Close() @@ -118,26 +119,26 @@ func Test_DatabaseCommand_db_migrate(t *testing.T) { t.Run("migrate usage", func(t *testing.T) { rootCmd := SetupCLI("x.y.z", "1234567890abcdef") - rootCmd.SetArgs([]string{"db", "migrate"}) + rootCmd.SetArgs([]string{"db", "sdp", "migrate"}) rootCmd.SetOut(buf) err = rootCmd.Execute() require.NoError(t, err) expectedContains := []string{ "Schema migration helpers", - "stellar-disbursement-platform db migrate [flags]", - "stellar-disbursement-platform db migrate [command]", + "stellar-disbursement-platform db sdp migrate [flags]", + "stellar-disbursement-platform db sdp migrate [command]", "down Migrates database down [count] migrations", - "up Migrates database up [count]", + "up Migrates database up [count] migrations", "-h, --help help for migrate", - `--all Apply the migrations to all tenants. (ALL)`, + `--all Apply the migrations to all tenants. Either --tenant-id or --all must be set, but the --all option will be ignored if --tenant-id is set.`, `--base-url string The SDP backend server's base URL. (BASE_URL) (default "http://localhost:8000")`, `--database-url string Postgres DB URL (DATABASE_URL) (default "postgres://localhost:5432/sdp?sslmode=disable")`, `--environment string The environment where the application is running. Example: "development", "staging", "production". (ENVIRONMENT) (default "development")`, `--log-level string The log level used in this project. Options: "TRACE", "DEBUG", "INFO", "WARN", "ERROR", "FATAL", or "PANIC". (LOG_LEVEL) (default "TRACE")`, `--network-passphrase string The Stellar network passphrase (NETWORK_PASSPHRASE) (default "Test SDF Network ; September 2015")`, `--sentry-dsn string The DSN (client key) of the Sentry project. If not provided, Sentry will not be used. (SENTRY_DSN)`, - `--tenant-id string The tenant ID where the migrations will be applied. (TENANT_ID)`, + `--tenant-id string The tenant ID where the migrations will be applied. Either --tenant-id or --all must be set, but the --all option will be ignored if --tenant-id is set. (TENANT_ID)`, } output := buf.String() @@ -164,7 +165,7 @@ func Test_DatabaseCommand_db_migrate(t *testing.T) { buf.Reset() log.DefaultLogger.SetOutput(buf) rootCmd := SetupCLI("x.y.z", "1234567890abcdef") - rootCmd.SetArgs([]string{"db", "migrate", "up", "1", "--database-url", dbt.DSN, "--log-level", "TRACE", "--all"}) + rootCmd.SetArgs([]string{"db", "sdp", "migrate", "up", "1", "--database-url", dbt.DSN, "--log-level", "TRACE", "--all"}) err = rootCmd.Execute() require.NoError(t, err) @@ -185,33 +186,33 @@ func Test_DatabaseCommand_db_migrate(t *testing.T) { // Checking if the migrations were applied on the Tenant 1 ids := getSDPMigrationsApplied(t, ctx, tenant1SchemaConnectionPool) assert.Equal(t, []string{"2023-01-20.0-initial.sql"}, ids) - assert.Contains(t, buf.String(), fmt.Sprintf("applying migrations on tenant ID %s", tnt1.ID)) - assert.Contains(t, buf.String(), "Successfully applied 1 migrations.") - tenant.TenantSchemaMatchTablesFixture(t, ctx, dbConnectionPool, "sdp_myorg1", []string{"gorp_migrations"}) + assert.Contains(t, buf.String(), fmt.Sprintf("Applying migrations on tenant ID %s", tnt1.ID)) + assert.Contains(t, buf.String(), "Successfully applied 1 migrations up.") + tenant.TenantSchemaMatchTablesFixture(t, ctx, dbConnectionPool, "sdp_myorg1", []string{"sdp_migrations"}) // Checking if the migrations were applied on the Tenant 2 ids = getSDPMigrationsApplied(t, ctx, tenant2SchemaConnectionPool) assert.Equal(t, []string{"2023-01-20.0-initial.sql"}, ids) - assert.Contains(t, buf.String(), fmt.Sprintf("applying migrations on tenant ID %s", tnt1.ID)) - assert.Contains(t, buf.String(), "Successfully applied 1 migrations.") - tenant.TenantSchemaMatchTablesFixture(t, ctx, dbConnectionPool, "sdp_myorg2", []string{"gorp_migrations"}) + assert.Contains(t, buf.String(), fmt.Sprintf("Applying migrations on tenant ID %s", tnt1.ID)) + assert.Contains(t, buf.String(), "Successfully applied 1 migrations up.") + tenant.TenantSchemaMatchTablesFixture(t, ctx, dbConnectionPool, "sdp_myorg2", []string{"sdp_migrations"}) buf.Reset() - rootCmd.SetArgs([]string{"db", "migrate", "down", "1", "--database-url", dbt.DSN, "--log-level", "TRACE", "--all"}) + rootCmd.SetArgs([]string{"db", "sdp", "migrate", "down", "1", "--database-url", dbt.DSN, "--log-level", "TRACE", "--all"}) err = rootCmd.Execute() require.NoError(t, err) // Checking if the migrations were applied on the Tenant 1 ids = getSDPMigrationsApplied(t, context.Background(), tenant1SchemaConnectionPool) assert.Equal(t, []string{}, ids) - assert.Contains(t, buf.String(), "Successfully applied 1 migrations.") - tenant.TenantSchemaMatchTablesFixture(t, ctx, dbConnectionPool, "sdp_myorg1", []string{"gorp_migrations"}) + assert.Contains(t, buf.String(), "Successfully applied 1 migrations down.") + tenant.TenantSchemaMatchTablesFixture(t, ctx, dbConnectionPool, "sdp_myorg1", []string{"sdp_migrations"}) // Checking if the migrations were applied on the Tenant 2 ids = getSDPMigrationsApplied(t, context.Background(), tenant2SchemaConnectionPool) assert.Equal(t, []string{}, ids) - assert.Contains(t, buf.String(), "Successfully applied 1 migrations.") - tenant.TenantSchemaMatchTablesFixture(t, ctx, dbConnectionPool, "sdp_myorg2", []string{"gorp_migrations"}) + assert.Contains(t, buf.String(), "Successfully applied 1 migrations down.") + tenant.TenantSchemaMatchTablesFixture(t, ctx, dbConnectionPool, "sdp_myorg2", []string{"sdp_migrations"}) }) t.Run("migrate up and down --tenant-id", func(t *testing.T) { @@ -232,7 +233,7 @@ func Test_DatabaseCommand_db_migrate(t *testing.T) { buf.Reset() log.DefaultLogger.SetOutput(buf) rootCmd := SetupCLI("x.y.z", "1234567890abcdef") - rootCmd.SetArgs([]string{"db", "migrate", "up", "1", "--database-url", dbt.DSN, "--log-level", "TRACE", "--tenant-id", tnt1.ID}) + rootCmd.SetArgs([]string{"db", "sdp", "migrate", "up", "1", "--database-url", dbt.DSN, "--log-level", "TRACE", "--tenant-id", tnt1.ID}) err = rootCmd.Execute() require.NoError(t, err) @@ -253,29 +254,29 @@ func Test_DatabaseCommand_db_migrate(t *testing.T) { // Checking if the migrations were applied on the Tenant 1 schema ids := getSDPMigrationsApplied(t, ctx, tenant1SchemaConnectionPool) assert.Equal(t, []string{"2023-01-20.0-initial.sql"}, ids) - assert.Contains(t, buf.String(), fmt.Sprintf("applying migrations on tenant ID %s", tnt1.ID)) - assert.Contains(t, buf.String(), "Successfully applied 1 migrations.") - tenant.TenantSchemaMatchTablesFixture(t, ctx, dbConnectionPool, "sdp_myorg1", []string{"gorp_migrations"}) + assert.Contains(t, buf.String(), fmt.Sprintf("Applying migrations on tenant ID %s", tnt1.ID)) + assert.Contains(t, buf.String(), "Successfully applied 1 migrations up.") + tenant.TenantSchemaMatchTablesFixture(t, ctx, dbConnectionPool, "sdp_myorg1", []string{"sdp_migrations"}) // Checking if the migrations were not applied on the Tenant 2 schema tenant.TenantSchemaMatchTablesFixture(t, ctx, dbConnectionPool, "sdp_myorg2", []string{}) - assert.NotContains(t, buf.String(), fmt.Sprintf("applying migrations on tenant ID %s", tnt2.ID)) + assert.NotContains(t, buf.String(), fmt.Sprintf("Applying migrations on tenant ID %s", tnt2.ID)) buf.Reset() - rootCmd.SetArgs([]string{"db", "migrate", "down", "1", "--database-url", dbt.DSN, "--log-level", "TRACE", "--tenant-id", tnt1.ID}) + rootCmd.SetArgs([]string{"db", "sdp", "migrate", "down", "1", "--database-url", dbt.DSN, "--log-level", "TRACE", "--tenant-id", tnt1.ID}) err = rootCmd.Execute() require.NoError(t, err) // Checking if the migrations were applied on the Tenant 1 schema ids = getSDPMigrationsApplied(t, ctx, tenant1SchemaConnectionPool) assert.Equal(t, []string{}, ids) - assert.Contains(t, buf.String(), fmt.Sprintf("applying migrations on tenant ID %s", tnt1.ID)) - assert.Contains(t, buf.String(), "Successfully applied 1 migrations.") - tenant.TenantSchemaMatchTablesFixture(t, ctx, dbConnectionPool, "sdp_myorg1", []string{"gorp_migrations"}) + assert.Contains(t, buf.String(), fmt.Sprintf("Applying migrations on tenant ID %s", tnt1.ID)) + assert.Contains(t, buf.String(), "Successfully applied 1 migrations down.") + tenant.TenantSchemaMatchTablesFixture(t, ctx, dbConnectionPool, "sdp_myorg1", []string{"sdp_migrations"}) // Checking if the migrations were not applied on the Tenant 2 schema tenant.TenantSchemaMatchTablesFixture(t, ctx, dbConnectionPool, "sdp_myorg2", []string{}) - assert.NotContains(t, buf.String(), fmt.Sprintf("applying migrations on tenant ID %s", tnt2.ID)) + assert.NotContains(t, buf.String(), fmt.Sprintf("Applying migrations on tenant ID %s", tnt2.ID)) }) t.Run("migrate up and down auth migrations --all", func(t *testing.T) { @@ -317,15 +318,15 @@ func Test_DatabaseCommand_db_migrate(t *testing.T) { // Checking if the migrations were applied on the Tenant 1 ids := getAuthMigrationsApplied(t, ctx, tenant1SchemaConnectionPool) assert.Equal(t, []string{"2023-02-09.0.add-users-table.sql"}, ids) - assert.Contains(t, buf.String(), fmt.Sprintf("applying migrations on tenant ID %s", tnt1.ID)) - assert.Contains(t, buf.String(), "Successfully applied 1 migrations.") + assert.Contains(t, buf.String(), fmt.Sprintf("Applying migrations on tenant ID %s", tnt1.ID)) + assert.Contains(t, buf.String(), "Successfully applied 1 migrations up.") tenant.TenantSchemaMatchTablesFixture(t, ctx, dbConnectionPool, "sdp_myorg1", []string{"auth_migrations", "auth_users"}) // Checking if the migrations were applied on the Tenant 2 ids = getAuthMigrationsApplied(t, ctx, tenant2SchemaConnectionPool) assert.Equal(t, []string{"2023-02-09.0.add-users-table.sql"}, ids) - assert.Contains(t, buf.String(), fmt.Sprintf("applying migrations on tenant ID %s", tnt1.ID)) - assert.Contains(t, buf.String(), "Successfully applied 1 migrations.") + assert.Contains(t, buf.String(), fmt.Sprintf("Applying migrations on tenant ID %s", tnt1.ID)) + assert.Contains(t, buf.String(), "Successfully applied 1 migrations up.") tenant.TenantSchemaMatchTablesFixture(t, ctx, dbConnectionPool, "sdp_myorg2", []string{"auth_migrations", "auth_users"}) buf.Reset() @@ -336,13 +337,13 @@ func Test_DatabaseCommand_db_migrate(t *testing.T) { // Checking if the migrations were applied on the Tenant 1 ids = getAuthMigrationsApplied(t, context.Background(), tenant1SchemaConnectionPool) assert.Equal(t, []string{}, ids) - assert.Contains(t, buf.String(), "Successfully applied 1 migrations.") + assert.Contains(t, buf.String(), "Successfully applied 1 migrations down.") tenant.TenantSchemaMatchTablesFixture(t, ctx, dbConnectionPool, "sdp_myorg1", []string{"auth_migrations"}) // Checking if the migrations were applied on the Tenant 2 ids = getAuthMigrationsApplied(t, context.Background(), tenant2SchemaConnectionPool) assert.Equal(t, []string{}, ids) - assert.Contains(t, buf.String(), "Successfully applied 1 migrations.") + assert.Contains(t, buf.String(), "Successfully applied 1 migrations down.") tenant.TenantSchemaMatchTablesFixture(t, ctx, dbConnectionPool, "sdp_myorg2", []string{"auth_migrations"}) }) @@ -385,13 +386,13 @@ func Test_DatabaseCommand_db_migrate(t *testing.T) { // Checking if the migrations were applied on the Tenant 1 schema ids := getAuthMigrationsApplied(t, ctx, tenant1SchemaConnectionPool) assert.Equal(t, []string{"2023-02-09.0.add-users-table.sql"}, ids) - assert.Contains(t, buf.String(), fmt.Sprintf("applying migrations on tenant ID %s", tnt1.ID)) - assert.Contains(t, buf.String(), "Successfully applied 1 migrations.") + assert.Contains(t, buf.String(), fmt.Sprintf("Applying migrations on tenant ID %s", tnt1.ID)) + assert.Contains(t, buf.String(), "Successfully applied 1 migrations up.") tenant.TenantSchemaMatchTablesFixture(t, ctx, dbConnectionPool, "sdp_myorg1", []string{"auth_migrations", "auth_users"}) // Checking if the migrations were not applied on the Tenant 2 schema tenant.TenantSchemaMatchTablesFixture(t, ctx, dbConnectionPool, "sdp_myorg2", []string{}) - assert.NotContains(t, buf.String(), fmt.Sprintf("applying migrations on tenant ID %s", tnt2.ID)) + assert.NotContains(t, buf.String(), fmt.Sprintf("Applying migrations on tenant ID %s", tnt2.ID)) buf.Reset() rootCmd.SetArgs([]string{"db", "auth", "migrate", "down", "1", "--database-url", dbt.DSN, "--log-level", "TRACE", "--tenant-id", tnt1.ID}) @@ -401,13 +402,13 @@ func Test_DatabaseCommand_db_migrate(t *testing.T) { // Checking if the migrations were applied on the Tenant 1 schema ids = getAuthMigrationsApplied(t, ctx, tenant1SchemaConnectionPool) assert.Equal(t, []string{}, ids) - assert.Contains(t, buf.String(), fmt.Sprintf("applying migrations on tenant ID %s", tnt1.ID)) - assert.Contains(t, buf.String(), "Successfully applied 1 migrations.") + assert.Contains(t, buf.String(), fmt.Sprintf("Applying migrations on tenant ID %s", tnt1.ID)) + assert.Contains(t, buf.String(), "Successfully applied 1 migrations down.") tenant.TenantSchemaMatchTablesFixture(t, ctx, dbConnectionPool, "sdp_myorg1", []string{"auth_migrations"}) // Checking if the migrations were not applied on the Tenant 2 schema tenant.TenantSchemaMatchTablesFixture(t, ctx, dbConnectionPool, "sdp_myorg2", []string{}) - assert.NotContains(t, buf.String(), fmt.Sprintf("applying migrations on tenant ID %s", tnt2.ID)) + assert.NotContains(t, buf.String(), fmt.Sprintf("Applying migrations on tenant ID %s", tnt2.ID)) }) } diff --git a/cmd/utils/default_cmd_inner_calls.go b/cmd/utils/default_cmd_inner_calls.go new file mode 100644 index 000000000..82b063e55 --- /dev/null +++ b/cmd/utils/default_cmd_inner_calls.go @@ -0,0 +1,20 @@ +package utils + +import ( + "fmt" + + "github.com/spf13/cobra" +) + +var PropagatePersistentPreRun = func(cmd *cobra.Command, args []string) { + if cmd.Parent().PersistentPreRun != nil { + cmd.Parent().PersistentPreRun(cmd.Parent(), args) + } +} + +var CallHelpCommand = func(cmd *cobra.Command, args []string) error { + if err := cmd.Help(); err != nil { + return fmt.Errorf("calling help command: %w", err) + } + return nil +} diff --git a/db/dbtest/dbtest.go b/db/dbtest/dbtest.go index 53dc15dec..5febac9ff 100644 --- a/db/dbtest/dbtest.go +++ b/db/dbtest/dbtest.go @@ -7,9 +7,9 @@ import ( migrate "github.com/rubenv/sql-migrate" "github.com/stellar/go/support/db/dbtest" "github.com/stellar/go/support/db/schema" + adminmigrations "github.com/stellar/stellar-disbursement-platform-backend/db/migrations/admin-migrations" 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 { @@ -23,24 +23,24 @@ func Open(t *testing.T) *dbtest.DB { conn := db.Open() defer conn.Close() - // Tenant migrations - ms := migrate.MigrationSet{TableName: "migrations"} + // Admin migrations + ms := migrate.MigrationSet{TableName: "admin_migrations"} migrateDirection := migrate.Up - m := migrate.HttpFileSystemMigrationSource{FileSystem: http.FS(tenantmigrations.FS)} + m := migrate.HttpFileSystemMigrationSource{FileSystem: http.FS(adminmigrations.FS)} _, err := ms.ExecMax(conn.DB, "postgres", m, migrateDirection, 0) if err != nil { t.Fatal(err) } - // SDP migrations - ms = migrate.MigrationSet{} + // Per-tenant SDP migrations + ms = migrate.MigrationSet{TableName: "sdp_migrations"} 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 + // Per-tenant 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) @@ -57,7 +57,7 @@ func OpenWithTenantMigrationsOnly(t *testing.T) *dbtest.DB { defer conn.Close() migrateDirection := schema.MigrateUp - m := migrate.HttpFileSystemMigrationSource{FileSystem: http.FS(tenantmigrations.FS)} + m := migrate.HttpFileSystemMigrationSource{FileSystem: http.FS(adminmigrations.FS)} _, err := schema.Migrate(conn.DB, m, migrateDirection, 0) if err != nil { t.Fatal(err) diff --git a/db/dbtest/dbtest_test.go b/db/dbtest/dbtest_test.go index c7ea19a4d..3d6a5f1fd 100644 --- a/db/dbtest/dbtest_test.go +++ b/db/dbtest/dbtest_test.go @@ -10,23 +10,22 @@ import ( 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`) + err := session.Get(&count, `SELECT COUNT(*) FROM admin_migrations`) require.NoError(t, err) assert.Greater(t, count, 0) - // SDP migrations - err = session.Get(&count, `SELECT COUNT(*) FROM gorp_migrations`) + // Per-tenant SDP migrations + err = session.Get(&count, `SELECT COUNT(*) FROM sdp_migrations`) require.NoError(t, err) assert.Greater(t, count, 0) - // Auth Migrations + // Per-tenant Auth Migrations err = session.Get(&count, `SELECT COUNT(*) FROM auth_migrations`) require.NoError(t, err) assert.Greater(t, count, 0) diff --git a/db/migrate.go b/db/migrate.go index 2abd2893b..6c24c9a21 100644 --- a/db/migrate.go +++ b/db/migrate.go @@ -13,9 +13,10 @@ import ( type MigrationTableName string const ( - StellarMultiTenantMigrationsTableName MigrationTableName = "migrations" - StellarSDPMigrationsTableName MigrationTableName = "gorp_migrations" - StellarAuthMigrationsTableName MigrationTableName = "auth_migrations" + // NOTE: these names are hardcoded in the `db.dbtest.Open` method and need to be kept in sync if updated. + StellarAdminMigrationsTableName MigrationTableName = "admin_migrations" + StellarPerTenantSDPMigrationsTableName MigrationTableName = "sdp_migrations" + StellarPerTenantAuthMigrationsTableName MigrationTableName = "auth_migrations" ) func Migrate(dbURL string, dir migrate.MigrationDirection, count int, migrationFiles embed.FS, tableName MigrationTableName) (int, error) { diff --git a/db/migrate_test.go b/db/migrate_test.go index f5cf70a8b..6974a82b0 100644 --- a/db/migrate_test.go +++ b/db/migrate_test.go @@ -8,9 +8,9 @@ import ( migrate "github.com/rubenv/sql-migrate" "github.com/stellar/stellar-disbursement-platform-backend/db/dbtest" + adminmigrations "github.com/stellar/stellar-disbursement-platform-backend/db/migrations/admin-migrations" 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" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) @@ -24,12 +24,12 @@ func TestMigrate_upApplyOne_SDP_migrations(t *testing.T) { ctx := context.Background() - n, err := Migrate(db.DSN, migrate.Up, 1, sdpmigrations.FS, StellarSDPMigrationsTableName) + n, err := Migrate(db.DSN, migrate.Up, 1, sdpmigrations.FS, StellarPerTenantSDPMigrationsTableName) require.NoError(t, err) assert.Equal(t, 1, n) ids := []string{} - err = dbConnectionPool.SelectContext(ctx, &ids, fmt.Sprintf("SELECT id FROM %s", StellarSDPMigrationsTableName)) + err = dbConnectionPool.SelectContext(ctx, &ids, fmt.Sprintf("SELECT id FROM %s", StellarPerTenantSDPMigrationsTableName)) require.NoError(t, err) wantIDs := []string{"2023-01-20.0-initial.sql"} assert.Equal(t, wantIDs, ids) @@ -44,16 +44,16 @@ func TestMigrate_downApplyOne_SDP_migrations(t *testing.T) { ctx := context.Background() - n, err := Migrate(db.DSN, migrate.Up, 2, sdpmigrations.FS, StellarSDPMigrationsTableName) + n, err := Migrate(db.DSN, migrate.Up, 2, sdpmigrations.FS, StellarPerTenantSDPMigrationsTableName) require.NoError(t, err) require.Equal(t, 2, n) - n, err = Migrate(db.DSN, migrate.Down, 1, sdpmigrations.FS, StellarSDPMigrationsTableName) + n, err = Migrate(db.DSN, migrate.Down, 1, sdpmigrations.FS, StellarPerTenantSDPMigrationsTableName) require.NoError(t, err) require.Equal(t, 1, n) ids := []string{} - err = dbConnectionPool.SelectContext(ctx, &ids, fmt.Sprintf("SELECT id FROM %s", StellarSDPMigrationsTableName)) + err = dbConnectionPool.SelectContext(ctx, &ids, fmt.Sprintf("SELECT id FROM %s", StellarPerTenantSDPMigrationsTableName)) require.NoError(t, err) wantIDs := []string{"2023-01-20.0-initial.sql"} assert.Equal(t, wantIDs, ids) @@ -77,19 +77,19 @@ func TestMigrate_upAndDownAllTheWayTwice_SDP_migrations(t *testing.T) { }) require.NoError(t, err) - n, err := Migrate(db.DSN, migrate.Up, count, sdpmigrations.FS, StellarSDPMigrationsTableName) + n, err := Migrate(db.DSN, migrate.Up, count, sdpmigrations.FS, StellarPerTenantSDPMigrationsTableName) require.NoError(t, err) require.Equal(t, count, n) - n, err = Migrate(db.DSN, migrate.Down, count, sdpmigrations.FS, StellarSDPMigrationsTableName) + n, err = Migrate(db.DSN, migrate.Down, count, sdpmigrations.FS, StellarPerTenantSDPMigrationsTableName) require.NoError(t, err) require.Equal(t, count, n) - n, err = Migrate(db.DSN, migrate.Up, count, sdpmigrations.FS, StellarSDPMigrationsTableName) + n, err = Migrate(db.DSN, migrate.Up, count, sdpmigrations.FS, StellarPerTenantSDPMigrationsTableName) require.NoError(t, err) require.Equal(t, count, n) - n, err = Migrate(db.DSN, migrate.Down, count, sdpmigrations.FS, StellarSDPMigrationsTableName) + n, err = Migrate(db.DSN, migrate.Down, count, sdpmigrations.FS, StellarPerTenantSDPMigrationsTableName) require.NoError(t, err) require.Equal(t, count, n) } @@ -103,18 +103,18 @@ func TestMigrate_upApplyOne_Tenant_migrations(t *testing.T) { ctx := context.Background() - n, err := Migrate(db.DSN, migrate.Up, 1, tenantmigrations.FS, StellarMultiTenantMigrationsTableName) + n, err := Migrate(db.DSN, migrate.Up, 1, adminmigrations.FS, StellarAdminMigrationsTableName) require.NoError(t, err) assert.Equal(t, 1, n) ids := []string{} - err = dbConnectionPool.SelectContext(ctx, &ids, fmt.Sprintf("SELECT id FROM %s", StellarMultiTenantMigrationsTableName)) + err = dbConnectionPool.SelectContext(ctx, &ids, fmt.Sprintf("SELECT id FROM %s", StellarAdminMigrationsTableName)) require.NoError(t, err) wantIDs := []string{"2023-10-16.0.add-tenants-table.sql"} assert.Equal(t, wantIDs, ids) } -func TestMigrate_downApplyOne_Tenant_migrations(t *testing.T) { +func TestMigrate_downApplyTwo_Tenant_migrations(t *testing.T) { db := dbtest.OpenWithoutMigrations(t) defer db.Close() dbConnectionPool, err := OpenDBConnectionPool(db.DSN) @@ -123,16 +123,16 @@ func TestMigrate_downApplyOne_Tenant_migrations(t *testing.T) { ctx := context.Background() - n, err := Migrate(db.DSN, migrate.Up, 2, tenantmigrations.FS, StellarMultiTenantMigrationsTableName) + n, err := Migrate(db.DSN, migrate.Up, 2, adminmigrations.FS, StellarAdminMigrationsTableName) require.NoError(t, err) require.Equal(t, 2, n) - n, err = Migrate(db.DSN, migrate.Down, 2, tenantmigrations.FS, StellarMultiTenantMigrationsTableName) + n, err = Migrate(db.DSN, migrate.Down, 2, adminmigrations.FS, StellarAdminMigrationsTableName) require.NoError(t, err) require.Equal(t, 2, n) ids := []string{} - err = dbConnectionPool.SelectContext(ctx, &ids, fmt.Sprintf("SELECT id FROM %s", StellarMultiTenantMigrationsTableName)) + err = dbConnectionPool.SelectContext(ctx, &ids, fmt.Sprintf("SELECT id FROM %s", StellarAdminMigrationsTableName)) require.NoError(t, err) wantIDs := []string{} assert.Equal(t, wantIDs, ids) @@ -147,7 +147,7 @@ func TestMigrate_upAndDownAllTheWayTwice_Tenant_migrations(t *testing.T) { // Get number of files in the migrations directory: var count int - err = fs.WalkDir(tenantmigrations.FS, ".", func(path string, d fs.DirEntry, err error) error { + err = fs.WalkDir(adminmigrations.FS, ".", func(path string, d fs.DirEntry, err error) error { require.NoError(t, err) if !d.IsDir() { count++ @@ -156,19 +156,19 @@ func TestMigrate_upAndDownAllTheWayTwice_Tenant_migrations(t *testing.T) { }) require.NoError(t, err) - n, err := Migrate(db.DSN, migrate.Up, count, tenantmigrations.FS, StellarMultiTenantMigrationsTableName) + n, err := Migrate(db.DSN, migrate.Up, count, adminmigrations.FS, StellarAdminMigrationsTableName) require.NoError(t, err) require.Equal(t, count, n) - n, err = Migrate(db.DSN, migrate.Down, count, tenantmigrations.FS, StellarMultiTenantMigrationsTableName) + n, err = Migrate(db.DSN, migrate.Down, count, adminmigrations.FS, StellarAdminMigrationsTableName) require.NoError(t, err) require.Equal(t, count, n) - n, err = Migrate(db.DSN, migrate.Up, count, tenantmigrations.FS, StellarMultiTenantMigrationsTableName) + n, err = Migrate(db.DSN, migrate.Up, count, adminmigrations.FS, StellarAdminMigrationsTableName) require.NoError(t, err) require.Equal(t, count, n) - n, err = Migrate(db.DSN, migrate.Down, count, tenantmigrations.FS, StellarMultiTenantMigrationsTableName) + n, err = Migrate(db.DSN, migrate.Down, count, adminmigrations.FS, StellarAdminMigrationsTableName) require.NoError(t, err) require.Equal(t, count, n) } @@ -182,12 +182,12 @@ func TestMigrate_upApplyOne_Auth_migrations(t *testing.T) { ctx := context.Background() - n, err := Migrate(db.DSN, migrate.Up, 1, authmigrations.FS, StellarAuthMigrationsTableName) + n, err := Migrate(db.DSN, migrate.Up, 1, authmigrations.FS, StellarPerTenantAuthMigrationsTableName) require.NoError(t, err) assert.Equal(t, 1, n) ids := []string{} - err = dbConnectionPool.SelectContext(ctx, &ids, fmt.Sprintf("SELECT id FROM %s", StellarAuthMigrationsTableName)) + err = dbConnectionPool.SelectContext(ctx, &ids, fmt.Sprintf("SELECT id FROM %s", StellarPerTenantAuthMigrationsTableName)) require.NoError(t, err) wantIDs := []string{"2023-02-09.0.add-users-table.sql"} assert.Equal(t, wantIDs, ids) @@ -202,16 +202,16 @@ func TestMigrate_downApplyOne_Auth_migrations(t *testing.T) { ctx := context.Background() - n, err := Migrate(db.DSN, migrate.Up, 2, authmigrations.FS, StellarAuthMigrationsTableName) + n, err := Migrate(db.DSN, migrate.Up, 2, authmigrations.FS, StellarPerTenantAuthMigrationsTableName) require.NoError(t, err) require.Equal(t, 2, n) - n, err = Migrate(db.DSN, migrate.Down, 1, authmigrations.FS, StellarAuthMigrationsTableName) + n, err = Migrate(db.DSN, migrate.Down, 1, authmigrations.FS, StellarPerTenantAuthMigrationsTableName) require.NoError(t, err) require.Equal(t, 1, n) ids := []string{} - err = dbConnectionPool.SelectContext(ctx, &ids, fmt.Sprintf("SELECT id FROM %s", StellarAuthMigrationsTableName)) + err = dbConnectionPool.SelectContext(ctx, &ids, fmt.Sprintf("SELECT id FROM %s", StellarPerTenantAuthMigrationsTableName)) require.NoError(t, err) wantIDs := []string{"2023-02-09.0.add-users-table.sql"} assert.Equal(t, wantIDs, ids) @@ -235,19 +235,19 @@ func TestMigrate_upAndDownAllTheWayTwice_Auth_migrations(t *testing.T) { }) require.NoError(t, err) - n, err := Migrate(db.DSN, migrate.Up, count, authmigrations.FS, StellarAuthMigrationsTableName) + n, err := Migrate(db.DSN, migrate.Up, count, authmigrations.FS, StellarPerTenantAuthMigrationsTableName) require.NoError(t, err) require.Equal(t, count, n) - n, err = Migrate(db.DSN, migrate.Down, count, authmigrations.FS, StellarAuthMigrationsTableName) + n, err = Migrate(db.DSN, migrate.Down, count, authmigrations.FS, StellarPerTenantAuthMigrationsTableName) require.NoError(t, err) require.Equal(t, count, n) - n, err = Migrate(db.DSN, migrate.Up, count, authmigrations.FS, StellarAuthMigrationsTableName) + n, err = Migrate(db.DSN, migrate.Up, count, authmigrations.FS, StellarPerTenantAuthMigrationsTableName) require.NoError(t, err) require.Equal(t, count, n) - n, err = Migrate(db.DSN, migrate.Down, count, authmigrations.FS, StellarAuthMigrationsTableName) + n, err = Migrate(db.DSN, migrate.Down, count, authmigrations.FS, StellarPerTenantAuthMigrationsTableName) require.NoError(t, err) require.Equal(t, count, n) } diff --git a/db/migrations/tenant-migrations/2023-10-16.0.add-tenants-table.sql b/db/migrations/admin-migrations/2023-10-16.0.add-tenants-table.sql similarity index 100% rename from db/migrations/tenant-migrations/2023-10-16.0.add-tenants-table.sql rename to db/migrations/admin-migrations/2023-10-16.0.add-tenants-table.sql diff --git a/db/migrations/tenant-migrations/2023-11-14.0.drop-unused-cors-column.sql b/db/migrations/admin-migrations/2023-11-14.0.drop-unused-cors-column.sql similarity index 100% rename from db/migrations/tenant-migrations/2023-11-14.0.drop-unused-cors-column.sql rename to db/migrations/admin-migrations/2023-11-14.0.drop-unused-cors-column.sql diff --git a/db/migrations/tenant-migrations/main.go b/db/migrations/admin-migrations/main.go similarity index 100% rename from db/migrations/tenant-migrations/main.go rename to db/migrations/admin-migrations/main.go diff --git a/dev/docker-compose-sdp-anchor.yml b/dev/docker-compose-sdp-anchor.yml index 0448198f7..ec9c989ca 100644 --- a/dev/docker-compose-sdp-anchor.yml +++ b/dev/docker-compose-sdp-anchor.yml @@ -69,9 +69,9 @@ services: - -c - | sleep 5 - ./stellar-disbursement-platform db tenant migrate up - ./stellar-disbursement-platform db migrate up --all + ./stellar-disbursement-platform db admin migrate up ./stellar-disbursement-platform db auth migrate up --all + ./stellar-disbursement-platform db sdp migrate up --all ./stellar-disbursement-platform db setup-for-network --all ./stellar-disbursement-platform serve depends_on: diff --git a/helmchart/sdp/templates/02.1-deployment-sdp.yaml b/helmchart/sdp/templates/02.1-deployment-sdp.yaml index 8c6470f05..f9b553bf3 100644 --- a/helmchart/sdp/templates/02.1-deployment-sdp.yaml +++ b/helmchart/sdp/templates/02.1-deployment-sdp.yaml @@ -59,9 +59,9 @@ spec: - sh - -c - | - ./stellar-disbursement-platform db tenant migrate up - ./stellar-disbursement-platform db migrate up --all + ./stellar-disbursement-platform db admin migrate up ./stellar-disbursement-platform db auth migrate up --all + ./stellar-disbursement-platform db sdp migrate up --all ./stellar-disbursement-platform db setup-for-network --all containers: diff --git a/internal/data/wallets.go b/internal/data/wallets.go index 0d220111d..8ef7ed86b 100644 --- a/internal/data/wallets.go +++ b/internal/data/wallets.go @@ -110,7 +110,7 @@ func (wm *WalletModel) FindWallets(ctx context.Context, enabledFilter *bool) ([] var args []interface{} if enabledFilter != nil { - whereClause = "WHERE w.enabled = $1" + whereClause = "WHERE w.enabled = $1 " args = append(args, *enabledFilter) } diff --git a/internal/integrationtests/docker-compose-e2e-tests.yml b/internal/integrationtests/docker-compose-e2e-tests.yml index c7ebce6e3..cbf613e90 100644 --- a/internal/integrationtests/docker-compose-e2e-tests.yml +++ b/internal/integrationtests/docker-compose-e2e-tests.yml @@ -72,10 +72,10 @@ services: - -c - | sleep 5 - ./stellar-disbursement-platform db tenant migrate up - ./stellar-disbursement-platform db migrate up --all + ./stellar-disbursement-platform db admin migrate up ./stellar-disbursement-platform db auth migrate up --all - ./stellar-disbursement-platform db setup-for-network + ./stellar-disbursement-platform db sdp migrate up --all + ./stellar-disbursement-platform db setup-for-network --all ./stellar-disbursement-platform serve depends_on: - db diff --git a/internal/serve/httphandler/wallets_handler_test.go b/internal/serve/httphandler/wallets_handler_test.go index bf0daf3dd..0a0813c7e 100644 --- a/internal/serve/httphandler/wallets_handler_test.go +++ b/internal/serve/httphandler/wallets_handler_test.go @@ -106,7 +106,7 @@ func Test_WalletsHandlerGetWallets(t *testing.T) { respBody, err := io.ReadAll(resp.Body) require.NoError(t, err) - require.Equal(t, http.StatusOK, resp.StatusCode) + assert.Equal(t, http.StatusOK, resp.StatusCode) require.JSONEq(t, string(expectedJSON), string(respBody)) }) diff --git a/stellar-auth/pkg/cli/migrate.go b/stellar-auth/pkg/cli/migrate.go index d9f93757a..f6f155df1 100644 --- a/stellar-auth/pkg/cli/migrate.go +++ b/stellar-auth/pkg/cli/migrate.go @@ -25,7 +25,7 @@ func MigrateCmd(databaseFlagName string) *cobra.Command { migrateUp := &cobra.Command{ Use: "up [count]", - Short: "Migrates database up [count]", + Short: "Migrates database up [count] migrations", Args: cobra.MaximumNArgs(1), Run: func(cmd *cobra.Command, args []string) { var count int @@ -77,7 +77,7 @@ func MigrateCmd(databaseFlagName string) *cobra.Command { } func runMigration(databaseURL string, dir migrate.MigrationDirection, count int) error { - numMigrationsRun, err := db.Migrate(databaseURL, dir, count, migrations.FS, db.StellarAuthMigrationsTableName) + numMigrationsRun, err := db.Migrate(databaseURL, dir, count, migrations.FS, db.StellarPerTenantAuthMigrationsTableName) if err != nil { return fmt.Errorf("running migrations: %w", err) } @@ -85,8 +85,15 @@ func runMigration(databaseURL string, dir migrate.MigrationDirection, count int) if numMigrationsRun == 0 { log.Info("No migrations applied.") } else { - log.Infof("Successfully applied %d migrations.", numMigrationsRun) + log.Infof("Successfully applied %d migrations %s.", numMigrationsRun, migrationDirectionStr(dir)) } return nil } + +func migrationDirectionStr(dir migrate.MigrationDirection) string { + if dir == migrate.Up { + return "up" + } + return "down" +} diff --git a/stellar-auth/pkg/cli/migrate_test.go b/stellar-auth/pkg/cli/migrate_test.go index db56f3901..b6e886b74 100644 --- a/stellar-auth/pkg/cli/migrate_test.go +++ b/stellar-auth/pkg/cli/migrate_test.go @@ -20,7 +20,7 @@ import ( ) func getMigrationsApplied(t *testing.T, ctx context.Context, db *sql.DB) []string { - rows, err := db.QueryContext(ctx, fmt.Sprintf("SELECT id FROM %s", dbpkg.StellarAuthMigrationsTableName)) + rows, err := db.QueryContext(ctx, fmt.Sprintf("SELECT id FROM %s", dbpkg.StellarPerTenantAuthMigrationsTableName)) require.NoError(t, err) defer rows.Close() @@ -52,17 +52,17 @@ func Test_MigrateCmd(t *testing.T) { { name: "test help command", args: []string{"migrate", "--help"}, - expect: "Apply Stellar Auth database migrations\n\nUsage:\n stellarauth migrate [flags]\n stellarauth migrate [command]\n\nAvailable Commands:\n down Migrates database down [count] migrations\n up Migrates database up [count]\n\nFlags:\n -h, --help help for migrate\n\nGlobal Flags:\n --database-url string Postgres DB URL (DATABASE_URL) (default \"postgres://postgres:postgres@localhost:5432/stellar-auth?sslmode=disable\")\n --log-level string The log level used in this project. Options: \"TRACE\", \"DEBUG\", \"INFO\", \"WARN\", \"ERROR\", \"FATAL\", or \"PANIC\". (LOG_LEVEL) (default \"TRACE\")\n\nUse \"stellarauth migrate [command] --help\" for more information about a command.\n", + expect: "Apply Stellar Auth database migrations\n\nUsage:\n stellarauth migrate [flags]\n stellarauth migrate [command]\n\nAvailable Commands:\n down Migrates database down [count] migrations\n up Migrates database up [count] migrations\n\nFlags:\n -h, --help help for migrate\n\nGlobal Flags:\n --database-url string Postgres DB URL (DATABASE_URL) (default \"postgres://postgres:postgres@localhost:5432/stellar-auth?sslmode=disable\")\n --log-level string The log level used in this project. Options: \"TRACE\", \"DEBUG\", \"INFO\", \"WARN\", \"ERROR\", \"FATAL\", or \"PANIC\". (LOG_LEVEL) (default \"TRACE\")\n\nUse \"stellarauth migrate [command] --help\" for more information about a command.\n", }, { name: "test short help command", args: []string{"migrate", "-h"}, - expect: "Apply Stellar Auth database migrations\n\nUsage:\n stellarauth migrate [flags]\n stellarauth migrate [command]\n\nAvailable Commands:\n down Migrates database down [count] migrations\n up Migrates database up [count]\n\nFlags:\n -h, --help help for migrate\n\nGlobal Flags:\n --database-url string Postgres DB URL (DATABASE_URL) (default \"postgres://postgres:postgres@localhost:5432/stellar-auth?sslmode=disable\")\n --log-level string The log level used in this project. Options: \"TRACE\", \"DEBUG\", \"INFO\", \"WARN\", \"ERROR\", \"FATAL\", or \"PANIC\". (LOG_LEVEL) (default \"TRACE\")\n\nUse \"stellarauth migrate [command] --help\" for more information about a command.\n", + expect: "Apply Stellar Auth database migrations\n\nUsage:\n stellarauth migrate [flags]\n stellarauth migrate [command]\n\nAvailable Commands:\n down Migrates database down [count] migrations\n up Migrates database up [count] migrations\n\nFlags:\n -h, --help help for migrate\n\nGlobal Flags:\n --database-url string Postgres DB URL (DATABASE_URL) (default \"postgres://postgres:postgres@localhost:5432/stellar-auth?sslmode=disable\")\n --log-level string The log level used in this project. Options: \"TRACE\", \"DEBUG\", \"INFO\", \"WARN\", \"ERROR\", \"FATAL\", or \"PANIC\". (LOG_LEVEL) (default \"TRACE\")\n\nUse \"stellarauth migrate [command] --help\" for more information about a command.\n", }, { name: "test migrate up successfully", args: []string{"--log-level", "TRACE", "--database-url", "", "migrate", "up", "1"}, - expect: "Successfully applied 1 migrations.", + expect: "Successfully applied 1 migrations up.", postRunFunc: func(db *sql.DB) { ids := getMigrationsApplied(t, context.Background(), db) assert.Equal(t, []string{"2023-02-09.0.add-users-table.sql"}, ids) @@ -72,7 +72,7 @@ func Test_MigrateCmd(t *testing.T) { name: "test migrate up successfully when using the DATABASE_URL env var", args: []string{"--log-level", "TRACE", "migrate", "up", "1"}, envVars: map[string]string{"DATABASE_URL": ""}, - expect: "Successfully applied 1 migrations.", + expect: "Successfully applied 1 migrations up.", postRunFunc: func(db *sql.DB) { ids := getMigrationsApplied(t, context.Background(), db) assert.Equal(t, []string{"2023-02-09.0.add-users-table.sql"}, ids) @@ -93,9 +93,9 @@ func Test_MigrateCmd(t *testing.T) { { name: "test migrate down successfully", args: []string{"--log-level", "TRACE", "--database-url", "", "migrate", "down", "1"}, - expect: "Successfully applied 1 migrations.", + expect: "Successfully applied 1 migrations down.", preRunFunc: func(t *testing.T, db *stellardbtest.DB) { - _, err := dbpkg.Migrate(db.DSN, migrate.Up, 1, migrations.FS, dbpkg.StellarAuthMigrationsTableName) + _, err := dbpkg.Migrate(db.DSN, migrate.Up, 1, migrations.FS, dbpkg.StellarPerTenantAuthMigrationsTableName) require.NoError(t, err) conn := db.Open() @@ -113,9 +113,9 @@ func Test_MigrateCmd(t *testing.T) { name: "test migrate up successfully when using the DATABASE_URL env var", args: []string{"--log-level", "TRACE", "migrate", "down", "1"}, envVars: map[string]string{"DATABASE_URL": ""}, - expect: "Successfully applied 1 migrations.", + expect: "Successfully applied 1 migrations down.", preRunFunc: func(t *testing.T, db *stellardbtest.DB) { - _, err := dbpkg.Migrate(db.DSN, migrate.Up, 1, migrations.FS, dbpkg.StellarAuthMigrationsTableName) + _, err := dbpkg.Migrate(db.DSN, migrate.Up, 1, migrations.FS, dbpkg.StellarPerTenantAuthMigrationsTableName) require.NoError(t, err) conn := db.Open() @@ -214,5 +214,5 @@ func Test_MigrateCmd_databaseFlagName(t *testing.T) { err = testCmd.Execute() require.NoError(t, err) - assert.Contains(t, buf.String(), "Successfully applied 1 migrations.") + assert.Contains(t, buf.String(), "Successfully applied 1 migrations up.") } diff --git a/stellar-multitenant/pkg/cli/add_tenants_test.go b/stellar-multitenant/pkg/cli/add_tenants_test.go index 258b6a397..093e918c3 100644 --- a/stellar-multitenant/pkg/cli/add_tenants_test.go +++ b/stellar-multitenant/pkg/cli/add_tenants_test.go @@ -245,7 +245,7 @@ Flags: "channel_accounts", "countries", "disbursements", - "gorp_migrations", + "sdp_migrations", "messages", "organizations", "payments", @@ -314,7 +314,7 @@ Flags: "channel_accounts", "countries", "disbursements", - "gorp_migrations", + "sdp_migrations", "messages", "organizations", "payments", diff --git a/stellar-multitenant/pkg/cli/migrate.go b/stellar-multitenant/pkg/cli/migrate.go index b4aa794c7..034fb38f1 100644 --- a/stellar-multitenant/pkg/cli/migrate.go +++ b/stellar-multitenant/pkg/cli/migrate.go @@ -5,7 +5,7 @@ import ( "strconv" "github.com/stellar/stellar-disbursement-platform-backend/db" - migrations "github.com/stellar/stellar-disbursement-platform-backend/db/migrations/tenant-migrations" + adminMigrations "github.com/stellar/stellar-disbursement-platform-backend/db/migrations/admin-migrations" migrate "github.com/rubenv/sql-migrate" "github.com/spf13/cobra" @@ -16,7 +16,7 @@ import ( func MigrateCmd(databaseFlagName string) *cobra.Command { migrateCmd := &cobra.Command{ Use: "migrate", - Short: "Apply Stellar Multitenant database migrations", + Short: "Apply admin migrations to configure the multi-tenant module that manages the tenants.", PersistentPreRun: func(cmd *cobra.Command, args []string) { if cmd.Parent().PersistentPreRun != nil { cmd.Parent().PersistentPreRun(cmd.Parent(), args) @@ -31,7 +31,7 @@ func MigrateCmd(databaseFlagName string) *cobra.Command { migrateUp := &cobra.Command{ Use: "up [count]", - Short: "Migrates database up [count]", + Short: "Migrates database up [count] migrations", Args: cobra.MaximumNArgs(1), Run: func(cmd *cobra.Command, args []string) { var count int @@ -83,7 +83,7 @@ func MigrateCmd(databaseFlagName string) *cobra.Command { } func runMigration(databaseURL string, dir migrate.MigrationDirection, count int) error { - numMigrationsRun, err := db.Migrate(databaseURL, dir, count, migrations.FS, db.StellarMultiTenantMigrationsTableName) + numMigrationsRun, err := db.Migrate(databaseURL, dir, count, adminMigrations.FS, db.StellarAdminMigrationsTableName) if err != nil { return fmt.Errorf("running migrations: %w", err) } @@ -91,8 +91,15 @@ func runMigration(databaseURL string, dir migrate.MigrationDirection, count int) if numMigrationsRun == 0 { log.Info("No migrations applied.") } else { - log.Infof("Successfully applied %d migrations.", numMigrationsRun) + log.Infof("Successfully applied %d migrations %s.", numMigrationsRun, migrationDirectionStr(dir)) } return nil } + +func migrationDirectionStr(dir migrate.MigrationDirection) string { + if dir == migrate.Up { + return "up" + } + return "down" +} diff --git a/stellar-multitenant/pkg/cli/migrate_test.go b/stellar-multitenant/pkg/cli/migrate_test.go index e195ef2db..0c3861a6e 100644 --- a/stellar-multitenant/pkg/cli/migrate_test.go +++ b/stellar-multitenant/pkg/cli/migrate_test.go @@ -14,13 +14,13 @@ import ( "github.com/stellar/go/support/log" dbpkg "github.com/stellar/stellar-disbursement-platform-backend/db" "github.com/stellar/stellar-disbursement-platform-backend/db/dbtest" - migrations "github.com/stellar/stellar-disbursement-platform-backend/db/migrations/tenant-migrations" + adminMigrations "github.com/stellar/stellar-disbursement-platform-backend/db/migrations/admin-migrations" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) func getMigrationsApplied(t *testing.T, ctx context.Context, db *sql.DB) []string { - rows, err := db.QueryContext(ctx, fmt.Sprintf("SELECT id FROM %s", dbpkg.StellarMultiTenantMigrationsTableName)) + rows, err := db.QueryContext(ctx, fmt.Sprintf("SELECT id FROM %s", dbpkg.StellarAdminMigrationsTableName)) require.NoError(t, err) defer rows.Close() @@ -52,17 +52,17 @@ func Test_MigrateCmd(t *testing.T) { { name: "test help command", args: []string{"migrate", "--help"}, - expect: "Apply Stellar Multitenant database migrations\n\nUsage:\n mtn migrate [flags]\n mtn migrate [command]\n\nAvailable Commands:\n down Migrates database down [count] migrations\n up Migrates database up [count]\n\nFlags:\n -h, --help help for migrate\n\nGlobal Flags:\n --multitenant-db-url string Postgres DB URL (MULTITENANT_DB_URL) (default \"postgres://postgres:postgres@localhost:5432/sdp_main?sslmode=disable\")\n\nUse \"mtn migrate [command] --help\" for more information about a command.\n", + expect: "Apply admin migrations to configure the multi-tenant module that manages the tenants.\n\nUsage:\n mtn migrate [flags]\n mtn migrate [command]\n\nAvailable Commands:\n down Migrates database down [count] migrations\n up Migrates database up [count] migrations\n\nFlags:\n -h, --help help for migrate\n\nGlobal Flags:\n --multitenant-db-url string Postgres DB URL (MULTITENANT_DB_URL) (default \"postgres://postgres:postgres@localhost:5432/sdp_main?sslmode=disable\")\n\nUse \"mtn migrate [command] --help\" for more information about a command.\n", }, { name: "test short help command", args: []string{"migrate", "-h"}, - expect: "Apply Stellar Multitenant database migrations\n\nUsage:\n mtn migrate [flags]\n mtn migrate [command]\n\nAvailable Commands:\n down Migrates database down [count] migrations\n up Migrates database up [count]\n\nFlags:\n -h, --help help for migrate\n\nGlobal Flags:\n --multitenant-db-url string Postgres DB URL (MULTITENANT_DB_URL) (default \"postgres://postgres:postgres@localhost:5432/sdp_main?sslmode=disable\")\n\nUse \"mtn migrate [command] --help\" for more information about a command.\n", + expect: "Apply admin migrations to configure the multi-tenant module that manages the tenants.\n\nUsage:\n mtn migrate [flags]\n mtn migrate [command]\n\nAvailable Commands:\n down Migrates database down [count] migrations\n up Migrates database up [count] migrations\n\nFlags:\n -h, --help help for migrate\n\nGlobal Flags:\n --multitenant-db-url string Postgres DB URL (MULTITENANT_DB_URL) (default \"postgres://postgres:postgres@localhost:5432/sdp_main?sslmode=disable\")\n\nUse \"mtn migrate [command] --help\" for more information about a command.\n", }, { name: "test migrate up successfully", args: []string{"--multitenant-db-url", "", "migrate", "up", "1"}, - expect: "Successfully applied 1 migrations.", + expect: "Successfully applied 1 migrations up.", postRunFunc: func(db *sql.DB) { ids := getMigrationsApplied(t, context.Background(), db) assert.Equal(t, []string{"2023-10-16.0.add-tenants-table.sql"}, ids) @@ -72,7 +72,7 @@ func Test_MigrateCmd(t *testing.T) { name: "test migrate up successfully when using the MULTITENANT_DB_URL env var", args: []string{"migrate", "up", "1"}, envVars: map[string]string{"MULTITENANT_DB_URL": ""}, - expect: "Successfully applied 1 migrations.", + expect: "Successfully applied 1 migrations up.", postRunFunc: func(db *sql.DB) { ids := getMigrationsApplied(t, context.Background(), db) assert.Equal(t, []string{"2023-10-16.0.add-tenants-table.sql"}, ids) @@ -93,9 +93,9 @@ func Test_MigrateCmd(t *testing.T) { { name: "test migrate down successfully", args: []string{"--multitenant-db-url", "", "migrate", "down", "1"}, - expect: "Successfully applied 1 migrations.", + expect: "Successfully applied 1 migrations down.", preRunFunc: func(t *testing.T, db *stellardbtest.DB) { - _, err := dbpkg.Migrate(db.DSN, migrate.Up, 1, migrations.FS, dbpkg.StellarMultiTenantMigrationsTableName) + _, err := dbpkg.Migrate(db.DSN, migrate.Up, 1, adminMigrations.FS, dbpkg.StellarAdminMigrationsTableName) require.NoError(t, err) conn := db.Open() @@ -113,9 +113,9 @@ func Test_MigrateCmd(t *testing.T) { name: "test migrate up successfully when using the MULTITENANT_DB_URL env var", args: []string{"migrate", "down", "1"}, envVars: map[string]string{"MULTITENANT_DB_URL": ""}, - expect: "Successfully applied 1 migrations.", + expect: "Successfully applied 1 migrations down.", preRunFunc: func(t *testing.T, db *stellardbtest.DB) { - _, err := dbpkg.Migrate(db.DSN, migrate.Up, 1, migrations.FS, dbpkg.StellarMultiTenantMigrationsTableName) + _, err := dbpkg.Migrate(db.DSN, migrate.Up, 1, adminMigrations.FS, dbpkg.StellarAdminMigrationsTableName) require.NoError(t, err) conn := db.Open() @@ -215,5 +215,5 @@ func Test_MigrateCmd_databaseFlagName(t *testing.T) { err = testCmd.Execute() require.NoError(t, err) - assert.Contains(t, buf.String(), "Successfully applied 1 migrations.") + assert.Contains(t, buf.String(), "Successfully applied 1 migrations up.") } diff --git a/stellar-multitenant/pkg/internal/httphandler/tenants_handler_test.go b/stellar-multitenant/pkg/internal/httphandler/tenants_handler_test.go index 758f81d84..5f0d73614 100644 --- a/stellar-multitenant/pkg/internal/httphandler/tenants_handler_test.go +++ b/stellar-multitenant/pkg/internal/httphandler/tenants_handler_test.go @@ -373,7 +373,7 @@ func Test_TenantHandler_Post(t *testing.T) { "channel_accounts", "countries", "disbursements", - "gorp_migrations", + "sdp_migrations", "messages", "organizations", "payments", diff --git a/stellar-multitenant/pkg/internal/provisioning/manager.go b/stellar-multitenant/pkg/internal/provisioning/manager.go index 604715ce3..66424ed37 100644 --- a/stellar-multitenant/pkg/internal/provisioning/manager.go +++ b/stellar-multitenant/pkg/internal/provisioning/manager.go @@ -50,13 +50,13 @@ func (m *Manager) ProvisionNewTenant( // Applying migrations log.Infof("applying SDP migrations on the tenant %s schema", t.Name) - err = m.RunMigrationsForTenant(ctx, t, u, migrate.Up, 0, sdpmigrations.FS, db.StellarSDPMigrationsTableName) + err = m.RunMigrationsForTenant(ctx, t, u, migrate.Up, 0, sdpmigrations.FS, db.StellarPerTenantSDPMigrationsTableName) if err != nil { return nil, fmt.Errorf("applying SDP migrations: %w", err) } log.Infof("applying stellar-auth migrations on the tenant %s schema", t.Name) - err = m.RunMigrationsForTenant(ctx, t, u, migrate.Up, 0, authmigrations.FS, db.StellarAuthMigrationsTableName) + err = m.RunMigrationsForTenant(ctx, t, u, migrate.Up, 0, authmigrations.FS, db.StellarPerTenantAuthMigrationsTableName) if err != nil { return nil, fmt.Errorf("applying stellar-auth migrations: %w", err) } diff --git a/stellar-multitenant/pkg/internal/provisioning/manager_test.go b/stellar-multitenant/pkg/internal/provisioning/manager_test.go index c4aeba8b3..cb0ae7d0a 100644 --- a/stellar-multitenant/pkg/internal/provisioning/manager_test.go +++ b/stellar-multitenant/pkg/internal/provisioning/manager_test.go @@ -81,7 +81,7 @@ func Test_Manager_ProvisionNewTenant(t *testing.T) { "channel_accounts", "countries", "disbursements", - "gorp_migrations", + "sdp_migrations", "messages", "organizations", "payments", @@ -138,7 +138,7 @@ func Test_Manager_ProvisionNewTenant(t *testing.T) { "channel_accounts", "countries", "disbursements", - "gorp_migrations", + "sdp_migrations", "messages", "organizations", "payments", @@ -210,9 +210,9 @@ func Test_Manager_RunMigrationsForTenant(t *testing.T) { require.NoError(t, err) p := NewManager(WithDatabase(dbConnectionPool)) - err = p.RunMigrationsForTenant(ctx, tnt1, tnt1DSN, migrate.Up, 0, sdpmigrations.FS, db.StellarSDPMigrationsTableName) + err = p.RunMigrationsForTenant(ctx, tnt1, tnt1DSN, migrate.Up, 0, sdpmigrations.FS, db.StellarPerTenantSDPMigrationsTableName) require.NoError(t, err) - err = p.RunMigrationsForTenant(ctx, tnt1, tnt1DSN, migrate.Up, 0, authmigrations.FS, db.StellarAuthMigrationsTableName) + err = p.RunMigrationsForTenant(ctx, tnt1, tnt1DSN, migrate.Up, 0, authmigrations.FS, db.StellarPerTenantAuthMigrationsTableName) require.NoError(t, err) expectedTablesAfterMigrationsApplied := []string{ @@ -224,7 +224,7 @@ func Test_Manager_RunMigrationsForTenant(t *testing.T) { "channel_accounts", "countries", "disbursements", - "gorp_migrations", + "sdp_migrations", "messages", "organizations", "payments", @@ -243,9 +243,9 @@ func Test_Manager_RunMigrationsForTenant(t *testing.T) { // Apply migrations for Tenant 2 tnt2DSN, err := tnt2SchemaConnectionPool.DSN(ctx) require.NoError(t, err) - err = p.RunMigrationsForTenant(ctx, tnt2, tnt2DSN, migrate.Up, 0, sdpmigrations.FS, db.StellarSDPMigrationsTableName) + err = p.RunMigrationsForTenant(ctx, tnt2, tnt2DSN, migrate.Up, 0, sdpmigrations.FS, db.StellarPerTenantSDPMigrationsTableName) require.NoError(t, err) - err = p.RunMigrationsForTenant(ctx, tnt2, tnt2DSN, migrate.Up, 0, authmigrations.FS, db.StellarAuthMigrationsTableName) + err = p.RunMigrationsForTenant(ctx, tnt2, tnt2DSN, migrate.Up, 0, authmigrations.FS, db.StellarPerTenantAuthMigrationsTableName) require.NoError(t, err) tenant.TenantSchemaMatchTablesFixture(t, ctx, dbConnectionPool, tnt2SchemaName, expectedTablesAfterMigrationsApplied) diff --git a/stellar-multitenant/pkg/tenant/fixtures.go b/stellar-multitenant/pkg/tenant/fixtures.go index c860a2539..528ead873 100644 --- a/stellar-multitenant/pkg/tenant/fixtures.go +++ b/stellar-multitenant/pkg/tenant/fixtures.go @@ -167,8 +167,8 @@ func ApplyMigrationsForTenantFixture(t *testing.T, ctx context.Context, dbConnec dsn, err := m.GetDSNForTenant(ctx, tenantName) require.NoError(t, err) - _, err = db.Migrate(dsn, migrate.Up, 0, sdpmigrations.FS, db.StellarSDPMigrationsTableName) + _, err = db.Migrate(dsn, migrate.Up, 0, sdpmigrations.FS, db.StellarPerTenantSDPMigrationsTableName) require.NoError(t, err) - _, err = db.Migrate(dsn, migrate.Up, 0, authmigrations.FS, db.StellarAuthMigrationsTableName) + _, err = db.Migrate(dsn, migrate.Up, 0, authmigrations.FS, db.StellarPerTenantAuthMigrationsTableName) require.NoError(t, err) }