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

0 Answers0