Skip to content

Commit

Permalink
Merge pull request #161 from liquibase/DAT-17950
Browse files Browse the repository at this point in the history
[DAT-17950] Add Alter Table Properties change
  • Loading branch information
vitaliimak authored Aug 1, 2024
2 parents e4b37ba + 6e3e901 commit 6888ba1
Show file tree
Hide file tree
Showing 15 changed files with 381 additions and 2 deletions.
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,12 @@
<version>2.6.38</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.32</version>
<scope>provided</scope>
</dependency>
</dependencies>

<build>
Expand Down
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;
}
}
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;
}

}
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";
}
}
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";
}
}
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())
};
}
}
3 changes: 2 additions & 1 deletion src/main/resources/META-INF/services/liquibase.change.Change
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ liquibase.ext.databricks.change.analyzeTable.AnalyzeTableChange
liquibase.ext.databricks.change.vacuumTable.VacuumTableChange
liquibase.ext.databricks.change.addLookupTable.AddLookupTableChangeDatabricks
liquibase.ext.databricks.change.addCheckConstraint.AddCheckConstraintChangeDatabricks
liquibase.ext.databricks.change.dropCheckConstraint.DropCheckConstraintChangeDatabricks
liquibase.ext.databricks.change.dropCheckConstraint.DropCheckConstraintChangeDatabricks
liquibase.ext.databricks.change.alterTableProperties.AlterTablePropertiesChangeDatabricks
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@ liquibase.ext.databricks.sqlgenerator.AddUniqueConstraintGeneratorDatabricks
liquibase.ext.databricks.sqlgenerator.InsertOrUpdateGeneratorDatabricks
liquibase.ext.databricks.sqlgenerator.UpdateGeneratorDatabricks
liquibase.ext.databricks.change.addCheckConstraint.AddCheckConstraintGeneratorDatabricks
liquibase.ext.databricks.change.dropCheckConstraint.DropCheckConstraintGeneratorDatabricks
liquibase.ext.databricks.change.dropCheckConstraint.DropCheckConstraintGeneratorDatabricks
liquibase.ext.databricks.sqlgenerator.AlterTablePropertiesGeneratorDatabricks
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,30 @@
<xsd:attribute name="tblProperties" type="xsd:string"/>
</xsd:complexType>
</xsd:element>

<xsd:element name="alterTableProperties">
<xsd:complexType>
<xsd:choice maxOccurs="1" minOccurs="1">
<xsd:sequence >
<xsd:element ref="setExtendedTableProperties" maxOccurs="1" minOccurs="1"/>
</xsd:sequence>
<xsd:sequence >
<xsd:element ref="unsetExtendedTableProperties" maxOccurs="1" minOccurs="1"/>
</xsd:sequence>
</xsd:choice>
<xsd:attribute name="tableName" type="xsd:string" use="required"/>
</xsd:complexType>
</xsd:element>

<xsd:element name="setExtendedTableProperties">
<xsd:complexType>
<xsd:attribute name="tblProperties" type="xsd:string"/>
</xsd:complexType>
</xsd:element>

<xsd:element name="unsetExtendedTableProperties">
<xsd:complexType>
<xsd:attribute name="tblProperties" type="xsd:string"/>
</xsd:complexType>
</xsd:element>
</xsd:schema>
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,30 @@
<xsd:attribute name="tblProperties" type="xsd:string"/>
</xsd:complexType>
</xsd:element>

<xsd:element name="alterTableProperties">
<xsd:complexType>
<xsd:choice maxOccurs="1" minOccurs="1">
<xsd:sequence >
<xsd:element ref="setExtendedTableProperties" maxOccurs="1" minOccurs="1"/>
</xsd:sequence>
<xsd:sequence >
<xsd:element ref="unsetExtendedTableProperties" maxOccurs="1" minOccurs="1"/>
</xsd:sequence>
</xsd:choice>
<xsd:attribute name="tableName" type="xsd:string" use="required"/>
</xsd:complexType>
</xsd:element>

<xsd:element name="setExtendedTableProperties">
<xsd:complexType>
<xsd:attribute name="tblProperties" type="xsd:string"/>
</xsd:complexType>
</xsd:element>

<xsd:element name="unsetExtendedTableProperties">
<xsd:complexType>
<xsd:attribute name="tblProperties" type="xsd:string"/>
</xsd:complexType>
</xsd:element>
</xsd:schema>
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'"
}
}
}
]
}
}
]
}
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>
Loading

0 comments on commit 6888ba1

Please sign in to comment.