liquibase.migrator
Class Migrator

java.lang.Object
  extended by liquibase.migrator.Migrator
Direct Known Subclasses:
IncludeMigrator

public class Migrator
extends java.lang.Object

Core class of the LiquiBase migrator. Although there are several ways of executing LiquiBase (Ant, command line, etc.) they are all wrappers around this class.

Using Migrator directly

  1. Construct an instance of Migrator passing in the changelog file and file opener.
  2. Call migrator.init(connection)
  3. Set any contexts with the setContexts() method
  4. Set the execution mode with setMode()
  5. Call migrate()


Nested Class Summary
static class Migrator.Mode
           
 
Field Summary
static java.lang.String DEFAULT_LOG_NAME
           
static java.lang.String SHOULD_RUN_SYSTEM_PROPERTY
           
 
Constructor Summary
  Migrator(java.lang.String changeLogFile, FileOpener fileOpener)
           
protected Migrator(java.lang.String changeLogFile, FileOpener fileOpener, boolean alreadyHasChangeLogLock)
           
 
Method Summary
 boolean acquireLock()
           
 void changelogSyncSQL(java.io.Writer outputWriter)
           
 void checkDatabaseChangeLogTable()
           
 void clearCheckSums()
          Sets checksums to null so they will be repopulated next run
 boolean contextMatches(ChangeSet changeSet)
           
 void dropAll()
          Drops all database objects owned by the current user.
 void dropAll(java.lang.String... schemas)
          Drops all database objects owned by the current user.
 void forceReleaseLock()
          Releases whatever locks are on the database change log table
 void futureRollbackSQL(java.io.Writer output)
           
 void generateDocumentation(java.lang.String outputDirectory)
           
 java.lang.String getBuildVersion()
           
 ChangeFactory getChangeFactory()
          Returns the ChangeFactory for converting tag strings to Change implementations.
 java.util.Set<java.lang.String> getContexts()
           
 Database getDatabase()
           
 FileOpener getFileOpener()
          FileOpener to use for accessing changelog files.
 Migrator.Mode getMode()
          Sets the mode of opereration for the Migrator.
 java.io.Writer getOutputSQLWriter()
          The Writer to append SQL if not executing directly against the database.
 PreconditionFactory getPreconditionFactory()
           
 java.util.List<RanChangeSet> getRanChangeSetList()
          Returns the ChangeSets that have been run against the current database.
 java.util.Date getRanDate(ChangeSet changeSet)
           
 java.lang.Integer getRollbackCount()
          Number of statements to rollback to if executing in rollback mode.
 java.util.Date getRollbackToDate()
          Date to rollback to if executing in rollback mode.
 java.lang.String getRollbackToTag()
          Tag to rollback to if executing in rollback mode.
 ChangeSet.RunStatus getRunStatus(ChangeSet changeSet)
          Returns the run status for the given ChangeSet
static java.lang.String getSchemaVersion()
           
protected  ValidateChangeLogHandler getValidatChangeLogHandler()
           
 void init(java.sql.Connection connection)
          Initializes the Migrator with the given connection.
 void init(Database database)
          Initializes the Migrator with the given database.
 void init(DatabaseConnection connection)
          Initializes the Migrator with the given connection.
 boolean isSafeToRunMigration()
          Returns true if it is "save" to migrate the database.
 DatabaseChangeLogLock[] listLocks()
          Display change log lock information.
 java.util.List<ChangeSet> listUnrunChangeSets()
           
 void markChangeSetAsRan(ChangeSet changeSet)
          After the change set has been ran against the database this method will update the change log table with the information.
 void markChangeSetAsReRan(ChangeSet changeSet)
           
 void migrate()
          The primary method to call on Migrator to actually do work.
 void migrateSQL(java.io.Writer outputWriter)
           
 void releaseLock()
           
 void removeRanStatus(ChangeSet changeSet)
           
 void reportLocks(java.io.PrintStream out)
           
 void reportStatus(boolean verbose, java.io.PrintStream out)
           
 void rollback(java.lang.String tag)
           
 void rollbackCount(int changesToRollback)
           
 void rollbackCountSQL(int changesToRollback, java.io.Writer output)
           
 void rollbackSQL(java.lang.String tagToRollBackTo, java.io.Writer output)
           
 void rollbackToDate(java.util.Date date)
           
 void rollbackToDateSQL(java.util.Date date, java.io.Writer output)
           
 void setContexts(java.lang.String contexts)
          Set the contexts to execute.
 void setCurrentDateTimeFunction(java.lang.String currentDateTimeFunction)
          Use this function to override the current date/time function used to insert dates into the database.
 void setMode(Migrator.Mode mode)
           
 void setOutputSQLWriter(java.io.Writer outputSQLWriter)
           
 void setRollbackCount(java.lang.Integer rollbackCount)
           
 void setRollbackToDate(java.util.Date rollbackToDate)
           
 void setRollbackToTag(java.lang.String rollbackToTag)
           
 boolean swingPromptForNonLocalDatabase()
          Displays swing-based dialog about running against a non-localhost database.
 void tag(java.lang.String tagString)
          'Tags' the database for future rollback
 void validate()
          Checks changelogs for bad MD5Sums and preconditions before attempting a migration
protected  boolean wasValidationRan()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

SHOULD_RUN_SYSTEM_PROPERTY

public static final java.lang.String SHOULD_RUN_SYSTEM_PROPERTY
See Also:
Constant Field Values

DEFAULT_LOG_NAME

public static final java.lang.String DEFAULT_LOG_NAME
See Also:
Constant Field Values
Constructor Detail

Migrator

public Migrator(java.lang.String changeLogFile,
                FileOpener fileOpener)

Migrator

protected Migrator(java.lang.String changeLogFile,
                   FileOpener fileOpener,
                   boolean alreadyHasChangeLogLock)
Method Detail

init

public void init(java.sql.Connection connection)
          throws JDBCException
Initializes the Migrator with the given connection. Needs to be called before actually using the Migrator.

Throws:
JDBCException

init

public void init(DatabaseConnection connection)
          throws JDBCException
Initializes the Migrator with the given connection. Needs to be called before actually using the Migrator.

Throws:
JDBCException

init

public void init(Database database)
          throws JDBCException
Initializes the Migrator with the given database. Needs to be called before actually using the Migrator.

Throws:
JDBCException

getChangeFactory

public ChangeFactory getChangeFactory()
Returns the ChangeFactory for converting tag strings to Change implementations.


getPreconditionFactory

public PreconditionFactory getPreconditionFactory()

getBuildVersion

public java.lang.String getBuildVersion()

getSchemaVersion

public static java.lang.String getSchemaVersion()

getDatabase

public Database getDatabase()

getMode

public Migrator.Mode getMode()
Sets the mode of opereration for the Migrator.


setMode

public void setMode(Migrator.Mode mode)

getOutputSQLWriter

public java.io.Writer getOutputSQLWriter()
The Writer to append SQL if not executing directly against the database.


setOutputSQLWriter

public void setOutputSQLWriter(java.io.Writer outputSQLWriter)

getRollbackToDate

public java.util.Date getRollbackToDate()
Date to rollback to if executing in rollback mode.


setRollbackToDate

public void setRollbackToDate(java.util.Date rollbackToDate)

getRollbackToTag

public java.lang.String getRollbackToTag()
Tag to rollback to if executing in rollback mode.


setRollbackToTag

public void setRollbackToTag(java.lang.String rollbackToTag)

getRollbackCount

public java.lang.Integer getRollbackCount()
Number of statements to rollback to if executing in rollback mode.


setRollbackCount

public void setRollbackCount(java.lang.Integer rollbackCount)

getFileOpener

public FileOpener getFileOpener()
FileOpener to use for accessing changelog files.


setCurrentDateTimeFunction

public void setCurrentDateTimeFunction(java.lang.String currentDateTimeFunction)
Use this function to override the current date/time function used to insert dates into the database. Especially useful when using an unsupported database.


getRanChangeSetList

public java.util.List<RanChangeSet> getRanChangeSetList()
                                                 throws JDBCException
Returns the ChangeSets that have been run against the current database.

Throws:
JDBCException

validate

public void validate()
              throws MigrationFailedException,
                     java.io.IOException,
                     JDBCException,
                     LockException
Checks changelogs for bad MD5Sums and preconditions before attempting a migration

Throws:
MigrationFailedException
java.io.IOException
JDBCException
LockException

migrate

public final void migrate()
                   throws LiquibaseException
The primary method to call on Migrator to actually do work. To use the Migrator, initialize it with the init(Connection) method, set the mode, outputSQLWriter, etc. as need be, then call the migrate() method.

Throws:
LiquibaseException

changelogSyncSQL

public void changelogSyncSQL(java.io.Writer outputWriter)
                      throws LiquibaseException
Throws:
LiquibaseException

migrateSQL

public void migrateSQL(java.io.Writer outputWriter)
                throws LiquibaseException
Throws:
LiquibaseException

rollback

public void rollback(java.lang.String tag)
              throws LiquibaseException
Throws:
LiquibaseException

rollbackToDate

public void rollbackToDate(java.util.Date date)
                    throws LiquibaseException
Throws:
LiquibaseException

rollbackCount

public void rollbackCount(int changesToRollback)
                   throws LiquibaseException
Throws:
LiquibaseException

rollbackSQL

public void rollbackSQL(java.lang.String tagToRollBackTo,
                        java.io.Writer output)
                 throws LiquibaseException
Throws:
LiquibaseException

rollbackToDateSQL

public void rollbackToDateSQL(java.util.Date date,
                              java.io.Writer output)
                       throws LiquibaseException
Throws:
LiquibaseException

rollbackCountSQL

public void rollbackCountSQL(int changesToRollback,
                             java.io.Writer output)
                      throws LiquibaseException
Throws:
LiquibaseException

futureRollbackSQL

public void futureRollbackSQL(java.io.Writer output)
                       throws LiquibaseException
Throws:
LiquibaseException

getValidatChangeLogHandler

protected ValidateChangeLogHandler getValidatChangeLogHandler()

wasValidationRan

protected boolean wasValidationRan()

dropAll

public final void dropAll()
                   throws JDBCException,
                          LockException
Drops all database objects owned by the current user.

Throws:
JDBCException
LockException

dropAll

public final void dropAll(java.lang.String... schemas)
                   throws JDBCException,
                          LockException
Drops all database objects owned by the current user.

Throws:
JDBCException
LockException

acquireLock

public boolean acquireLock()
                    throws LockException
Throws:
LockException

releaseLock

public void releaseLock()
                 throws LockException
Throws:
LockException

forceReleaseLock

public void forceReleaseLock()
                      throws LockException,
                             JDBCException,
                             java.io.IOException
Releases whatever locks are on the database change log table

Throws:
LockException
JDBCException
java.io.IOException

tag

public void tag(java.lang.String tagString)
         throws JDBCException
'Tags' the database for future rollback

Throws:
JDBCException

checkDatabaseChangeLogTable

public void checkDatabaseChangeLogTable()
                                 throws JDBCException,
                                        java.io.IOException
Throws:
JDBCException
java.io.IOException

isSafeToRunMigration

public boolean isSafeToRunMigration()
                             throws JDBCException
Returns true if it is "save" to migrate the database. Currently, "safe" is defined as running in an output-sql mode or against a database on localhost. It is fine to run the migrator against a "non-safe" database, the method is mainly used to determine if the user should be prompted before continuing.

Throws:
JDBCException

listLocks

public DatabaseChangeLogLock[] listLocks()
                                  throws JDBCException,
                                         java.io.IOException,
                                         LockException
Display change log lock information.

Throws:
JDBCException
java.io.IOException
LockException

reportLocks

public void reportLocks(java.io.PrintStream out)
                 throws LockException,
                        java.io.IOException,
                        JDBCException
Throws:
LockException
java.io.IOException
JDBCException

setContexts

public void setContexts(java.lang.String contexts)
Set the contexts to execute. If more than once, comma separate them.


getContexts

public java.util.Set<java.lang.String> getContexts()

getRunStatus

public ChangeSet.RunStatus getRunStatus(ChangeSet changeSet)
                                 throws JDBCException,
                                        DatabaseHistoryException
Returns the run status for the given ChangeSet

Throws:
JDBCException
DatabaseHistoryException

getRanDate

public java.util.Date getRanDate(ChangeSet changeSet)
                          throws JDBCException,
                                 DatabaseHistoryException
Throws:
JDBCException
DatabaseHistoryException

swingPromptForNonLocalDatabase

public boolean swingPromptForNonLocalDatabase()
                                       throws JDBCException
Displays swing-based dialog about running against a non-localhost database. Returns true if the user selected that they are OK with that.

Throws:
JDBCException

contextMatches

public boolean contextMatches(ChangeSet changeSet)

listUnrunChangeSets

public java.util.List<ChangeSet> listUnrunChangeSets()
                                              throws LiquibaseException
Throws:
LiquibaseException

reportStatus

public void reportStatus(boolean verbose,
                         java.io.PrintStream out)
                  throws LiquibaseException
Throws:
LiquibaseException

markChangeSetAsRan

public void markChangeSetAsRan(ChangeSet changeSet)
                        throws JDBCException,
                               java.io.IOException
After the change set has been ran against the database this method will update the change log table with the information.

Throws:
JDBCException
java.io.IOException

markChangeSetAsReRan

public void markChangeSetAsReRan(ChangeSet changeSet)
                          throws JDBCException,
                                 java.io.IOException
Throws:
JDBCException
java.io.IOException

removeRanStatus

public void removeRanStatus(ChangeSet changeSet)
                     throws JDBCException,
                            java.io.IOException
Throws:
JDBCException
java.io.IOException

clearCheckSums

public void clearCheckSums()
                    throws JDBCException
Sets checksums to null so they will be repopulated next run

Throws:
JDBCException

generateDocumentation

public void generateDocumentation(java.lang.String outputDirectory)
                           throws LockException,
                                  java.io.IOException,
                                  JDBCException,
                                  MigrationFailedException,
                                  DatabaseHistoryException
Throws:
LockException
java.io.IOException
JDBCException
MigrationFailedException
DatabaseHistoryException