Files
TrisolarisServer/src/main/kotlin/com/android/trisolarisserver/config/RatePlanSchemaFix.kt
androidlover5842 3546b2de62
All checks were successful
build-and-deploy / build-deploy (push) Successful in 35s
Allow rate plan code per room type
2026-01-29 05:35:19 +05:30

43 lines
1.8 KiB
Kotlin

package com.android.trisolarisserver.config
import org.springframework.jdbc.core.JdbcTemplate
import org.springframework.stereotype.Component
@Component
class RatePlanSchemaFix(
private val jdbcTemplate: JdbcTemplate
) : PostgresSchemaFix(jdbcTemplate) {
override fun runPostgres(jdbcTemplate: JdbcTemplate) {
val constraints = jdbcTemplate.query(
"""
select tc.constraint_name,
array_agg(kcu.column_name order by kcu.ordinal_position) as cols
from information_schema.table_constraints tc
join information_schema.key_column_usage kcu
on tc.constraint_name = kcu.constraint_name
and tc.table_schema = kcu.table_schema
where tc.table_name = 'rate_plan'
and tc.constraint_type = 'UNIQUE'
group by tc.constraint_name
""".trimIndent()
) { rs, _ ->
rs.getString("constraint_name") to (rs.getArray("cols").array as Array<*>).map { it.toString() }
}
val oldConstraint = constraints.firstOrNull { it.second == listOf("property_id", "code") }
if (oldConstraint != null) {
logger.info("Dropping old unique constraint on rate_plan(property_id, code)")
jdbcTemplate.execute("alter table rate_plan drop constraint if exists ${oldConstraint.first}")
}
val hasNew = constraints.any { it.second == listOf("property_id", "room_type_id", "code") }
if (!hasNew) {
logger.info("Adding unique constraint on rate_plan(property_id, room_type_id, code)")
jdbcTemplate.execute(
"alter table rate_plan add constraint rate_plan_property_roomtype_code_key unique (property_id, room_type_id, code)"
)
}
}
}