Skip to content

Commit

Permalink
Merge pull request #3123 from liquibase/DAT-11181
Browse files Browse the repository at this point in the history
Fix columnNames snapshot attribute of uniqueConstraints for Snowflake
  • Loading branch information
yodzhubeiskyi committed Aug 2, 2022
2 parents 205996a + 40e5ab6 commit 9c3666a
Showing 1 changed file with 8 additions and 21 deletions.
Expand Up @@ -10,10 +10,7 @@
import liquibase.snapshot.SnapshotGenerator;
import liquibase.statement.core.RawSqlStatement;
import liquibase.structure.DatabaseObject;
import liquibase.structure.core.Relation;
import liquibase.structure.core.Schema;
import liquibase.structure.core.Table;
import liquibase.structure.core.UniqueConstraint;
import liquibase.structure.core.*;

import java.sql.SQLException;
import java.util.List;
Expand Down Expand Up @@ -47,25 +44,15 @@ protected List<CachedRow> listConstraints(Table table, DatabaseSnapshot snapshot
protected List<Map<String, ?>> listColumns(UniqueConstraint example, Database database, DatabaseSnapshot snapshot)
throws DatabaseException {
Relation table = example.getRelation();
Schema schema = table.getSchema();
String name = example.getName();
String schemaName = database.correctObjectName(schema.getName(), Schema.class);
String constraintName = database.correctObjectName(name, UniqueConstraint.class);
String tableName = database.correctObjectName(table.getName(), Table.class);
//TODO figure out what to do with COLUMN_NAME
// https://community.snowflake.com/s/question/0D50Z00008Y3VUbSAN/table-details-including-the-constraints
String sql = "select CONSTRAINT_NAME, CONSTRAINT_NAME as COLUMN_NAME " + "from " + database.getSystemSchema() +
".TABLE_CONSTRAINTS "
+ "where CONSTRAINT_TYPE='UNIQUE'";
if (schemaName != null) {
sql += "and CONSTRAINT_SCHEMA='" + schemaName + "' ";
}
if (tableName != null) {
sql += "and TABLE_NAME='" + tableName + "' ";
}
if (constraintName != null) {
sql += "and CONSTRAINT_NAME='" + constraintName + "'";
}
String constraintName = database.correctObjectName(name, UniqueConstraint.class);

String showSql = "SHOW UNIQUE KEYS IN " + tableName;
String sql = "SELECT \"column_name\" AS COLUMN_NAME FROM TABLE(result_scan(last_query_id())) WHERE \"constraint_name\"= '" + constraintName +"'";

Scope.getCurrentScope().getSingleton(ExecutorService.class).getExecutor("jdbc", database)
.queryForList(new RawSqlStatement(showSql));

return Scope.getCurrentScope().getSingleton(ExecutorService.class).getExecutor("jdbc", database)
.queryForList(new RawSqlStatement(sql));
Expand Down

0 comments on commit 9c3666a

Please sign in to comment.