Skip to content

Commit

Permalink
added missingTable generator
Browse files Browse the repository at this point in the history
  • Loading branch information
KushnirykOleh committed Oct 8, 2024
1 parent 59a17e2 commit dc8439a
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 58 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import liquibase.database.Database;
import liquibase.exception.ValidationErrors;
import liquibase.ext.databricks.database.DatabricksDatabase;
import liquibase.ext.databricks.parser.NamespaceDetailsDatabricks;
import liquibase.servicelocator.PrioritizedService;
import liquibase.statement.core.CreateTableStatement;
import lombok.Setter;
Expand Down Expand Up @@ -71,4 +72,9 @@ public ExtendedTableProperties getExtendedTableProperties() {
return extendedTableProperties;
}

@Override
public String getSerializedObjectNamespace() {
return NamespaceDetailsDatabricks.DATABRICKS_NAMESPACE;
}

}
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
package liquibase.ext.databricks.change.createTable;

import liquibase.serializer.AbstractLiquibaseSerializable;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

@NoArgsConstructor
@AllArgsConstructor
@Setter
@Getter
public class ExtendedTableProperties extends AbstractLiquibaseSerializable{
private String tableLocation;
private String tblProperties;
Expand All @@ -15,20 +23,4 @@ public String getSerializedObjectName() {
public String getSerializedObjectNamespace() {
return "http://www.liquibase.org/xml/ns/databricks";
}

public String getTableLocation() {
return tableLocation;
}

public void setTableLocation(String tableLocation) {
this.tableLocation = tableLocation;
}

public String getTblProperties() {
return tblProperties;
}

public void setTblProperties(String tblProperties) {
this.tblProperties = tblProperties;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package liquibase.ext.databricks.diff.output.changelog;

import liquibase.change.Change;
import liquibase.change.core.CreateTableChange;
import liquibase.database.Database;
import liquibase.diff.output.DiffOutputControl;
import liquibase.diff.output.changelog.ChangeGeneratorChain;
import liquibase.diff.output.changelog.core.MissingTableChangeGenerator;
import liquibase.ext.databricks.change.createTable.CreateTableChangeDatabricks;
import liquibase.ext.databricks.change.createTable.ExtendedTableProperties;
import liquibase.ext.databricks.database.DatabricksDatabase;
import liquibase.structure.DatabaseObject;
import liquibase.structure.core.Table;

public class MissingTableChangeGeneratorDatabricks extends MissingTableChangeGenerator {


@Override
public int getPriority(Class<? extends DatabaseObject> objectType, Database database) {
if (database instanceof DatabricksDatabase && Table.class.isAssignableFrom(objectType)) {
return PRIORITY_DATABASE;
} else {
return PRIORITY_NONE;
}
}

@Override
public Change[] fixMissing(DatabaseObject missingObject, DiffOutputControl control, Database referenceDatabase, Database comparisonDatabase,
ChangeGeneratorChain chain) {
Change[] changes = super.fixMissing(missingObject, control, referenceDatabase, comparisonDatabase, chain);
if (changes == null || changes.length == 0) {
return changes;
}
ExtendedTableProperties extendedTableProperties = new ExtendedTableProperties(
missingObject.getAttribute("Location", String.class),
missingObject.getAttribute("tblProperties", String.class));

changes[0] = getCreateTableChangeDatabricks(extendedTableProperties, changes);
return changes;
}

private CreateTableChangeDatabricks getCreateTableChangeDatabricks(ExtendedTableProperties extendedTableProperties, Change[] changes) {
CreateTableChange temp = (CreateTableChange) changes[0];
CreateTableChangeDatabricks createTableChangeDatabricks = new CreateTableChangeDatabricks();
createTableChangeDatabricks.setColumns(temp.getColumns());
createTableChangeDatabricks.setTableType(temp.getTableType());
createTableChangeDatabricks.setCatalogName(temp.getCatalogName());
createTableChangeDatabricks.setSchemaName(temp.getSchemaName());
createTableChangeDatabricks.setTableName(temp.getTableName());
createTableChangeDatabricks.setTablespace(temp.getTablespace());
createTableChangeDatabricks.setRemarks(temp.getRemarks());
createTableChangeDatabricks.setIfNotExists(temp.getIfNotExists());
createTableChangeDatabricks.setRowDependencies(temp.getRowDependencies());

createTableChangeDatabricks.setExtendedTableProperties(extendedTableProperties);
return createTableChangeDatabricks;
}

protected CreateTableChange createCreateTableChange() {
return new CreateTableChangeDatabricks();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,11 @@
import java.util.List;
import java.util.Map;

/**
* Created by vesterma on 06/02/14.
*/
public class TableSnapshotGeneratorDatabricks extends TableSnapshotGenerator {

private final static String LOCATION = "Location";
private final static String TABLE_PROPERTIES = "Table Properties";
private final static String TBL_PROPERTIES = "tblProperties";
private final static String DETAILED_TABLE_INFORMATION_NODE = "# Detailed Table Information";

@Override
Expand Down Expand Up @@ -51,48 +49,12 @@ protected DatabaseObject snapshotObject(DatabaseObject example, DatabaseSnapshot
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"));
String tblProperties = (String) tableProperty.get("DATA_TYPE");
table.setAttribute(TBL_PROPERTIES, tblProperties.substring(1, tblProperties.length() - 1));// remove starting and ending square brackets
}
//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<Map<String, ?>> tablePropertiesResponse = Scope.getCurrentScope().getSingleton(ExecutorService.class)
// .getExecutor("jdbc", database).queryForList(new RawParameterizedSqlStatement(query));
// for (Map<String, ?> 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<Map<String, ?>> tablePropertiesResponse = Scope.getCurrentScope().getSingleton(ExecutorService.class)
// .getExecutor("jdbc", database).queryForList(new RawParameterizedSqlStatement(query));
// for (Map<String, ?> tableProperty : tablePropertiesResponse) {
// //TODO combine into
// // "'key0'='value0', 'key1'='value1', .... 'keyN'='valueN'"
// // csv string
//
// }
// return table;
}

// @Override
// public Class<? extends SnapshotGenerator>[] replaces() {
// return new Class[]{TableSnapshotGenerator.class};
// }

}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
liquibase.ext.databricks.diff.output.changelog.MissingTableChangeGeneratorDatabricks

0 comments on commit dc8439a

Please sign in to comment.