Remove amenity schema auto-fix
All checks were successful
build-and-deploy / build-deploy (push) Successful in 27s
All checks were successful
build-and-deploy / build-deploy (push) Successful in 27s
This commit is contained in:
@@ -1,79 +0,0 @@
|
||||
package com.android.trisolarisserver.config
|
||||
|
||||
import org.slf4j.LoggerFactory
|
||||
import org.springframework.boot.ApplicationArguments
|
||||
import org.springframework.boot.ApplicationRunner
|
||||
import org.springframework.jdbc.core.JdbcTemplate
|
||||
import org.springframework.stereotype.Component
|
||||
|
||||
@Component
|
||||
class RoomAmenitySchemaFix(
|
||||
private val jdbcTemplate: JdbcTemplate
|
||||
) : ApplicationRunner {
|
||||
|
||||
private val logger = LoggerFactory.getLogger(RoomAmenitySchemaFix::class.java)
|
||||
|
||||
override fun run(args: ApplicationArguments) {
|
||||
val version = jdbcTemplate.queryForObject("select version()", String::class.java) ?: return
|
||||
if (!version.contains("PostgreSQL", ignoreCase = true)) {
|
||||
return
|
||||
}
|
||||
|
||||
val propertyIdExists = jdbcTemplate.queryForObject(
|
||||
"""
|
||||
select count(*)
|
||||
from information_schema.columns
|
||||
where table_name = 'room_amenity'
|
||||
and column_name = 'property_id'
|
||||
""".trimIndent(),
|
||||
Int::class.java
|
||||
) ?: 0
|
||||
|
||||
if (propertyIdExists > 0) {
|
||||
val isNullable = jdbcTemplate.queryForObject(
|
||||
"""
|
||||
select is_nullable
|
||||
from information_schema.columns
|
||||
where table_name = 'room_amenity'
|
||||
and column_name = 'property_id'
|
||||
""".trimIndent(),
|
||||
String::class.java
|
||||
)
|
||||
if (isNullable == "NO") {
|
||||
logger.info("Altering room_amenity.property_id to be nullable")
|
||||
jdbcTemplate.execute("alter table room_amenity alter column property_id drop not null")
|
||||
}
|
||||
}
|
||||
|
||||
val uniqueConstraints = jdbcTemplate.query(
|
||||
"""
|
||||
select tc.constraint_name, array_agg(kcu.column_name order by kcu.column_name) as columns
|
||||
from information_schema.table_constraints tc
|
||||
join information_schema.key_column_usage kcu
|
||||
on tc.constraint_name = kcu.constraint_name
|
||||
and tc.table_name = kcu.table_name
|
||||
where tc.table_name = 'room_amenity'
|
||||
and tc.constraint_type = 'UNIQUE'
|
||||
group by tc.constraint_name
|
||||
""".trimIndent()
|
||||
) { rs, _ ->
|
||||
rs.getString("constraint_name") to (rs.getArray("columns").array as Array<*>).map { it.toString() }
|
||||
}
|
||||
|
||||
var hasNameUnique = false
|
||||
for ((name, cols) in uniqueConstraints) {
|
||||
if (cols.size == 1 && cols[0] == "name") {
|
||||
hasNameUnique = true
|
||||
}
|
||||
if (cols.size == 2 && cols.contains("property_id") && cols.contains("name")) {
|
||||
logger.info("Dropping old unique constraint {} on room_amenity(property_id, name)", name)
|
||||
jdbcTemplate.execute("""alter table room_amenity drop constraint "$name"""")
|
||||
}
|
||||
}
|
||||
|
||||
if (!hasNameUnique) {
|
||||
logger.info("Adding unique constraint on room_amenity(name)")
|
||||
jdbcTemplate.execute("alter table room_amenity add constraint room_amenity_name_key unique (name)")
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user