-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #148 from ebean-orm/feature/refactor-fastCheck
Refactor fastCheck, move startMs and move setAutoCommitFalse()
- Loading branch information
Showing
8 changed files
with
148 additions
and
103 deletions.
There are no files selected for viewing
89 changes: 89 additions & 0 deletions
89
ebean-migration/src/main/java/io/ebean/migration/runner/FirstCheck.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
package io.ebean.migration.runner; | ||
|
||
import io.ebean.migration.MigrationConfig; | ||
|
||
import java.sql.Connection; | ||
import java.sql.SQLException; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.stream.Collectors; | ||
|
||
/** | ||
* First initial check to see if migrations exist and exactly match. | ||
*/ | ||
final class FirstCheck { | ||
|
||
final MigrationConfig config; | ||
final MigrationPlatform platform; | ||
final Connection connection; | ||
final String schema; | ||
final String table; | ||
final String sqlTable; | ||
boolean tableKnownToExist; | ||
private int count; | ||
|
||
FirstCheck(MigrationConfig config, Connection connection, MigrationPlatform platform) { | ||
this.config = config; | ||
this.platform = platform; | ||
this.connection = connection; | ||
this.schema = config.getDbSchema(); | ||
this.table = config.getMetaTable(); | ||
this.sqlTable = schema != null ? schema + '.' + table : table; | ||
} | ||
|
||
MigrationTable initTable(boolean checkStateOnly) { | ||
return new MigrationTable(this, checkStateOnly); | ||
} | ||
|
||
boolean fastModeCheck(List<LocalMigrationResource> versions) { | ||
try { | ||
final List<MigrationMetaRow> rows = fastRead(); | ||
tableKnownToExist = !rows.isEmpty(); | ||
if (rows.size() != versions.size() + 1) { | ||
// difference in count of migrations | ||
return false; | ||
} | ||
final Map<String, Integer> dbChecksums = dbChecksumMap(rows); | ||
for (LocalMigrationResource local : versions) { | ||
Integer dbChecksum = dbChecksums.get(local.key()); | ||
if (dbChecksum == null) { | ||
// no match, unexpected missing migration | ||
return false; | ||
} | ||
int localChecksum = checksumFor(local); | ||
if (localChecksum != dbChecksum) { | ||
// no match, perhaps repeatable migration change | ||
return false; | ||
} | ||
} | ||
// successful fast check | ||
count = versions.size(); | ||
return true; | ||
} catch (SQLException e) { | ||
// probably migration table does not exist | ||
return false; | ||
} | ||
} | ||
|
||
private static Map<String, Integer> dbChecksumMap(List<MigrationMetaRow> rows) { | ||
return rows.stream().collect(Collectors.toMap(MigrationMetaRow::version, MigrationMetaRow::checksum)); | ||
} | ||
|
||
private int checksumFor(LocalMigrationResource local) { | ||
if (local instanceof LocalUriMigrationResource) { | ||
return ((LocalUriMigrationResource) local).checksum(); | ||
} else if (local instanceof LocalDdlMigrationResource) { | ||
return Checksum.calculate(local.content()); | ||
} else { | ||
return ((LocalJdbcMigrationResource) local).checksum(); | ||
} | ||
} | ||
|
||
List<MigrationMetaRow> fastRead() throws SQLException { | ||
return platform.fastReadMigrations(sqlTable, connection); | ||
} | ||
|
||
int count() { | ||
return count; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.