fix: rely on direct snapshot schema update
parent
17e2f3a59b
commit
277a8a6fb8
@ -1,43 +0,0 @@
|
|||||||
package com.yau.digitalrmb.identity.infrastructure.persistence;
|
|
||||||
|
|
||||||
import javax.annotation.PostConstruct;
|
|
||||||
import org.springframework.jdbc.core.JdbcTemplate;
|
|
||||||
import org.springframework.stereotype.Component;
|
|
||||||
|
|
||||||
@Component
|
|
||||||
public class PlatformUserSnapshotSchemaUpgrade {
|
|
||||||
private static final String[][] PROFILE_COLUMNS = {
|
|
||||||
{"school_id", "VARCHAR(64) NULL"},
|
|
||||||
{"school_name", "VARCHAR(128) NULL"},
|
|
||||||
{"college_id", "VARCHAR(64) NULL"},
|
|
||||||
{"college_name", "VARCHAR(128) NULL"},
|
|
||||||
{"major_id", "VARCHAR(64) NULL"},
|
|
||||||
{"major_name", "VARCHAR(128) NULL"},
|
|
||||||
{"role_id", "BIGINT NULL"},
|
|
||||||
{"class_id", "VARCHAR(64) NULL"},
|
|
||||||
{"class_name", "VARCHAR(128) NULL"},
|
|
||||||
{"student_id", "VARCHAR(64) NULL"}
|
|
||||||
};
|
|
||||||
|
|
||||||
private final JdbcTemplate jdbcTemplate;
|
|
||||||
|
|
||||||
public PlatformUserSnapshotSchemaUpgrade(JdbcTemplate jdbcTemplate) {
|
|
||||||
this.jdbcTemplate = jdbcTemplate;
|
|
||||||
}
|
|
||||||
|
|
||||||
@PostConstruct
|
|
||||||
public void ensureColumns() {
|
|
||||||
for (String[] column : PROFILE_COLUMNS) {
|
|
||||||
if (!columnExists(column[0])) {
|
|
||||||
jdbcTemplate.execute("ALTER TABLE platform_user_snapshot ADD COLUMN " + column[0] + " " + column[1]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private boolean columnExists(String columnName) {
|
|
||||||
Integer count = jdbcTemplate.queryForObject("SELECT COUNT(*) FROM INFORMATION_SCHEMA.COLUMNS "
|
|
||||||
+ "WHERE LOWER(TABLE_NAME) = 'platform_user_snapshot' AND LOWER(COLUMN_NAME) = ?", Integer.class,
|
|
||||||
columnName);
|
|
||||||
return count != null && count > 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -1,28 +0,0 @@
|
|||||||
package com.yau.digitalrmb.identity;
|
|
||||||
|
|
||||||
import com.yau.digitalrmb.identity.infrastructure.persistence.PlatformUserSnapshotSchemaUpgrade;
|
|
||||||
import org.junit.jupiter.api.Test;
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
|
||||||
import org.springframework.boot.test.context.SpringBootTest;
|
|
||||||
import org.springframework.jdbc.core.JdbcTemplate;
|
|
||||||
import org.springframework.test.context.ActiveProfiles;
|
|
||||||
|
|
||||||
import static org.assertj.core.api.Assertions.assertThat;
|
|
||||||
|
|
||||||
@SpringBootTest
|
|
||||||
@ActiveProfiles("test")
|
|
||||||
class PlatformUserSnapshotSchemaUpgradeTest {
|
|
||||||
@Autowired private JdbcTemplate jdbcTemplate;
|
|
||||||
@Autowired private PlatformUserSnapshotSchemaUpgrade schemaUpgrade;
|
|
||||||
|
|
||||||
@Test
|
|
||||||
void restoresMissingProfileColumnWithPortableAlterStatement() {
|
|
||||||
jdbcTemplate.execute("ALTER TABLE platform_user_snapshot DROP COLUMN student_id");
|
|
||||||
|
|
||||||
schemaUpgrade.ensureColumns();
|
|
||||||
|
|
||||||
Integer count = jdbcTemplate.queryForObject("SELECT COUNT(*) FROM INFORMATION_SCHEMA.COLUMNS "
|
|
||||||
+ "WHERE LOWER(TABLE_NAME) = 'platform_user_snapshot' AND LOWER(COLUMN_NAME) = 'student_id'", Integer.class);
|
|
||||||
assertThat(count).isEqualTo(1);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Loading…
Reference in New Issue