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 #161 from liquibase/DAT-17950
[DAT-17950] Add Alter Table Properties change
- Loading branch information
Showing
15 changed files
with
381 additions
and
2 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
89 changes: 89 additions & 0 deletions
89
...base/ext/databricks/change/alterTableProperties/AlterTablePropertiesChangeDatabricks.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,89 @@ | ||
package liquibase.ext.databricks.change.alterTableProperties; | ||
|
||
import liquibase.change.AbstractChange; | ||
import liquibase.change.DatabaseChange; | ||
import liquibase.change.DatabaseChangeProperty; | ||
import liquibase.database.Database; | ||
import liquibase.exception.ValidationErrors; | ||
import liquibase.ext.databricks.database.DatabricksDatabase; | ||
import liquibase.servicelocator.PrioritizedService; | ||
import liquibase.statement.SqlStatement; | ||
import lombok.Setter; | ||
|
||
import java.text.MessageFormat; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import static liquibase.statement.SqlStatement.EMPTY_SQL_STATEMENT; | ||
|
||
@Setter | ||
@DatabaseChange(name = "alterTableProperties", description = "Alter Table Properties", priority = PrioritizedService.PRIORITY_DATABASE + 500) | ||
public class AlterTablePropertiesChangeDatabricks extends AbstractChange { | ||
|
||
private String tableName; | ||
private String catalogName; | ||
private String schemaName; | ||
private SetExtendedTableProperties setExtendedTableProperties; | ||
private UnsetExtendedTableProperties unsetExtendedTableProperties; | ||
|
||
@Override | ||
public boolean supports(Database database) { | ||
return database instanceof DatabricksDatabase; | ||
} | ||
|
||
@Override | ||
public ValidationErrors validate(Database database) { | ||
ValidationErrors validationErrors = new ValidationErrors(); | ||
validationErrors.addAll(super.validate(database)); | ||
|
||
if (setExtendedTableProperties == null && unsetExtendedTableProperties == null) { | ||
validationErrors.addError("Alter Table Properties change require 'setExtendedTableProperties' or 'unsetExtendedTableProperties' element, please add at least one option."); | ||
} | ||
return validationErrors; | ||
} | ||
|
||
@Override | ||
public String getConfirmationMessage() { | ||
return MessageFormat.format("{0}.{1}.{2} successfully altered.", getCatalogName(), getSchemaName(), getTableName()); | ||
} | ||
|
||
@Override | ||
public SqlStatement[] generateStatements(Database database) { | ||
AlterTablePropertiesStatementDatabricks statement = new AlterTablePropertiesStatementDatabricks(getCatalogName(), getSchemaName(), getTableName()); | ||
|
||
if (setExtendedTableProperties != null) { | ||
statement.setSetExtendedTableProperties(setExtendedTableProperties); | ||
} else if (unsetExtendedTableProperties != null) { | ||
statement.setUnsetExtendedTableProperties(unsetExtendedTableProperties); | ||
} | ||
|
||
List<SqlStatement> statements = new ArrayList<>(); | ||
statements.add(statement); | ||
return statements.toArray(EMPTY_SQL_STATEMENT); | ||
} | ||
|
||
@DatabaseChangeProperty | ||
public String getCatalogName() { | ||
return catalogName; | ||
} | ||
|
||
@DatabaseChangeProperty | ||
public String getSchemaName() { | ||
return schemaName; | ||
} | ||
|
||
@DatabaseChangeProperty | ||
public String getTableName() { | ||
return tableName; | ||
} | ||
|
||
@DatabaseChangeProperty | ||
public SetExtendedTableProperties getSetExtendedTableProperties() { | ||
return setExtendedTableProperties; | ||
} | ||
|
||
@DatabaseChangeProperty | ||
public UnsetExtendedTableProperties getUnsetExtendedTableProperties() { | ||
return unsetExtendedTableProperties; | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
...e/ext/databricks/change/alterTableProperties/AlterTablePropertiesStatementDatabricks.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,23 @@ | ||
package liquibase.ext.databricks.change.alterTableProperties; | ||
|
||
import liquibase.statement.AbstractSqlStatement; | ||
import lombok.Getter; | ||
import lombok.Setter; | ||
|
||
@Setter | ||
@Getter | ||
public class AlterTablePropertiesStatementDatabricks extends AbstractSqlStatement { | ||
|
||
private String tableName; | ||
private String catalogName; | ||
private String schemaName; | ||
private SetExtendedTableProperties setExtendedTableProperties; | ||
private UnsetExtendedTableProperties unsetExtendedTableProperties; | ||
|
||
public AlterTablePropertiesStatementDatabricks(String catalogName, String schemaName, String tableName) { | ||
this.tableName = tableName; | ||
this.catalogName = catalogName; | ||
this.schemaName = schemaName; | ||
} | ||
|
||
} |
21 changes: 21 additions & 0 deletions
21
...java/liquibase/ext/databricks/change/alterTableProperties/SetExtendedTableProperties.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,21 @@ | ||
package liquibase.ext.databricks.change.alterTableProperties; | ||
|
||
import liquibase.serializer.AbstractLiquibaseSerializable; | ||
import lombok.Getter; | ||
import lombok.Setter; | ||
|
||
@Getter | ||
@Setter | ||
public class SetExtendedTableProperties extends AbstractLiquibaseSerializable { | ||
private String tblProperties; | ||
|
||
@Override | ||
public String getSerializedObjectName() { | ||
return "setExtendedTableProperties"; | ||
} | ||
|
||
@Override | ||
public String getSerializedObjectNamespace() { | ||
return "http://www.liquibase.org/xml/ns/databricks"; | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
...va/liquibase/ext/databricks/change/alterTableProperties/UnsetExtendedTableProperties.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,21 @@ | ||
package liquibase.ext.databricks.change.alterTableProperties; | ||
|
||
import liquibase.serializer.AbstractLiquibaseSerializable; | ||
import lombok.Getter; | ||
import lombok.Setter; | ||
|
||
@Setter | ||
@Getter | ||
public class UnsetExtendedTableProperties extends AbstractLiquibaseSerializable{ | ||
private String tblProperties; | ||
|
||
@Override | ||
public String getSerializedObjectName() { | ||
return "unsetExtendedTableProperties"; | ||
} | ||
|
||
@Override | ||
public String getSerializedObjectNamespace() { | ||
return "http://www.liquibase.org/xml/ns/databricks"; | ||
} | ||
} |
52 changes: 52 additions & 0 deletions
52
...n/java/liquibase/ext/databricks/sqlgenerator/AlterTablePropertiesGeneratorDatabricks.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,52 @@ | ||
package liquibase.ext.databricks.sqlgenerator; | ||
|
||
import liquibase.database.Database; | ||
import liquibase.exception.ValidationErrors; | ||
import liquibase.ext.databricks.change.alterTableProperties.AlterTablePropertiesStatementDatabricks; | ||
import liquibase.ext.databricks.database.DatabricksDatabase; | ||
import liquibase.sql.Sql; | ||
import liquibase.sql.UnparsedSql; | ||
import liquibase.sqlgenerator.SqlGeneratorChain; | ||
import liquibase.sqlgenerator.core.AbstractSqlGenerator; | ||
|
||
public class AlterTablePropertiesGeneratorDatabricks extends AbstractSqlGenerator<AlterTablePropertiesStatementDatabricks> { | ||
|
||
@Override | ||
public boolean supports(AlterTablePropertiesStatementDatabricks statement, Database database) { | ||
return super.supports(statement, database) && (database instanceof DatabricksDatabase); | ||
} | ||
|
||
@Override | ||
public int getPriority() { | ||
return DatabricksDatabase.DATABRICKS_PRIORITY_DATABASE; | ||
} | ||
|
||
@Override | ||
public ValidationErrors validate(AlterTablePropertiesStatementDatabricks statement, Database database, SqlGeneratorChain<AlterTablePropertiesStatementDatabricks> sqlGeneratorChain) { | ||
ValidationErrors validationErrors = new ValidationErrors(); | ||
if (statement.getSetExtendedTableProperties() == null && statement.getUnsetExtendedTableProperties() == null){ | ||
validationErrors.addError("WARNING! Alter Table Properties change require 'setExtendedTableProperties' or 'unsetExtendedTableProperties' element, please add at least one option."); | ||
} | ||
return validationErrors; | ||
} | ||
|
||
@Override | ||
public Sql[] generateSql(AlterTablePropertiesStatementDatabricks statement, Database database, SqlGeneratorChain<AlterTablePropertiesStatementDatabricks> sqlGeneratorChain) { | ||
StringBuilder buffer = new StringBuilder(); | ||
|
||
buffer.append("ALTER TABLE "); | ||
buffer.append(database.escapeTableName(statement.getCatalogName(), statement.getSchemaName(), statement.getTableName())); | ||
if (statement.getSetExtendedTableProperties() != null) { | ||
buffer.append(" SET TBLPROPERTIES ("); | ||
buffer.append(statement.getSetExtendedTableProperties().getTblProperties()); | ||
} else if (statement.getUnsetExtendedTableProperties() != null) { | ||
buffer.append(" UNSET TBLPROPERTIES ("); | ||
buffer.append(statement.getUnsetExtendedTableProperties().getTblProperties()); | ||
} | ||
buffer.append(")"); | ||
|
||
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
51 changes: 51 additions & 0 deletions
51
src/test/resources/liquibase/harness/change/changelogs/databricks/alterTableProperties.json
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,51 @@ | ||
{ | ||
"databaseChangeLog": [ | ||
{ | ||
"changeSet": { | ||
"id": "1", | ||
"author": "your.name", | ||
"changes": [ | ||
{ | ||
"createTable": { | ||
"tableName": "test_alter_table_properties", | ||
"columns": [ | ||
{ | ||
"column": { | ||
"name":"test_id", | ||
"type": "int" | ||
} | ||
} | ||
] | ||
} | ||
} | ||
] | ||
} | ||
}, | ||
{ | ||
"changeSet": { | ||
"id": "2", | ||
"author": "your.name", | ||
"changes": [ | ||
{ | ||
"alterTableProperties": { | ||
"tableName": "test_alter_table_properties", | ||
"setExtendedTableProperties": { | ||
"tblProperties": "'external.location'='s3://mybucket/mytable','this.is.my.key'=12,'this.is.my.key2'=true" | ||
} | ||
} | ||
} | ||
], | ||
"rollback": [ | ||
{ | ||
"alterTableProperties": { | ||
"tableName": "test_alter_table_properties", | ||
"unsetExtendedTableProperties": { | ||
"tblProperties": "'external.location', 'this.is.my.key','this.is.my.key2'" | ||
} | ||
} | ||
} | ||
] | ||
} | ||
} | ||
] | ||
} |
33 changes: 33 additions & 0 deletions
33
src/test/resources/liquibase/harness/change/changelogs/databricks/alterTableProperties.xml
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,33 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<databaseChangeLog | ||
xmlns="http://www.liquibase.org/xml/ns/dbchangelog" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xmlns:databricks="http://www.liquibase.org/xml/ns/databricks" | ||
|
||
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog | ||
http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-latest.xsd | ||
http://www.liquibase.org/xml/ns/databricks | ||
http://www.liquibase.org/xml/ns/databricks/liquibase-databricks-latest.xsd"> | ||
|
||
<changeSet id="1" author="as"> | ||
<createTable tableName="test_alter_table_properties"> | ||
<column name="test_id" type="int"> | ||
<constraints primaryKey="true" nullable="false"/> | ||
</column> | ||
<column name="test_column" type="varchar(50)"> | ||
<constraints nullable="false"/> | ||
</column> | ||
</createTable> | ||
</changeSet> | ||
|
||
<changeSet id="2" author="as"> | ||
<databricks:alterTableProperties tableName="test_alter_table_properties"> | ||
<databricks:setExtendedTableProperties tblProperties="'external.location'='s3://mybucket/mytable','this.is.my.key'=12,'this.is.my.key2'=true"/> | ||
</databricks:alterTableProperties> | ||
<rollback> | ||
<databricks:alterTableProperties tableName="test_alter_table_properties"> | ||
<databricks:unsetExtendedTableProperties tblProperties="'external.location', 'this.is.my.key','this.is.my.key2'"/> | ||
</databricks:alterTableProperties> | ||
</rollback> | ||
</changeSet> | ||
</databaseChangeLog> |
Oops, something went wrong.