37 lines
956 B
JavaScript
37 lines
956 B
JavaScript
/// <reference path="../pb_data/types.d.ts" />
|
|
migrate((app) => {
|
|
// Dedupe before adding the unique index: keep the best row per
|
|
// (player, badge) — earned first, then highest progress, then newest.
|
|
app.db().newQuery(`
|
|
DELETE FROM badge_progress
|
|
WHERE id NOT IN (
|
|
SELECT id FROM (
|
|
SELECT id, ROW_NUMBER() OVER (
|
|
PARTITION BY player, badge
|
|
ORDER BY earned DESC, progress DESC, updated DESC
|
|
) AS rn
|
|
FROM badge_progress
|
|
)
|
|
WHERE rn = 1
|
|
)
|
|
`).execute()
|
|
|
|
const collection = app.findCollectionByNameOrId("pbc_1063824264")
|
|
|
|
unmarshal({
|
|
"indexes": [
|
|
"CREATE UNIQUE INDEX `idx_badge_progress_player_badge` ON `badge_progress` (`player`, `badge`)"
|
|
]
|
|
}, collection)
|
|
|
|
return app.save(collection)
|
|
}, (app) => {
|
|
const collection = app.findCollectionByNameOrId("pbc_1063824264")
|
|
|
|
unmarshal({
|
|
"indexes": []
|
|
}, collection)
|
|
|
|
return app.save(collection)
|
|
})
|