Skip to content

Instantly share code, notes, and snippets.

@cemo
Forked from smacharacek/MySqlLiquibaseDatabase.java
Created February 11, 2021 18:39
Show Gist options
  • Save cemo/ca06cc38706889474478faf7375cd636 to your computer and use it in GitHub Desktop.
Save cemo/ca06cc38706889474478faf7375cd636 to your computer and use it in GitHub Desktop.
Generator for jOOQ for MySql with Liquibase & Testcontainers (with gradle code generation)
plugins {
id 'nu.studer.jooq'
}
jooq {
version = '3.11.3'
test(sourceSets.main) {
jdbc {
driver = 'org.testcontainers.jdbc.ContainerDatabaseDriver'
url = ''
}
generator {
name = 'org.jooq.codegen.DefaultGenerator'
database {
name = 'com.example.MySqlLiquibaseDatabase'
includes = '.*'
excludes = 'DATABASECHANGELOG|DATABASECHANGELOGLOCK'
inputSchema = 'test'
properties {
property {
key = 'scripts'
value = 'src/main/resources/liquibase/changelog.yaml'
}
property {
key = 'mySqlVersion'
value = '5.7.21'
}
}
}
target {
packageName = 'com.example'
}
generate {
}
}
}
}
package com.example
import liquibase.Contexts;
import liquibase.LabelExpression;
import liquibase.Liquibase;
import liquibase.database.Database;
import liquibase.database.DatabaseFactory;
import liquibase.database.jvm.JdbcConnection;
import liquibase.resource.FileSystemResourceAccessor;
import org.jooq.DSLContext;
import org.jooq.exception.DataAccessException;
import org.jooq.impl.DSL;
import org.jooq.meta.mysql.MySQLDatabase;
import org.jooq.tools.jdbc.JDBCUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.testcontainers.containers.MySQLContainer;
import java.io.File;
import java.sql.Connection;
import java.util.Objects;
// this class needs to be placed in a separate module built before jOOQ classes generation
@SuppressWarnings("rawtypes")
public class MySqlLiquibaseDatabase extends MySQLDatabase {
private static final Logger log = LoggerFactory.getLogger(MySqlLiquibaseDatabase.class);
private MySQLContainer mySQLContainer;
private Connection connection;
private DSLContext ctx;
@Override
protected DSLContext create0() {
if (connection == null) {
String scripts = Objects.requireNonNull(getProperties().getProperty("scripts"));
String mySqlVersion = getProperties().getProperty("mySqlVersion", "5.7.11");
try {
mySQLContainer = new MySQLContainer(MySQLContainer.IMAGE + ":" + mySqlVersion);
mySQLContainer.start();
connection = mySQLContainer.createConnection("");
ctx = DSL.using(connection);
File scriptFile = new File(scripts);
Database database = DatabaseFactory
.getInstance()
.findCorrectDatabaseImplementation(new JdbcConnection(connection));
Liquibase liquibase = new Liquibase(
scriptFile.getName(),
new FileSystemResourceAccessor(scriptFile.getParent()),
database);
liquibase.update(new Contexts(), new LabelExpression());
} catch (Exception e) {
log.error("Error while preparing schema for code generation", e);
throw new DataAccessException("Error while preparing schema for code generation", e);
}
}
return ctx;
}
@Override
public void close() {
JDBCUtils.safeClose(connection);
connection = null;
if (mySQLContainer != null) {
mySQLContainer.close();
}
ctx = null;
super.close();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment