45 lines
1.5 KiB
Kotlin
45 lines
1.5 KiB
Kotlin
package com.android.trisolarisserver.config
|
|
|
|
import org.springframework.jdbc.core.JdbcTemplate
|
|
import org.springframework.stereotype.Component
|
|
|
|
@Component
|
|
class IssuedCardSchemaFix(
|
|
private val jdbcTemplate: JdbcTemplate
|
|
) : PostgresSchemaFix(jdbcTemplate) {
|
|
|
|
override fun runPostgres(jdbcTemplate: JdbcTemplate) {
|
|
val isNullable = jdbcTemplate.queryForObject(
|
|
"""
|
|
select count(*)
|
|
from information_schema.columns
|
|
where table_name = 'issued_card'
|
|
and column_name = 'room_stay_id'
|
|
and is_nullable = 'YES'
|
|
""".trimIndent(),
|
|
Int::class.java
|
|
) ?: 0
|
|
|
|
if (isNullable == 0) {
|
|
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")
|
|
}
|
|
|
|
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("Dropping unique index on issued_card(property_id, lower(card_id))")
|
|
jdbcTemplate.execute("drop index if exists idx_issued_card_property_card_id_unique")
|
|
}
|
|
}
|
|
}
|