From 0a13ce65508286cca9c5f4182648a2926e50b658 Mon Sep 17 00:00:00 2001 From: Michael Hoennig Date: Mon, 27 Jan 2025 16:10:38 +0100 Subject: [PATCH] test Liquibase-migration from a prod-dump --- .../0-base/009-check-environment.sql | 2 +- .../db/changelog/1-rbac/1050-rbac-base.sql | 11 ++- ...LiquibaseCompatibilityIntegrationTest.java | 80 +++++++++++++++++++ .../hs/migration/LiquibaseConfig.java | 26 ++++++ 4 files changed, 115 insertions(+), 4 deletions(-) create mode 100644 src/test/java/net/hostsharing/hsadminng/hs/migration/LiquibaseCompatibilityIntegrationTest.java create mode 100644 src/test/java/net/hostsharing/hsadminng/hs/migration/LiquibaseConfig.java diff --git a/src/main/resources/db/changelog/0-base/009-check-environment.sql b/src/main/resources/db/changelog/0-base/009-check-environment.sql index d4a2f867..8ee97f3b 100644 --- a/src/main/resources/db/changelog/0-base/009-check-environment.sql +++ b/src/main/resources/db/changelog/0-base/009-check-environment.sql @@ -3,7 +3,7 @@ -- ============================================================================ -- NUMERIC-HASH-FUNCTIONS ---changeset michael.hoennig:hash endDelimiter:--// +--changeset michael.hoennig:hash runOnChange:true validCheckSum:ANY endDelimiter:--// -- ---------------------------------------------------------------------------- do $$ diff --git a/src/main/resources/db/changelog/1-rbac/1050-rbac-base.sql b/src/main/resources/db/changelog/1-rbac/1050-rbac-base.sql index 04bd36d2..680aa4a0 100644 --- a/src/main/resources/db/changelog/1-rbac/1050-rbac-base.sql +++ b/src/main/resources/db/changelog/1-rbac/1050-rbac-base.sql @@ -870,18 +870,23 @@ $$; -- ============================================================================ ---changeset michael.hoennig:rbac-base-PGSQL-ROLES context:!external-db endDelimiter:--// +--changeset michael.hoennig:rbac-base-PGSQL-ROLES runOnChange:true validCheckSum:ANY context:!external-db endDelimiter:--// -- ---------------------------------------------------------------------------- do $$ begin if '${HSADMINNG_POSTGRES_ADMIN_USERNAME}'='admin' then - create role admin; + if not exists (select from pg_catalog.pg_roles where rolname = 'admin') then + create role admin; + end if; grant all privileges on all tables in schema public to admin; end if; if '${HSADMINNG_POSTGRES_RESTRICTED_USERNAME}'='restricted' then - create role restricted; + if not exists (select from pg_catalog.pg_roles where rolname = 'restricted') then + create role restricted; + end if; + grant all privileges on all tables in schema public to restricted; end if; end $$; diff --git a/src/test/java/net/hostsharing/hsadminng/hs/migration/LiquibaseCompatibilityIntegrationTest.java b/src/test/java/net/hostsharing/hsadminng/hs/migration/LiquibaseCompatibilityIntegrationTest.java new file mode 100644 index 00000000..83795643 --- /dev/null +++ b/src/test/java/net/hostsharing/hsadminng/hs/migration/LiquibaseCompatibilityIntegrationTest.java @@ -0,0 +1,80 @@ +package net.hostsharing.hsadminng.hs.migration; + +import liquibase.Liquibase; +import net.hostsharing.hsadminng.context.Context; +import net.hostsharing.hsadminng.rbac.test.JpaAttempt; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Tag; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; +import org.springframework.context.annotation.Import; +import org.springframework.test.annotation.DirtiesContext; +import org.springframework.test.context.ActiveProfiles; +import org.springframework.test.context.TestPropertySource; +import org.springframework.test.context.jdbc.Sql; + +import javax.sql.DataSource; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.springframework.test.context.jdbc.Sql.ExecutionPhase.BEFORE_TEST_CLASS; + +/* + +1. clean the database: + pg-sql-reset + +2. populate the database: + ./gradlew bootRun --args='--spring.profiles.active=only-office' + +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 +*/ +@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}" +}) +@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) +public class LiquibaseCompatibilityIntegrationTest extends CsvDataImport { + + @Autowired + private DataSource dataSource; + + @Autowired + private Liquibase liquibase; + + @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 liquibaseScripts = em.createNativeQuery("SELECT * FROM public.databasechangelog").getResultList(); + assertThat(liquibaseScripts).hasSize(286); + + // Step 2: Run Liquibase migrations + liquibase.update(new liquibase.Contexts(), new liquibase.LabelExpression()); + } + + @Test + void test() { + // FIXME: check if changes got applied + } +} diff --git a/src/test/java/net/hostsharing/hsadminng/hs/migration/LiquibaseConfig.java b/src/test/java/net/hostsharing/hsadminng/hs/migration/LiquibaseConfig.java new file mode 100644 index 00000000..91aeb70a --- /dev/null +++ b/src/test/java/net/hostsharing/hsadminng/hs/migration/LiquibaseConfig.java @@ -0,0 +1,26 @@ +package net.hostsharing.hsadminng.hs.migration; + +import liquibase.Liquibase; +import liquibase.database.DatabaseFactory; +import liquibase.database.jvm.JdbcConnection; +import liquibase.resource.ClassLoaderResourceAccessor; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +import javax.sql.DataSource; + +@Configuration +public class LiquibaseConfig { + + @Bean + public Liquibase liquibase(DataSource dataSource) throws Exception { + final var connection = dataSource.getConnection(); + final var database = DatabaseFactory.getInstance() + .findCorrectDatabaseImplementation(new JdbcConnection(connection)); + return new Liquibase( + "db/changelog/db.changelog-master.yaml", // Path to your Liquibase changelog + new ClassLoaderResourceAccessor(), + database + ); + } +}