Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
Change |
|
| 1.0;1 |
1 | package liquibase.change; | |
2 | ||
3 | import java.util.Set; | |
4 | ||
5 | import liquibase.changelog.ChangeSet; | |
6 | import liquibase.database.Database; | |
7 | import liquibase.database.structure.DatabaseObject; | |
8 | import liquibase.exception.RollbackImpossibleException; | |
9 | import liquibase.exception.SetupException; | |
10 | import liquibase.exception.UnsupportedChangeException; | |
11 | import liquibase.exception.ValidationErrors; | |
12 | import liquibase.exception.Warnings; | |
13 | import liquibase.resource.ResourceAccessor; | |
14 | import liquibase.statement.SqlStatement; | |
15 | ||
16 | /** | |
17 | * Interface all changes (refactorings) implement. | |
18 | * <p/> | |
19 | * <b>How changes are constructed and run when reading changelogs:</b> | |
20 | * <ol> | |
21 | * <li>As the changelog handler gets to each element inside a changeSet, it passes the tag name to | |
22 | * liquibase.change.ChangeFactory which looks through all the registered changes until it finds one with matching | |
23 | * specified tag name</li> | |
24 | * <li>The ChangeFactory then constructs a new instance of the change</li> | |
25 | * <li>For each attribute in the XML node, reflection is used to call a corresponding set* method on the change class</li> | |
26 | * <li>The correct generateStatements(*) method is called for the current database</li> | |
27 | * </ol> | |
28 | * <p/> | |
29 | * <b>To implement a new change:</b> | |
30 | * <ol> | |
31 | * <li>Create a new class that implements Change (normally extend AbstractChange)</li> | |
32 | * <li>Implement the abstract generateStatements(*) methods which return the correct SQL calls for each database</li> | |
33 | * <li>Implement the createMessage() method to create a descriptive message for logs and dialogs | |
34 | * <li>Implement the createNode() method to generate an XML element based on the values in this change</li> | |
35 | * <li>Add the new class to the liquibase.change.ChangeFactory</li> | |
36 | * </ol> | |
37 | * <p> | |
38 | * <b>Implementing automatic rollback support</b><br> | |
39 | * <br> | |
40 | * The easiest way to allow automatic rollback support is by overriding the createInverses() method. If there are no | |
41 | * corresponding inverse changes, you can override the generateRollbackStatements(*) and canRollBack() methods. | |
42 | * <p/> | |
43 | * <b>Notes for generated SQL:</b><br> | |
44 | * Because migration and rollback scripts can be generated for execution at a different time, or against a different | |
45 | * database, changes you implement cannot directly reference data in the database. For example, you cannot implement a | |
46 | * change that selects all rows from a database and modifies them based on the primary keys you find because when the | |
47 | * SQL is actually run, those rows may not longer exist and/or new rows may have been added. | |
48 | * <p/> | |
49 | * We chose the name "change" over "refactoring" because changes will sometimes change functionality whereas true | |
50 | * refactoring will not. | |
51 | * | |
52 | * @see ChangeFactory | |
53 | * @see Database | |
54 | */ | |
55 | public interface Change { | |
56 | ||
57 | public ChangeMetaData getChangeMetaData(); | |
58 | ||
59 | public ChangeSet getChangeSet(); | |
60 | ||
61 | public void setChangeSet(ChangeSet changeSet); | |
62 | ||
63 | /** | |
64 | * Sets the fileOpener that should be used for any file loading and resource finding for files that are provided by | |
65 | * the user. | |
66 | */ | |
67 | public void setResourceAccessor(ResourceAccessor resourceAccessor); | |
68 | ||
69 | /** | |
70 | * This method will be called after the no arg constructor and all of the properties have been set to allow the task | |
71 | * to do any heavy tasks or more importantly generate any exceptions to report to the user about the settings | |
72 | * provided. | |
73 | */ | |
74 | public void init() throws SetupException; | |
75 | ||
76 | boolean supports(Database database); | |
77 | ||
78 | public Warnings warn(Database database); | |
79 | ||
80 | public ValidationErrors validate(Database database); | |
81 | ||
82 | public Set<DatabaseObject> getAffectedDatabaseObjects(Database database); | |
83 | ||
84 | /** | |
85 | * Calculates the checksum (currently MD5 hash) for the current configuration of this change. | |
86 | */ | |
87 | public CheckSum generateCheckSum(); | |
88 | ||
89 | /** | |
90 | * @return Confirmation message to be displayed after the change is executed | |
91 | */ | |
92 | public String getConfirmationMessage(); | |
93 | ||
94 | /** | |
95 | * Generates the SQL statements required to run the change | |
96 | * | |
97 | * @param database | |
98 | * databasethe target {@link liquibase.database.Database} associated to this change's statements | |
99 | * @return an array of {@link String}s with the statements | |
100 | */ | |
101 | public SqlStatement[] generateStatements(Database database); | |
102 | ||
103 | /** | |
104 | * Can this change be rolled back | |
105 | * | |
106 | * @return <i>true</i> if rollback is supported, <i>false</i> otherwise | |
107 | * @param database | |
108 | */ | |
109 | public boolean supportsRollback(Database database); | |
110 | ||
111 | /** | |
112 | * Generates the SQL statements required to roll back the change | |
113 | * | |
114 | * @param database | |
115 | * database databasethe target {@link Database} associated to this change's rollback statements | |
116 | * @return an array of {@link String}s with the rollback statements | |
117 | * @throws UnsupportedChangeException | |
118 | * if this change is not supported by the {@link Database} passed as argument | |
119 | * @throws RollbackImpossibleException | |
120 | * if rollback is not supported for this change | |
121 | */ | |
122 | public SqlStatement[] generateRollbackStatements(Database database) throws UnsupportedChangeException, | |
123 | RollbackImpossibleException; | |
124 | ||
125 | /** | |
126 | * Does this change require access to the database metadata? If true, the change cannot be used in an | |
127 | * updateSql-style command. | |
128 | */ | |
129 | public boolean requiresUpdatedDatabaseMetadata(Database database); | |
130 | } |