public class DBMaintainer extends Object
ExecutedScriptInfoSource,
ScriptSource, a ScriptRunner, DBClearer, DBCleaner,
ConstraintsDisabler, SequenceUpdater and a DataSetStructureGenerator
The #updateDatabase() method check what is the current version of the database, and
see if existing scripts have been modified. If yes, the database is cleared and all available
database scripts, are executed on the database. If no existing scripts have been modified, but
new scripts were added, only the new scripts are executed. Before executing an update, data from
the database is removed, to avoid problems when e.g. adding a not null column. If a database
update causes an error, a UnitilsException is thrown. After a failing update, the
database is always completely recreated from scratch. After updating the database, following
steps are optionally executed on the database (depending on the configuration):
DBMaintainer, invoke the constructor
#DBMaintainer(Properties,SQLHandler) with a TestDataSource providing
access to the database and a Configuration object containing all necessary
properties.| Modifier and Type | Field and Description |
|---|---|
protected ConstraintsDisabler |
constraintsDisabler
Disabler of constraints
|
protected DataSetStructureGenerator |
dataSetStructureGenerator
Database DTD generator
|
protected DBCleaner |
dbCleaner
Cleaner of the database (deletes all data from all tables before updating
|
protected DBClearer |
dbClearer
Clearer of the database (removed all tables, sequences, ...) before updating
|
protected String |
dialect |
protected boolean |
disableConstraintsEnabled
Indicates if foreign key and not null constraints should removed after updating the database
structure
|
protected boolean |
fromScratchEnabled
Indicates whether updating the database from scratch is enabled.
|
protected boolean |
keepRetryingAfterError
Indicates whether a from scratch update should be performed when the previous update failed,
but none of the scripts were modified since that last update.
|
static String |
PROPKEY_CLEAR_DB_CODE_ENABLED
Property indicating if database code should be cleared before installing a new version of
the code or when updating the database from scratch
|
static String |
PROPKEY_DB_CLEANER_ENABLED
Property indicating if deleting all data from all tables before updating is enabled
|
static String |
PROPKEY_DISABLE_CONSTRAINTS_ENABLED
Property indicating if the database constraints should org disabled after updating the database
|
static String |
PROPKEY_FROM_SCRATCH_ENABLED
Property indicating if updating the database from scratch is enabled
|
static String |
PROPKEY_GENERATE_DATA_SET_STRUCTURE_ENABLED
Property that indicates if a data set DTD or XSD is to be generated or not
|
static String |
PROPKEY_KEEP_RETRYING_AFTER_ERROR_ENABLED
Property indicating if an retry of an update should only be performed when changes to script files were made
|
static String |
PROPKEY_UPDATE_SEQUENCES_ENABLED
Property indicating if the database constraints should org disabled after updating the database
|
protected ScriptRunner |
scriptRunner
Executer of the scripts
|
protected ScriptSource |
scriptSource
Provider of scripts for updating the database to a higher version
|
protected SequenceUpdater |
sequenceUpdater
Database sequence updater
|
protected ExecutedScriptInfoSource |
versionSource
Provider of the current version of the database, and means to increment it
|
| Modifier | Constructor and Description |
|---|---|
protected |
DBMaintainer()
Default constructor for testing.
|
|
DBMaintainer(Properties configuration,
SQLHandler sqlHandler,
String dialect,
List<String> schemaNames)
Create a new instance of
DBMaintainer, The concrete implementations of all
helper classes are derived from the given Configuration object. |
| Modifier and Type | Method and Description |
|---|---|
protected boolean |
errorInIndexedScriptDuringLastUpdate(Set<ExecutedScript> alreadyExecutedScripts) |
protected void |
executePostProcessingScripts(List<Script> postProcessingScripts)
Executes the given post processing scripts on the database.
|
protected void |
executeScripts(List<Script> scripts)
Executes the given scripts and updates the database version and state appropriatly.
|
protected Version |
getHighestExecutedScriptVersion(Set<ExecutedScript> executedScripts) |
void |
resetDatabaseState(String databaseName,
boolean defaultDatabase)
Updates the database version to the current version of the update scripts, without changing
anything else in the database.
|
protected void |
setDialect(String dialect) |
protected boolean |
shouldUpdateDatabaseFromScratch(Version currentVersion,
Set<ExecutedScript> alreadyExecutedScripts,
String databaseName,
boolean defaultDatabase)
Checks whether the database should be updated from scratch or just incrementally.
|
protected void |
updateDatabase(List<Script> scripts,
String schema,
boolean defaultDatabase)
Updates the state of the database using the given scripts.
|
void |
updateDatabase(String databaseName,
boolean defaultDatabase)
Checks if the new scripts are available to update the version of the database.
|
public static final String PROPKEY_DB_CLEANER_ENABLED
public static final String PROPKEY_FROM_SCRATCH_ENABLED
public static final String PROPKEY_CLEAR_DB_CODE_ENABLED
public static final String PROPKEY_KEEP_RETRYING_AFTER_ERROR_ENABLED
public static final String PROPKEY_DISABLE_CONSTRAINTS_ENABLED
public static final String PROPKEY_UPDATE_SEQUENCES_ENABLED
public static final String PROPKEY_GENERATE_DATA_SET_STRUCTURE_ENABLED
protected ExecutedScriptInfoSource versionSource
protected ScriptSource scriptSource
protected ScriptRunner scriptRunner
protected DBClearer dbClearer
protected DBCleaner dbCleaner
protected ConstraintsDisabler constraintsDisabler
protected SequenceUpdater sequenceUpdater
protected DataSetStructureGenerator dataSetStructureGenerator
protected boolean fromScratchEnabled
protected boolean disableConstraintsEnabled
protected boolean keepRetryingAfterError
protected String dialect
protected DBMaintainer()
public DBMaintainer(Properties configuration, SQLHandler sqlHandler, String dialect, List<String> schemaNames)
DBMaintainer, The concrete implementations of all
helper classes are derived from the given Configuration object.configuration - the configuration, not nullsqlHandler - the data source, not nullpublic void updateDatabase(String databaseName, boolean defaultDatabase)
UnitilsException is thrown.protected Version getHighestExecutedScriptVersion(Set<ExecutedScript> executedScripts)
public void resetDatabaseState(String databaseName, boolean defaultDatabase)
protected void updateDatabase(List<Script> scripts, String schema, boolean defaultDatabase)
scripts - The scripts, not nullprotected void executeScripts(List<Script> scripts)
scripts - The scripts to execute, not nullprotected void executePostProcessingScripts(List<Script> postProcessingScripts)
postProcessingScripts - The scripts to execute, not nullprotected boolean shouldUpdateDatabaseFromScratch(Version currentVersion, Set<ExecutedScript> alreadyExecutedScripts, String databaseName, boolean defaultDatabase)
PROPKEY_FROM_SCRATCH_ENABLED is
set to true. If the PROPKEY_KEEP_RETRYING_AFTER_ERROR_ENABLED is set to false, the
database will only be rebuilt again after an unsuccessful build when changes were made to the
script files.currentVersion - The current database version, not nullprotected boolean errorInIndexedScriptDuringLastUpdate(Set<ExecutedScript> alreadyExecutedScripts)
protected void setDialect(String dialect)
Copyright © 2017. All rights reserved.