feature/test-liquibase-migration-from-a-prod-dump #152

Merged
hsh-michaelhoennig merged 9 commits from feature/test-liquibase-migration-from-a-prod-dump into master 2025-01-28 12:28:03 +01:00
Showing only changes of commit 9c6a4263bc - Show all commits

View File

@ -19,34 +19,49 @@ import javax.sql.DataSource;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
import static org.springframework.test.context.jdbc.Sql.ExecutionPhase.BEFORE_TEST_CLASS; import static org.springframework.test.context.jdbc.Sql.ExecutionPhase.BEFORE_TEST_CLASS;
/* /** Tests, if the Liquibase scripts can be applied to a database ionitialized with schemas
and test-data from a previous version.
1. clean the database: <p>The test needs a dump, ideally from the version of the lastest prod-release:</p>
pg-sql-reset
2. populate the database: <ol>
./gradlew bootRun --args='--spring.profiles.active=only-office' <li>1. clean the database:<br/>
<code>pg-sql-reset</code>
create the dump: <li>populate the database:</br>
docker exec -i hsadmin-ng-postgres /usr/bin/pg_dump --create --column-inserts --disable-dollar-quoting -U postgres postgres >src/main/resources/db/changelog/prod-schema-only-office-with-test-data-full.sql <code>./gradlew bootRun --args='--spring.profiles.active=only-office'</code>
<li>create the reference-schema SQL-file with some initializations:</li>
<pre><code>cat >src/test/resources/db/prod-only-office-schema-with-test-data.sql <<EOF
CREATE ROLE postgres;
CREATE ROLE admin;
GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA public TO admin;
CREATE ROLE restricted;
GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA public TO restricted;
EOF
</code></pre>
<li>add the dump to that reference-schema SQL-file:</p>
<pre><code>docker exec -i hsadmin-ng-postgres /usr/bin/pg_dump \
--column-inserts --disable-dollar-quoting -U postgres postgres \
>>src/test/resources/db/prod-only-office-schema-with-test-data.sql
</code></pre>
</ol>
<p>The generated dump has to be committed to git and will be used in future test-runs
until it gets replaced at the next release.</p>
*/ */
@Tag("importOfficeData") @Tag("importOfficeData")
@DataJpaTest(properties = { @DataJpaTest(properties = {
//"app.early-init.enabled=true", // Enable the early initializer "spring.liquibase.enabled=false" // @Sql should go first, Liquibase will be initialized programmtically
"spring.liquibase.enabled=false", // @Sql should go first, Liquibase will be initialized programmtically
"liquibase.liquibaseSchemaName=public",
"spring.datasource.url=${HSADMINNG_POSTGRES_JDBC_URL:jdbc:tc:postgresql:15.5-bookworm:///liquibaseCompatTC}",
"spring.datasource.username=${HSADMINNG_POSTGRES_ADMIN_USERNAME:admin}",
"spring.datasource.password=${HSADMINNG_POSTGRES_ADMIN_PASSWORD:password}",
"hsadminng.postgres.admin.username=postgres",
"hsadminng.postgres.restricted.username=restricted",
"hsadminng.superuser=${HSADMINNG_SUPERUSER:superuser-alex@hostsharing.net}"
}) })
@ActiveProfiles("only-office") @ActiveProfiles("only-office")
@TestPropertySource(properties = "spring.liquibase.contexts=only-office") @TestPropertySource(properties = "spring.liquibase.contexts=only-office")
@DirtiesContext @DirtiesContext
@Import({ Context.class, JpaAttempt.class, LiquibaseConfig.class}) @Import({ Context.class, JpaAttempt.class, LiquibaseConfig.class})
@Sql(value = "classpath:db/changelog/prod-schema-only-office-with-test-data-full.sql", executionPhase = BEFORE_TEST_CLASS) @Sql(value = "/db/prod-only-office-schema-with-test-data.sql", executionPhase = BEFORE_TEST_CLASS)
public class LiquibaseCompatibilityIntegrationTest extends CsvDataImport { public class LiquibaseCompatibilityIntegrationTest extends CsvDataImport {
@Autowired @Autowired
@ -58,13 +73,8 @@ public class LiquibaseCompatibilityIntegrationTest extends CsvDataImport {
@BeforeEach @BeforeEach
public void setup() throws Exception { public void setup() throws Exception {
// Step 1: Run your custom SQL script final var schemas = em.createNativeQuery("SELECT * FROM pg_catalog.pg_tables WHERE schemaname='public'").getResultList();
// final var populator = new ResourceDatabasePopulator(); assertThat(schemas).hasSize(2);
// populator.addScript(new ClassPathResource("db/changelog/prod-schema-only-office-with-test-data-part.sql"));
// populator.execute(dataSource);
final var subjects = em.createNativeQuery("SELECT * FROM pg_catalog.pg_tables WHERE schemaname='public'").getResultList();
assertThat(subjects).hasSize(2);
final var liquibaseScripts = em.createNativeQuery("SELECT * FROM public.databasechangelog").getResultList(); final var liquibaseScripts = em.createNativeQuery("SELECT * FROM public.databasechangelog").getResultList();
assertThat(liquibaseScripts).hasSize(286); assertThat(liquibaseScripts).hasSize(286);