I have the following scripted out and it works, but it was suggested that I try using testcontainers.
function die { mvn docker:stop ; exit 1; }
function initializeDatabase {
docker exec -it mysql-1 mysql -uroot -proot \
-e "DROP USER IF EXISTS 'myuser'@'%';" \
-e "DROP DATABASE IF EXISTS mydb;" \
-e "CREATE DATABASE mydb;" \
-e "CREATE USER 'myuser'@'%' IDENTIFIED WITH mysql_native_password BY 'mypassword';" \
-e "GRANT ALL PRIVILEGES ON mydb.* TO 'myuser'@'%';"
}
mvn docker:start && sleep 10
initializeDatabase || die
mvn liquibase:update@execution1 || die
initializeDatabase || die
mvn liquibase:update@execution2 || die
initializeDatabase || die
mvn liquibase:update@execution3 || die
initializeDatabase || die
mvn liquibase:update@execution4 || die
initializeDatabase || die
mvn liquibase:update@execution5 || die
initializeDatabase || die
mvn liquibase:update@execution6 || die
mvn docker:stop
The above takes about 6 minutes to run. It starts a docker container running mysql and initializes the database and runs some liquibase migrations against it 6 different times, mimicking 6 different environments. It takes a little over 6 minutes to run. From Java, using testcontainers and the liquibase java API, it takes over 10 minutes to run. Any thoughts on how I can improve the performance of my testcontainers implementation? I don't think it's liquibase because it's the same liquibase code being invoked behind the scenes.
Below is my code:
import liquibase.Liquibase;
import liquibase.database.jvm.JdbcConnection;
import liquibase.exception.LiquibaseException;
import liquibase.resource.FileSystemResourceAccessor;
import liquibase.resource.SearchPathResourceAccessor;
import org.junit.*;
import org.testcontainers.containers.MySQLContainer;
import org.testcontainers.utility.DockerImageName;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
public class SimpleMySQLTest {
@ClassRule
public static MySQLContainer<?> mysql = new MySQLContainer<>(DockerImageName.parse("mysql:latest"))
.withEnv("TESTCONTAINERS_REUSE_ENABLED", "true")
.withEnv("TESTCONTAINERS_CHECKS_DISABLE", "true")
.withEnv("MYSQL_ROOT_PASSWORD", "root")
.withEnv("MYSQL_ROOT_HOST", "%")
.withDatabaseName("root")
.withUsername("root")
.withPassword("root");
@BeforeClass
public static void beforeClass() {
mysql.start();
}
@AfterClass
public static void afterClass() {
mysql.stop();
}
@Test
public void NAMSDev() {
try (Connection connection = DriverManager.getConnection(mysql.getJdbcUrl().replace("/root", "/rms"), "rms", "rms")) {
Liquibase liquibase = new Liquibase("src/main/sql/baseline/mysql/NAMSDev/databaseChangeLog.master.xml", new SearchPathResourceAccessor(".", new FileSystemResourceAccessor()), new JdbcConnection(connection));
liquibase.update("NAMS");
} catch (SQLException e) {
throw new RuntimeException(e);
} catch (LiquibaseException e) {
throw new RuntimeException(e);
}
}
@Before
public void initializeDatabase() {
try (Connection connection = DriverManager.getConnection(mysql.getJdbcUrl(), "root", "root")) {
Statement statement = connection.createStatement();
statement.execute("DROP USER IF EXISTS 'rms'@'%';");
statement.execute("DROP DATABASE IF EXISTS rms;");
statement.execute("CREATE DATABASE rms;");
statement.execute("CREATE USER 'rms'@'%' IDENTIFIED WITH mysql_native_password BY 'rms';");
statement.execute("GRANT ALL PRIVILEGES ON rms.* TO 'rms'@'%';");
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
@Test
public void NAMSQc() {
try (Connection connection = DriverManager.getConnection(mysql.getJdbcUrl().replace("/root", "/rms"), "rms", "rms")) {
Liquibase liquibase = new Liquibase("src/main/sql/baseline/mysql/NAMSQc/databaseChangeLog.master.xml", new SearchPathResourceAccessor(".", new FileSystemResourceAccessor()), new JdbcConnection(connection));
liquibase.update("NAMS");
} catch (SQLException e) {
throw new RuntimeException(e);
} catch (LiquibaseException e) {
throw new RuntimeException(e);
}
}
@Test
public void NAMSProd() {
try (Connection connection = DriverManager.getConnection(mysql.getJdbcUrl().replace("/root", "/rms"), "rms", "rms")) {
Liquibase liquibase = new Liquibase("src/main/sql/baseline/mysql/NAMSProd/databaseChangeLog.master.xml", new SearchPathResourceAccessor(".", new FileSystemResourceAccessor()), new JdbcConnection(connection));
liquibase.update("NAMS");
} catch (SQLException e) {
throw new RuntimeException(e);
} catch (LiquibaseException e) {
throw new RuntimeException(e);
}
}
@Test
public void NORDICSDev() {
try (Connection connection = DriverManager.getConnection(mysql.getJdbcUrl().replace("/root", "/rms"), "rms", "rms")) {
Liquibase liquibase = new Liquibase("src/main/sql/baseline/mysql/NORDICSDev/databaseChangeLog.master.xml", new SearchPathResourceAccessor(".", new FileSystemResourceAccessor()), new JdbcConnection(connection));
liquibase.update("NORDICS");
} catch (SQLException e) {
throw new RuntimeException(e);
} catch (LiquibaseException e) {
throw new RuntimeException(e);
}
}
@Test
public void NORDICSQc() {
try (Connection connection = DriverManager.getConnection(mysql.getJdbcUrl().replace("/root", "/rms"), "rms", "rms")) {
Liquibase liquibase = new Liquibase("src/main/sql/baseline/mysql/NORDICSQc/databaseChangeLog.master.xml", new SearchPathResourceAccessor(".", new FileSystemResourceAccessor()), new JdbcConnection(connection));
liquibase.update("NORDICS");
} catch (SQLException e) {
throw new RuntimeException(e);
} catch (LiquibaseException e) {
throw new RuntimeException(e);
}
}
@Test
public void NORDICSProd() {
try (Connection connection = DriverManager.getConnection(mysql.getJdbcUrl().replace("/root", "/rms"), "rms", "rms")) {
Liquibase liquibase = new Liquibase("src/main/sql/baseline/mysql/NORDICSProd/databaseChangeLog.master.xml", new SearchPathResourceAccessor(".", new FileSystemResourceAccessor()), new JdbcConnection(connection));
liquibase.update("NORDICS");
} catch (SQLException e) {
throw new RuntimeException(e);
} catch (LiquibaseException e) {
throw new RuntimeException(e);
}
}
}