Enforce unique cardId per property
All checks were successful
build-and-deploy / build-deploy (push) Successful in 34s

This commit is contained in:
androidlover5842
2026-01-28 18:22:23 +05:30
parent f082328abe
commit 914d824e51

View File

@@ -34,5 +34,39 @@ class IssuedCardSchemaFix(
logger.info("Dropping NOT NULL on issued_card.room_stay_id") logger.info("Dropping NOT NULL on issued_card.room_stay_id")
jdbcTemplate.execute("alter table issued_card alter column room_stay_id drop not null") jdbcTemplate.execute("alter table issued_card alter column room_stay_id drop not null")
} }
val uniqueIndexExists = jdbcTemplate.queryForObject(
"""
select count(*)
from pg_indexes
where schemaname = 'public'
and tablename = 'issued_card'
and indexname = 'idx_issued_card_property_card_id_unique'
""".trimIndent(),
Int::class.java
) ?: 0
if (uniqueIndexExists == 0) {
logger.info("Removing duplicate issued_card rows by property_id + card_id")
jdbcTemplate.execute(
"""
with ranked as (
select id,
row_number() over (
partition by property_id, lower(card_id)
order by issued_at desc, id desc
) as rn
from issued_card
where card_id is not null
)
delete from issued_card
where id in (select id from ranked where rn > 1)
""".trimIndent()
)
logger.info("Creating unique index on issued_card(property_id, lower(card_id))")
jdbcTemplate.execute(
"create unique index if not exists idx_issued_card_property_card_id_unique on issued_card (property_id, lower(card_id))"
)
}
} }
} }