| 1 | |
package liquibase.precondition.core; |
| 2 | |
|
| 3 | |
import liquibase.changelog.ChangeSet; |
| 4 | |
import liquibase.changelog.DatabaseChangeLog; |
| 5 | |
import liquibase.database.Database; |
| 6 | |
import liquibase.exception.DatabaseException; |
| 7 | |
import liquibase.exception.PreconditionErrorException; |
| 8 | |
import liquibase.exception.PreconditionFailedException; |
| 9 | |
import liquibase.exception.ValidationErrors; |
| 10 | |
import liquibase.exception.Warnings; |
| 11 | |
import liquibase.precondition.Precondition; |
| 12 | |
import liquibase.snapshot.DatabaseSnapshotGeneratorFactory; |
| 13 | |
import liquibase.util.StringUtils; |
| 14 | |
|
| 15 | 8 | public class IndexExistsPrecondition implements Precondition { |
| 16 | |
private String schemaName; |
| 17 | |
private String tableName; |
| 18 | |
private String columnNames; |
| 19 | |
private String indexName; |
| 20 | |
|
| 21 | |
public String getSchemaName() { |
| 22 | 0 | return schemaName; |
| 23 | |
} |
| 24 | |
|
| 25 | |
public void setSchemaName(String schemaName) { |
| 26 | 0 | this.schemaName = StringUtils.trimToNull(schemaName); |
| 27 | 0 | } |
| 28 | |
|
| 29 | |
public String getTableName() { |
| 30 | 0 | return tableName; |
| 31 | |
} |
| 32 | |
|
| 33 | |
public void setTableName(String tableName) { |
| 34 | 0 | this.tableName = tableName; |
| 35 | 0 | } |
| 36 | |
|
| 37 | |
public String getIndexName() { |
| 38 | 0 | return indexName; |
| 39 | |
} |
| 40 | |
|
| 41 | |
public void setIndexName(String indexName) { |
| 42 | 0 | this.indexName = indexName; |
| 43 | 0 | } |
| 44 | |
|
| 45 | |
public String getColumnNames() { |
| 46 | 0 | return columnNames; |
| 47 | |
} |
| 48 | |
|
| 49 | |
public void setColumnNames(String columnNames) { |
| 50 | 0 | this.columnNames = columnNames; |
| 51 | 0 | } |
| 52 | |
|
| 53 | |
@Override |
| 54 | |
public Warnings warn(Database database) { |
| 55 | 0 | return new Warnings(); |
| 56 | |
} |
| 57 | |
|
| 58 | |
@Override |
| 59 | |
public ValidationErrors validate(Database database) { |
| 60 | 0 | ValidationErrors validationErrors = new ValidationErrors(); |
| 61 | 0 | if (getIndexName() == null && getTableName() == null && getColumnNames() == null) { |
| 62 | 0 | validationErrors.addError("indexName OR tableName and columnNames is required"); |
| 63 | |
} |
| 64 | 0 | return validationErrors; |
| 65 | |
} |
| 66 | |
|
| 67 | |
@Override |
| 68 | |
public void check(Database database, DatabaseChangeLog changeLog, ChangeSet changeSet) |
| 69 | |
throws PreconditionFailedException, PreconditionErrorException { |
| 70 | |
try { |
| 71 | 0 | if (!DatabaseSnapshotGeneratorFactory.getInstance().getGenerator(database) |
| 72 | |
.hasIndex(getSchemaName(), getTableName(), getIndexName(), database, getColumnNames())) { |
| 73 | 0 | String name = ""; |
| 74 | |
|
| 75 | 0 | if (getIndexName() != null) { |
| 76 | 0 | name += database.escapeStringForDatabase(getIndexName()); |
| 77 | |
} |
| 78 | |
|
| 79 | 0 | if (StringUtils.trimToNull(getTableName()) != null) { |
| 80 | 0 | name += " on " + database.escapeStringForDatabase(getTableName()); |
| 81 | |
|
| 82 | 0 | if (StringUtils.trimToNull(getColumnNames()) != null) { |
| 83 | 0 | name += " columns " + getColumnNames(); |
| 84 | |
} |
| 85 | |
} |
| 86 | 0 | throw new PreconditionFailedException("Index " + name + " does not exist", changeLog, this); |
| 87 | |
} |
| 88 | 0 | } catch (DatabaseException e) { |
| 89 | 0 | throw new PreconditionErrorException(e, changeLog, this); |
| 90 | 0 | } |
| 91 | 0 | } |
| 92 | |
|
| 93 | |
@Override |
| 94 | |
public String getName() { |
| 95 | 8 | return "indexExists"; |
| 96 | |
} |
| 97 | |
} |