From 0323bcbc4648f37741b74f284ca66ead3803dd3e Mon Sep 17 00:00:00 2001 From: Thibault Meyer <0xbaadf00d@users.noreply.github.com> Date: Thu, 6 Jul 2017 10:41:20 +0200 Subject: [PATCH] Add new checkState methods (#31) * Add new checkState methods - checkState(DataSource dataSource) - checkState(Connection connection) Resolves: #30 Signed-off-by: Thibault Meyer * Avoid using "state" variable Signed-off-by: Thibault Meyer --- .../io/ebean/dbmigration/MigrationRunner.java | 44 ++++++++++++++----- 1 file changed, 33 insertions(+), 11 deletions(-) diff --git a/src/main/java/io/ebean/dbmigration/MigrationRunner.java b/src/main/java/io/ebean/dbmigration/MigrationRunner.java index 437e9eb..1886d11 100644 --- a/src/main/java/io/ebean/dbmigration/MigrationRunner.java +++ b/src/main/java/io/ebean/dbmigration/MigrationRunner.java @@ -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 checkMigrations; public MigrationRunner(MigrationConfig migrationConfig) { @@ -38,7 +33,6 @@ 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()); } @@ -46,8 +40,23 @@ public void run() { * Return the migrations that would be applied if the migration is run. */ public List 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 checkState(DataSource dataSource) { + run(getConnection(dataSource), true); + return checkMigrations; + } + + /** + * Return the migrations that would be applied if the migration is run. + */ + public List checkState(Connection connection) { + run(connection, true); return checkMigrations; } @@ -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()) { @@ -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) { @@ -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); @@ -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. */