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.
Merge pull request #158 from liquibase/DAT-17951
[DAT-17951] Add AlterCluster change
- Loading branch information
Showing
14 changed files
with
492 additions
and
2 deletions.
There are no files selected for viewing
107 changes: 107 additions & 0 deletions
107
src/main/java/liquibase/ext/databricks/change/alterCluster/AlterClusterChangeDatabricks.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,107 @@ | ||
package liquibase.ext.databricks.change.alterCluster; | ||
|
||
import liquibase.change.AbstractChange; | ||
import liquibase.change.DatabaseChange; | ||
import liquibase.change.DatabaseChangeProperty; | ||
import liquibase.database.Database; | ||
import liquibase.exception.ValidationErrors; | ||
import liquibase.servicelocator.PrioritizedService; | ||
import liquibase.statement.SqlStatement; | ||
|
||
import java.text.MessageFormat; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
@DatabaseChange(name = "alterCluster", description = "Alter Cluster", priority = PrioritizedService.PRIORITY_DATABASE +500) | ||
public class AlterClusterChangeDatabricks extends AbstractChange { | ||
|
||
private String tableName; | ||
private String catalogName; | ||
private String schemaName; | ||
private List<ColumnConfig> columns; | ||
private List<NoneConfig> clusterBy; | ||
|
||
@Override | ||
public ValidationErrors validate(Database database) { | ||
ValidationErrors validationErrors = new ValidationErrors(); | ||
validationErrors.addAll(super.validate(database)); | ||
|
||
if (columns == null && clusterBy == null) { | ||
validationErrors.addError("Alter Cluster change require list of columns or element 'ClusterBy', please add at least one option."); | ||
} | ||
return validationErrors; | ||
} | ||
|
||
public AlterClusterChangeDatabricks() { | ||
super(); | ||
columns = new ArrayList<>(); | ||
clusterBy = new ArrayList<>(); | ||
} | ||
|
||
@Override | ||
public String getConfirmationMessage() { | ||
return MessageFormat.format("{0}.{1}.{2} successfully altered.", getCatalogName(), getSchemaName(), getTableName()); | ||
} | ||
|
||
@Override | ||
public SqlStatement[] generateStatements(Database database) { | ||
AlterClusterDatabricksStatement statement = new AlterClusterDatabricksStatement(tableName, catalogName, schemaName); | ||
if (getColumns() != null && !getColumns().isEmpty()) { | ||
statement.setColumns(getColumns()); | ||
} else if (getClusterBy() != null && !getClusterBy().isEmpty()) { | ||
statement.setClusterBy(getClusterBy()); | ||
} | ||
return new SqlStatement[]{statement}; | ||
} | ||
|
||
@DatabaseChangeProperty | ||
public String getTableName() { | ||
return tableName; | ||
} | ||
|
||
public void setTableName(String tableName) { | ||
this.tableName = tableName; | ||
} | ||
|
||
@DatabaseChangeProperty | ||
public List<ColumnConfig> getColumns() { | ||
if (columns == null) { | ||
return new ArrayList<>(); | ||
} | ||
return columns; | ||
} | ||
|
||
public void setColumns(List<ColumnConfig> columns) { | ||
this.columns = columns; | ||
} | ||
|
||
@DatabaseChangeProperty | ||
public String getCatalogName() { | ||
return catalogName; | ||
} | ||
|
||
public void setCatalogName(String catalogName) { | ||
this.catalogName = catalogName; | ||
} | ||
|
||
@DatabaseChangeProperty | ||
public String getSchemaName() { | ||
return schemaName; | ||
} | ||
|
||
public void setSchemaName(String schemaName) { | ||
this.schemaName = schemaName; | ||
} | ||
|
||
@DatabaseChangeProperty | ||
public List<NoneConfig> getClusterBy() { | ||
if (clusterBy == null) { | ||
return new ArrayList<>(); | ||
} | ||
return clusterBy; | ||
} | ||
|
||
public void setClusterBy(List<NoneConfig> clusterBy) { | ||
this.clusterBy = clusterBy; | ||
} | ||
} |
60 changes: 60 additions & 0 deletions
60
...in/java/liquibase/ext/databricks/change/alterCluster/AlterClusterDatabricksStatement.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,60 @@ | ||
package liquibase.ext.databricks.change.alterCluster; | ||
|
||
import liquibase.statement.AbstractSqlStatement; | ||
|
||
import java.util.List; | ||
|
||
public class AlterClusterDatabricksStatement extends AbstractSqlStatement { | ||
|
||
private String tableName; | ||
private String catalogName; | ||
private String schemaName; | ||
private List<ColumnConfig> columns; | ||
private List<NoneConfig> clusterBy; | ||
|
||
public AlterClusterDatabricksStatement(String tableName, String catalogName, String schemaName) { | ||
this.tableName = tableName; | ||
this.catalogName = catalogName; | ||
this.schemaName = schemaName; | ||
} | ||
|
||
public String getTableName() { | ||
return tableName; | ||
} | ||
|
||
public void setTableName(String tableName) { | ||
this.tableName = tableName; | ||
} | ||
|
||
public List<ColumnConfig> getColumns() { | ||
return columns; | ||
} | ||
|
||
public void setColumns(List<ColumnConfig> columns) { | ||
this.columns = columns; | ||
} | ||
|
||
public String getCatalogName() { | ||
return catalogName; | ||
} | ||
|
||
public void setCatalogName(String catalogName) { | ||
this.catalogName = catalogName; | ||
} | ||
|
||
public String getSchemaName() { | ||
return schemaName; | ||
} | ||
|
||
public void setSchemaName(String schemaName) { | ||
this.schemaName = schemaName; | ||
} | ||
|
||
public List<NoneConfig> getClusterBy() { | ||
return clusterBy; | ||
} | ||
|
||
public void setClusterBy(List<NoneConfig> clusterBy) { | ||
this.clusterBy = clusterBy; | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
src/main/java/liquibase/ext/databricks/change/alterCluster/ColumnConfig.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,26 @@ | ||
package liquibase.ext.databricks.change.alterCluster; | ||
|
||
import liquibase.serializer.AbstractLiquibaseSerializable; | ||
|
||
public class ColumnConfig extends AbstractLiquibaseSerializable { | ||
|
||
private String name; | ||
|
||
@Override | ||
public String getSerializedObjectName() { | ||
return "column"; | ||
} | ||
|
||
@Override | ||
public String getSerializedObjectNamespace() { | ||
return "http://www.liquibase.org/xml/ns/databricks"; | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public void setName(String name) { | ||
this.name = name; | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
src/main/java/liquibase/ext/databricks/change/alterCluster/NoneConfig.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,26 @@ | ||
package liquibase.ext.databricks.change.alterCluster; | ||
|
||
import liquibase.serializer.AbstractLiquibaseSerializable; | ||
|
||
public class NoneConfig extends AbstractLiquibaseSerializable { | ||
|
||
private String none; | ||
|
||
@Override | ||
public String getSerializedObjectName() { | ||
return "clusterBy"; | ||
} | ||
|
||
@Override | ||
public String getSerializedObjectNamespace() { | ||
return "http://www.liquibase.org/xml/ns/databricks"; | ||
} | ||
|
||
public String getNone() { | ||
return none; | ||
} | ||
|
||
public void setNone(String none) { | ||
this.none = none; | ||
} | ||
} |
49 changes: 49 additions & 0 deletions
49
src/main/java/liquibase/ext/databricks/sqlgenerator/AlterClusterGeneratorDatabricks.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,49 @@ | ||
package liquibase.ext.databricks.sqlgenerator; | ||
|
||
import liquibase.database.Database; | ||
import liquibase.exception.ValidationErrors; | ||
import liquibase.ext.databricks.change.alterCluster.AlterClusterDatabricksStatement; | ||
import liquibase.ext.databricks.change.alterCluster.ColumnConfig; | ||
import liquibase.sql.Sql; | ||
import liquibase.sql.UnparsedSql; | ||
import liquibase.sqlgenerator.SqlGeneratorChain; | ||
import liquibase.sqlgenerator.core.AbstractSqlGenerator; | ||
|
||
public class AlterClusterGeneratorDatabricks extends AbstractSqlGenerator<AlterClusterDatabricksStatement> { | ||
@Override | ||
public ValidationErrors validate(AlterClusterDatabricksStatement statement, Database database, SqlGeneratorChain<AlterClusterDatabricksStatement> sqlGeneratorChain) { | ||
ValidationErrors validationErrors = new ValidationErrors(); | ||
if (statement.getClusterBy() == null && statement.getColumns() == null){ | ||
validationErrors.addError("WARNING! Alter Cluster change require list of columns or element 'ClusterBy', please add at least one option."); | ||
} | ||
if (statement.getClusterBy() != null && (statement.getClusterBy().isEmpty() || !statement.getClusterBy().get(0).getNone().equals("true"))) { | ||
validationErrors.addError("WARNING! ClusterBy attribute require attribute 'none=\"true\"'"); | ||
} | ||
return validationErrors; | ||
} | ||
|
||
@Override | ||
public Sql[] generateSql(AlterClusterDatabricksStatement statement, Database database, SqlGeneratorChain<AlterClusterDatabricksStatement> sqlGeneratorChain) { | ||
StringBuilder buffer = new StringBuilder(); | ||
|
||
buffer.append("ALTER TABLE "); | ||
buffer.append(database.escapeTableName(statement.getCatalogName(), statement.getSchemaName(), statement.getTableName())); | ||
buffer.append(" CLUSTER BY "); | ||
if (statement.getColumns() != null && !statement.getColumns().isEmpty()) { | ||
buffer.append("("); | ||
for (ColumnConfig column : statement.getColumns()) { | ||
buffer.append(column.getName()); | ||
buffer.append(","); | ||
} | ||
buffer.deleteCharAt(buffer.length() - 1); | ||
buffer.append(")"); | ||
} else if (statement.getClusterBy() != null && !statement.getClusterBy().isEmpty()) { | ||
buffer.append("NONE"); | ||
} | ||
|
||
return new Sql[]{ | ||
new UnparsedSql(buffer.toString()) | ||
}; | ||
} | ||
|
||
} |
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
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
Oops, something went wrong.