@@ -26,7 +26,7 @@ final class MigrationTable {
2626 private static final int AUTO_PATCH_CHECKSUM = -1 ;
2727
2828 private final MigrationConfig config ;
29- private final Connection connection ;
29+ private final MigrationContext context ;
3030 private final boolean checkStateOnly ;
3131 private boolean earlyChecksumMode ;
3232 private final MigrationPlatform platform ;
@@ -72,18 +72,18 @@ final class MigrationTable {
7272 private int executionCount ;
7373 private boolean patchLegacyChecksums ;
7474 private MigrationMetaRow initMetaRow ;
75- private boolean tableKnownToExist ;
75+ private final boolean tableKnownToExist ;
7676
7777 public MigrationTable (FirstCheck firstCheck , boolean checkStateOnly ) {
7878 this .config = firstCheck .config ;
7979 this .platform = firstCheck .platform ;
80- this .connection = firstCheck .connection ;
80+ this .context = firstCheck .context ;
8181 this .schema = firstCheck .schema ;
8282 this .table = firstCheck .table ;
8383 this .sqlTable = firstCheck .sqlTable ;
8484 this .tableKnownToExist = firstCheck .tableKnownToExist ;
8585
86- this .scriptRunner = new MigrationScriptRunner (connection , platform );
86+ this .scriptRunner = new MigrationScriptRunner (context . connection () , platform );
8787 this .checkStateOnly = checkStateOnly ;
8888 this .earlyChecksumMode = config .isEarlyChecksumMode ();
8989 this .migrations = new LinkedHashMap <>();
@@ -142,7 +142,7 @@ private ScriptTransform createScriptTransform(MigrationConfig config) {
142142 void createIfNeededAndLock () throws SQLException , IOException {
143143 SQLException suppressedException = null ;
144144 if (!tableKnownToExist ) {
145- MigrationSchema .createIfNeeded (config , connection );
145+ MigrationSchema .createIfNeeded (config , context . connection () );
146146 if (!tableExists ()) {
147147 try {
148148 createTable ();
@@ -174,14 +174,14 @@ void createIfNeededAndLock() throws SQLException, IOException {
174174 * contain all the executed migrations in that case.
175175 */
176176 private void obtainLockWithWait () throws SQLException {
177- platform .lockMigrationTable (sqlTable , connection );
177+ platform .lockMigrationTable (sqlTable , context . connection () );
178178 }
179179
180180 /**
181181 * Release a lock on the migration table (MySql, MariaDB only).
182182 */
183183 void unlockMigrationTable () {
184- platform .unlockMigrationTable (sqlTable , connection );
184+ platform .unlockMigrationTable (sqlTable , context . connection () );
185185 }
186186
187187 /**
@@ -191,12 +191,13 @@ void unlockMigrationTable() {
191191 * executed during the wait for the lock.
192192 */
193193 private void readExistingMigrations () throws SQLException {
194- for (MigrationMetaRow metaRow : platform .readExistingMigrations (sqlTable , connection )) {
194+ for (MigrationMetaRow metaRow : platform .readExistingMigrations (sqlTable , context . connection () )) {
195195 addMigration (metaRow .version (), metaRow );
196196 }
197197 }
198198
199199 void createTable () throws IOException , SQLException {
200+ Connection connection = context .connection ();
200201 try {
201202 scriptRunner .runScript (createTableDdl (), "create migration table" );
202203 createInitMetaRow ().executeInsert (connection , insertSql );
@@ -253,6 +254,7 @@ private ClassLoader classLoader() {
253254 * Return true if the table exists.
254255 */
255256 boolean tableExists () throws SQLException {
257+ Connection connection = context .connection ();
256258 String migTable = table ;
257259 DatabaseMetaData metaData = connection .getMetaData ();
258260 if (metaData .storesUpperCaseIdentifiers ()) {
@@ -351,7 +353,7 @@ boolean skipMigration(int checksum, int checksum2, LocalMigrationResource local,
351353 } else if (patchLegacyChecksums && (existing .checksum () == checksum2 || checksum2 == AUTO_PATCH_CHECKSUM )) {
352354 if (!checkStateOnly ) {
353355 log .log (INFO , "Auto patch migration, set early mode checksum on {0} to {1,number} from {2,number}" , local .location (), checksum , existing .checksum ());
354- existing .resetChecksum (checksum , connection , updateChecksumSql );
356+ existing .resetChecksum (checksum , context . connection () , updateChecksumSql );
355357 }
356358 return true ;
357359
@@ -373,7 +375,7 @@ boolean skipMigration(int checksum, int checksum2, LocalMigrationResource local,
373375 private boolean patchResetChecksum (MigrationMetaRow existing , int newChecksum ) throws SQLException {
374376 if (isResetOnVersion (existing .version ())) {
375377 if (!checkStateOnly ) {
376- existing .resetChecksum (newChecksum , connection , updateChecksumSql );
378+ existing .resetChecksum (newChecksum , context . connection () , updateChecksumSql );
377379 }
378380 return true ;
379381 } else {
@@ -405,7 +407,7 @@ private void executeMigration(LocalMigrationResource local, String script, int c
405407 }
406408 if (existing != null ) {
407409 existing .rerun (checksum , exeMillis , envUserName , runOn );
408- existing .executeUpdate (connection , updateSql );
410+ existing .executeUpdate (context . connection () , updateSql );
409411 } else {
410412 insertIntoHistory (local , checksum , exeMillis );
411413 }
@@ -424,7 +426,7 @@ private long executeMigration(LocalMigrationResource local, String script) throw
424426 if (local instanceof LocalJdbcMigrationResource ) {
425427 JdbcMigration migration = ((LocalJdbcMigrationResource ) local ).migration ();
426428 log .log (INFO , "Executing jdbc migration version: {0} - {1}" , local .version (), migration );
427- migration .migrate (connection );
429+ migration .migrate (context . connection () );
428430 } else {
429431 log .log (DEBUG , "run migration {0}" , local .location ());
430432 scriptRunner .runScript (script , "run migration version: " + local .version ());
@@ -435,7 +437,7 @@ private long executeMigration(LocalMigrationResource local, String script) throw
435437
436438 private void insertIntoHistory (LocalMigrationResource local , int checksum , long exeMillis ) throws SQLException {
437439 MigrationMetaRow metaRow = createMetaRow (local , checksum , exeMillis );
438- metaRow .executeInsert (connection , insertSql );
440+ metaRow .executeInsert (context . connection () , insertSql );
439441 addMigration (local .key (), metaRow );
440442 }
441443
@@ -525,7 +527,7 @@ List<MigrationResource> runAll(List<LocalMigrationResource> localVersions) throw
525527 }
526528 if (patchLegacyChecksums && !checkStateOnly ) {
527529 // only patch the legacy checksums once
528- initMetaRow .resetChecksum (EARLY_MODE_CHECKSUM , connection , updateChecksumSql );
530+ initMetaRow .resetChecksum (EARLY_MODE_CHECKSUM , context . connection () , updateChecksumSql );
529531 }
530532 return checkMigrations ;
531533 }
0 commit comments