mirror of
https://github.com/hanzo-fi/stack.git
synced 2026-10-30 20:34:43 +00:00
77 lines
2.1 KiB
Go
77 lines
2.1 KiB
Go
package migrations
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/formancehq/stack/libs/go-libs/migrations"
|
|
"github.com/uptrace/bun"
|
|
)
|
|
|
|
func Migrate(ctx context.Context, db *bun.DB) error {
|
|
migrator := migrations.NewMigrator()
|
|
registerMigrations(migrator)
|
|
|
|
return migrator.Up(ctx, db)
|
|
}
|
|
|
|
func registerMigrations(migrator *migrations.Migrator) {
|
|
migrator.RegisterMigrations(
|
|
migrations.Migration{
|
|
Up: func(tx bun.Tx) error {
|
|
_, err := tx.Exec(`
|
|
CREATE SCHEMA IF NOT EXISTS reconciliations;
|
|
|
|
CREATE TABLE IF NOT EXISTS reconciliations.policy (
|
|
id uuid NOT NULL,
|
|
created_at timestamp with time zone NOT NULL,
|
|
name text NOT NULL,
|
|
ledger_name text NOT NULL,
|
|
ledger_query jsonb NOT NULL,
|
|
payments_pool_id uuid NOT NULL,
|
|
CONSTRAINT policy_pk PRIMARY KEY (id)
|
|
);
|
|
|
|
CREATE TABLE IF NOT EXISTS reconciliations.reconciliation (
|
|
id uuid NOT NULL,
|
|
policy_id uuid NOT NULL,
|
|
created_at timestamp with time zone NOT NULL UNIQUE,
|
|
reconciled_at timestamp with time zone,
|
|
status text NOT NULL,
|
|
ledger_balances jsonb NOT NULL,
|
|
payments_balances jsonb NOT NULL,
|
|
error text,
|
|
CONSTRAINT reconciliation_pk PRIMARY KEY (id)
|
|
);
|
|
|
|
ALTER TABLE reconciliations.reconciliation DROP CONSTRAINT IF EXISTS reconciliation_policy_fk;
|
|
ALTER TABLE reconciliations.reconciliation ADD CONSTRAINT reconciliation_policy_fk
|
|
FOREIGN KEY (policy_id)
|
|
REFERENCES reconciliations.policy (id)
|
|
ON DELETE CASCADE
|
|
NOT DEFERRABLE
|
|
INITIALLY IMMEDIATE
|
|
;
|
|
`)
|
|
return err
|
|
},
|
|
},
|
|
migrations.Migration{
|
|
Up: func(tx bun.Tx) error {
|
|
_, err := tx.Exec(`
|
|
ALTER TABLE reconciliations.reconciliation RENAME COLUMN reconciled_at TO reconciled_at_ledger;
|
|
ALTER TABLE reconciliations.reconciliation ADD COLUMN reconciled_at_payments timestamp with time zone;
|
|
`)
|
|
return err
|
|
},
|
|
},
|
|
migrations.Migration{
|
|
Up: func(tx bun.Tx) error {
|
|
_, err := tx.Exec(`
|
|
ALTER TABLE reconciliations.reconciliation ADD COLUMN drift_balances jsonb;
|
|
`)
|
|
return err
|
|
},
|
|
},
|
|
)
|
|
}
|