39 lines
1.2 KiB
Go
39 lines
1.2 KiB
Go
package migrations
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/uptrace/bun"
|
|
"github.com/uptrace/bun/dialect"
|
|
)
|
|
|
|
//nolint:lll
|
|
func init() {
|
|
migrations.MustRegister(func(ctx context.Context, db *bun.DB) error {
|
|
var err error
|
|
|
|
if db.Dialect().Name() == dialect.PG {
|
|
// hash_record_extended is Postgres specific
|
|
// https://dba.stackexchange.com/questions/299098/why-doesnt-my-unique-constraint-trigger/299107#299107
|
|
_, err = db.ExecContext(
|
|
ctx,
|
|
`create unique index concurrently if not exists ennoblements_hash_key
|
|
on ennoblements (hash_record_extended(
|
|
ROW (server_key, village_id, new_owner_id, new_tribe_id, old_owner_id, old_tribe_id, points, created_at),
|
|
0::bigint));`,
|
|
)
|
|
} else {
|
|
_, err = db.ExecContext(
|
|
ctx,
|
|
`create unique index if not exists ennoblements_hash_key
|
|
on ennoblements (server_key, coalesce(village_id, 0), coalesce(new_owner_id, 0), coalesce(new_tribe_id, 0), coalesce(old_owner_id, 0), coalesce(old_tribe_id, 0), points, created_at);`,
|
|
)
|
|
}
|
|
|
|
return err
|
|
}, func(ctx context.Context, db *bun.DB) error {
|
|
_, err := db.NewDropIndex().IfExists().Index("ennoblements_hash_key").Concurrently().Exec(ctx)
|
|
return err
|
|
})
|
|
}
|