将BAK文件导入SQL Server测试容器
在我的情况下,我需要在管道中自动化飞行迁移测试。
在开始执行数据库迁移的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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论