Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Close underlying connections during migration #12710

Merged
merged 1 commit into from
May 11, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't know you could assign a variable of that type in this statement like this, thats cool!

try {
closeable.close();
} catch (final IOException e) {
LOGGER.warn("Unable to close data source.", e);
}
}
super.close();
}

Expand Down