Skip to content
Snippets Groups Projects
Commit b1f47464 authored by ribeaudc's avatar ribeaudc
Browse files

Add logging to DBMigrationEngine.

SVN: 237
parent b063bfd7
No related branches found
No related tags found
No related merge requests found
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<classpath> <classpath>
<classpathentry kind="src" path="source/java"/> <classpathentry kind="src" path="source/java"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/> <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry kind="lib" path="/libraries/spring/spring.jar" sourcepath="/libraries/spring/src.zip"/> <classpathentry kind="lib" path="/libraries/spring/spring.jar" sourcepath="/libraries/spring/src.zip"/>
<classpathentry combineaccessrules="false" kind="src" path="/common"/> <classpathentry combineaccessrules="false" kind="src" path="/common"/>
<classpathentry kind="output" path="targets/classes"/> <classpathentry kind="lib" path="/libraries/log4j/log4j.jar" sourcepath="/libraries/log4j/src.zip"/>
</classpath> <classpathentry kind="output" path="targets/classes"/>
</classpath>
...@@ -25,30 +25,37 @@ import java.util.List; ...@@ -25,30 +25,37 @@ import java.util.List;
import javax.sql.DataSource; import javax.sql.DataSource;
import org.apache.log4j.Logger;
import org.springframework.jdbc.BadSqlGrammarException; import org.springframework.jdbc.BadSqlGrammarException;
import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.simple.ParameterizedRowMapper; import org.springframework.jdbc.core.simple.ParameterizedRowMapper;
import org.springframework.jdbc.core.simple.SimpleJdbcTemplate; import org.springframework.jdbc.core.simple.SimpleJdbcTemplate;
import ch.systemsx.cisd.common.exceptions.EnvironmentFailureException; import ch.systemsx.cisd.common.exceptions.EnvironmentFailureException;
import ch.systemsx.cisd.common.logging.LogCategory;
import ch.systemsx.cisd.common.logging.LogFactory;
import ch.systemsx.cisd.common.utilities.FileUtilities; import ch.systemsx.cisd.common.utilities.FileUtilities;
/** /**
* Class for creating and migrating a database. * Class for creating and migrating a database.
* *
* @author felmer * @author felmer
*/ */
public class DBMigrationEngine public class DBMigrationEngine
{ {
private static final Logger operationLog = LogFactory.getLogger(LogCategory.OPERATION, DBMigrationEngine.class);
private static final String INSERT_DB_VERSION = "INSERT INTO DATABASE_VERSION VALUES (1, ?)"; private static final String INSERT_DB_VERSION = "INSERT INTO DATABASE_VERSION VALUES (1, ?)";
private static final String CREATE_DB_VERSION_TABLE = "CREATE TABLE DATABASE_VERSION (DB_VERSION SMALLINT NOT NULL,"
+ "DB_INSTALLATION_DATE DATE)"; private static final String CREATE_DB_VERSION_TABLE =
"CREATE TABLE DATABASE_VERSION (DB_VERSION SMALLINT NOT NULL," + "DB_INSTALLATION_DATE DATE)";
private static final class DatabaseVersion private static final class DatabaseVersion
{ {
private final int version; private final int version;
private final Date installationDate; private final Date installationDate;
public DatabaseVersion(int version, Date installationDate) public DatabaseVersion(int version, Date installationDate)
{ {
this.version = version; this.version = version;
...@@ -59,22 +66,27 @@ public class DBMigrationEngine ...@@ -59,22 +66,27 @@ public class DBMigrationEngine
{ {
return installationDate; return installationDate;
} }
int getVersion() int getVersion()
{ {
return version; return version;
} }
} }
private final DataSource metaDataSource; private final DataSource metaDataSource;
private final DataSource dataSource; private final DataSource dataSource;
private final File scriptFolder; private final File scriptFolder;
private final String initialDataScriptFile; private final String initialDataScriptFile;
private final String owner; private final String owner;
private final String databaseName; private final String databaseName;
public DBMigrationEngine(DataSource metaDataSource, DataSource dataSource, String scriptFolder, public DBMigrationEngine(DataSource metaDataSource, DataSource dataSource, String scriptFolder,
String initialDataScript, String owner, String databaseName) String initialDataScript, String owner, String databaseName)
{ {
this.metaDataSource = metaDataSource; this.metaDataSource = metaDataSource;
this.dataSource = dataSource; this.dataSource = dataSource;
...@@ -94,18 +106,24 @@ public class DBMigrationEngine ...@@ -94,18 +106,24 @@ public class DBMigrationEngine
JdbcTemplate template = new JdbcTemplate(metaDataSource); JdbcTemplate template = new JdbcTemplate(metaDataSource);
String createUserSQL = createScript("createUser.sql", owner, databaseName); String createUserSQL = createScript("createUser.sql", owner, databaseName);
String createDatabaseSQL = createScript("createDatabase.sql", owner, databaseName); String createDatabaseSQL = createScript("createDatabase.sql", owner, databaseName);
try try
{ {
// If the user already exists, the database will throw an exception.
// But, the exception thrown could have another reason. Right now, there is no possibility
// to differentiate one case from another.
template.execute(createUserSQL); template.execute(createUserSQL);
} catch (BadSqlGrammarException ex) } catch (BadSqlGrammarException ex)
{ {
// TODO: have better error checking here. operationLog.error("Executing following script '" + createUserSQL + "' threw an exception.", ex);
} }
template.execute(createDatabaseSQL); template.execute(createDatabaseSQL);
migrateOrCreate(version); migrateOrCreate(version);
System.out.println("Database created"); if (operationLog.isInfoEnabled())
{
operationLog.info("Database '" + databaseName + "' has been successfully created.");
}
} }
} }
...@@ -114,13 +132,20 @@ public class DBMigrationEngine ...@@ -114,13 +132,20 @@ public class DBMigrationEngine
String script = loadScript(scriptTemplateFile); String script = loadScript(scriptTemplateFile);
return script.replace("$USER", user).replace("$DATABASE", database); return script.replace("$USER", user).replace("$DATABASE", database);
} }
/** Loads given script name. */
private String loadScript(String scriptName) private String loadScript(String scriptName)
{ {
String script = FileUtilities.loadStringResource(getClass(), "/" + scriptFolder + "/" + scriptName); String resource = "/" + scriptFolder + "/" + scriptName;
String script = FileUtilities.loadStringResource(getClass(), resource);
if (script == null) if (script == null)
{ {
script = FileUtilities.loadText(new File(scriptFolder, scriptName)); File file = new File(scriptFolder, scriptName);
if (operationLog.isDebugEnabled())
{
operationLog.debug("Resource '" + resource + "' could not be found. Trying '" + file.getPath() + "'.");
}
script = FileUtilities.loadText(file);
} }
return script; return script;
} }
...@@ -130,16 +155,16 @@ public class DBMigrationEngine ...@@ -130,16 +155,16 @@ public class DBMigrationEngine
try try
{ {
SimpleJdbcTemplate template = new SimpleJdbcTemplate(dataSource); SimpleJdbcTemplate template = new SimpleJdbcTemplate(dataSource);
List<DatabaseVersion> list List<DatabaseVersion> list =
= template.query("SELECT * FROM DATABASE_VERSION", new ParameterizedRowMapper<DatabaseVersion>() template.query("SELECT * FROM DATABASE_VERSION", new ParameterizedRowMapper<DatabaseVersion>()
{
public DatabaseVersion mapRow(ResultSet rs, int rowNum) throws SQLException
{ {
public DatabaseVersion mapRow(ResultSet rs, int rowNum) throws SQLException int dbVersion = rs.getInt("DB_VERSION");
{ java.sql.Date date = rs.getDate("DB_INSTALLATION_DATE");
int dbVersion = rs.getInt("DB_VERSION"); return new DatabaseVersion(dbVersion, date);
java.sql.Date date = rs.getDate("DB_INSTALLATION_DATE"); }
return new DatabaseVersion(dbVersion, date); });
}
});
int size = list.size(); int size = list.size();
if (size == 0) if (size == 0)
{ {
...@@ -157,11 +182,14 @@ public class DBMigrationEngine ...@@ -157,11 +182,14 @@ public class DBMigrationEngine
} }
if (version > dbVersion) if (version > dbVersion)
{ {
System.out.println("migrate from " + dbVersion + " -> " + version); if (operationLog.isInfoEnabled())
{
operationLog.info("Migrating database from version '" + dbVersion + "' to '" + version + "'.");
}
} else } else
{ {
throw new EnvironmentFailureException("Couldn't revert from version " + dbVersion throw new EnvironmentFailureException("Couldn't revert from version " + dbVersion
+ " to previous version " + version + "."); + " to previous version " + version + ".");
} }
} }
} catch (BadSqlGrammarException ex) } catch (BadSqlGrammarException ex)
...@@ -183,7 +211,8 @@ public class DBMigrationEngine ...@@ -183,7 +211,8 @@ public class DBMigrationEngine
JdbcTemplate template = new JdbcTemplate(dataSource); JdbcTemplate template = new JdbcTemplate(dataSource);
// TODO: Should be made transactionally save // TODO: Should be made transactionally save
template.execute(CREATE_DB_VERSION_TABLE); template.execute(CREATE_DB_VERSION_TABLE);
template.update(INSERT_DB_VERSION, new Object[] {new Date()}); template.update(INSERT_DB_VERSION, new Object[]
{ new Date() });
template.execute(createScript); template.execute(createScript);
if (initialDataScript != null) if (initialDataScript != null)
{ {
...@@ -191,7 +220,7 @@ public class DBMigrationEngine ...@@ -191,7 +220,7 @@ public class DBMigrationEngine
} }
} }
} }
private boolean databaseExists() private boolean databaseExists()
{ {
try try
...@@ -213,7 +242,7 @@ public class DBMigrationEngine ...@@ -213,7 +242,7 @@ public class DBMigrationEngine
throw new EnvironmentFailureException("Couldn't connect database server.", ex); throw new EnvironmentFailureException("Couldn't connect database server.", ex);
} }
} }
protected boolean isDBNotExistException(SQLException exception) protected boolean isDBNotExistException(SQLException exception)
{ {
String message = exception.getMessage(); String message = exception.getMessage();
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment