|
| 1 | +package org.broadinstitute.consent.http.db; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 4 | +import static org.junit.jupiter.api.Assertions.assertFalse; |
| 5 | +import static org.junit.jupiter.api.Assertions.assertNull; |
| 6 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
| 7 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 8 | + |
| 9 | +import java.sql.Connection; |
| 10 | +import java.sql.DriverManager; |
| 11 | +import java.sql.ResultSet; |
| 12 | +import java.sql.SQLException; |
| 13 | +import java.sql.Statement; |
| 14 | +import liquibase.Contexts; |
| 15 | +import liquibase.LabelExpression; |
| 16 | +import liquibase.Liquibase; |
| 17 | +import liquibase.database.Database; |
| 18 | +import liquibase.database.DatabaseFactory; |
| 19 | +import liquibase.database.jvm.JdbcConnection; |
| 20 | +import liquibase.exception.LiquibaseException; |
| 21 | +import liquibase.resource.ClassLoaderResourceAccessor; |
| 22 | +import org.junit.jupiter.api.AfterAll; |
| 23 | +import org.junit.jupiter.api.BeforeAll; |
| 24 | +import org.junit.jupiter.api.BeforeEach; |
| 25 | +import org.junit.jupiter.api.Test; |
| 26 | +import org.junit.jupiter.params.ParameterizedTest; |
| 27 | +import org.junit.jupiter.params.provider.ValueSource; |
| 28 | +import org.testcontainers.containers.PostgreSQLContainer; |
| 29 | + |
| 30 | +class DatasetAliasSequenceMigrationTest { |
| 31 | + |
| 32 | + private static final String CHANGELOG = |
| 33 | + "changesets/changelog-consent-2026-08-10-dataset-alias-sequence.xml"; |
| 34 | + private static PostgreSQLContainer<?> postgres; |
| 35 | + |
| 36 | + @BeforeAll |
| 37 | + static void startPostgres() { |
| 38 | + postgres = new PostgreSQLContainer<>(DAOTestHelper.POSTGRES_IMAGE); |
| 39 | + postgres.start(); |
| 40 | + } |
| 41 | + |
| 42 | + @AfterAll |
| 43 | + static void stopPostgres() { |
| 44 | + postgres.stop(); |
| 45 | + } |
| 46 | + |
| 47 | + @BeforeEach |
| 48 | + void createPreMigrationSchema() throws SQLException { |
| 49 | + try (Connection connection = connection(); |
| 50 | + Statement statement = connection.createStatement()) { |
| 51 | + statement.execute("DROP SCHEMA public CASCADE"); |
| 52 | + statement.execute("CREATE SCHEMA public"); |
| 53 | + // Dev's legacy alias column is numeric, which exercises the setval bigint cast. |
| 54 | + statement.execute( |
| 55 | + "CREATE TABLE dataset (dataset_id bigserial PRIMARY KEY, alias numeric DEFAULT 0)"); |
| 56 | + statement.execute("INSERT INTO dataset (alias) VALUES (42), (900000), (0)"); |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + @Test |
| 61 | + void migrationPreservesAliasesAndAllocatesAboveMaximumForOldAndNewWriters() throws Exception { |
| 62 | + update(); |
| 63 | + |
| 64 | + assertEquals(42, queryLong("SELECT alias FROM dataset WHERE dataset_id = 1")); |
| 65 | + assertEquals(900000, queryLong("SELECT alias FROM dataset WHERE dataset_id = 2")); |
| 66 | + assertEquals(0, queryLong("SELECT alias FROM dataset WHERE dataset_id = 3")); |
| 67 | + |
| 68 | + // An old instance supplies its MAX(alias) + 1 result, but the compatibility trigger replaces |
| 69 | + // it. |
| 70 | + assertEquals( |
| 71 | + 900001, |
| 72 | + queryLong( |
| 73 | + "INSERT INTO dataset (alias) " |
| 74 | + + "SELECT COALESCE(MAX(alias), 0) + 1 FROM dataset RETURNING alias")); |
| 75 | + execute("DELETE FROM dataset WHERE alias = 900001"); |
| 76 | + // A new instance omits alias entirely. |
| 77 | + assertEquals(900002, queryLong("INSERT INTO dataset DEFAULT VALUES RETURNING alias")); |
| 78 | + |
| 79 | + assertThrows( |
| 80 | + SQLException.class, () -> execute("UPDATE dataset SET alias = NULL WHERE dataset_id = 1")); |
| 81 | + assertThrows( |
| 82 | + SQLException.class, () -> execute("UPDATE dataset SET alias = 42 WHERE dataset_id = 2")); |
| 83 | + assertThrows( |
| 84 | + SQLException.class, () -> execute("UPDATE dataset SET alias = -1 WHERE dataset_id = 1")); |
| 85 | + assertThrows( |
| 86 | + SQLException.class, () -> execute("UPDATE dataset SET alias = 1.5 WHERE dataset_id = 1")); |
| 87 | + assertThrows( |
| 88 | + SQLException.class, |
| 89 | + () -> execute("UPDATE dataset SET alias = 2147483648 WHERE dataset_id = 1")); |
| 90 | + execute("UPDATE dataset SET alias = 2147483647 WHERE dataset_id = 1"); |
| 91 | + assertEquals(2147483647, queryLong("SELECT alias FROM dataset WHERE dataset_id = 1")); |
| 92 | + } |
| 93 | + |
| 94 | + @Test |
| 95 | + void migrationAllocatesLastIntegerAliasWhenExistingMaximumLeavesRoom() throws Exception { |
| 96 | + execute("UPDATE dataset SET alias = 2147483646 WHERE dataset_id = 2"); |
| 97 | + |
| 98 | + update(); |
| 99 | + |
| 100 | + assertEquals(2147483647, queryLong("INSERT INTO dataset DEFAULT VALUES RETURNING alias")); |
| 101 | + assertThrows( |
| 102 | + SQLException.class, () -> queryLong("INSERT INTO dataset DEFAULT VALUES RETURNING alias")); |
| 103 | + } |
| 104 | + |
| 105 | + @ParameterizedTest |
| 106 | + @ValueSource(strings = {"NULL", "-1", "1.5", "42", "2147483647"}) |
| 107 | + void migrationRejectsUnsafeExistingAliasesBeforeChangingSchema(String unsafeAlias) |
| 108 | + throws Exception { |
| 109 | + execute("INSERT INTO dataset (alias) VALUES (" + unsafeAlias + ")"); |
| 110 | + |
| 111 | + assertThrows(LiquibaseException.class, this::update); |
| 112 | + |
| 113 | + assertNull(queryObject("SELECT to_regclass('dataset_alias_seq')")); |
| 114 | + assertFalse( |
| 115 | + queryBoolean( |
| 116 | + "SELECT EXISTS (SELECT 1 FROM pg_trigger WHERE tgname = 'dataset_alias_allocate')")); |
| 117 | + assertFalse( |
| 118 | + queryBoolean( |
| 119 | + "SELECT EXISTS (SELECT 1 FROM pg_constraint " |
| 120 | + + "WHERE conname = 'dataset_alias_valid_integer')")); |
| 121 | + } |
| 122 | + |
| 123 | + @Test |
| 124 | + void rollbackRestoresLegacyDefaultAndWriterBehavior() throws Exception { |
| 125 | + update(); |
| 126 | + rollback(); |
| 127 | + |
| 128 | + assertNull(queryObject("SELECT to_regclass('dataset_alias_seq')")); |
| 129 | + assertFalse( |
| 130 | + queryBoolean( |
| 131 | + "SELECT EXISTS (SELECT 1 FROM pg_trigger WHERE tgname = 'dataset_alias_allocate')")); |
| 132 | + assertFalse( |
| 133 | + queryBoolean( |
| 134 | + "SELECT EXISTS (SELECT 1 FROM pg_constraint " |
| 135 | + + "WHERE conname = 'dataset_alias_valid_integer')")); |
| 136 | + assertTrue( |
| 137 | + queryBoolean( |
| 138 | + "SELECT is_nullable = 'YES' FROM information_schema.columns " |
| 139 | + + "WHERE table_schema = 'public' AND table_name = 'dataset' AND column_name = 'alias'")); |
| 140 | + assertEquals( |
| 141 | + "0", |
| 142 | + queryObject( |
| 143 | + "SELECT pg_get_expr(adbin, adrelid) FROM pg_attrdef " |
| 144 | + + "WHERE adrelid = 'dataset'::regclass AND adnum = " |
| 145 | + + "(SELECT attnum FROM pg_attribute WHERE attrelid = 'dataset'::regclass AND attname = 'alias')")); |
| 146 | + |
| 147 | + assertEquals(0, queryLong("INSERT INTO dataset DEFAULT VALUES RETURNING alias")); |
| 148 | + assertEquals(7, queryLong("INSERT INTO dataset (alias) VALUES (7) RETURNING alias")); |
| 149 | + } |
| 150 | + |
| 151 | + private static Connection connection() throws SQLException { |
| 152 | + return DriverManager.getConnection( |
| 153 | + postgres.getJdbcUrl(), postgres.getUsername(), postgres.getPassword()); |
| 154 | + } |
| 155 | + |
| 156 | + private void update() throws Exception { |
| 157 | + try (Connection connection = connection()) { |
| 158 | + Database database = |
| 159 | + DatabaseFactory.getInstance() |
| 160 | + .findCorrectDatabaseImplementation(new JdbcConnection(connection)); |
| 161 | + try (Liquibase liquibase = |
| 162 | + new Liquibase(CHANGELOG, new ClassLoaderResourceAccessor(), database)) { |
| 163 | + liquibase.update(new Contexts(), new LabelExpression()); |
| 164 | + } |
| 165 | + } |
| 166 | + } |
| 167 | + |
| 168 | + private void rollback() throws Exception { |
| 169 | + try (Connection connection = connection()) { |
| 170 | + Database database = |
| 171 | + DatabaseFactory.getInstance() |
| 172 | + .findCorrectDatabaseImplementation(new JdbcConnection(connection)); |
| 173 | + try (Liquibase liquibase = |
| 174 | + new Liquibase(CHANGELOG, new ClassLoaderResourceAccessor(), database)) { |
| 175 | + liquibase.rollback(1, new Contexts(), new LabelExpression()); |
| 176 | + } |
| 177 | + } |
| 178 | + } |
| 179 | + |
| 180 | + private void execute(String sql) throws SQLException { |
| 181 | + try (Connection connection = connection(); |
| 182 | + Statement statement = connection.createStatement()) { |
| 183 | + statement.execute(sql); |
| 184 | + } |
| 185 | + } |
| 186 | + |
| 187 | + private long queryLong(String sql) throws SQLException { |
| 188 | + return ((Number) queryObject(sql)).longValue(); |
| 189 | + } |
| 190 | + |
| 191 | + private boolean queryBoolean(String sql) throws SQLException { |
| 192 | + return (Boolean) queryObject(sql); |
| 193 | + } |
| 194 | + |
| 195 | + private Object queryObject(String sql) throws SQLException { |
| 196 | + try (Connection connection = connection(); |
| 197 | + Statement statement = connection.createStatement(); |
| 198 | + ResultSet resultSet = statement.executeQuery(sql)) { |
| 199 | + resultSet.next(); |
| 200 | + return resultSet.getObject(1); |
| 201 | + } |
| 202 | + } |
| 203 | +} |
0 commit comments