Skip to content
Snippets Groups Projects
Commit 2289de6a authored by felmer's avatar felmer
Browse files

LMS-529 the necessity of sequence updating can be configured.

SVN: 7949
parent 6081921f
No related branches found
No related tags found
No related merge requests found
...@@ -57,6 +57,8 @@ public class DatabaseConfigurationContext implements DisposableBean ...@@ -57,6 +57,8 @@ public class DatabaseConfigurationContext implements DisposableBean
} }
private ISequenceNameMapper sequenceNameMapper; private ISequenceNameMapper sequenceNameMapper;
private boolean sequenceUpdateNeeded;
private String adminUser; private String adminUser;
...@@ -419,6 +421,16 @@ public class DatabaseConfigurationContext implements DisposableBean ...@@ -419,6 +421,16 @@ public class DatabaseConfigurationContext implements DisposableBean
this.sequenceNameMapper = sequenceNameMapper; this.sequenceNameMapper = sequenceNameMapper;
} }
public final boolean isSequenceUpdateNeeded()
{
return sequenceUpdateNeeded;
}
public final void setSequenceUpdateNeeded(boolean sequenceUpdateNeeded)
{
this.sequenceUpdateNeeded = sequenceUpdateNeeded;
}
/** /**
* Returns <code>true</code> if the current database should be dropped and (re)created from * Returns <code>true</code> if the current database should be dropped and (re)created from
* scratch. * scratch.
......
...@@ -20,6 +20,7 @@ import java.sql.SQLException; ...@@ -20,6 +20,7 @@ import java.sql.SQLException;
import javax.sql.DataSource; import javax.sql.DataSource;
import ch.systemsx.cisd.common.db.ISequenceNameMapper;
import ch.systemsx.cisd.common.db.ISqlScriptExecutor; import ch.systemsx.cisd.common.db.ISqlScriptExecutor;
import ch.systemsx.cisd.common.exceptions.CheckedExceptionTunnel; import ch.systemsx.cisd.common.exceptions.CheckedExceptionTunnel;
import ch.systemsx.cisd.dbmigration.DatabaseConfigurationContext; import ch.systemsx.cisd.dbmigration.DatabaseConfigurationContext;
...@@ -60,7 +61,9 @@ public class PostgreSQLDAOFactory implements IDAOFactory ...@@ -60,7 +61,9 @@ public class PostgreSQLDAOFactory implements IDAOFactory
databaseVersionLogDAO = new DatabaseVersionLogDAO(dataSource, context.getLobHandler()); databaseVersionLogDAO = new DatabaseVersionLogDAO(dataSource, context.getLobHandler());
try try
{ {
massUploader = new PostgreSQLMassUploader(dataSource, context.getSequenceNameMapper()); ISequenceNameMapper mapper = context.getSequenceNameMapper();
boolean sequenceUpdateNeeded = context.isSequenceUpdateNeeded();
massUploader = new PostgreSQLMassUploader(dataSource, mapper, sequenceUpdateNeeded);
} catch (final SQLException ex) } catch (final SQLException ex)
{ {
throw new CheckedExceptionTunnel(ex); throw new CheckedExceptionTunnel(ex);
......
...@@ -40,6 +40,7 @@ import org.springframework.jdbc.core.simple.SimpleJdbcDaoSupport; ...@@ -40,6 +40,7 @@ import org.springframework.jdbc.core.simple.SimpleJdbcDaoSupport;
import ch.systemsx.cisd.common.db.ISequenceNameMapper; import ch.systemsx.cisd.common.db.ISequenceNameMapper;
import ch.systemsx.cisd.common.exceptions.CheckedExceptionTunnel; import ch.systemsx.cisd.common.exceptions.CheckedExceptionTunnel;
import ch.systemsx.cisd.common.exceptions.EnvironmentFailureException;
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;
import ch.systemsx.cisd.dbmigration.IMassUploader; import ch.systemsx.cisd.dbmigration.IMassUploader;
...@@ -60,14 +61,17 @@ public class PostgreSQLMassUploader extends SimpleJdbcDaoSupport implements IMas ...@@ -60,14 +61,17 @@ public class PostgreSQLMassUploader extends SimpleJdbcDaoSupport implements IMas
private final ISequenceNameMapper sequenceNameMapper; private final ISequenceNameMapper sequenceNameMapper;
private final boolean sequenceUpdateNeeded;
/** /**
* Creates an instance for the specified data source and sequence mapper. * Creates an instance for the specified data source and sequence mapper.
*/ */
public PostgreSQLMassUploader(final DataSource dataSource, public PostgreSQLMassUploader(final DataSource dataSource,
final ISequenceNameMapper sequenceNameMapper) throws SQLException final ISequenceNameMapper sequenceNameMapper, boolean sequenceUpdateNeeded) throws SQLException
{ {
this.dataSource = dataSource; this.dataSource = dataSource;
this.sequenceNameMapper = sequenceNameMapper; this.sequenceNameMapper = sequenceNameMapper;
this.sequenceUpdateNeeded = sequenceUpdateNeeded;
setDataSource(dataSource); setDataSource(dataSource);
} }
...@@ -88,9 +92,17 @@ public class PostgreSQLMassUploader extends SimpleJdbcDaoSupport implements IMas ...@@ -88,9 +92,17 @@ public class PostgreSQLMassUploader extends SimpleJdbcDaoSupport implements IMas
{ {
performMassUpload(file, tables); performMassUpload(file, tables);
} }
for (final String name : tables) if (sequenceUpdateNeeded)
{ {
fixSequence(name); boolean successful = true;
for (final String name : tables)
{
successful &= fixSequence(name);
}
if (successful == false)
{
throw new EnvironmentFailureException("At least one sequence couldn't be updated.");
}
} }
} }
...@@ -123,8 +135,8 @@ public class PostgreSQLMassUploader extends SimpleJdbcDaoSupport implements IMas ...@@ -123,8 +135,8 @@ public class PostgreSQLMassUploader extends SimpleJdbcDaoSupport implements IMas
{ {
getCopyManager().copyInQuery( getCopyManager().copyInQuery(
"COPY " + tableName + " FROM STDIN WITH CSV HEADER", is); "COPY " + tableName + " FROM STDIN WITH CSV HEADER", is);
tables.add(tableName);
} }
tables.add(tableName);
} finally } finally
{ {
IOUtils.closeQuietly(is); IOUtils.closeQuietly(is);
...@@ -135,12 +147,12 @@ public class PostgreSQLMassUploader extends SimpleJdbcDaoSupport implements IMas ...@@ -135,12 +147,12 @@ public class PostgreSQLMassUploader extends SimpleJdbcDaoSupport implements IMas
} }
} }
private final void fixSequence(final String tableName) private final boolean fixSequence(final String tableName)
{ {
final String sequenceName = sequenceNameMapper.getSequencerForTable(tableName); final String sequenceName = sequenceNameMapper.getSequencerForTable(tableName);
if (sequenceName == null) if (sequenceName == null)
{ {
return; return true;
} }
try try
{ {
...@@ -154,10 +166,12 @@ public class PostgreSQLMassUploader extends SimpleJdbcDaoSupport implements IMas ...@@ -154,10 +166,12 @@ public class PostgreSQLMassUploader extends SimpleJdbcDaoSupport implements IMas
operationLog.info("Updating sequence " + sequenceName + " for table " + tableName operationLog.info("Updating sequence " + sequenceName + " for table " + tableName
+ " to value " + newSequenceValue); + " to value " + newSequenceValue);
} }
return true;
} catch (final DataAccessException ex) } catch (final DataAccessException ex)
{ {
operationLog.error("Failed to set new value for sequence '" + sequenceName operationLog.error("Failed to set new value for sequence '" + sequenceName
+ "' of table '" + tableName + "'.", ex); + "' of table '" + tableName + "'.", ex);
return false;
} }
} }
......
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