generated from liquibase/liquibase-extension-example
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
DAT-18897: merged main changes, renamed ChangedTblPropertiesUtil.getE…
…xtendedProperties -> getFilteredTblProperties. Removed filtering of table properties in snapshot.
- Loading branch information
Showing
10 changed files
with
397 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
...liquibase/ext/databricks/diff/output/changelog/ChangedTableChangeGeneratorDatabricks.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package liquibase.ext.databricks.diff.output.changelog; | ||
|
||
import liquibase.change.Change; | ||
import liquibase.database.Database; | ||
import liquibase.diff.Difference; | ||
import liquibase.diff.ObjectDifferences; | ||
import liquibase.diff.output.DiffOutputControl; | ||
import liquibase.diff.output.changelog.ChangeGeneratorChain; | ||
import liquibase.diff.output.changelog.core.ChangedTableChangeGenerator; | ||
import liquibase.ext.databricks.change.AbstractAlterPropertiesChangeDatabricks; | ||
import liquibase.ext.databricks.database.DatabricksDatabase; | ||
import liquibase.structure.DatabaseObject; | ||
import liquibase.structure.core.Table; | ||
|
||
import java.util.Arrays; | ||
|
||
import static liquibase.ext.databricks.diff.output.changelog.ChangedTblPropertiesUtil.getAlterTablePropertiesChangeDatabricks; | ||
|
||
/** | ||
* Custom diff change generator for Databricks | ||
*/ | ||
public class ChangedTableChangeGeneratorDatabricks extends ChangedTableChangeGenerator { | ||
|
||
@Override | ||
public int getPriority(Class<? extends DatabaseObject> objectType, Database database) { | ||
if (database instanceof DatabricksDatabase && super.getPriority(objectType, database) > PRIORITY_NONE) { | ||
return PRIORITY_DATABASE; | ||
} | ||
return PRIORITY_NONE; | ||
} | ||
|
||
@Override | ||
public Change[] fixChanged(DatabaseObject changedObject, ObjectDifferences differences, DiffOutputControl control, Database referenceDatabase, Database comparisonDatabase, ChangeGeneratorChain chain) { | ||
Change[] changes = super.fixChanged(changedObject, differences, control, referenceDatabase, comparisonDatabase, chain); | ||
for (Difference difference : differences.getDifferences()) { | ||
if (difference.getField().equals("tblProperties")) { | ||
AbstractAlterPropertiesChangeDatabricks[] change = getAlterTablePropertiesChangeDatabricks((Table) changedObject, control, difference); | ||
|
||
if (changes == null || changes.length == 0) { | ||
changes = change; | ||
} else { | ||
changes = Arrays.copyOf(changes, changes.length + change.length); | ||
System.arraycopy(change, 0, changes, changes.length - change.length, change.length); | ||
} | ||
} | ||
} | ||
return changes; | ||
} | ||
|
||
} |
129 changes: 129 additions & 0 deletions
129
src/main/java/liquibase/ext/databricks/diff/output/changelog/ChangedTblPropertiesUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
package liquibase.ext.databricks.diff.output.changelog; | ||
|
||
import liquibase.diff.Difference; | ||
import liquibase.diff.output.DiffOutputControl; | ||
import liquibase.exception.UnexpectedLiquibaseException; | ||
import liquibase.ext.databricks.change.AbstractAlterPropertiesChangeDatabricks; | ||
import liquibase.ext.databricks.change.alterTableProperties.AlterTablePropertiesChangeDatabricks; | ||
import liquibase.ext.databricks.change.alterTableProperties.SetExtendedTableProperties; | ||
import liquibase.ext.databricks.change.alterTableProperties.UnsetExtendedTableProperties; | ||
import liquibase.ext.databricks.change.alterViewProperties.AlterViewPropertiesChangeDatabricks; | ||
import liquibase.structure.AbstractDatabaseObject; | ||
import liquibase.structure.core.Table; | ||
import liquibase.structure.core.View; | ||
|
||
import java.util.Arrays; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.stream.Collectors; | ||
import java.util.stream.Stream; | ||
|
||
/** | ||
* Utility class for changed table properties diff | ||
*/ | ||
public class ChangedTblPropertiesUtil { | ||
|
||
|
||
private static final String SPLIT_ON_COMMAS = ",(?=(?:[^\"]*\"[^\"]*\")*[^\"$])"; | ||
private static final String SPLIT_ON_EQUALS = "=(?=(?:[^\"]*\"[^\"]*\")*[^\"$])"; | ||
|
||
private ChangedTblPropertiesUtil() { | ||
} | ||
|
||
/** | ||
* Get the AlterViewPropertiesChangeDatabricks changes | ||
*/ | ||
static AbstractAlterPropertiesChangeDatabricks[] getAlterViewPropertiesChangeDatabricks(View changedObject, DiffOutputControl control, Difference difference) { | ||
AbstractAlterPropertiesChangeDatabricks[] change = getAbstractTablePropertiesChangeDatabricks(changedObject, control, difference, AlterViewPropertiesChangeDatabricks.class); | ||
Stream.of(change).forEach(c -> ((AlterViewPropertiesChangeDatabricks)c).setViewName(changedObject.getName())); | ||
return change; | ||
} | ||
|
||
/** | ||
* Get the AlterTablePropertiesChangeDatabricks changes | ||
*/ | ||
static AbstractAlterPropertiesChangeDatabricks[] getAlterTablePropertiesChangeDatabricks(Table changedObject, DiffOutputControl control, Difference difference) { | ||
AbstractAlterPropertiesChangeDatabricks[] change = getAbstractTablePropertiesChangeDatabricks(changedObject, control, difference, AlterTablePropertiesChangeDatabricks.class); | ||
Stream.of(change).forEach(c -> ((AlterTablePropertiesChangeDatabricks)c).setTableName(changedObject.getName())); | ||
return change; | ||
} | ||
|
||
static AbstractAlterPropertiesChangeDatabricks[] getAbstractTablePropertiesChangeDatabricks(AbstractDatabaseObject changedObject, DiffOutputControl control, Difference difference, Class<? extends AbstractAlterPropertiesChangeDatabricks> clazz) { | ||
AbstractAlterPropertiesChangeDatabricks[] changes = new AbstractAlterPropertiesChangeDatabricks[0]; | ||
Map<String, String> referencedValuesMap = convertToMapExcludingDeltaParameters(difference.getReferenceValue()); | ||
Map<String, String> comparedValuesMap = convertToMapExcludingDeltaParameters(difference.getComparedValue()); | ||
|
||
Map<String, String> addPropertiesMap = new HashMap<>(); | ||
//first we add the missing or changed properties | ||
referencedValuesMap.forEach((key, value) -> { | ||
if (!comparedValuesMap.containsKey(key) || !comparedValuesMap.get(key).equals(value)) { | ||
addPropertiesMap.put(key, value); | ||
} | ||
}); | ||
//then we remove the properties that are not in the reference | ||
Map<String, String> removePropertiesMap = comparedValuesMap.entrySet().stream() | ||
.filter(entry -> !referencedValuesMap.containsKey(entry.getKey())) | ||
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)); | ||
|
||
if (!addPropertiesMap.isEmpty()) { | ||
SetExtendedTableProperties setExtendedTableProperties = new SetExtendedTableProperties(); | ||
setExtendedTableProperties.setTblProperties(addPropertiesMap.entrySet().stream().map(e -> e.getKey() + "=" + e.getValue()).collect(Collectors.joining(","))); | ||
AbstractAlterPropertiesChangeDatabricks change = getAbstractAlterPropertiesChangeDatabricks(changedObject, control, clazz); | ||
change.setSetExtendedTableProperties(setExtendedTableProperties); | ||
changes = new AbstractAlterPropertiesChangeDatabricks[]{change}; | ||
} | ||
|
||
if (!removePropertiesMap.isEmpty()) { | ||
UnsetExtendedTableProperties unsetExtendedTableProperties = new UnsetExtendedTableProperties(); | ||
unsetExtendedTableProperties.setTblProperties(String.join(",", removePropertiesMap.keySet())); | ||
AbstractAlterPropertiesChangeDatabricks change = getAbstractAlterPropertiesChangeDatabricks(changedObject, control, clazz); | ||
change.setUnsetExtendedTableProperties(unsetExtendedTableProperties); | ||
if (changes.length == 0) { | ||
changes = new AbstractAlterPropertiesChangeDatabricks[]{change}; | ||
} else { | ||
changes = Arrays.copyOf(changes, changes.length + 1); | ||
changes[changes.length - 1] = change; | ||
} | ||
} | ||
|
||
return changes; | ||
} | ||
|
||
/** | ||
* Convert the reference value to a map excluding delta parameters | ||
*/ | ||
private static Map<String, String> convertToMapExcludingDeltaParameters(Object referenceValueObject) { | ||
String referenceValue = referenceValueObject == null ? "" : referenceValueObject.toString(); | ||
return Arrays.stream(referenceValue.split(SPLIT_ON_COMMAS)) | ||
.map(s -> s.split(SPLIT_ON_EQUALS)) | ||
.filter(a -> a.length > 1) | ||
.map(a -> new String[]{a[0].trim(), a[1].trim()}) | ||
.filter(a -> !a[0].replace("'", "").matches("^delta.+")) | ||
.collect(Collectors.toMap(a -> a[0], a -> a[1])); | ||
} | ||
|
||
/** | ||
* Get the extended properties excluding delta parameters | ||
*/ | ||
public static String getFilteredTblProperties(String tblProperties) { | ||
Map<String, String> properties = convertToMapExcludingDeltaParameters(tblProperties); | ||
return properties.entrySet().stream().map(e -> e.getKey() + "=" + e.getValue()).collect(Collectors.joining(",")); | ||
} | ||
|
||
private static AbstractAlterPropertiesChangeDatabricks getAbstractAlterPropertiesChangeDatabricks(AbstractDatabaseObject changedObject, DiffOutputControl control, Class<? extends AbstractAlterPropertiesChangeDatabricks> clazz) { | ||
AbstractAlterPropertiesChangeDatabricks change; | ||
try { | ||
change = clazz.getDeclaredConstructor().newInstance(); | ||
} catch (Exception e) { | ||
throw new UnexpectedLiquibaseException("Reflection error", e); | ||
} | ||
if (control.getIncludeCatalog()) { | ||
change.setCatalogName(changedObject.getSchema().getCatalogName()); | ||
} | ||
|
||
if (control.getIncludeSchema()) { | ||
change.setSchemaName(changedObject.getSchema().getName()); | ||
} | ||
return change; | ||
} | ||
} |
61 changes: 61 additions & 0 deletions
61
.../liquibase/ext/databricks/diff/output/changelog/ChangedViewChangeGeneratorDatabricks.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package liquibase.ext.databricks.diff.output.changelog; | ||
|
||
import liquibase.change.Change; | ||
import liquibase.database.Database; | ||
import liquibase.diff.Difference; | ||
import liquibase.diff.ObjectDifferences; | ||
import liquibase.diff.output.DiffOutputControl; | ||
import liquibase.diff.output.changelog.ChangeGeneratorChain; | ||
import liquibase.diff.output.changelog.core.ChangedViewChangeGenerator; | ||
import liquibase.ext.databricks.change.AbstractAlterPropertiesChangeDatabricks; | ||
import liquibase.ext.databricks.database.DatabricksDatabase; | ||
import liquibase.structure.DatabaseObject; | ||
import liquibase.structure.core.View; | ||
|
||
import java.util.Arrays; | ||
|
||
import static liquibase.ext.databricks.diff.output.changelog.ChangedTblPropertiesUtil.getAlterViewPropertiesChangeDatabricks; | ||
|
||
|
||
public class ChangedViewChangeGeneratorDatabricks extends ChangedViewChangeGenerator { | ||
|
||
@Override | ||
public int getPriority(Class<? extends DatabaseObject> objectType, Database database) { | ||
if (database instanceof DatabricksDatabase && super.getPriority(objectType, database) > PRIORITY_NONE) { | ||
return PRIORITY_DATABASE; | ||
} | ||
return PRIORITY_NONE; | ||
} | ||
|
||
@Override | ||
public Change[] fixChanged(DatabaseObject changedObject, ObjectDifferences differences, DiffOutputControl control, Database referenceDatabase, Database comparisonDatabase, ChangeGeneratorChain chain) { | ||
Change[] changes = null; | ||
for (Difference difference : differences.getDifferences()) { | ||
if (difference.getField().equals("tblProperties")) { | ||
AbstractAlterPropertiesChangeDatabricks[] change = getAlterViewPropertiesChangeDatabricks((View) changedObject, control, difference); | ||
|
||
if (changes == null) { | ||
changes = change; | ||
} else { | ||
changes = Arrays.copyOf(changes, changes.length + change.length); | ||
System.arraycopy(change, 0, changes, changes.length - change.length, change.length); | ||
} | ||
differences.removeDifference("tblProperties"); | ||
} | ||
} | ||
|
||
if (differences.hasDifferences()) { | ||
Change[] otherChanges = super.fixChanged(changedObject, differences, control, referenceDatabase, comparisonDatabase, chain); | ||
if (otherChanges != null) { | ||
if (changes == null) { | ||
changes = otherChanges; | ||
} else { | ||
changes = Arrays.copyOf(changes, changes.length + otherChanges.length); | ||
System.arraycopy(otherChanges, 0, changes, changes.length - otherChanges.length, otherChanges.length); | ||
} | ||
} | ||
} | ||
|
||
return changes; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 2 additions & 0 deletions
2
src/main/resources/META-INF/services/liquibase.diff.output.changelog.ChangeGenerator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,4 @@ | ||
liquibase.ext.databricks.diff.output.changelog.MissingTableChangeGeneratorDatabricks | ||
liquibase.ext.databricks.diff.output.changelog.MissingViewChangeGeneratorDatabricks | ||
liquibase.ext.databricks.diff.output.changelog.ChangedTableChangeGeneratorDatabricks | ||
liquibase.ext.databricks.diff.output.changelog.ChangedViewChangeGeneratorDatabricks |
Oops, something went wrong.