43 lines
1.8 KiB
Kotlin
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)"
|
|
)
|
|
}
|
|
}
|
|
}
|