liquibase.change
Interface Change

All Known Implementing Classes:
AbstractChange, AbstractSQLChange, AddAutoIncrementChange, AddColumnChange, AddDefaultValueChange, AddForeignKeyConstraintChange, AddLookupTableChange, AddNotNullConstraintChange, AddPrimaryKeyChange, AddUniqueConstraintChange, AlterSequenceChange, CreateIndexChange, CreateProcedureChange, CreateSequenceChange, CreateTableChange, CreateViewChange, CustomChangeWrapper, DeleteDataChange, DropAllForeignKeyConstraintsChange, DropColumnChange, DropDefaultValueChange, DropForeignKeyConstraintChange, DropIndexChange, DropNotNullConstraintChange, DropPrimaryKeyChange, DropSequenceChange, DropTableChange, DropUniqueConstraintChange, DropViewChange, EmptyChange, ExecuteShellCommandChange, InsertDataChange, LoadDataChange, MergeColumnChange, ModifyColumnChange, RawSQLChange, RenameColumnChange, RenameTableChange, RenameViewChange, SQLFileChange, StopChange, TagDatabaseChange, UpdateDataChange

public interface Change

Interface all changes (refactorings) implement.

How changes are constructed and run when reading changelogs:

  1. As the changelog handler gets to each element inside a changeSet, it passes the tag name to liquibase.change.ChangeFactory which looks through all the registered changes until it finds one with matching specified tag name
  2. The ChangeFactory then constructs a new instance of the change
  3. For each attribute in the XML node, reflection is used to call a corresponding set* method on the change class
  4. The correct generateStatements(*) method is called for the current database

To implement a new change:

  1. Create a new class that implements Change (normally extend AbstractChange)
  2. Implement the abstract generateStatements(*) methods which return the correct SQL calls for each database
  3. Implement the createMessage() method to create a descriptive message for logs and dialogs
  4. Implement the createNode() method to generate an XML element based on the values in this change
  5. Add the new class to the liquibase.change.ChangeFactory

Implementing automatic rollback support

The easiest way to allow automatic rollback support is by overriding the createInverses() method. If there are no corresponding inverse changes, you can override the generateRollbackStatements(*) and canRollBack() methods.

Notes for generated SQL:
Because migration and rollback scripts can be generated for execution at a different time, or against a different database, changes you implement cannot directly reference data in the database. For example, you cannot implement a change that selects all rows from a database and modifies them based on the primary keys you find because when the SQL is actually run, those rows may not longer exist and/or new rows may have been added.

We chose the name "change" over "refactoring" because changes will sometimes change functionality whereas true refactoring will not.

See Also:
ChangeFactory, Database

Method Summary
 boolean canRollBack()
          Can this change be rolled back
 org.w3c.dom.Node createNode(org.w3c.dom.Document currentChangeLogDOM)
          Creates an XML element (of type Node) of the change object, and adds it to the Document object passed as argument
 void executeRollbackStatements(Database database, java.util.List<SqlVisitor> sqlVisitors)
          Rolls back the statements in this change against the Database passed as argument
 void executeStatements(Database database, java.util.List<SqlVisitor> sqlVisitors)
          Executes the statements in this change against the database passed in the argument
 SqlStatement[] generateRollbackStatements(Database database)
          Generates the SQL statements required to roll back the change
 SqlStatement[] generateStatements(Database database)
          Generates the SQL statements required to run the change
 java.util.Set<DatabaseObject> getAffectedDatabaseObjects()
           
 java.lang.String getChangeName()
          Returns the name of this change
 ChangeSet getChangeSet()
           
 java.lang.String getConfirmationMessage()
          Confirmation message to be displayed after the change is executed
 java.lang.String getMD5Sum()
          Calculates the MD5 hash for the string representation of the XML element of this change
 java.lang.String getTagName()
          Returns the tag name of this change
 void saveRollbackStatement(Database database, java.util.List<SqlVisitor> sqlVisitors, java.io.Writer writer)
          Outputs the statements necessary to roll back this change
 void saveStatements(Database database, java.util.List<SqlVisitor> sqlVisitors, java.io.Writer writer)
          Outputs the SQL statements generated by this change
 void setChangeSet(ChangeSet changeSet)
           
 void setFileOpener(FileOpener fileOpener)
          Sets the fileOpener that should be used for any file loading and resource finding for files that are provided by the user.
 void setUp()
          This method will be called after the no arg constructor and all of the properties have been set to allow the task to do any heavy tasks or more importantly generate any exceptions to report to the user about the settings provided.
 void validate(Database database)
           
 

Method Detail

getChangeName

java.lang.String getChangeName()
Returns the name of this change

Returns:
the name of the change

getTagName

java.lang.String getTagName()
Returns the tag name of this change

Returns:
the tag name of the change

executeStatements

void executeStatements(Database database,
                       java.util.List<SqlVisitor> sqlVisitors)
                       throws JDBCException,
                              UnsupportedChangeException
Executes the statements in this change against the database passed in the argument

Parameters:
database - the reference to the target Database to which the statements are executed
Throws:
JDBCException - if there were problems executing the statements
UnsupportedChangeException - if this change is not supported by the Database passed as argument

saveStatements

void saveStatements(Database database,
                    java.util.List<SqlVisitor> sqlVisitors,
                    java.io.Writer writer)
                    throws java.io.IOException,
                           UnsupportedChangeException,
                           StatementNotSupportedOnDatabaseException
Outputs the SQL statements generated by this change

Parameters:
database - the target Database associated to this change's statements
writer - the target Writer to which the statements are appended
Throws:
java.io.IOException - if there were problems appending the statements to the writer
UnsupportedChangeException - if this change is not supported by the Database passed as argument
StatementNotSupportedOnDatabaseException

executeRollbackStatements

void executeRollbackStatements(Database database,
                               java.util.List<SqlVisitor> sqlVisitors)
                               throws JDBCException,
                                      UnsupportedChangeException,
                                      RollbackImpossibleException
Rolls back the statements in this change against the Database passed as argument

Parameters:
database - the target Database associated to this change's rollback statements
Throws:
JDBCException - if there were problems executing the rollback statements
UnsupportedChangeException - if this change is not supported by the Database passed as argument
RollbackImpossibleException - if rollback is not supported for this change

saveRollbackStatement

void saveRollbackStatement(Database database,
                           java.util.List<SqlVisitor> sqlVisitors,
                           java.io.Writer writer)
                           throws java.io.IOException,
                                  UnsupportedChangeException,
                                  RollbackImpossibleException,
                                  StatementNotSupportedOnDatabaseException
Outputs the statements necessary to roll back this change

Parameters:
database - the target Database associated to this change's rollback statements
writer - writer the target Writer to which the rollback statements are appended
Throws:
java.io.IOException - if there were problems appending the rollback statements to the writer
UnsupportedChangeException - if this change is not supported by the Database passed as argument
RollbackImpossibleException - if rollback is not supported for this change
StatementNotSupportedOnDatabaseException

generateStatements

SqlStatement[] generateStatements(Database database)
                                  throws UnsupportedChangeException
Generates the SQL statements required to run the change

Parameters:
database - databasethe target Database associated to this change's statements
Returns:
an array of Strings with the statements
Throws:
UnsupportedChangeException - if this change is not supported by the Database passed as argument

generateRollbackStatements

SqlStatement[] generateRollbackStatements(Database database)
                                          throws UnsupportedChangeException,
                                                 RollbackImpossibleException
Generates the SQL statements required to roll back the change

Parameters:
database - database databasethe target Database associated to this change's rollback statements
Returns:
an array of Strings with the rollback statements
Throws:
UnsupportedChangeException - if this change is not supported by the Database passed as argument
RollbackImpossibleException - if rollback is not supported for this change

canRollBack

boolean canRollBack()
Can this change be rolled back

Returns:
true if rollback is supported, false otherwise

getConfirmationMessage

java.lang.String getConfirmationMessage()
Confirmation message to be displayed after the change is executed

Returns:
a String containing the message after the change is executed

createNode

org.w3c.dom.Node createNode(org.w3c.dom.Document currentChangeLogDOM)
Creates an XML element (of type Node) of the change object, and adds it to the Document object passed as argument

Parameters:
currentChangeLogDOM - the current Document where this element is being added
Returns:
the Node object created

getMD5Sum

java.lang.String getMD5Sum()
Calculates the MD5 hash for the string representation of the XML element of this change

Returns:
the MD5 hash

setFileOpener

void setFileOpener(FileOpener fileOpener)
Sets the fileOpener that should be used for any file loading and resource finding for files that are provided by the user.


setUp

void setUp()
           throws SetupException
This method will be called after the no arg constructor and all of the properties have been set to allow the task to do any heavy tasks or more importantly generate any exceptions to report to the user about the settings provided.

Throws:
SetupException

getAffectedDatabaseObjects

java.util.Set<DatabaseObject> getAffectedDatabaseObjects()

getChangeSet

ChangeSet getChangeSet()

setChangeSet

void setChangeSet(ChangeSet changeSet)

validate

void validate(Database database)
              throws InvalidChangeDefinitionException
Throws:
InvalidChangeDefinitionException