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
}
private ISequenceNameMapper sequenceNameMapper;
private boolean sequenceUpdateNeeded;
private String adminUser;
......@@ -419,6 +421,16 @@ public class DatabaseConfigurationContext implements DisposableBean
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
* scratch.
......
......@@ -20,6 +20,7 @@ import java.sql.SQLException;
import javax.sql.DataSource;
import ch.systemsx.cisd.common.db.ISequenceNameMapper;
import ch.systemsx.cisd.common.db.ISqlScriptExecutor;
import ch.systemsx.cisd.common.exceptions.CheckedExceptionTunnel;
import ch.systemsx.cisd.dbmigration.DatabaseConfigurationContext;
......@@ -60,7 +61,9 @@ public class PostgreSQLDAOFactory implements IDAOFactory
databaseVersionLogDAO = new DatabaseVersionLogDAO(dataSource, context.getLobHandler());
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)
{
throw new CheckedExceptionTunnel(ex);
......
......@@ -40,6 +40,7 @@ import org.springframework.jdbc.core.simple.SimpleJdbcDaoSupport;
import ch.systemsx.cisd.common.db.ISequenceNameMapper;
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.LogFactory;
import ch.systemsx.cisd.dbmigration.IMassUploader;
......@@ -60,14 +61,17 @@ public class PostgreSQLMassUploader extends SimpleJdbcDaoSupport implements IMas
private final ISequenceNameMapper sequenceNameMapper;
private final boolean sequenceUpdateNeeded;
/**
* Creates an instance for the specified data source and sequence mapper.
*/
public PostgreSQLMassUploader(final DataSource dataSource,
final ISequenceNameMapper sequenceNameMapper) throws SQLException
final ISequenceNameMapper sequenceNameMapper, boolean sequenceUpdateNeeded) throws SQLException
{
this.dataSource = dataSource;
this.sequenceNameMapper = sequenceNameMapper;
this.sequenceUpdateNeeded = sequenceUpdateNeeded;
setDataSource(dataSource);
}
......@@ -88,9 +92,17 @@ public class PostgreSQLMassUploader extends SimpleJdbcDaoSupport implements IMas
{
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
{
getCopyManager().copyInQuery(
"COPY " + tableName + " FROM STDIN WITH CSV HEADER", is);
tables.add(tableName);
}
tables.add(tableName);
} finally
{
IOUtils.closeQuietly(is);
......@@ -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);
if (sequenceName == null)
{
return;
return true;
}
try
{
......@@ -154,10 +166,12 @@ public class PostgreSQLMassUploader extends SimpleJdbcDaoSupport implements IMas
operationLog.info("Updating sequence " + sequenceName + " for table " + tableName
+ " to value " + newSequenceValue);
}
return true;
} catch (final DataAccessException ex)
{
operationLog.error("Failed to set new value for sequence '" + sequenceName
+ "' 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