test Liquibase-migration from a prod-dump

This commit is contained in:
Michael Hoennig 2025-01-27 16:10:38 +01:00
parent cd01d0ab8f
commit 0a13ce6550
4 changed files with 115 additions and 4 deletions

View File

@ -3,7 +3,7 @@
-- ============================================================================
-- NUMERIC-HASH-FUNCTIONS
--changeset michael.hoennig:hash endDelimiter:--//
--changeset michael.hoennig:hash runOnChange:true validCheckSum:ANY endDelimiter:--//
-- ----------------------------------------------------------------------------
do $$

View File

@ -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
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
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 $$;

View File

@ -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
}
}

View File

@ -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
);
}
}