如何在Oracle Testcontainer中使用liquibase和bootique的多个模式?

s1ag04yj  于 2023-10-16  发布在  Oracle
关注(0)|答案(1)|浏览(96)

我有一个应用程序,可以访问多个模式并在模式之间复制/转换数据。对于每个模式,我都嵌套了liquibase变更日志,并使用Bootique允许变更日志从类路径导入其他变更日志。
oracle testcontainer没有API方法来定义多个模式。如何定义多个架构并将liquibase变更日志应用于每个架构?
当我尝试应用多个liquibase更新日志时,测试确实执行了它们,但偶然发现在更新日志中使用了重复的标识符:

@BQTestTool(BQTestScope.GLOBAL)
static final DbTester<TcDbTester> tester = TcDbTester
        .db(oracleContainer, "USER", "USERPASS")
        .initDBWithLiquibaseChangelog("classpath:changelogSchema1.yml")
        .initDBWithLiquibaseChangelog("classpath:changelogSchema2.yml");
wwtsj6pe

wwtsj6pe1#

在OracleContainer中,可以使用构建器方法withCopyFileToContainer将init脚本作为SYSDBA运行。
因此,当我使用该方法从 test/resources/sql/initAdditionalSchemas.sql 应用以下init脚本时:

ALTER SESSION SET CONTAINER=XEPDB1;
CREATE USER SCHEMA2 IDENTIFIED BY SCHEMA2PASS QUOTA UNLIMITED ON USERS;
GRANT CREATE SESSION TO SCHEMA2;
GRANT CONNECT, RESOURCE, DBA TO SCHEMA2;

则此测试成功:

@BQTest
@Testcontainers
public class MyTestContainerTest {

    @Container
    static final OracleContainer oracleContainer = new OracleContainer("gvenzl/oracle-xe:21-slim-faststart")
            // no database name, use XEPDB1
            .withUsername("SCHEMA1") // define first schema
            .withPassword("SCHEMA1PASS")
            .withCopyFileToContainer( // define additional schemas
                    MountableFile.forClasspathResource("sql/initAdditionalSchemas.sql"),
                    "/container-entrypoint-initdb.d/initAdditionalSchemas.sql");

    static {
        oracleContainer.start();
    }

    @BQTestTool(BQTestScope.GLOBAL)
    static final DbTester<TcDbTester> testerSchema1 = TcDbTester
            .db(oracleContainer, "SCHEMA1", "SCHEMA1PASS")
            .initDBWithLiquibaseChangelog("classpath:changelogSchema1.yml");

    @BQTestTool(BQTestScope.GLOBAL)
    static final DbTester<TcDbTester> testerSchema2 = TcDbTester
            .db(oracleContainer, "SCHEMA2", "SCHEMA2PASS")
            .initDBWithLiquibaseChangelog("classpath:changelogSchema2.yml");

    @Test
    public void canAccessTablesFromTwoSchemas() {
        Table schema1Table = testerSchema1.getTable("TABLE_FROM_SCHEMA1");
        assertEquals("TABLE_FROM_SCHEMA1", schema1Table.getMetadata().getName().getTable());
        Table schema2Table = testerSchema2.getTable("TABLE_FROM_SCHEMA2");
        assertEquals("TABLE_FROM_SCHEMA2", schema2Table.getMetadata().getName().getTable());
    }
}

相关问题