| 1 | |
package liquibase.change.core; |
| 2 | |
|
| 3 | |
import liquibase.change.AbstractChange; |
| 4 | |
import liquibase.change.Change; |
| 5 | |
import liquibase.change.ChangeMetaData; |
| 6 | |
import liquibase.change.ColumnConfig; |
| 7 | |
import liquibase.database.Database; |
| 8 | |
import liquibase.database.core.SQLiteDatabase; |
| 9 | |
import liquibase.database.core.SQLiteDatabase.AlterTableVisitor; |
| 10 | |
import liquibase.database.structure.Index; |
| 11 | |
import liquibase.statement.SqlStatement; |
| 12 | |
import liquibase.statement.core.AddUniqueConstraintStatement; |
| 13 | |
import liquibase.util.StringUtils; |
| 14 | |
|
| 15 | |
import java.util.ArrayList; |
| 16 | |
import java.util.List; |
| 17 | |
|
| 18 | |
|
| 19 | |
|
| 20 | |
|
| 21 | |
public class AddUniqueConstraintChange extends AbstractChange { |
| 22 | |
|
| 23 | |
private String schemaName; |
| 24 | |
private String tableName; |
| 25 | |
private String columnNames; |
| 26 | |
private String constraintName; |
| 27 | |
private String tablespace; |
| 28 | |
|
| 29 | |
private Boolean deferrable; |
| 30 | |
private Boolean initiallyDeferred; |
| 31 | |
private Boolean disabled; |
| 32 | |
|
| 33 | |
public AddUniqueConstraintChange() { |
| 34 | 17 | super("addUniqueConstraint", "Add Unique Constraint", ChangeMetaData.PRIORITY_DEFAULT); |
| 35 | 17 | } |
| 36 | |
|
| 37 | |
public String getSchemaName() { |
| 38 | 45 | return schemaName; |
| 39 | |
} |
| 40 | |
|
| 41 | |
public void setSchemaName(String schemaName) { |
| 42 | 2 | this.schemaName = StringUtils.trimToNull(schemaName); |
| 43 | 2 | } |
| 44 | |
|
| 45 | |
public String getTableName() { |
| 46 | 46 | return tableName; |
| 47 | |
} |
| 48 | |
|
| 49 | |
public void setTableName(String tableName) { |
| 50 | 3 | this.tableName = tableName; |
| 51 | 3 | } |
| 52 | |
|
| 53 | |
public String getColumnNames() { |
| 54 | 46 | return columnNames; |
| 55 | |
} |
| 56 | |
|
| 57 | |
public void setColumnNames(String columnNames) { |
| 58 | 3 | this.columnNames = columnNames; |
| 59 | 3 | } |
| 60 | |
|
| 61 | |
public String getConstraintName() { |
| 62 | 45 | return constraintName; |
| 63 | |
} |
| 64 | |
|
| 65 | |
public void setConstraintName(String constraintName) { |
| 66 | 2 | this.constraintName = constraintName; |
| 67 | 2 | } |
| 68 | |
|
| 69 | |
public String getTablespace() { |
| 70 | 45 | return tablespace; |
| 71 | |
} |
| 72 | |
|
| 73 | |
public void setTablespace(String tablespace) { |
| 74 | 2 | this.tablespace = tablespace; |
| 75 | 2 | } |
| 76 | |
|
| 77 | |
public Boolean getDeferrable() { |
| 78 | 45 | return deferrable; |
| 79 | |
} |
| 80 | |
|
| 81 | |
public void setDeferrable(Boolean deferrable) { |
| 82 | 2 | this.deferrable = deferrable; |
| 83 | 2 | } |
| 84 | |
|
| 85 | |
public Boolean getInitiallyDeferred() { |
| 86 | 45 | return initiallyDeferred; |
| 87 | |
} |
| 88 | |
|
| 89 | |
public void setInitiallyDeferred(Boolean initiallyDeferred) { |
| 90 | 2 | this.initiallyDeferred = initiallyDeferred; |
| 91 | 2 | } |
| 92 | |
|
| 93 | |
public Boolean getDisabled() { |
| 94 | 45 | return disabled; |
| 95 | |
} |
| 96 | |
|
| 97 | |
public void setDisabled(Boolean disabled) { |
| 98 | 2 | this.disabled = disabled; |
| 99 | 2 | } |
| 100 | |
|
| 101 | |
public SqlStatement[] generateStatements(Database database) { |
| 102 | |
|
| 103 | |
|
| 104 | |
|
| 105 | |
|
| 106 | |
|
| 107 | |
|
| 108 | 45 | boolean deferrable = false; |
| 109 | 45 | if (getDeferrable() != null) { |
| 110 | 0 | deferrable = getDeferrable(); |
| 111 | |
} |
| 112 | |
|
| 113 | 45 | boolean initiallyDeferred = false; |
| 114 | 45 | if (getInitiallyDeferred() != null) { |
| 115 | 0 | initiallyDeferred = getInitiallyDeferred(); |
| 116 | |
} |
| 117 | 45 | boolean disabled = false; |
| 118 | 45 | if (getDisabled() != null) { |
| 119 | 0 | disabled = getDisabled(); |
| 120 | |
} |
| 121 | |
|
| 122 | 45 | AddUniqueConstraintStatement statement = new AddUniqueConstraintStatement( |
| 123 | |
getSchemaName() == null ? database.getDefaultSchemaName() : getSchemaName(), getTableName(), |
| 124 | |
getColumnNames(), getConstraintName()); |
| 125 | 45 | statement.setTablespace(getTablespace()).setDeferrable(deferrable).setInitiallyDeferred(initiallyDeferred) |
| 126 | |
.setDisabled(disabled); |
| 127 | |
|
| 128 | 45 | return new SqlStatement[] { statement }; |
| 129 | |
} |
| 130 | |
|
| 131 | |
private SqlStatement[] generateStatementsForSQLiteDatabase(Database database) { |
| 132 | |
|
| 133 | |
|
| 134 | |
|
| 135 | |
|
| 136 | |
|
| 137 | 0 | List<SqlStatement> statements = new ArrayList<SqlStatement>(); |
| 138 | |
|
| 139 | |
|
| 140 | 0 | AlterTableVisitor rename_alter_visitor = new AlterTableVisitor() { |
| 141 | |
public ColumnConfig[] getColumnsToAdd() { |
| 142 | 0 | return new ColumnConfig[0]; |
| 143 | |
} |
| 144 | |
|
| 145 | |
public boolean copyThisColumn(ColumnConfig column) { |
| 146 | 0 | return true; |
| 147 | |
} |
| 148 | |
|
| 149 | |
public boolean createThisColumn(ColumnConfig column) { |
| 150 | 0 | String[] split_columns = getColumnNames().split("[ ]*,[ ]*"); |
| 151 | 0 | for (String split_column : split_columns) { |
| 152 | 0 | if (column.getName().equals(split_column)) { |
| 153 | 0 | column.getConstraints().setUnique(true); |
| 154 | |
} |
| 155 | |
} |
| 156 | 0 | return true; |
| 157 | |
} |
| 158 | |
|
| 159 | |
public boolean createThisIndex(Index index) { |
| 160 | 0 | return true; |
| 161 | |
} |
| 162 | |
}; |
| 163 | |
|
| 164 | |
try { |
| 165 | |
|
| 166 | 0 | statements.addAll(SQLiteDatabase.getAlterTableStatements(rename_alter_visitor, database, getSchemaName(), |
| 167 | |
getTableName())); |
| 168 | 0 | } catch (Exception e) { |
| 169 | 0 | e.printStackTrace(); |
| 170 | 0 | } |
| 171 | |
|
| 172 | 0 | return statements.toArray(new SqlStatement[statements.size()]); |
| 173 | |
} |
| 174 | |
|
| 175 | |
public String getConfirmationMessage() { |
| 176 | 1 | return "Unique constraint added to " + getTableName() + "(" + getColumnNames() + ")"; |
| 177 | |
} |
| 178 | |
|
| 179 | |
@Override |
| 180 | |
protected Change[] createInverses() { |
| 181 | 0 | DropUniqueConstraintChange inverse = new DropUniqueConstraintChange(); |
| 182 | 0 | inverse.setSchemaName(getSchemaName()); |
| 183 | 0 | inverse.setTableName(getTableName()); |
| 184 | 0 | inverse.setConstraintName(getConstraintName()); |
| 185 | |
|
| 186 | 0 | return new Change[] { inverse, }; |
| 187 | |
} |
| 188 | |
} |