Skip to content

Commit

Permalink
feat: handle migrations in migrate command
Browse files Browse the repository at this point in the history
  • Loading branch information
gfyrag committed Feb 8, 2024
1 parent c153925 commit ca35653
Show file tree
Hide file tree
Showing 20 changed files with 247 additions and 84 deletions.
1 change: 1 addition & 0 deletions components/ledger/libs/bun/bunconnect/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ func InitFlags(flags *pflag.FlagSet) {

func ConnectionOptionsFromFlags(v *viper.Viper, output io.Writer, debug bool) (*ConnectionOptions, error) {
var connector func(string) (driver.Connector, error)

if v.GetBool(PostgresAWSEnableIAMFlag) {
cfg, err := config.LoadDefaultConfig(context.Background(), iam.LoadOptionFromViper(v))
if err != nil {
Expand Down
30 changes: 0 additions & 30 deletions components/ledger/libs/bun/bunmigrate/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,8 @@ package bunmigrate

import (
"github.com/formancehq/stack/libs/go-libs/bun/bunconnect"
sharedlogging "github.com/formancehq/stack/libs/go-libs/logging"
"github.com/pkg/errors"

"github.com/formancehq/stack/libs/go-libs/service"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/uptrace/bun"
"github.com/uptrace/bun/extra/bundebug"

// Import the postgres driver.
_ "github.com/lib/pq"
)
Expand All @@ -31,26 +24,3 @@ func NewDefaultCommand(executor Executor, options ...func(command *cobra.Command
bunconnect.InitFlags(ret.Flags())
return ret
}

func Run(cmd *cobra.Command, args []string, executor Executor) error {
connectionOptions, err := bunconnect.ConnectionOptionsFromFlags(viper.GetViper(), cmd.OutOrStdout(), viper.GetBool(service.DebugFlag))
if err != nil {
return errors.Wrap(err, "evaluating connection options")
}

db, err := bunconnect.OpenSQLDB(*connectionOptions)
if err != nil {
return errors.Wrap(err, "opening database")
}
defer func() {
err := db.Close()
if err != nil {
sharedlogging.FromContext(cmd.Context()).Errorf("Closing database: %s", err)
}
}()
if viper.GetBool(service.DebugFlag) {
db.AddQueryHook(bundebug.NewQueryHook(bundebug.WithWriter(cmd.OutOrStdout())))
}

return errors.Wrap(executor(cmd, args, db), "executing migration")
}
88 changes: 88 additions & 0 deletions components/ledger/libs/bun/bunmigrate/run.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package bunmigrate

import (
"context"
"database/sql"
"fmt"
"github.com/formancehq/stack/libs/go-libs/bun/bunconnect"
sharedlogging "github.com/formancehq/stack/libs/go-libs/logging"
"github.com/formancehq/stack/libs/go-libs/pointer"
"github.com/formancehq/stack/libs/go-libs/service"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/uptrace/bun"
"github.com/uptrace/bun/extra/bundebug"
"github.com/xo/dburl"
"io"
)

func ensureDatabaseExists(ctx context.Context, connectionOptions bunconnect.ConnectionOptions) error {
url, err := dburl.Parse(connectionOptions.DatabaseSourceName)
if err != nil {
return err
}
originalPath := url.Path
url.Path = "postgres" // notes(gfyrag): default "postgres" database (most of the time?)
connectionOptions.DatabaseSourceName = url.String()

db, err := bunconnect.OpenSQLDB(connectionOptions)
if err != nil {
return errors.Wrap(err, "opening database")
}
defer func() {
err := db.Close()
if err != nil {
sharedlogging.FromContext(ctx).Errorf("Closing database: %s", err)
}
}()

row := db.QueryRowContext(ctx, `SELECT datname FROM pg_database WHERE datname = ?`, originalPath[1:])
if row.Err() != nil {
return row.Err()
}

if err := row.Scan(pointer.For("")); err != nil {
if !errors.Is(err, sql.ErrNoRows) {
return err
}

_, err = db.ExecContext(ctx, fmt.Sprintf(`CREATE DATABASE "%s"`, originalPath[1:]))
if err != nil {
return err
}
}

return nil
}

func run(ctx context.Context, output io.Writer, args []string, connectionOptions *bunconnect.ConnectionOptions,
executor func(args []string, db *bun.DB) error) error {

if err := ensureDatabaseExists(ctx, *connectionOptions); err != nil {
return err
}

db, err := bunconnect.OpenSQLDB(*connectionOptions)
if err != nil {
return errors.Wrap(err, "opening database")
}
defer func() {
_ = db.Close()
}()
if viper.GetBool(service.DebugFlag) {
db.AddQueryHook(bundebug.NewQueryHook(bundebug.WithWriter(output)))
}

return errors.Wrap(executor(args, db), "executing migration")
}

func Run(cmd *cobra.Command, args []string, executor Executor) error {
connectionOptions, err := bunconnect.ConnectionOptionsFromFlags(viper.GetViper(), cmd.OutOrStdout(), viper.GetBool(service.DebugFlag))
if err != nil {
return errors.Wrap(err, "evaluating connection options")
}
return run(cmd.Context(), cmd.OutOrStdout(), args, connectionOptions, func(args []string, db *bun.DB) error {
return executor(cmd, args, db)
})
}
34 changes: 34 additions & 0 deletions components/ledger/libs/bun/bunmigrate/run_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package bunmigrate

import (
"github.com/formancehq/stack/libs/go-libs/bun/bunconnect"
"github.com/formancehq/stack/libs/go-libs/logging"
"github.com/formancehq/stack/libs/go-libs/pgtesting"
"github.com/stretchr/testify/require"
"github.com/uptrace/bun"
"os"
"testing"
)

func TestRunMigrate(t *testing.T) {
require.NoError(t, pgtesting.CreatePostgresServer())
t.Cleanup(func() {
require.NoError(t, pgtesting.DestroyPostgresServer())
})

connectionOptions := &bunconnect.ConnectionOptions{
DatabaseSourceName: pgtesting.Server().GetDatabaseDSN("testing"),
Debug: testing.Verbose(),
Writer: os.Stdout,
}
executor := func(args []string, db *bun.DB) error {
return nil
}

err := run(logging.TestingContext(), os.Stdout, []string{}, connectionOptions, executor)
require.NoError(t, err)

// Must be idempotent
err = run(logging.TestingContext(), os.Stdout, []string{}, connectionOptions, executor)
require.NoError(t, err)
}
1 change: 0 additions & 1 deletion components/operator/docs/crd.md
Original file line number Diff line number Diff line change
Expand Up @@ -683,7 +683,6 @@ _Appears in:_
| `ready` _boolean_ | |
| `info` _string_ | |
| `conditions` _[Condition](#condition) array_ | |
| `version` _string_ | |



Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,8 +167,6 @@ spec:
type: string
ready:
type: boolean
version:
type: string
type: object
type: object
served: true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,8 +165,6 @@ spec:
items:
type: string
type: array
version:
type: string
required:
- authEnabled
type: object
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,12 +165,8 @@ spec:
type: array
info:
type: string
isMigratedOnV2:
type: boolean
ready:
type: boolean
version:
type: string
type: object
type: object
served: true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,6 @@ spec:
type: boolean
temporalURI:
type: string
version:
type: string
type: object
type: object
served: true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,6 @@ spec:
type: string
ready:
type: boolean
version:
type: string
type: object
type: object
served: true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,6 @@ spec:
type: string
ready:
type: boolean
version:
type: string
type: object
type: object
served: true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,6 @@ spec:
type: string
ready:
type: boolean
version:
type: string
type: object
type: object
served: true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,6 @@ spec:
type: string
ready:
type: boolean
version:
type: string
type: object
type: object
served: true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,6 @@ spec:
type: string
ready:
type: boolean
version:
type: string
type: object
type: object
served: true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,6 @@ spec:
type: string
ready:
type: boolean
version:
type: string
type: object
type: object
served: true
Expand Down
1 change: 1 addition & 0 deletions libs/go-libs/bun/bunconnect/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ func InitFlags(flags *pflag.FlagSet) {

func ConnectionOptionsFromFlags(v *viper.Viper, output io.Writer, debug bool) (*ConnectionOptions, error) {
var connector func(string) (driver.Connector, error)

if v.GetBool(PostgresAWSEnableIAMFlag) {
cfg, err := config.LoadDefaultConfig(context.Background(), iam.LoadOptionFromViper(v))
if err != nil {
Expand Down
30 changes: 0 additions & 30 deletions libs/go-libs/bun/bunmigrate/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,8 @@ package bunmigrate

import (
"github.com/formancehq/stack/libs/go-libs/bun/bunconnect"
sharedlogging "github.com/formancehq/stack/libs/go-libs/logging"
"github.com/pkg/errors"

"github.com/formancehq/stack/libs/go-libs/service"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/uptrace/bun"
"github.com/uptrace/bun/extra/bundebug"

// Import the postgres driver.
_ "github.com/lib/pq"
)
Expand All @@ -31,26 +24,3 @@ func NewDefaultCommand(executor Executor, options ...func(command *cobra.Command
bunconnect.InitFlags(ret.Flags())
return ret
}

func Run(cmd *cobra.Command, args []string, executor Executor) error {
connectionOptions, err := bunconnect.ConnectionOptionsFromFlags(viper.GetViper(), cmd.OutOrStdout(), viper.GetBool(service.DebugFlag))
if err != nil {
return errors.Wrap(err, "evaluating connection options")
}

db, err := bunconnect.OpenSQLDB(*connectionOptions)
if err != nil {
return errors.Wrap(err, "opening database")
}
defer func() {
err := db.Close()
if err != nil {
sharedlogging.FromContext(cmd.Context()).Errorf("Closing database: %s", err)
}
}()
if viper.GetBool(service.DebugFlag) {
db.AddQueryHook(bundebug.NewQueryHook(bundebug.WithWriter(cmd.OutOrStdout())))
}

return errors.Wrap(executor(cmd, args, db), "executing migration")
}
Loading

0 comments on commit ca35653

Please sign in to comment.