diff --git a/src/main/kotlin/com/android/trisolarisserver/config/RoomStaySchemaFix.kt b/src/main/kotlin/com/android/trisolarisserver/config/RoomStaySchemaFix.kt new file mode 100644 index 0000000..82aab1b --- /dev/null +++ b/src/main/kotlin/com/android/trisolarisserver/config/RoomStaySchemaFix.kt @@ -0,0 +1,35 @@ +package com.android.trisolarisserver.config + +import org.springframework.jdbc.core.JdbcTemplate +import org.springframework.stereotype.Component + +@Component +class RoomStaySchemaFix( + private val jdbcTemplate: JdbcTemplate +) : PostgresSchemaFix(jdbcTemplate) { + + override fun runPostgres(jdbcTemplate: JdbcTemplate) { + val exists = jdbcTemplate.queryForObject( + """ + select count(*) + from information_schema.table_constraints + where table_name = 'room_stay' + and constraint_name = 'room_stay_rate_source_check' + """.trimIndent(), + Int::class.java + ) ?: 0 + + if (exists > 0) { + logger.info("Updating room_stay_rate_source_check constraint") + jdbcTemplate.execute("alter table room_stay drop constraint room_stay_rate_source_check") + } + + jdbcTemplate.execute( + """ + alter table room_stay + add constraint room_stay_rate_source_check + check (rate_source in ('MANUAL','PRESET','RATE_PLAN','NEGOTIATED','OTA')) + """.trimIndent() + ) + } +}