org.unitils.dbmaintainer.script.impl
Class DefaultScriptSource

java.lang.Object
  extended by org.unitils.core.util.BaseConfigurable
      extended by org.unitils.dbmaintainer.script.impl.DefaultScriptSource
All Implemented Interfaces:
Configurable, ScriptSource

public class DefaultScriptSource
extends BaseConfigurable
implements ScriptSource

Implementation of ScriptSource that reads script files from the filesystem.

Script files should be located in the directory configured by PROPKEY_SCRIPT_LOCATIONS. Valid script files start with a version number followed by an underscore, and end with the extension configured by PROPKEY_SCRIPT_EXTENSIONS.

Author:
Filip Neven, Tim Ducheyne

Field Summary
protected  List<Script> allPostProcessingScripts
           
protected  List<Script> allUpdateScripts
           
static String PROPKEY_POSTPROCESSINGSCRIPT_DIRNAME
          Property key for the directory in which the code script files are located
static String PROPKEY_SCRIPT_EXTENSIONS
          Property key for the extension of the script files
static String PROPKEY_SCRIPT_LOCATIONS
          Property key for the directory in which the script files are located
static String PROPKEY_USESCRIPTFILELASTMODIFICATIONDATES
           
 
Fields inherited from class org.unitils.core.util.BaseConfigurable
configuration
 
Constructor Summary
DefaultScriptSource()
           
 
Method Summary
protected  void assertNoDuplicateIndexes(List<Script> scripts)
          Asserts that, in the given list of database update scripts, there are no two indexed scripts with the same version.
protected  Map<String,Script> convertToScriptNameScriptMap(Set<ExecutedScript> executedScripts)
           
protected  Script createScript(File scriptFile, String relativePath)
          Creates a script object for the given script file
 List<Script> getAllUpdateScripts()
          Gets a list of all available update scripts.
protected  List<Script> getIncrementalScripts()
           
 List<Script> getNewScripts(Version currentVersion, Set<ExecutedScript> alreadyExecutedScripts)
          Returns a list of scripts with a higher version or whose contents were changed.
 List<Script> getPostProcessingScripts()
          Gets the configured post-processing script files and verfies that they on the file system.
protected  List<String> getScriptExtensions()
          Gets the configured extensions for the script files.
protected  void getScriptsAt(List<Script> scripts, String scriptRoot, String relativeLocation)
          Adds all scripts available in the given directory or one of its subdirectories to the given List of files
 boolean isExistingIndexedScriptModified(Version currentVersion, Set<ExecutedScript> alreadyExecutedScripts)
          Returns true if one or more scripts that have a version index equal to or lower than the index specified by the given version object has been modified since the timestamp specfied by the given version.
protected  boolean isPostProcessingScript(Script script)
           
protected  boolean isScriptFile(File file)
          Indicates if the given file is a database update script file
protected  List<Script> loadAllScripts()
           
protected  void loadAndOrganizeAllScripts()
          Loads all scripts and organizes them: Splits them into update and postprocessing scripts, sorts them in their execution order, and makes sure there are no 2 update or postprocessing scripts with the same index.
protected  boolean useScriptFileLastModificationDates()
           
 
Methods inherited from class org.unitils.core.util.BaseConfigurable
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.core.util.Configurable
init
 

Field Detail

PROPKEY_SCRIPT_LOCATIONS

public static final String PROPKEY_SCRIPT_LOCATIONS
Property key for the directory in which the script files are located

See Also:
Constant Field Values

PROPKEY_SCRIPT_EXTENSIONS

public static final String PROPKEY_SCRIPT_EXTENSIONS
Property key for the extension of the script files

See Also:
Constant Field Values

PROPKEY_POSTPROCESSINGSCRIPT_DIRNAME

public static final String PROPKEY_POSTPROCESSINGSCRIPT_DIRNAME
Property key for the directory in which the code script files are located

See Also:
Constant Field Values

PROPKEY_USESCRIPTFILELASTMODIFICATIONDATES

public static final String PROPKEY_USESCRIPTFILELASTMODIFICATIONDATES
See Also:
Constant Field Values

allUpdateScripts

protected List<Script> allUpdateScripts

allPostProcessingScripts

protected List<Script> allPostProcessingScripts
Constructor Detail

DefaultScriptSource

public DefaultScriptSource()
Method Detail

getAllUpdateScripts

public List<Script> getAllUpdateScripts()
Gets a list of all available update scripts. These scripts can be used to completely recreate the database from scratch, not null.

The scripts are returned in the order in which they should be executed.

Specified by:
getAllUpdateScripts in interface ScriptSource
Returns:
all available database update scripts, not null

getIncrementalScripts

protected List<Script> getIncrementalScripts()
Returns:
All scripts that are incremental, i.e. non-repeatable, i.e. whose file name starts with an index

assertNoDuplicateIndexes

protected void assertNoDuplicateIndexes(List<Script> scripts)
Asserts that, in the given list of database update scripts, there are no two indexed scripts with the same version.

Parameters:
scripts - The list of scripts, must be sorted by version

getNewScripts

public List<Script> getNewScripts(Version currentVersion,
                                  Set<ExecutedScript> alreadyExecutedScripts)
Returns a list of scripts with a higher version or whose contents were changed.

The scripts are returned in the order in which they should be executed.

Specified by:
getNewScripts in interface ScriptSource
Parameters:
currentVersion - The start version, not null
alreadyExecutedScripts - The scripts which were already executed on the database
Returns:
The scripts that have a higher index of timestamp than the start version, not null.

isExistingIndexedScriptModified

public boolean isExistingIndexedScriptModified(Version currentVersion,
                                               Set<ExecutedScript> alreadyExecutedScripts)
Returns true if one or more scripts that have a version index equal to or lower than the index specified by the given version object has been modified since the timestamp specfied by the given version.

Specified by:
isExistingIndexedScriptModified in interface ScriptSource
Parameters:
currentVersion - The current database version, not null
Returns:
True if an existing script has been modified, false otherwise

useScriptFileLastModificationDates

protected boolean useScriptFileLastModificationDates()

getPostProcessingScripts

public List<Script> getPostProcessingScripts()
Gets the configured post-processing script files and verfies that they on the file system. If one of them doesn't exist or is not a file, an exception is thrown.

Specified by:
getPostProcessingScripts in interface ScriptSource
Returns:
All the postprocessing code scripts, not null

loadAndOrganizeAllScripts

protected void loadAndOrganizeAllScripts()
Loads all scripts and organizes them: Splits them into update and postprocessing scripts, sorts them in their execution order, and makes sure there are no 2 update or postprocessing scripts with the same index.


loadAllScripts

protected List<Script> loadAllScripts()
Returns:
A List containing all scripts in the given script locations, not null

getScriptsAt

protected void getScriptsAt(List<Script> scripts,
                            String scriptRoot,
                            String relativeLocation)
Adds all scripts available in the given directory or one of its subdirectories to the given List of files

Parameters:
scriptLocation - The current script location, not null
currentParentIndexes - The indexes of the current parent folders, not null
scriptFiles - The list to which the available script have to be added

isPostProcessingScript

protected boolean isPostProcessingScript(Script script)
Parameters:
script - A database script, not null
Returns:
True if the given script is a post processing script according to the script source configuration

isScriptFile

protected boolean isScriptFile(File file)
Indicates if the given file is a database update script file

Parameters:
file - The file, not null
Returns:
True if the given file is a database update script file

createScript

protected Script createScript(File scriptFile,
                              String relativePath)
Creates a script object for the given script file

Parameters:
scriptFile - The script file, not null
Returns:
The script, not null

getScriptExtensions

protected List<String> getScriptExtensions()
Gets the configured extensions for the script files.

Returns:
The extensions, not null

convertToScriptNameScriptMap

protected Map<String,Script> convertToScriptNameScriptMap(Set<ExecutedScript> executedScripts)


Copyright © 2011. All Rights Reserved.