cleanup and add the current schema-dump

This commit is contained in:
Michael Hoennig 2025-01-28 06:13:10 +01:00
parent 0a13ce6550
commit 9c6a4263bc

View File

@ -19,34 +19,49 @@ import javax.sql.DataSource;
import static org.assertj.core.api.Assertions.assertThat;
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:
pg-sql-reset
<p>The test needs a dump, ideally from the version of the lastest prod-release:</p>
2. populate the database:
./gradlew bootRun --args='--spring.profiles.active=only-office'
<ol>
<li>1. clean the database:<br/>
<code>pg-sql-reset</code>
create the dump:
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
<li>populate the database:</br>
<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")
@DataJpaTest(properties = {
//"app.early-init.enabled=true", // Enable the early initializer
"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}"
"spring.liquibase.enabled=false" // @Sql should go first, Liquibase will be initialized programmtically
})
@ActiveProfiles("only-office")
@TestPropertySource(properties = "spring.liquibase.contexts=only-office")
@DirtiesContext
@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 {
@Autowired
@ -58,13 +73,8 @@ public class LiquibaseCompatibilityIntegrationTest extends CsvDataImport {
@BeforeEach
public void setup() throws Exception {
// Step 1: Run your custom SQL script
// final var populator = new ResourceDatabasePopulator();
// 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 schemas = em.createNativeQuery("SELECT * FROM pg_catalog.pg_tables WHERE schemaname='public'").getResultList();
assertThat(schemas).hasSize(2);
final var liquibaseScripts = em.createNativeQuery("SELECT * FROM public.databasechangelog").getResultList();
assertThat(liquibaseScripts).hasSize(286);