Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fetching datasource properties in init function of testcontainers

I have configured test containers using jdbc url and trying to use init function to run flyway. Is there any example were the following is achieved. I am struggling to fetch the datasource properties dynamically in init function

public class JDBCDriverTest {
    public static void sampleInitFunction(Connection connection) throws SQLException {
        Flyway flyway = Flyway.configure().dataSource("", "", "").load();
        flyway.migrate();
    }
}
like image 780
Harshad Thombare Avatar asked May 28 '26 13:05

Harshad Thombare


1 Answers

I achieved it in following way

public class JDBCDriverTest {
   public static void sampleInitFunction(Connection connection) throws SQLException {
       Properties datasourceProperties = ((ConnectionImpl) connection).getProperties();
       String user = datasourceProperties.getProperty("user");
       String password = (String) datasourceProperties.get("password");
       String url = ((ConnectionImpl) connection).getURL();
       Flyway flyway = Flyway.configure().dataSource(url, user, password).load();
       flyway.migrate();
   }
}
like image 196
Harshad Thombare Avatar answered Jun 03 '26 01:06

Harshad Thombare



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!