Skip to content

Commit

Permalink
Fixed issue that unique constraint precondition does not honor schema…
Browse files Browse the repository at this point in the history
…Name attribute on MySQL (#5783)

* Fixed issue that unique constraint precondition does not honor schemaName attribute on MySQL

* Fix issue that failed to honor default schema in unique constraint exists precondition

* Refactor code to fall back to default schema

* Added test for UniqueConstraintExistsPrecondition

---------

Co-authored-by: Daniel Mallorga <75833793+MalloD12@users.noreply.github.com>
  • Loading branch information
mpvvliet and MalloD12 committed May 9, 2024
1 parent 77f1384 commit c095fa2
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1352,4 +1352,10 @@
</preConditions>
</changeSet>

<changeSet id="check-uniqueConstraintExists" author="mpvvliet">
<preConditions>
<uniqueConstraintExists tableName="createTableNamedUqConst" constraintName="uq_uqtest1"/>
</preConditions>
</changeSet>

</databaseChangeLog>
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,12 @@ public ValidationErrors validate(Database database) {
public void check(Database database, DatabaseChangeLog changeLog, ChangeSet changeSet, ChangeExecListener changeExecListener)
throws PreconditionFailedException, PreconditionErrorException {

String schemaName = getSchema(database);

UniqueConstraint example = new UniqueConstraint(
StringUtil.trimToNull(getConstraintName()),
StringUtil.trimToNull(getCatalogName()),
StringUtil.trimToNull(getSchemaName()),
StringUtil.trimToNull(schemaName),
StringUtil.trimToNull(getTableName()));

String columnNames = StringUtil.trimToNull(getColumnNames());
Expand All @@ -117,6 +119,14 @@ public void check(Database database, DatabaseChangeLog changeLog, ChangeSet chan
}
}

private String getSchema(Database database) {
String schemaName = getSchemaName();
if (schemaName == null) {
schemaName = database.getDefaultSchemaName();
}
return schemaName;
}

private List<Column> toColumns(Database database, String columnNames) {
return Arrays.stream(columnNames.split("\\s*,\\s*"))
.map(columnName -> database.correctObjectName(columnName, Column.class))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ protected List<CachedRow> listConstraints(Table table, DatabaseSnapshot snapshot
+ "on const.constraint_schema=col.constraint_schema "
+ "and const.table_name=col.table_name "
+ "and const.constraint_name=col.constraint_name "
+ "where const.constraint_schema='" + database.correctObjectName(schema.getCatalogName(), Catalog.class) + "' ";
+ "where const.constraint_schema='" + database.correctObjectName(schema.getSchema().getName(), Schema.class) + "' ";
if (!bulkQuery) {
sql += "and const.table_name='" + database.correctObjectName(example.getRelation().getName(), Table.class) + "' "
+ "and const.constraint_name='" + database.correctObjectName(name, UniqueConstraint.class) + "'";
Expand Down

0 comments on commit c095fa2

Please sign in to comment.