Skip to content

Commit

Permalink
Add new checkState methods (#31)
Browse files Browse the repository at this point in the history
* Add new checkState methods

  - checkState(DataSource dataSource)
  - checkState(Connection connection)

Resolves: #30
Signed-off-by: Thibault Meyer <[email protected]>

* Avoid using "state" variable

Signed-off-by: Thibault Meyer <[email protected]>
  • Loading branch information
0xBAADF00D authored and rbygrave committed Jul 6, 2017
1 parent 0a88b62 commit 0323bcb
Showing 1 changed file with 33 additions and 11 deletions.
44 changes: 33 additions & 11 deletions src/main/java/io/ebean/dbmigration/MigrationRunner.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,6 @@ public class MigrationRunner {

private final MigrationConfig migrationConfig;

/**
* Set to true when only checking as to what migrations will run.
*/
private boolean checkStateMode;

private List<LocalMigrationResource> checkMigrations;

public MigrationRunner(MigrationConfig migrationConfig) {
Expand All @@ -38,16 +33,30 @@ public MigrationRunner(MigrationConfig migrationConfig) {
* Run by creating a DB connection from driver, url, username defined in MigrationConfig.
*/
public void run() {
this.checkStateMode = false;
run(migrationConfig.createConnection());
}

/**
* Return the migrations that would be applied if the migration is run.
*/
public List<LocalMigrationResource> checkState() {
this.checkStateMode = true;
run(migrationConfig.createConnection());
run(migrationConfig.createConnection(), true);
return checkMigrations;
}

/**
* Return the migrations that would be applied if the migration is run.
*/
public List<LocalMigrationResource> checkState(DataSource dataSource) {
run(getConnection(dataSource), true);
return checkMigrations;
}

/**
* Return the migrations that would be applied if the migration is run.
*/
public List<LocalMigrationResource> checkState(Connection connection) {
run(connection, true);
return checkMigrations;
}

Expand Down Expand Up @@ -77,6 +86,13 @@ private Connection getConnection(DataSource dataSource) {
* Run the migrations if there are any that need running.
*/
public void run(Connection connection) {
run(connection, false);
}

/**
* Run the migrations if there are any that need running.
*/
public void run(Connection connection, boolean checkStateMode) {

LocalMigrationResources resources = new LocalMigrationResources(migrationConfig);
if (!resources.readResources()) {
Expand All @@ -90,7 +106,7 @@ public void run(Connection connection) {
MigrationSchema schema = new MigrationSchema(migrationConfig, connection);
schema.createAndSetIfNeeded();

runMigrations(resources, connection);
runMigrations(resources, connection, checkStateMode);
connection.commit();

} catch (MigrationException e) {
Expand All @@ -109,8 +125,7 @@ public void run(Connection connection) {
/**
* Run all the migrations as needed.
*/
private void runMigrations(LocalMigrationResources resources, Connection connection) throws SQLException, IOException {

private void runMigrations(LocalMigrationResources resources, Connection connection, boolean checkStateMode) throws SQLException, IOException {
derivePlatformName(migrationConfig, connection);

MigrationTable table = new MigrationTable(migrationConfig, connection, checkStateMode);
Expand All @@ -135,6 +150,13 @@ private void runMigrations(LocalMigrationResources resources, Connection connect
}
}

/**
* Run all the migrations as needed.
*/
private void runMigrations(LocalMigrationResources resources, Connection connection) throws SQLException, IOException {
runMigrations(resources, connection, false);
}

/**
* Derive and set the platform name if required.
*/
Expand Down

0 comments on commit 0323bcb

Please sign in to comment.