将BAK文件导入SQL Server测试容器

发布于 2025-01-27 12:24:50 字数 1425 浏览 5 评论 0原文

在我的情况下,我需要在管道中自动化飞行迁移测试。

在开始执行数据库迁移的Flyway之前,我需要导入一个BAK文件来启动我的数据库结构,

如何将SQL Server备份文件导入Junit TestContainer?

这是我的代码:

@Testcontainers
@ActiveProfiles("test-containers-flyway")
class TestFlyway {
    
    @Container
    private static final MSSQLServerContainer MY_SQL_CONTAINER = new MSSQLServerContainer().acceptLicense();

    @BeforeAll
    static void before() {
        ClassicConfiguration configuration = new ClassicConfiguration();
        configuration.setDataSource(MY_SQL_CONTAINER.getJdbcUrl(), MY_SQL_CONTAINER.getUsername(), MY_SQL_CONTAINER.getPassword());
        configuration.setLocations(new Location("classpath:db/migration"));
        Flyway flyway = new Flyway(configuration);
        flyway.migrate();
    }

    @Test
    void test() throws Exception {
        assertTrue(MY_SQL_CONTAINER.isRunning());

        Connection connection = DriverManager.getConnection(MY_SQL_CONTAINER.getJdbcUrl(), MY_SQL_CONTAINER.getUsername(), MY_SQL_CONTAINER.getPassword());
        Statement statement = connection.createStatement();

        ResultSet result = statement.executeQuery("SELECT value FROM table");
        while (result.next()) {
            String value = result.getString("value");
            System.out.println(value);
        }
    }
}
  • 我认为可以将一个解决方案作为SQL Flyway DB迁移创建一个备份文件,并将其作为版本1执行

in my scenario, I need to automate the flyway migration testing in the pipelines.

before starting flyway for executing DB migration I need to import a bak file to init my database structure

how can I import the SQL server backup file to JUnit testContainer?

this is my code:

@Testcontainers
@ActiveProfiles("test-containers-flyway")
class TestFlyway {
    
    @Container
    private static final MSSQLServerContainer MY_SQL_CONTAINER = new MSSQLServerContainer().acceptLicense();

    @BeforeAll
    static void before() {
        ClassicConfiguration configuration = new ClassicConfiguration();
        configuration.setDataSource(MY_SQL_CONTAINER.getJdbcUrl(), MY_SQL_CONTAINER.getUsername(), MY_SQL_CONTAINER.getPassword());
        configuration.setLocations(new Location("classpath:db/migration"));
        Flyway flyway = new Flyway(configuration);
        flyway.migrate();
    }

    @Test
    void test() throws Exception {
        assertTrue(MY_SQL_CONTAINER.isRunning());

        Connection connection = DriverManager.getConnection(MY_SQL_CONTAINER.getJdbcUrl(), MY_SQL_CONTAINER.getUsername(), MY_SQL_CONTAINER.getPassword());
        Statement statement = connection.createStatement();

        ResultSet result = statement.executeQuery("SELECT value FROM table");
        while (result.next()) {
            String value = result.getString("value");
            System.out.println(value);
        }
    }
}
  • I think one solution can be create a backup file as SQL flyway db migration and execute it as version 1

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文