From 59a17e2192a102a2b294498e5a8c6c7ebf2f118f Mon Sep 17 00:00:00 2001 From: KushnirykOleh Date: Mon, 7 Oct 2024 16:04:28 +0300 Subject: [PATCH] wip --- .../jvm/TableSnapshotGeneratorDatabricks.java | 45 ++++++++++++++++++- 1 file changed, 43 insertions(+), 2 deletions(-) diff --git a/src/main/java/liquibase/ext/databricks/snapshot/jvm/TableSnapshotGeneratorDatabricks.java b/src/main/java/liquibase/ext/databricks/snapshot/jvm/TableSnapshotGeneratorDatabricks.java index 4080499a..02a56f29 100644 --- a/src/main/java/liquibase/ext/databricks/snapshot/jvm/TableSnapshotGeneratorDatabricks.java +++ b/src/main/java/liquibase/ext/databricks/snapshot/jvm/TableSnapshotGeneratorDatabricks.java @@ -19,6 +19,10 @@ */ public class TableSnapshotGeneratorDatabricks extends TableSnapshotGenerator { + private final static String LOCATION = "Location"; + private final static String TABLE_PROPERTIES = "Table Properties"; + private final static String DETAILED_TABLE_INFORMATION_NODE = "# Detailed Table Information"; + @Override public int getPriority(Class objectType, Database database) { if (database instanceof DatabricksDatabase) @@ -35,16 +39,53 @@ protected DatabaseObject snapshotObject(DatabaseObject example, DatabaseSnapshot String query = String.format("DESCRIBE TABLE EXTENDED %s.%s.%s;", database.getDefaultCatalogName(), database.getDefaultSchemaName(), example.getName()); List> tablePropertiesResponse = Scope.getCurrentScope().getSingleton(ExecutorService.class) .getExecutor("jdbc", database).queryForList(new RawParameterizedSqlStatement(query)); + // DESCRIBE TABLE EXTENDED returns both columns and additional information. + // We need to make sure "Location" is not column in the table, but table location in s3 + boolean detailedInformationNode = false; for (Map tableProperty : tablePropertiesResponse) { - table.setAttribute((String) tableProperty.get("KEY"), tableProperty.get("VALUE")); + if (tableProperty.get("COL_NAME").equals(DETAILED_TABLE_INFORMATION_NODE)) { + detailedInformationNode = true; + continue; + } + if (detailedInformationNode && tableProperty.get("COL_NAME").equals(LOCATION)) { + table.setAttribute(LOCATION, tableProperty.get("DATA_TYPE")); + } + if (detailedInformationNode && tableProperty.get("COL_NAME").equals(TABLE_PROPERTIES)) { + //TODO should i parse and split TableProperties or keep them all together? + table.setAttribute(TABLE_PROPERTIES, tableProperty.get("DATA_TYPE")); + } + //TODO i can get default value for column here `# Column Default Values` ->"COL_NAME" -> "title", "DATA_TYPE" -> "string", "COMMENT" -> + // "'title_test'" --actual default value for the column is in comment column of this resultSet } return table; + +// String query = String.format("SHOW TBLPROPERTIES %s.%s.%s;", database.getDefaultCatalogName(), database.getDefaultSchemaName(), example.getName()); + +// String query = String.format("SHOW TABLE EXTENDED IN %s.%s LIKE '%s';", database.getDefaultCatalogName(), database.getDefaultSchemaName(), +// example.getName()); +// List> tablePropertiesResponse = Scope.getCurrentScope().getSingleton(ExecutorService.class) +// .getExecutor("jdbc", database).queryForList(new RawParameterizedSqlStatement(query)); +// for (Map tableProperty : tablePropertiesResponse) { +// String[] tableParts = ((String) tableProperty.get("INFORMATION")).split("\\r?\\n"); +// for (String tablePart : tableParts) { +// if (tablePart.startsWith("Location:")) { +// table.setAttribute("Location", tablePart.replace("Location: ", "")); +// } +// if (tablePart.startsWith("Table Properties:")) { +// table.setAttribute("Table Properties", tablePart.replace("Table Properties: [", "").replace("]", "")); +// } +// } +// } + // String query = String.format("SHOW TBLPROPERTIES %s.%s.%s;", database.getDefaultCatalogName(), database.getDefaultSchemaName(), example.getName()); // List> tablePropertiesResponse = Scope.getCurrentScope().getSingleton(ExecutorService.class) // .getExecutor("jdbc", database).queryForList(new RawParameterizedSqlStatement(query)); // for (Map tableProperty : tablePropertiesResponse) { -// table.setAttribute((String) tableProperty.get("KEY"), tableProperty.get("VALUE")); +// //TODO combine into +// // "'key0'='value0', 'key1'='value1', .... 'keyN'='valueN'" +// // csv string +// // } // return table; }