Skip to content

Commit

Permalink
Close underlying connections during migration (#12710)
Browse files Browse the repository at this point in the history
  • Loading branch information
jdpgrailsdev authored and suhomud committed May 23, 2022
1 parent 0365c29 commit 0b2b772
Showing 1 changed file with 19 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import io.airbyte.db.factory.DSLContextFactory;
import io.airbyte.db.factory.DataSourceFactory;
import io.airbyte.db.factory.FlywayFactory;
import java.io.Closeable;
import java.io.IOException;
import java.sql.Connection;
import javax.sql.DataSource;
Expand All @@ -18,6 +19,8 @@
import org.jooq.meta.postgres.PostgresDatabase;
import org.jooq.tools.StringUtils;
import org.jooq.tools.jdbc.JDBCUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.testcontainers.containers.PostgreSQLContainer;

/**
Expand All @@ -33,10 +36,16 @@
*/
public abstract class FlywayMigrationDatabase extends PostgresDatabase {

private static final Logger LOGGER = LoggerFactory.getLogger(FlywayMigrationDatabase.class);

private static final String DEFAULT_DOCKER_IMAGE = "postgres:13-alpine";

private Connection connection;

private DataSource dataSource;

private DSLContext dslContext;

protected abstract Database getAndInitializeDatabase(DSLContext dslContext) throws IOException;

protected abstract DatabaseMigrator getDatabaseMigrator(Database database, Flyway flyway);
Expand Down Expand Up @@ -75,9 +84,9 @@ private void createInternalConnection() throws Exception {
.withPassword("jooq_generator");
container.start();

final DataSource dataSource =
dataSource =
DataSourceFactory.create(container.getUsername(), container.getPassword(), container.getDriverClassName(), container.getJdbcUrl());
final DSLContext dslContext = DSLContextFactory.create(dataSource, SQLDialect.POSTGRES);
dslContext = DSLContextFactory.create(dataSource, SQLDialect.POSTGRES);
final Flyway flyway = FlywayFactory.create(dataSource, getInstalledBy(), getDbIdentifier(), getMigrationFileLocations());
final Database database = getAndInitializeDatabase(dslContext);
final DatabaseMigrator migrator = getDatabaseMigrator(database, flyway);
Expand All @@ -91,6 +100,14 @@ private void createInternalConnection() throws Exception {
public void close() {
JDBCUtils.safeClose(connection);
connection = null;
dslContext.close();
if (dataSource instanceof Closeable closeable) {
try {
closeable.close();
} catch (final IOException e) {
LOGGER.warn("Unable to close data source.", e);
}
}
super.close();
}

Expand Down

0 comments on commit 0b2b772

Please sign in to comment.