org.dbmaintain
Class DefaultDbMaintainer

java.lang.Object
  extended by org.dbmaintain.DefaultDbMaintainer
All Implemented Interfaces:
DbMaintainer

public class DefaultDbMaintainer
extends Object
implements DbMaintainer

Class that offers operations for automatically maintaining a database.

The updateDatabase(boolean) operation can be used to bring the database to the latest version. The markDatabaseAsUpToDate() operation updates the state of the database to indicate that all scripts have been executed, without actually executing them.

Author:
Filip Neven, Tim Ducheyne

Field Summary
protected  boolean allowOutOfSequenceExecutionOfPatchScripts
           
protected  ScriptIndexes baseLineRevision
           
protected  boolean cleanDb
           
protected  ConstraintsDisabler constraintsDisabler
           
protected  DBCleaner dbCleaner
           
protected  DBClearer dbClearer
           
protected  boolean disableConstraints
           
protected  ExecutedScriptInfoSource executedScriptInfoSource
           
protected  boolean fromScratchEnabled
          Indicates whether updating the database from scratch is enabled.
protected  long maxNrOfCharsWhenLoggingScriptContent
           
protected  ScriptRepository scriptRepository
           
protected  ScriptRunner scriptRunner
           
protected  ScriptUpdatesFormatter scriptUpdatesFormatter
           
protected  SequenceUpdater sequenceUpdater
           
protected  SQLHandler sqlHandler
           
protected  boolean updateSequences
           
protected  boolean useScriptFileLastModificationDates
          Defines whether script last modification dates can be used to decide whether an existing script has changed.
 
Constructor Summary
DefaultDbMaintainer(ScriptRunner scriptRunner, ScriptRepository scriptRepository, ExecutedScriptInfoSource executedScriptInfoSource, boolean fromScratchEnabled, boolean useScriptFileLastModificationDates, boolean allowOutOfSequenceExecutionOfPatchScripts, boolean cleanDb, boolean disableConstraints, boolean updateSequences, DBClearer dbClearer, DBCleaner dbCleaner, ConstraintsDisabler constraintsDisabler, SequenceUpdater sequenceUpdater, ScriptUpdatesFormatter scriptUpdatesFormatter, SQLHandler sqlHandler, long maxNrOfCharsWhenLoggingScriptContent, ScriptIndexes baseLineRevision)
          Creates a new instance
 
Method Summary
protected  void executePostprocessingScripts()
          Executes all postprocessing scripts
protected  void executeScript(Script script)
          Executes the given script and updates the database execution registry appropriately.
protected  void executeScripts(SortedSet<Script> scripts)
          Executes the given scripts and updates the database execution registry appropriately.
protected  void executeScriptUpdates(SortedSet<ScriptUpdate> scriptUpdates)
          Executes the given scripts and updates the database execution registry appropriately.
protected  Map<Script,ExecutedScript> getAlreadyExecutedScripts()
           
protected  String getErrorMessage(Script script, DbMaintainException e)
           
protected  String getErrorScriptOptionsMessage(Script script)
           
protected  SortedSet<ExecutedScript> getIncrementalScriptsThatFailedDuringLastUpdate()
           
protected  SortedSet<ExecutedScript> getRepeatableScriptsThatFailedDuringLastUpdate()
           
 ScriptUpdates getScriptUpdates()
          This operation calcutes and logs which script updates have been performed since the last database update.
protected  boolean isInitialDatabaseUpdate()
           
 void markDatabaseAsUpToDate()
          This operation updates the state of the database to indicate that all scripts have been executed, without actually executing them.
protected  void performRegularScriptRenamesInExecutedScripts(SortedSet<ScriptUpdate> regularScriptRenames)
          Updates the records in the DBMAINTAIN_SCRIPTS table for all scripts that were regularly renamed (i.e. renamed without changing the order of the incremental scripts.
protected  void removeDeletedRepeatableScriptsFromExecutedScripts(SortedSet<ScriptUpdate> repeatableScriptDeletions)
          Removes all executed scripts that indicate repeatable scripts that were removed since the last database update
 boolean updateDatabase(boolean dryRun)
          This operation can be used to bring the database to the latest version.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

executedScriptInfoSource

protected ExecutedScriptInfoSource executedScriptInfoSource

scriptRepository

protected ScriptRepository scriptRepository

scriptRunner

protected ScriptRunner scriptRunner

dbClearer

protected DBClearer dbClearer

dbCleaner

protected DBCleaner dbCleaner

constraintsDisabler

protected ConstraintsDisabler constraintsDisabler

sequenceUpdater

protected SequenceUpdater sequenceUpdater

sqlHandler

protected SQLHandler sqlHandler

cleanDb

protected boolean cleanDb

fromScratchEnabled

protected boolean fromScratchEnabled
Indicates whether updating the database from scratch is enabled. If true, the database is cleared before updating if an already executed script is modified


useScriptFileLastModificationDates

protected boolean useScriptFileLastModificationDates
Defines whether script last modification dates can be used to decide whether an existing script has changed. If set to true, the dbmaintainer will decide that a file didn't change since the last time if it's last modification date hasn't changed. If it did change, it will first calculate the checksum of the file to verify that the content really changed. Setting this property to true improves performance: if set to false the checksum of every script must be calculated for each run of the dbmaintainer.


allowOutOfSequenceExecutionOfPatchScripts

protected boolean allowOutOfSequenceExecutionOfPatchScripts

disableConstraints

protected boolean disableConstraints

updateSequences

protected boolean updateSequences

scriptUpdatesFormatter

protected ScriptUpdatesFormatter scriptUpdatesFormatter

maxNrOfCharsWhenLoggingScriptContent

protected long maxNrOfCharsWhenLoggingScriptContent

baseLineRevision

protected ScriptIndexes baseLineRevision
Constructor Detail

DefaultDbMaintainer

public DefaultDbMaintainer(ScriptRunner scriptRunner,
                           ScriptRepository scriptRepository,
                           ExecutedScriptInfoSource executedScriptInfoSource,
                           boolean fromScratchEnabled,
                           boolean useScriptFileLastModificationDates,
                           boolean allowOutOfSequenceExecutionOfPatchScripts,
                           boolean cleanDb,
                           boolean disableConstraints,
                           boolean updateSequences,
                           DBClearer dbClearer,
                           DBCleaner dbCleaner,
                           ConstraintsDisabler constraintsDisabler,
                           SequenceUpdater sequenceUpdater,
                           ScriptUpdatesFormatter scriptUpdatesFormatter,
                           SQLHandler sqlHandler,
                           long maxNrOfCharsWhenLoggingScriptContent,
                           ScriptIndexes baseLineRevision)
Creates a new instance

Parameters:
scriptRunner - runner that executes the database scripts
scriptRepository - provides access to all database scripts
executedScriptInfoSource - provides information about which scripts were already executed on the database
fromScratchEnabled - if true, the database will be cleared and recreated from scratch if needed
useScriptFileLastModificationDates - if true, the dbmaintainer decides that a script hasn't changed if the last modification date is identical to the one of the last update, without looking at the contents of the script
allowOutOfSequenceExecutionOfPatchScripts - if true, patch scripts can be executed out-of-sequence
cleanDb - if true, the data from all tables is removed before performing any updates
disableConstraints - if true, all foreign key and not null constraints are automatically disabled or removed after each update
updateSequences - if true, the value of all sequences is set to a minimal value after each update
dbClearer - helper object that clears the database, i.e. drop all database objects
dbCleaner - helper object that cleans the database, i.e. remove the data from all tables
constraintsDisabler - helper object that disables or removes all foreign key or not null constraints
sequenceUpdater - helper object that updates all sequences to a minimal value
scriptUpdatesFormatter - helper object that formats the script updates in a well-readable format for the user
sqlHandler - helper object that performs sql statements on the database
maxNrOfCharsWhenLoggingScriptContent - The maximum length of a script that is logged in an exception, 0 to not log any script content
baseLineRevision - The baseline revision. If set, all scripts with a lower revision will be ignored
Method Detail

updateDatabase

public boolean updateDatabase(boolean dryRun)
This operation can be used to bring the database to the latest version. First it checks which scripts were already applied to the database and executes the new scripts or the updated repeatable scripts. If an existing incremental script was changed, removed, or if a new incremental script has been added with a lower index than one that was already executed, an error is given; unless the option is enabled: in that case all database objects at the end.

Specified by:
updateDatabase in interface DbMaintainer
Parameters:
dryRun - if true, no updates have to be performed on the database - we do a simulation of the database update instead of actually performing the database update.
Returns:
whether updates were performed on the database

getScriptUpdates

public ScriptUpdates getScriptUpdates()
This operation calcutes and logs which script updates have been performed since the last database update.

Returns:
the scripts that have been updated since the last database update

isInitialDatabaseUpdate

protected boolean isInitialDatabaseUpdate()
Returns:
Whether we are running dbmaintain for the first time. If there are no scripts available yet, this method returns false.

executePostprocessingScripts

protected void executePostprocessingScripts()
Executes all postprocessing scripts


getAlreadyExecutedScripts

protected Map<Script,ExecutedScript> getAlreadyExecutedScripts()
Returns:
The already executed scripts, as a map from Script => ExecutedScript

removeDeletedRepeatableScriptsFromExecutedScripts

protected void removeDeletedRepeatableScriptsFromExecutedScripts(SortedSet<ScriptUpdate> repeatableScriptDeletions)
Removes all executed scripts that indicate repeatable scripts that were removed since the last database update

Parameters:
repeatableScriptDeletions - The scripts that were removed since the last database updates

performRegularScriptRenamesInExecutedScripts

protected void performRegularScriptRenamesInExecutedScripts(SortedSet<ScriptUpdate> regularScriptRenames)
Updates the records in the DBMAINTAIN_SCRIPTS table for all scripts that were regularly renamed (i.e. renamed without changing the order of the incremental scripts.

Parameters:
regularScriptRenames - the scripts that were regularly renamed

markDatabaseAsUpToDate

public void markDatabaseAsUpToDate()
This operation updates the state of the database to indicate that all scripts have been executed, without actually executing them. This can be useful when you want to start using DbMaintain on an existing database, or after having fixed a problem directly on the database.

Specified by:
markDatabaseAsUpToDate in interface DbMaintainer

executeScriptUpdates

protected void executeScriptUpdates(SortedSet<ScriptUpdate> scriptUpdates)
Executes the given scripts and updates the database execution registry appropriately. After each successful script execution, the script execution is registered in the database and marked as successful. If a script execution fails, the script execution is registered in the database and marked as unsuccessful.

Parameters:
scriptUpdates - the script updates to be executed

executeScripts

protected void executeScripts(SortedSet<Script> scripts)
Executes the given scripts and updates the database execution registry appropriately. After each successful script execution, the script execution is registered in the database and marked as successful. If a script execution fails, the script execution is registered in the database and marked as unsuccessful.

Parameters:
scripts - the scripts to be executed on the database

executeScript

protected void executeScript(Script script)
Executes the given script and updates the database execution registry appropriately. If successfully, the script execution is registered in the database and marked as successful. If an error occurred executing the script, the script execution is registered in the database and marked as unsuccessful.

Parameters:
script - The script to execute, not null

getErrorMessage

protected String getErrorMessage(Script script,
                                 DbMaintainException e)

getErrorScriptOptionsMessage

protected String getErrorScriptOptionsMessage(Script script)

getIncrementalScriptsThatFailedDuringLastUpdate

protected SortedSet<ExecutedScript> getIncrementalScriptsThatFailedDuringLastUpdate()
Returns:
the incremental scripts that failed during the last database update

getRepeatableScriptsThatFailedDuringLastUpdate

protected SortedSet<ExecutedScript> getRepeatableScriptsThatFailedDuringLastUpdate()
Returns:
the repeatable scripts that failed during the last database update


Copyright © 2011. All Rights Reserved.