2023-12-20 07:03:03 +00:00
|
|
|
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 {
|
2024-01-06 09:26:56 +00:00
|
|
|
var err error
|
2023-12-20 07:03:03 +00:00
|
|
|
|
2024-01-06 09:26:56 +00:00
|
|
|
//nolint:exhaustive
|
|
|
|
switch db.Dialect().Name() {
|
|
|
|
case 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 tribe_changes_hash_key
|
2023-12-20 07:03:03 +00:00
|
|
|
on tribe_changes (hash_record_extended(
|
|
|
|
ROW (player_id, new_tribe_id, old_tribe_id, server_key, date_trunc('hours'::text, (created_at AT TIME ZONE 'UTC'::text))),
|
|
|
|
0::bigint));`,
|
2024-01-06 09:26:56 +00:00
|
|
|
)
|
|
|
|
case dialect.SQLite:
|
|
|
|
_, err = db.ExecContext(
|
|
|
|
ctx,
|
|
|
|
`create unique index if not exists tribe_changes_hash_key
|
|
|
|
on tribe_changes (server_key, coalesce(player_id, 0), coalesce(new_tribe_id, 0), coalesce(old_tribe_id, 0), strftime('%Y-%m-%d-%H', created_at));`,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2023-12-20 07:03:03 +00:00
|
|
|
return err
|
|
|
|
}, func(ctx context.Context, db *bun.DB) error {
|
2024-01-04 10:44:36 +00:00
|
|
|
_, err := db.NewDropIndex().IfExists().Index("tribe_changes_hash_key").Concurrently().Exec(ctx)
|
2023-12-20 07:03:03 +00:00
|
|
|
return err
|
|
|
|
})
|
|
|
|
}
|