mirror of
https://github.com/hanzo-fi/go-libs.git
synced 2026-10-30 20:53:44 +00:00
52 lines
1.1 KiB
Go
52 lines
1.1 KiB
Go
package storage
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
|
|
"github.com/uptrace/bun"
|
|
|
|
"github.com/hanzo-fi/go-libs/v5/pkg/storage/migrations"
|
|
)
|
|
|
|
func registerMigrations(migrator *migrations.Migrator, schema string) {
|
|
migrator.RegisterMigrations(
|
|
migrations.Migration{
|
|
Up: func(ctx context.Context, db bun.IDB) error {
|
|
return db.RunInTx(ctx, &sql.TxOptions{}, func(ctx context.Context, tx bun.Tx) error {
|
|
_, err := tx.Exec("set search_path = ?", schema)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
_, err = tx.Exec(initialSchema)
|
|
return err
|
|
})
|
|
},
|
|
},
|
|
)
|
|
}
|
|
|
|
func Migrate(ctx context.Context, schema string, db *bun.DB) error {
|
|
migrator := migrations.NewMigrator(
|
|
db,
|
|
migrations.WithTableName("circuit_breaker_migrations"),
|
|
)
|
|
|
|
registerMigrations(migrator, schema)
|
|
|
|
return migrator.Up(ctx)
|
|
}
|
|
|
|
const initialSchema = `
|
|
CREATE TABLE IF NOT EXISTS "circuit_breaker" (
|
|
id bigserial NOT NULL,
|
|
created_at timestamp with time zone NOT NULL,
|
|
topic text NOT NULL,
|
|
data bytea NOT NULL,
|
|
metadata jsonb,
|
|
PRIMARY KEY ("id")
|
|
);
|
|
|
|
CREATE INDEX IF NOT EXISTS "circuit_breaker_creation_date_idx" ON "circuit_breaker" ("created_at" ASC);
|
|
`
|