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 #831 from FadiDev/only-create-schema-version-table…
…-when-non-existent Only create schema version table if non-existent
- Loading branch information
Showing
2 changed files
with
156 additions
and
156 deletions.
There are no files selected for viewing
210 changes: 105 additions & 105 deletions
210
db-migrator/src/main/java/org/javalite/db_migrator/MigrationManager.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 |
---|---|---|
@@ -1,105 +1,105 @@ | ||
package org.javalite.db_migrator; | ||
|
||
import org.apache.maven.plugin.logging.Log; | ||
|
||
import java.sql.SQLException; | ||
import java.util.ArrayList; | ||
import java.util.Collection; | ||
import java.util.Date; | ||
import java.util.List; | ||
|
||
import static org.javalite.db_migrator.DbUtils.connection; | ||
import static org.javalite.db_migrator.DbUtils.databaseType; | ||
|
||
public class MigrationManager { | ||
|
||
private DatabaseType dbType; | ||
private VersionStrategy versionStrategy = new VersionStrategy(); | ||
private MigrationResolver migrationResolver; | ||
|
||
public MigrationManager(String migrationLocation) throws SQLException { | ||
this.dbType = determineDatabaseType(); | ||
migrationResolver = new MigrationResolver(migrationLocation); | ||
} | ||
|
||
/** | ||
* Validates whether the database is currently up-to-date. | ||
* | ||
* @return true if the database is up-to-date, false if it is not or is unversioned | ||
*/ | ||
public boolean validate() { | ||
return getPendingMigrations().isEmpty(); | ||
} | ||
|
||
/** | ||
* Returns pending migrations. | ||
* | ||
* @return a sorted set of pending migrations | ||
*/ | ||
public List<Migration> getPendingMigrations() { | ||
List<String> appliedMigrations = getAppliedMigrationVersions(); | ||
|
||
List<Migration> allMigrations = migrationResolver.resolve(); | ||
List<Migration> pendingMigrations = new ArrayList<Migration>(); | ||
for (Migration migration : allMigrations) { | ||
if(!appliedMigrations.contains(migration.getVersion())){ | ||
pendingMigrations.add(migration); | ||
} | ||
} | ||
return pendingMigrations; | ||
} | ||
|
||
/** | ||
* Migrates the database to the latest version, enabling migrations if necessary. | ||
*/ | ||
public void migrate(Log log, String encoding) { | ||
|
||
if (!versionTableExists()) { | ||
createSchemaVersionTable(); | ||
} | ||
|
||
final Collection<Migration> pendingMigrations = getPendingMigrations(); | ||
|
||
if (pendingMigrations.isEmpty()) { | ||
log.info("No new migrations are found"); | ||
return; | ||
} | ||
log.info("Migrating database, applying " + pendingMigrations.size() + " migration(s)"); | ||
Migration currentMigration = null; | ||
|
||
try { | ||
connection().setAutoCommit(false); | ||
for (Migration migration : pendingMigrations) { | ||
currentMigration = migration; | ||
log.info("Running migration " + currentMigration.getName()); | ||
long start = System.currentTimeMillis(); | ||
|
||
currentMigration.migrate(encoding); | ||
versionStrategy.recordMigration(currentMigration.getVersion(), new Date(start), (start - System.currentTimeMillis())); | ||
connection().commit(); | ||
} | ||
} catch (Exception e) { | ||
try{connection().rollback();}catch(Exception ex){throw new MigrationException(e);} | ||
assert currentMigration != null; | ||
throw new MigrationException("Migration for version " + currentMigration.getVersion() + " failed, rolling back and terminating migration.", e); | ||
} | ||
log.info("Migrated database"); | ||
} | ||
|
||
protected DatabaseType determineDatabaseType() throws SQLException { | ||
return databaseType(connection().getMetaData().getURL()); | ||
|
||
} | ||
|
||
protected boolean versionTableExists() { | ||
return versionStrategy.versionTableExists(); | ||
} | ||
|
||
public void createSchemaVersionTable() { | ||
versionStrategy.createSchemaVersionTable(dbType); | ||
} | ||
|
||
protected List<String> getAppliedMigrationVersions() { | ||
return versionStrategy.getAppliedMigrations(); | ||
} | ||
} | ||
package org.javalite.db_migrator; | ||
|
||
import org.apache.maven.plugin.logging.Log; | ||
|
||
import java.sql.SQLException; | ||
import java.util.ArrayList; | ||
import java.util.Collection; | ||
import java.util.Date; | ||
import java.util.List; | ||
|
||
import static org.javalite.db_migrator.DbUtils.connection; | ||
import static org.javalite.db_migrator.DbUtils.databaseType; | ||
|
||
public class MigrationManager { | ||
|
||
private DatabaseType dbType; | ||
private VersionStrategy versionStrategy = new VersionStrategy(); | ||
private MigrationResolver migrationResolver; | ||
|
||
public MigrationManager(String migrationLocation) throws SQLException { | ||
this.dbType = determineDatabaseType(); | ||
migrationResolver = new MigrationResolver(migrationLocation); | ||
} | ||
|
||
/** | ||
* Validates whether the database is currently up-to-date. | ||
* | ||
* @return true if the database is up-to-date, false if it is not or is unversioned | ||
*/ | ||
public boolean validate() { | ||
return getPendingMigrations().isEmpty(); | ||
} | ||
|
||
/** | ||
* Returns pending migrations. | ||
* | ||
* @return a sorted set of pending migrations | ||
*/ | ||
public List<Migration> getPendingMigrations() { | ||
List<String> appliedMigrations = getAppliedMigrationVersions(); | ||
|
||
List<Migration> allMigrations = migrationResolver.resolve(); | ||
List<Migration> pendingMigrations = new ArrayList<Migration>(); | ||
for (Migration migration : allMigrations) { | ||
if(!appliedMigrations.contains(migration.getVersion())){ | ||
pendingMigrations.add(migration); | ||
} | ||
} | ||
return pendingMigrations; | ||
} | ||
|
||
/** | ||
* Migrates the database to the latest version, enabling migrations if necessary. | ||
*/ | ||
public void migrate(Log log, String encoding) { | ||
|
||
createSchemaVersionTable(); | ||
|
||
final Collection<Migration> pendingMigrations = getPendingMigrations(); | ||
|
||
if (pendingMigrations.isEmpty()) { | ||
log.info("No new migrations are found"); | ||
return; | ||
} | ||
log.info("Migrating database, applying " + pendingMigrations.size() + " migration(s)"); | ||
Migration currentMigration = null; | ||
|
||
try { | ||
connection().setAutoCommit(false); | ||
for (Migration migration : pendingMigrations) { | ||
currentMigration = migration; | ||
log.info("Running migration " + currentMigration.getName()); | ||
long start = System.currentTimeMillis(); | ||
|
||
currentMigration.migrate(encoding); | ||
versionStrategy.recordMigration(currentMigration.getVersion(), new Date(start), (start - System.currentTimeMillis())); | ||
connection().commit(); | ||
} | ||
} catch (Exception e) { | ||
try{connection().rollback();}catch(Exception ex){throw new MigrationException(e);} | ||
assert currentMigration != null; | ||
throw new MigrationException("Migration for version " + currentMigration.getVersion() + " failed, rolling back and terminating migration.", e); | ||
} | ||
log.info("Migrated database"); | ||
} | ||
|
||
protected DatabaseType determineDatabaseType() throws SQLException { | ||
return databaseType(connection().getMetaData().getURL()); | ||
|
||
} | ||
|
||
protected boolean versionTableExists() { | ||
return versionStrategy.versionTableExists(); | ||
} | ||
|
||
public void createSchemaVersionTable() { | ||
if (!versionTableExists()) { | ||
versionStrategy.createSchemaVersionTable(dbType); | ||
} | ||
} | ||
|
||
protected List<String> getAppliedMigrationVersions() { | ||
return versionStrategy.getAppliedMigrations(); | ||
} | ||
} |
102 changes: 51 additions & 51 deletions
102
db-migrator/src/main/java/org/javalite/db_migrator/maven/CreateMojo.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 |
---|---|---|
@@ -1,51 +1,51 @@ | ||
package org.javalite.db_migrator.maven; | ||
|
||
import org.apache.maven.plugin.MojoExecutionException; | ||
import org.javalite.db_migrator.DbUtils; | ||
import org.javalite.db_migrator.MigrationManager; | ||
|
||
import static java.lang.String.format; | ||
import static org.javalite.db_migrator.DbUtils.*; | ||
|
||
|
||
/** | ||
* @goal create | ||
*/ | ||
public class CreateMojo extends AbstractDbMigrationMojo { | ||
|
||
public void executeMojo() throws MojoExecutionException { | ||
|
||
try { | ||
|
||
String createSql = blank(getCreateSql()) ? "create database %s" : getCreateSql(); | ||
String databaseName = DbUtils.extractDatabaseName(getUrl()); | ||
switch (DbUtils.databaseType(getUrl())) { | ||
case MYSQL: | ||
break; | ||
case SQL_SERVER: | ||
break; | ||
case POSTGRESQL: | ||
databaseName = "\"" + databaseName + "\""; | ||
break; | ||
} | ||
|
||
openConnection(true); | ||
exec(format(createSql, databaseName)); | ||
getLog().info("Created database " + getUrl()); | ||
} catch (Exception e) { | ||
throw new MojoExecutionException("Failed to create database: " + getUrl(), e); | ||
} finally { | ||
closeConnection(); | ||
} | ||
|
||
try{ | ||
openConnection(); | ||
new MigrationManager(getMigrationsPath()).createSchemaVersionTable(); | ||
|
||
}catch(Exception e){ | ||
throw new MojoExecutionException("failed to create SCHEMA_VERSION table", e); | ||
}finally { | ||
closeConnection(); | ||
} | ||
} | ||
} | ||
package org.javalite.db_migrator.maven; | ||
|
||
import org.apache.maven.plugin.MojoExecutionException; | ||
import org.javalite.db_migrator.DbUtils; | ||
import org.javalite.db_migrator.MigrationManager; | ||
|
||
import static java.lang.String.format; | ||
import static org.javalite.db_migrator.DbUtils.*; | ||
|
||
|
||
/** | ||
* @goal create | ||
*/ | ||
public class CreateMojo extends AbstractDbMigrationMojo { | ||
|
||
public void executeMojo() throws MojoExecutionException { | ||
|
||
try { | ||
|
||
String createSql = blank(getCreateSql()) ? "create database %s" : getCreateSql(); | ||
String databaseName = DbUtils.extractDatabaseName(getUrl()); | ||
switch (DbUtils.databaseType(getUrl())) { | ||
case MYSQL: | ||
break; | ||
case SQL_SERVER: | ||
break; | ||
case POSTGRESQL: | ||
databaseName = "\"" + databaseName + "\""; | ||
break; | ||
} | ||
|
||
openConnection(true); | ||
exec(format(createSql, databaseName)); | ||
getLog().info("Created database " + getUrl()); | ||
} catch (Exception e) { | ||
throw new MojoExecutionException("Failed to create database: " + getUrl(), e); | ||
} finally { | ||
closeConnection(); | ||
} | ||
|
||
try{ | ||
openConnection(); | ||
new MigrationManager(getMigrationsPath()).createSchemaVersionTable(); | ||
|
||
}catch(Exception e){ | ||
throw new MojoExecutionException("failed to create SCHEMA_VERSION table", e); | ||
}finally { | ||
closeConnection(); | ||
} | ||
} | ||
} |