-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: migration from mongo to postgres data storage (#65)
* feat: migration from mongo to postgres data storage Signed-off-by: Lawrence Zawila <[email protected]> * fix: move models & migrations to app root Signed-off-by: Lawrence Zawila <[email protected]> * fix: lint & env var support Signed-off-by: Lawrence Zawila <[email protected]> * fix: payment enums order Signed-off-by: Lawrence Zawila <[email protected]> * fix: uint type assertion Signed-off-by: Lawrence Zawila <[email protected]> * feat: update docker-compose * feat: Update to PG14 * feat: storage mappings and usage flow adjustments Signed-off-by: Lawrence Zawila <[email protected]> * fix: drop type assertion to lower integer bounds Signed-off-by: Lawrence Zawila <[email protected]> * fix: kafka message format Signed-off-by: Lawrence Zawila <[email protected]> * fix: tasks response formatting Signed-off-by: Lawrence Zawila <[email protected]> * fix: missing provider in kafka message Signed-off-by: Lawrence Zawila <[email protected]> * fix: object instead of an array for payments kafka message Signed-off-by: Lawrence Zawila <[email protected]> * feat: drop adjustments from kafka message Signed-off-by: Lawrence Zawila <[email protected]> * feat: drop raw data in kafka message Signed-off-by: Lawrence Zawila <[email protected]> * fix: payment selection by id Signed-off-by: Lawrence Zawila <[email protected]> * feat: migration from mongo to postgres data storage Signed-off-by: Lawrence Zawila <[email protected]> * fix: move models & migrations to app root Signed-off-by: Lawrence Zawila <[email protected]> * fix: lint & env var support Signed-off-by: Lawrence Zawila <[email protected]> * fix: payment enums order Signed-off-by: Lawrence Zawila <[email protected]> * fix: uint type assertion Signed-off-by: Lawrence Zawila <[email protected]> * feat: update docker-compose * feat: Update to PG14 * feat: storage mappings and usage flow adjustments Signed-off-by: Lawrence Zawila <[email protected]> * fix: drop type assertion to lower integer bounds Signed-off-by: Lawrence Zawila <[email protected]> * fix: kafka message format Signed-off-by: Lawrence Zawila <[email protected]> * fix: tasks response formatting Signed-off-by: Lawrence Zawila <[email protected]> * fix: missing provider in kafka message Signed-off-by: Lawrence Zawila <[email protected]> * fix: object instead of an array for payments kafka message Signed-off-by: Lawrence Zawila <[email protected]> * feat: drop adjustments from kafka message Signed-off-by: Lawrence Zawila <[email protected]> * feat: drop raw data in kafka message Signed-off-by: Lawrence Zawila <[email protected]> * fix: payment selection by id Signed-off-by: Lawrence Zawila <[email protected]> * fix: router paths for connectors Signed-off-by: Lawrence Zawila <[email protected]> * fix: case insensitivity for connector paths Signed-off-by: Lawrence Zawila <[email protected]> * fix: default to empty array instead of null in json responses Signed-off-by: Lawrence Zawila <[email protected]> Signed-off-by: Lawrence Zawila <[email protected]> Co-authored-by: Maxence Maireaux <[email protected]>
- Loading branch information
1 parent
587160e
commit 655feec
Showing
153 changed files
with
3,212 additions
and
2,991 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
|
||
"github.com/spf13/viper" | ||
|
||
// allow blank import to initiate migrations. | ||
_ "github.com/formancehq/payments/internal/app/migrations" | ||
_ "github.com/lib/pq" | ||
|
||
"github.com/pressly/goose/v3" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
func newMigrate() *cobra.Command { | ||
return &cobra.Command{ | ||
Use: "migrate", | ||
Short: "Run migrations", | ||
RunE: runMigrate, | ||
} | ||
} | ||
|
||
// Usage: `go run cmd/main.go migrate --postgres-uri {uri} {command}` | ||
/* | ||
Commands: | ||
up Migrate the DB to the most recent version available | ||
up-by-one Migrate the DB up by 1 | ||
up-to VERSION Migrate the DB to a specific VERSION | ||
down Roll back the version by 1 | ||
down-to VERSION Roll back to a specific VERSION | ||
redo Re-run the latest migration | ||
reset Roll back all migrations | ||
status Dump the migration status for the current DB | ||
version Print the current version of the database | ||
create NAME [sql|go] Creates new migration file with the current timestamp | ||
fix Apply sequential ordering to migrations | ||
*/ | ||
|
||
func runMigrate(cmd *cobra.Command, args []string) error { | ||
postgresURI := viper.GetString(postgresURIFlag) | ||
if postgresURI == "" { | ||
postgresURI = cmd.Flag(postgresURIFlag).Value.String() | ||
} | ||
|
||
if postgresURI == "" { | ||
return fmt.Errorf("postgres uri is not set") | ||
} | ||
|
||
database, err := goose.OpenDBWithDriver("postgres", postgresURI) | ||
if err != nil { | ||
return fmt.Errorf("failed to open database: %w", err) | ||
} | ||
|
||
defer func() { | ||
if err = database.Close(); err != nil { | ||
log.Fatalf("failed to close DB: %v\n", err) | ||
} | ||
}() | ||
|
||
if len(args) == 0 { | ||
return fmt.Errorf("missing migration direction") | ||
} | ||
|
||
command := args[0] | ||
|
||
if err = goose.Run(command, database, ".", args[1:]...); err != nil { | ||
log.Printf("migrate %v: %v", command, err) | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,40 @@ | ||
version: '3.8' | ||
volumes: | ||
postgres: | ||
|
||
services: | ||
mongodb: | ||
image: bitnami/mongodb:4.4 | ||
environment: | ||
MONGODB_REPLICA_SET_MODE: primary | ||
MONGODB_REPLICA_SET_KEY: abcdef | ||
MONGODB_ADVERTISED_HOSTNAME: mongodb | ||
MONGODB_ROOT_PASSWORD: root | ||
postgres: | ||
image: "postgres:14-alpine" | ||
healthcheck: | ||
test: ["CMD-SHELL", "pg_isready -U payments"] | ||
interval: 10s | ||
timeout: 5s | ||
retries: 5 | ||
ports: | ||
- "27017:27017/tcp" | ||
- "5432:5432" | ||
environment: | ||
POSTGRES_USER: "payments" | ||
POSTGRES_PASSWORD: "payments" | ||
POSTGRES_DB: "payments" | ||
PGDATA: /data/postgres | ||
volumes: | ||
- postgres:/data/postgres | ||
|
||
payments: | ||
image: golang:1.19.3-alpine3.16 | ||
command: go run ./ migrate up && go run ./ server | ||
healthcheck: | ||
test: [ "CMD", "curl", "-f", "http://127.0.0.1:8080/_healthcheck" ] | ||
interval: 10s | ||
timeout: 5s | ||
retries: 5 | ||
depends_on: | ||
- mongodb | ||
command: | ||
- go | ||
- run | ||
- main.go | ||
- server | ||
- postgres | ||
ports: | ||
- "8080:8080" | ||
volumes: | ||
- .:/src | ||
working_dir: /src | ||
- .:/app | ||
working_dir: /app | ||
environment: | ||
DEBUG: ${DEBUG:-"true"} | ||
MONGODB_URI: mongodb://root:root@mongodb:27017 | ||
POSTGRES_URI: postgres://payments:payments@postgres/payments?sslmode=disable |
Oops, something went wrong.