Skip to content
Snippets Groups Projects
Commit 73e86d71 authored by brinn's avatar brinn
Browse files

review: minor refactorings, improved log messages and an additional unit test

SVN: 760
parent 6d0b84f0
No related branches found
No related tags found
No related merge requests found
...@@ -81,18 +81,18 @@ public class DBMigrationEngine ...@@ -81,18 +81,18 @@ public class DBMigrationEngine
String databaseVersion = entry.getVersion(); String databaseVersion = entry.getVersion();
if (version.equals(databaseVersion)) if (version.equals(databaseVersion))
{ {
if (operationLog.isInfoEnabled()) if (operationLog.isDebugEnabled())
{ {
String databaseName = adminDAO.getDatabaseName(); String databaseName = adminDAO.getDatabaseName();
operationLog.info("No migration needed for database '" + databaseName + "'. Current version: " operationLog.debug("No migration needed for database '" + databaseName + "'. It has the right version ("
+ version + "."); + version + ").");
} }
} else if (version.compareTo(databaseVersion) > 0) } else if (version.compareTo(databaseVersion) > 0)
{ {
if (operationLog.isInfoEnabled()) if (operationLog.isInfoEnabled())
{ {
String databaseName = adminDAO.getDatabaseName(); String databaseName = adminDAO.getDatabaseName();
operationLog.info("Migrating database '" + databaseName + "' from version " + databaseVersion operationLog.info("Trying to migrate database '" + databaseName + "' from version " + databaseVersion
+ " to " + version + "."); + " to " + version + ".");
} }
migrate(databaseVersion, version); migrate(databaseVersion, version);
...@@ -105,8 +105,8 @@ public class DBMigrationEngine ...@@ -105,8 +105,8 @@ public class DBMigrationEngine
} else } else
{ {
String databaseName = adminDAO.getDatabaseName(); String databaseName = adminDAO.getDatabaseName();
String message = "Couldn't revert database '" + databaseName + "' from version " String message = "Cannot revert database '" + databaseName + "' from version "
+ databaseVersion + " to previous version " + version + "."; + databaseVersion + " to earlier version " + version + ".";
operationLog.error(message); operationLog.error(message);
throw new EnvironmentFailureException(message); throw new EnvironmentFailureException(message);
} }
...@@ -190,8 +190,8 @@ public class DBMigrationEngine ...@@ -190,8 +190,8 @@ public class DBMigrationEngine
Script migrationScript = scriptProvider.getMigrationScript(version, nextVersion); Script migrationScript = scriptProvider.getMigrationScript(version, nextVersion);
if (migrationScript == null) if (migrationScript == null)
{ {
String databaseName = adminDAO.getDatabaseName(); final String databaseName = adminDAO.getDatabaseName();
String message = "Cannot migrate database '" + databaseName + "' from version " + version + " to " final String message = "Cannot migrate database '" + databaseName + "' from version " + version + " to "
+ nextVersion + " because of missing migration script."; + nextVersion + " because of missing migration script.";
operationLog.error(message); operationLog.error(message);
throw new EnvironmentFailureException(message); throw new EnvironmentFailureException(message);
...@@ -201,7 +201,8 @@ public class DBMigrationEngine ...@@ -201,7 +201,8 @@ public class DBMigrationEngine
} while (version.equals(toVersion) == false); } while (version.equals(toVersion) == false);
} }
private String increment(String version) // @Private
static String increment(String version)
{ {
char[] characters = new char[version.length()]; char[] characters = new char[version.length()];
version.getChars(0, characters.length, characters, 0); version.getChars(0, characters.length, characters, 0);
...@@ -220,10 +221,10 @@ public class DBMigrationEngine ...@@ -220,10 +221,10 @@ public class DBMigrationEngine
return new String(characters); return new String(characters);
} }
private void executeScript(Script script, String version) throws Error private void executeScript(Script script, String version)
{ {
String code = script.getCode(); final String name = script.getName();
String name = script.getName(); final String code = script.getCode();
logDAO.logStart(version, name, code); logDAO.logStart(version, name, code);
try try
{ {
...@@ -231,7 +232,7 @@ public class DBMigrationEngine ...@@ -231,7 +232,7 @@ public class DBMigrationEngine
logDAO.logSuccess(version, name); logDAO.logSuccess(version, name);
} catch (Throwable t) } catch (Throwable t)
{ {
operationLog.error("Executing script '" + name + "' failed", t); operationLog.error("Executing script '" + name + "' failed.", t);
logDAO.logFailure(version, name, t); logDAO.logFailure(version, name, t);
if (t instanceof RuntimeException) if (t instanceof RuntimeException)
{ {
......
...@@ -40,4 +40,15 @@ class DBUtilities ...@@ -40,4 +40,15 @@ class DBUtilities
} }
private DBUtilities() {} private DBUtilities() {}
/**
* Checks whether given <code>DataAccessException</code> is caused by a "user already exists" exception.
* <p>
* This is database specific.
* </p>
*/
static boolean ownerAlreadyExists(DataAccessException ex) {
// 42710 DUPLICATE OBJECT
return SQLStateUtils.isDuplicateObject(SQLStateUtils.getSqlState(ex));
}
} }
...@@ -20,28 +20,27 @@ import javax.sql.DataSource; ...@@ -20,28 +20,27 @@ import javax.sql.DataSource;
import org.apache.log4j.Logger; import org.apache.log4j.Logger;
import org.springframework.dao.DataAccessException; import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.simple.SimpleJdbcDaoSupport; import org.springframework.jdbc.core.simple.SimpleJdbcDaoSupport;
import ch.systemsx.cisd.common.db.SQLStateUtils;
import ch.systemsx.cisd.common.logging.LogCategory; import ch.systemsx.cisd.common.logging.LogCategory;
import ch.systemsx.cisd.common.logging.LogFactory; import ch.systemsx.cisd.common.logging.LogFactory;
/** /**
* Implementation of {@link IDatabaseAdminDAO} for PostgreSQL. * Implementation of {@link IDatabaseAdminDAO} for PostgreSQL.
* *
* @author Franz-Josef Elmer * @author Franz-Josef Elmer
*/ */
public class PostgreSQLAdminDAO extends SimpleJdbcDaoSupport implements IDatabaseAdminDAO public class PostgreSQLAdminDAO extends SimpleJdbcDaoSupport implements IDatabaseAdminDAO
{ {
private static final Logger operationLog = LogFactory.getLogger(LogCategory.OPERATION, PostgreSQLAdminDAO.class); private static final Logger operationLog = LogFactory.getLogger(LogCategory.OPERATION, PostgreSQLAdminDAO.class);
private final String owner; private final String owner;
private final String database; private final String database;
/** /**
* Creates an instance. * Creates an instance.
* *
* @param dataSource Data source able to create/drop the specified database. * @param dataSource Data source able to create/drop the specified database.
* @param owner Owner to be created if it doesn't exist. * @param owner Owner to be created if it doesn't exist.
* @param database Name of the database. * @param database Name of the database.
...@@ -52,7 +51,7 @@ public class PostgreSQLAdminDAO extends SimpleJdbcDaoSupport implements IDatabas ...@@ -52,7 +51,7 @@ public class PostgreSQLAdminDAO extends SimpleJdbcDaoSupport implements IDatabas
this.database = database; this.database = database;
setDataSource(dataSource); setDataSource(dataSource);
} }
public String getDatabaseName() public String getDatabaseName()
{ {
return database; return database;
...@@ -65,38 +64,28 @@ public class PostgreSQLAdminDAO extends SimpleJdbcDaoSupport implements IDatabas ...@@ -65,38 +64,28 @@ public class PostgreSQLAdminDAO extends SimpleJdbcDaoSupport implements IDatabas
getJdbcTemplate().execute("create user " + owner); getJdbcTemplate().execute("create user " + owner);
} catch (DataAccessException ex) } catch (DataAccessException ex)
{ {
if (ownerAlreadyExists(ex)) if (DBUtilities.ownerAlreadyExists(ex))
{ {
if (operationLog.isInfoEnabled()) if (operationLog.isInfoEnabled())
{ {
operationLog.info("Owner '" + owner + "' already exists."); operationLog.info("Owner '" + owner + "' already exists.");
} }
} else { } else
{
operationLog.error("Database owner couldn't be created:", ex); operationLog.error("Database owner couldn't be created:", ex);
throw ex; throw ex;
} }
} }
} }
/**
* Checks whether given <code>DataAccessException</code> is caused by a "user already exists" exception.
* <p>
* This is database specific.
* </p>
*/
protected boolean ownerAlreadyExists(DataAccessException ex) {
// 42710 DUPLICATE OBJECT
return SQLStateUtils.isDuplicateObject(SQLStateUtils.getSqlState(ex));
}
public void createDatabase() public void createDatabase()
{ {
JdbcTemplate template = getJdbcTemplate(); getJdbcTemplate().execute(
template.execute("create database " + database + " with owner = " + owner "create database " + database + " with owner = " + owner
+ " encoding = 'utf8' tablespace = pg_default;" + " encoding = 'utf8' tablespace = pg_default;" + "alter database " + database
+ "alter database " + database + " set default_with_oids = off;"); + " set default_with_oids = off;");
} }
public void dropDatabase() public void dropDatabase()
{ {
try try
...@@ -110,5 +99,5 @@ public class PostgreSQLAdminDAO extends SimpleJdbcDaoSupport implements IDatabas ...@@ -110,5 +99,5 @@ public class PostgreSQLAdminDAO extends SimpleJdbcDaoSupport implements IDatabas
} }
} }
} }
} }
...@@ -35,7 +35,7 @@ public class PostgreSQLDAOFactory implements IDAOFactory ...@@ -35,7 +35,7 @@ public class PostgreSQLDAOFactory implements IDAOFactory
public PostgreSQLDAOFactory(DatabaseConfigurationContext context) public PostgreSQLDAOFactory(DatabaseConfigurationContext context)
{ {
databaseDAO = new PostgreSQLAdminDAO(context.getAdminDataSource(), context.getOwner(), context.getDatabaseName()); databaseDAO = new PostgreSQLAdminDAO(context.getAdminDataSource(), context.getOwner(), context.getDatabaseName());
DataSource dataSource = context.getDataSource(); final DataSource dataSource = context.getDataSource();
sqlScriptExecutor = new SqlScriptExecutor(dataSource); sqlScriptExecutor = new SqlScriptExecutor(dataSource);
databaseVersionLogDAO = new DatabaseVersionLogDAO(dataSource, context.getLobHandler()); databaseVersionLogDAO = new DatabaseVersionLogDAO(dataSource, context.getLobHandler());
} }
......
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