org.unitils.dbmaintainer.version.impl
Class DefaultExecutedScriptInfoSource

java.lang.Object
  extended by org.unitils.dbmaintainer.util.BaseDatabaseAccessor
      extended by org.unitils.dbmaintainer.version.impl.DefaultExecutedScriptInfoSource
All Implemented Interfaces:
DatabaseAccessing, ExecutedScriptInfoSource

public class DefaultExecutedScriptInfoSource
extends BaseDatabaseAccessor
implements ExecutedScriptInfoSource

Implementation of VersionSource that stores the version in the database. The version is stored in the table whose name is defined by the property PROPERTY_EXECUTED_SCRIPTS_TABLE_NAME. The version index column name is defined by PROPERTY_FILE_NAME_COLUMN_NAME, the version timestamp colmumn name is defined by PROPERTY_SCRIPT_VERSION_COLUMN_NAME. The last updated succeeded column name is defined by PROPERTY_EXECUTED_AT_COLUMN_NAME.

Author:
Filip Neven, Tim Ducheyne

Field Summary
protected  boolean autoCreateExecutedScriptsTable
          True if the scripts table should be created automatically if it does not exist yet
protected  String checksumColumnName
          The name of the database column in which the checksum calculated on the script content is stored
protected  int checksumColumnSize
           
protected  String executedAtColumnName
          The name of the database column in which the script execution timestamp is stored
protected  int executedAtColumnSize
           
protected  Set<ExecutedScript> executedScripts
           
protected  String executedScriptsTableName
          The name of the database table in which the executed script info is stored
protected  String fileLastModifiedAtColumnName
          The name of the database column in which the file last modification timestamp is stored
protected  String fileNameColumnName
          The name of the database column in which the script name is stored
protected  int fileNameColumnSize
           
static String PROPERTY_AUTO_CREATE_EXECUTED_SCRIPTS_TABLE
           
static String PROPERTY_CHECKSUM_COLUMN_NAME
           
static String PROPERTY_CHECKSUM_COLUMN_SIZE
           
static String PROPERTY_EXECUTED_AT_COLUMN_NAME
           
static String PROPERTY_EXECUTED_AT_COLUMN_SIZE
           
static String PROPERTY_EXECUTED_SCRIPTS_TABLE_NAME
           
static String PROPERTY_FILE_LAST_MODIFIED_AT_COLUMN_NAME
           
static String PROPERTY_FILE_NAME_COLUMN_NAME
           
static String PROPERTY_FILE_NAME_COLUMN_SIZE
           
static String PROPERTY_SCRIPT_VERSION_COLUMN_NAME
           
static String PROPERTY_SCRIPT_VERSION_COLUMN_SIZE
           
static String PROPERTY_SUCCEEDED_COLUMN_NAME
           
static String PROPERTY_TIMESTAMP_FORMAT
           
protected  String succeededColumnName
          The name of the database column in which the script name is stored
protected  DateFormat timestampFormat
          Format of the contents of the executed_at column
protected  String versionColumnName
          The name of the database column in which the script version is stored
protected  int versionColumnSize
           
 
Fields inherited from class org.unitils.dbmaintainer.util.BaseDatabaseAccessor
configuration, dbSupports, defaultDbSupport, sqlHandler
 
Constructor Summary
DefaultExecutedScriptInfoSource()
           
 
Method Summary
protected  boolean checkExecutedScriptsTable()
          Checks if the version table and columns are available and if a record exists in which the version info is stored.
 void clearAllExecutedScripts()
          Clears all script executions that have been registered.
protected  void createExecutedScriptsTable()
          Creates the version table and inserts a version record.
protected  void doClearAllExecutedScripts()
           
protected  Set<ExecutedScript> doGetExecutedScripts()
          Precondition: The table dbmaintain_scripts must exist
protected  void doInit(Properties configuration)
          Initializes the name of the version table and its columns using the given configuration.
protected  void doRegisterExecutedScript(ExecutedScript executedScript)
          Registers the fact that the given script has been executed on the database Precondition: The table dbmaintain_scripts must exist
protected  void doSaveExecutedScript(ExecutedScript executedScript)
          Saves the given registered script Precondition: The table dbmaintain_scripts must exist
protected  void doUpdateExecutedScript(ExecutedScript executedScript)
          Updates the given registered script Precondition: The table dbmaintain_scripts must exist
protected  String getCreateExecutedScriptsTableStatement()
           
 Set<ExecutedScript> getExecutedScripts()
           
protected  boolean isExecutedScriptsTableValid()
          Checks if the version table and columns are available and if a record exists in which the version info is stored.
 boolean isFromScratchUpdateRecommended()
          This method returns whether a from scratch update is recommended: It will return true if the database is in it's initial state (i.e. the dbmaintain_scripts table doesn't exist yet or is invalid) and the autoCreateExecutedScriptsTable property is set to true.
 void registerExecutedScript(ExecutedScript executedScript)
          Registers the fact that the given script has been executed on the database
 void updateExecutedScript(ExecutedScript executedScript)
          Updates the given registered script
 
Methods inherited from class org.unitils.dbmaintainer.util.BaseDatabaseAccessor
getDbSupport, init
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface org.unitils.dbmaintainer.util.DatabaseAccessing
init
 

Field Detail

PROPERTY_EXECUTED_SCRIPTS_TABLE_NAME

public static final String PROPERTY_EXECUTED_SCRIPTS_TABLE_NAME
See Also:
Constant Field Values

PROPERTY_FILE_NAME_COLUMN_NAME

public static final String PROPERTY_FILE_NAME_COLUMN_NAME
See Also:
Constant Field Values

PROPERTY_FILE_NAME_COLUMN_SIZE

public static final String PROPERTY_FILE_NAME_COLUMN_SIZE
See Also:
Constant Field Values

PROPERTY_SCRIPT_VERSION_COLUMN_NAME

public static final String PROPERTY_SCRIPT_VERSION_COLUMN_NAME
See Also:
Constant Field Values

PROPERTY_SCRIPT_VERSION_COLUMN_SIZE

public static final String PROPERTY_SCRIPT_VERSION_COLUMN_SIZE
See Also:
Constant Field Values

PROPERTY_FILE_LAST_MODIFIED_AT_COLUMN_NAME

public static final String PROPERTY_FILE_LAST_MODIFIED_AT_COLUMN_NAME
See Also:
Constant Field Values

PROPERTY_CHECKSUM_COLUMN_NAME

public static final String PROPERTY_CHECKSUM_COLUMN_NAME
See Also:
Constant Field Values

PROPERTY_CHECKSUM_COLUMN_SIZE

public static final String PROPERTY_CHECKSUM_COLUMN_SIZE
See Also:
Constant Field Values

PROPERTY_EXECUTED_AT_COLUMN_NAME

public static final String PROPERTY_EXECUTED_AT_COLUMN_NAME
See Also:
Constant Field Values

PROPERTY_EXECUTED_AT_COLUMN_SIZE

public static final String PROPERTY_EXECUTED_AT_COLUMN_SIZE
See Also:
Constant Field Values

PROPERTY_SUCCEEDED_COLUMN_NAME

public static final String PROPERTY_SUCCEEDED_COLUMN_NAME
See Also:
Constant Field Values

PROPERTY_AUTO_CREATE_EXECUTED_SCRIPTS_TABLE

public static final String PROPERTY_AUTO_CREATE_EXECUTED_SCRIPTS_TABLE
See Also:
Constant Field Values

PROPERTY_TIMESTAMP_FORMAT

public static final String PROPERTY_TIMESTAMP_FORMAT
See Also:
Constant Field Values

executedScripts

protected Set<ExecutedScript> executedScripts

executedScriptsTableName

protected String executedScriptsTableName
The name of the database table in which the executed script info is stored


fileNameColumnName

protected String fileNameColumnName
The name of the database column in which the script name is stored


fileNameColumnSize

protected int fileNameColumnSize

versionColumnName

protected String versionColumnName
The name of the database column in which the script version is stored


versionColumnSize

protected int versionColumnSize

fileLastModifiedAtColumnName

protected String fileLastModifiedAtColumnName
The name of the database column in which the file last modification timestamp is stored


checksumColumnName

protected String checksumColumnName
The name of the database column in which the checksum calculated on the script content is stored


checksumColumnSize

protected int checksumColumnSize

executedAtColumnName

protected String executedAtColumnName
The name of the database column in which the script execution timestamp is stored


executedAtColumnSize

protected int executedAtColumnSize

succeededColumnName

protected String succeededColumnName
The name of the database column in which the script name is stored


autoCreateExecutedScriptsTable

protected boolean autoCreateExecutedScriptsTable
True if the scripts table should be created automatically if it does not exist yet


timestampFormat

protected DateFormat timestampFormat
Format of the contents of the executed_at column

Constructor Detail

DefaultExecutedScriptInfoSource

public DefaultExecutedScriptInfoSource()
Method Detail

doInit

protected void doInit(Properties configuration)
Initializes the name of the version table and its columns using the given configuration.

Overrides:
doInit in class BaseDatabaseAccessor
Parameters:
configuration - the configuration, not null

isFromScratchUpdateRecommended

public boolean isFromScratchUpdateRecommended()
This method returns whether a from scratch update is recommended: It will return true if the database is in it's initial state (i.e. the dbmaintain_scripts table doesn't exist yet or is invalid) and the autoCreateExecutedScriptsTable property is set to true.

The reasoning behind this is that before executing the first script, it's a good idea to clear the database in order to start with a clean situation.

Specified by:
isFromScratchUpdateRecommended in interface ExecutedScriptInfoSource
Returns:
True if a from-scratch update is recommended

getExecutedScripts

public Set<ExecutedScript> getExecutedScripts()
Specified by:
getExecutedScripts in interface ExecutedScriptInfoSource
Returns:
All scripts that were registered as executed on the database

doGetExecutedScripts

protected Set<ExecutedScript> doGetExecutedScripts()
Precondition: The table dbmaintain_scripts must exist

Returns:
All scripts that were registered as executed on the database

registerExecutedScript

public void registerExecutedScript(ExecutedScript executedScript)
Registers the fact that the given script has been executed on the database

Specified by:
registerExecutedScript in interface ExecutedScriptInfoSource
Parameters:
executedScript - The script that was executed on the database

doRegisterExecutedScript

protected void doRegisterExecutedScript(ExecutedScript executedScript)
Registers the fact that the given script has been executed on the database Precondition: The table dbmaintain_scripts must exist

Parameters:
executedScript - The script that was executed on the database

updateExecutedScript

public void updateExecutedScript(ExecutedScript executedScript)
Updates the given registered script

Specified by:
updateExecutedScript in interface ExecutedScriptInfoSource
Parameters:
executedScript - The script, not null

doSaveExecutedScript

protected void doSaveExecutedScript(ExecutedScript executedScript)
Saves the given registered script Precondition: The table dbmaintain_scripts must exist

Parameters:
executedScript - The script, not null

doUpdateExecutedScript

protected void doUpdateExecutedScript(ExecutedScript executedScript)
Updates the given registered script Precondition: The table dbmaintain_scripts must exist

Parameters:
executedScript - The script, not null

clearAllExecutedScripts

public void clearAllExecutedScripts()
Clears all script executions that have been registered. After having invoked this method, getExecutedScripts() will return an empty set.

Specified by:
clearAllExecutedScripts in interface ExecutedScriptInfoSource

doClearAllExecutedScripts

protected void doClearAllExecutedScripts()

checkExecutedScriptsTable

protected boolean checkExecutedScriptsTable()
Checks if the version table and columns are available and if a record exists in which the version info is stored. If not, the table, columns and record are created if auto-create is true, else an exception is raised.

Returns:
False if the version table was not ok and therefore auto-created

isExecutedScriptsTableValid

protected boolean isExecutedScriptsTableValid()
Checks if the version table and columns are available and if a record exists in which the version info is stored. If not, the table, columns and record are created.

Returns:
False if the version table was not ok and therefore re-created

createExecutedScriptsTable

protected void createExecutedScriptsTable()
Creates the version table and inserts a version record.


getCreateExecutedScriptsTableStatement

protected String getCreateExecutedScriptsTableStatement()
Returns:
The statement to create the version table.


Copyright © 2011. All Rights Reserved.