Skip to content

Commit 7312bba

Browse files
author
Igor Polevoy
authored
Merge pull request #831 from FadiDev/only-create-schema-version-table-when-non-existent
Only create schema version table if non-existent
2 parents 1252d64 + 6d1741c commit 7312bba

File tree

2 files changed

+156
-156
lines changed

2 files changed

+156
-156
lines changed
Lines changed: 105 additions & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -1,105 +1,105 @@
1-
package org.javalite.db_migrator;
2-
3-
import org.apache.maven.plugin.logging.Log;
4-
5-
import java.sql.SQLException;
6-
import java.util.ArrayList;
7-
import java.util.Collection;
8-
import java.util.Date;
9-
import java.util.List;
10-
11-
import static org.javalite.db_migrator.DbUtils.connection;
12-
import static org.javalite.db_migrator.DbUtils.databaseType;
13-
14-
public class MigrationManager {
15-
16-
private DatabaseType dbType;
17-
private VersionStrategy versionStrategy = new VersionStrategy();
18-
private MigrationResolver migrationResolver;
19-
20-
public MigrationManager(String migrationLocation) throws SQLException {
21-
this.dbType = determineDatabaseType();
22-
migrationResolver = new MigrationResolver(migrationLocation);
23-
}
24-
25-
/**
26-
* Validates whether the database is currently up-to-date.
27-
*
28-
* @return true if the database is up-to-date, false if it is not or is unversioned
29-
*/
30-
public boolean validate() {
31-
return getPendingMigrations().isEmpty();
32-
}
33-
34-
/**
35-
* Returns pending migrations.
36-
*
37-
* @return a sorted set of pending migrations
38-
*/
39-
public List<Migration> getPendingMigrations() {
40-
List<String> appliedMigrations = getAppliedMigrationVersions();
41-
42-
List<Migration> allMigrations = migrationResolver.resolve();
43-
List<Migration> pendingMigrations = new ArrayList<Migration>();
44-
for (Migration migration : allMigrations) {
45-
if(!appliedMigrations.contains(migration.getVersion())){
46-
pendingMigrations.add(migration);
47-
}
48-
}
49-
return pendingMigrations;
50-
}
51-
52-
/**
53-
* Migrates the database to the latest version, enabling migrations if necessary.
54-
*/
55-
public void migrate(Log log, String encoding) {
56-
57-
if (!versionTableExists()) {
58-
createSchemaVersionTable();
59-
}
60-
61-
final Collection<Migration> pendingMigrations = getPendingMigrations();
62-
63-
if (pendingMigrations.isEmpty()) {
64-
log.info("No new migrations are found");
65-
return;
66-
}
67-
log.info("Migrating database, applying " + pendingMigrations.size() + " migration(s)");
68-
Migration currentMigration = null;
69-
70-
try {
71-
connection().setAutoCommit(false);
72-
for (Migration migration : pendingMigrations) {
73-
currentMigration = migration;
74-
log.info("Running migration " + currentMigration.getName());
75-
long start = System.currentTimeMillis();
76-
77-
currentMigration.migrate(encoding);
78-
versionStrategy.recordMigration(currentMigration.getVersion(), new Date(start), (start - System.currentTimeMillis()));
79-
connection().commit();
80-
}
81-
} catch (Exception e) {
82-
try{connection().rollback();}catch(Exception ex){throw new MigrationException(e);}
83-
assert currentMigration != null;
84-
throw new MigrationException("Migration for version " + currentMigration.getVersion() + " failed, rolling back and terminating migration.", e);
85-
}
86-
log.info("Migrated database");
87-
}
88-
89-
protected DatabaseType determineDatabaseType() throws SQLException {
90-
return databaseType(connection().getMetaData().getURL());
91-
92-
}
93-
94-
protected boolean versionTableExists() {
95-
return versionStrategy.versionTableExists();
96-
}
97-
98-
public void createSchemaVersionTable() {
99-
versionStrategy.createSchemaVersionTable(dbType);
100-
}
101-
102-
protected List<String> getAppliedMigrationVersions() {
103-
return versionStrategy.getAppliedMigrations();
104-
}
105-
}
1+
package org.javalite.db_migrator;
2+
3+
import org.apache.maven.plugin.logging.Log;
4+
5+
import java.sql.SQLException;
6+
import java.util.ArrayList;
7+
import java.util.Collection;
8+
import java.util.Date;
9+
import java.util.List;
10+
11+
import static org.javalite.db_migrator.DbUtils.connection;
12+
import static org.javalite.db_migrator.DbUtils.databaseType;
13+
14+
public class MigrationManager {
15+
16+
private DatabaseType dbType;
17+
private VersionStrategy versionStrategy = new VersionStrategy();
18+
private MigrationResolver migrationResolver;
19+
20+
public MigrationManager(String migrationLocation) throws SQLException {
21+
this.dbType = determineDatabaseType();
22+
migrationResolver = new MigrationResolver(migrationLocation);
23+
}
24+
25+
/**
26+
* Validates whether the database is currently up-to-date.
27+
*
28+
* @return true if the database is up-to-date, false if it is not or is unversioned
29+
*/
30+
public boolean validate() {
31+
return getPendingMigrations().isEmpty();
32+
}
33+
34+
/**
35+
* Returns pending migrations.
36+
*
37+
* @return a sorted set of pending migrations
38+
*/
39+
public List<Migration> getPendingMigrations() {
40+
List<String> appliedMigrations = getAppliedMigrationVersions();
41+
42+
List<Migration> allMigrations = migrationResolver.resolve();
43+
List<Migration> pendingMigrations = new ArrayList<Migration>();
44+
for (Migration migration : allMigrations) {
45+
if(!appliedMigrations.contains(migration.getVersion())){
46+
pendingMigrations.add(migration);
47+
}
48+
}
49+
return pendingMigrations;
50+
}
51+
52+
/**
53+
* Migrates the database to the latest version, enabling migrations if necessary.
54+
*/
55+
public void migrate(Log log, String encoding) {
56+
57+
createSchemaVersionTable();
58+
59+
final Collection<Migration> pendingMigrations = getPendingMigrations();
60+
61+
if (pendingMigrations.isEmpty()) {
62+
log.info("No new migrations are found");
63+
return;
64+
}
65+
log.info("Migrating database, applying " + pendingMigrations.size() + " migration(s)");
66+
Migration currentMigration = null;
67+
68+
try {
69+
connection().setAutoCommit(false);
70+
for (Migration migration : pendingMigrations) {
71+
currentMigration = migration;
72+
log.info("Running migration " + currentMigration.getName());
73+
long start = System.currentTimeMillis();
74+
75+
currentMigration.migrate(encoding);
76+
versionStrategy.recordMigration(currentMigration.getVersion(), new Date(start), (start - System.currentTimeMillis()));
77+
connection().commit();
78+
}
79+
} catch (Exception e) {
80+
try{connection().rollback();}catch(Exception ex){throw new MigrationException(e);}
81+
assert currentMigration != null;
82+
throw new MigrationException("Migration for version " + currentMigration.getVersion() + " failed, rolling back and terminating migration.", e);
83+
}
84+
log.info("Migrated database");
85+
}
86+
87+
protected DatabaseType determineDatabaseType() throws SQLException {
88+
return databaseType(connection().getMetaData().getURL());
89+
90+
}
91+
92+
protected boolean versionTableExists() {
93+
return versionStrategy.versionTableExists();
94+
}
95+
96+
public void createSchemaVersionTable() {
97+
if (!versionTableExists()) {
98+
versionStrategy.createSchemaVersionTable(dbType);
99+
}
100+
}
101+
102+
protected List<String> getAppliedMigrationVersions() {
103+
return versionStrategy.getAppliedMigrations();
104+
}
105+
}
Lines changed: 51 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,51 @@
1-
package org.javalite.db_migrator.maven;
2-
3-
import org.apache.maven.plugin.MojoExecutionException;
4-
import org.javalite.db_migrator.DbUtils;
5-
import org.javalite.db_migrator.MigrationManager;
6-
7-
import static java.lang.String.format;
8-
import static org.javalite.db_migrator.DbUtils.*;
9-
10-
11-
/**
12-
* @goal create
13-
*/
14-
public class CreateMojo extends AbstractDbMigrationMojo {
15-
16-
public void executeMojo() throws MojoExecutionException {
17-
18-
try {
19-
20-
String createSql = blank(getCreateSql()) ? "create database %s" : getCreateSql();
21-
String databaseName = DbUtils.extractDatabaseName(getUrl());
22-
switch (DbUtils.databaseType(getUrl())) {
23-
case MYSQL:
24-
break;
25-
case SQL_SERVER:
26-
break;
27-
case POSTGRESQL:
28-
databaseName = "\"" + databaseName + "\"";
29-
break;
30-
}
31-
32-
openConnection(true);
33-
exec(format(createSql, databaseName));
34-
getLog().info("Created database " + getUrl());
35-
} catch (Exception e) {
36-
throw new MojoExecutionException("Failed to create database: " + getUrl(), e);
37-
} finally {
38-
closeConnection();
39-
}
40-
41-
try{
42-
openConnection();
43-
new MigrationManager(getMigrationsPath()).createSchemaVersionTable();
44-
45-
}catch(Exception e){
46-
throw new MojoExecutionException("failed to create SCHEMA_VERSION table", e);
47-
}finally {
48-
closeConnection();
49-
}
50-
}
51-
}
1+
package org.javalite.db_migrator.maven;
2+
3+
import org.apache.maven.plugin.MojoExecutionException;
4+
import org.javalite.db_migrator.DbUtils;
5+
import org.javalite.db_migrator.MigrationManager;
6+
7+
import static java.lang.String.format;
8+
import static org.javalite.db_migrator.DbUtils.*;
9+
10+
11+
/**
12+
* @goal create
13+
*/
14+
public class CreateMojo extends AbstractDbMigrationMojo {
15+
16+
public void executeMojo() throws MojoExecutionException {
17+
18+
try {
19+
20+
String createSql = blank(getCreateSql()) ? "create database %s" : getCreateSql();
21+
String databaseName = DbUtils.extractDatabaseName(getUrl());
22+
switch (DbUtils.databaseType(getUrl())) {
23+
case MYSQL:
24+
break;
25+
case SQL_SERVER:
26+
break;
27+
case POSTGRESQL:
28+
databaseName = "\"" + databaseName + "\"";
29+
break;
30+
}
31+
32+
openConnection(true);
33+
exec(format(createSql, databaseName));
34+
getLog().info("Created database " + getUrl());
35+
} catch (Exception e) {
36+
throw new MojoExecutionException("Failed to create database: " + getUrl(), e);
37+
} finally {
38+
closeConnection();
39+
}
40+
41+
try{
42+
openConnection();
43+
new MigrationManager(getMigrationsPath()).createSchemaVersionTable();
44+
45+
}catch(Exception e){
46+
throw new MojoExecutionException("failed to create SCHEMA_VERSION table", e);
47+
}finally {
48+
closeConnection();
49+
}
50+
}
51+
}

0 commit comments

Comments
 (0)