Skip to content

Commit

Permalink
DAT-18263: refactored alter properties change and statements for bett…
Browse files Browse the repository at this point in the history
…er code reuse.
  • Loading branch information
Mykhailo Savchenko committed Sep 27, 2024
1 parent a63bfca commit 96396f3
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 138 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import liquibase.exception.ValidationErrors;
import liquibase.ext.databricks.change.alterTableProperties.SetExtendedTableProperties;
import liquibase.ext.databricks.change.alterTableProperties.UnsetExtendedTableProperties;
import liquibase.ext.databricks.change.alterViewProperties.AlterViewPropertiesStatementDatabricks;
import liquibase.ext.databricks.database.DatabricksDatabase;
import liquibase.statement.SqlStatement;
import lombok.Setter;
Expand All @@ -15,6 +14,7 @@
import java.util.ArrayList;
import java.util.List;

import static com.databricks.client.jdbc42.internal.apache.commons.lang.StringUtils.capitalize;
import static liquibase.statement.SqlStatement.EMPTY_SQL_STATEMENT;

@Setter
Expand All @@ -24,11 +24,6 @@ public abstract class AbstractAlterPropertiesChangeDatabricks extends AbstractCh
private String schemaName;
private SetExtendedTableProperties setExtendedTableProperties;
private UnsetExtendedTableProperties unsetExtendedTableProperties;
private final String elementName;

public AbstractAlterPropertiesChangeDatabricks (String elementName) {
this.elementName = elementName;
}

@Override
public boolean supports(Database database) {
Expand All @@ -48,15 +43,19 @@ public ValidationErrors validate(Database database) {

protected abstract String getNoPropertiesErrorMessage();

@Override
public String getConfirmationMessage() {
return MessageFormat.format("{0}.{1}.{2} successfully altered.", getCatalogName(), getSchemaName(), getElementName());
protected String applySubjectToErrorPattern(String subject) {
return MessageFormat.format("Alter %s Properties change require 'setExtendedTableProperties' or 'unsetExtendedTableProperties' element, please add at least one option.", capitalize(subject));
}

@Override
public SqlStatement[] generateStatements(Database database) {
AlterViewPropertiesStatementDatabricks statement = new AlterViewPropertiesStatementDatabricks(getCatalogName(), getSchemaName(), getElementName());
public abstract String getConfirmationMessage();

protected String getConfirmationMessage(String elementName) {
return MessageFormat.format("{0}.{1}.{2} successfully altered.", getCatalogName(), getSchemaName(), elementName);
}

public abstract SqlStatement[] generateStatements(Database database);

protected SqlStatement[] generateStatements(AbstractAlterPropertiesStatementDatabricks statement) {
if (setExtendedTableProperties != null) {
statement.setSetExtendedTableProperties(setExtendedTableProperties);
} else if (unsetExtendedTableProperties != null) {
Expand All @@ -78,10 +77,6 @@ public String getSchemaName() {
return schemaName;
}

public String getElementName() {
return elementName;
}

@DatabaseChangeProperty
public SetExtendedTableProperties getSetExtendedTableProperties() {
return setExtendedTableProperties;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package liquibase.ext.databricks.change;

import liquibase.ext.databricks.change.alterTableProperties.SetExtendedTableProperties;
import liquibase.ext.databricks.change.alterTableProperties.UnsetExtendedTableProperties;
import liquibase.statement.AbstractSqlStatement;
import lombok.Getter;
import lombok.Setter;

@Setter
@Getter
public abstract class AbstractAlterPropertiesStatementDatabricks extends AbstractSqlStatement {

private String catalogName;
private String schemaName;
private SetExtendedTableProperties setExtendedTableProperties;
private UnsetExtendedTableProperties unsetExtendedTableProperties;

public AbstractAlterPropertiesStatementDatabricks(String catalogName, String schemaName) {
this.catalogName = catalogName;
this.schemaName = schemaName;
}

}
Original file line number Diff line number Diff line change
@@ -1,89 +1,45 @@
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.change.AbstractAlterPropertiesChangeDatabricks;
import liquibase.ext.databricks.change.AbstractAlterPropertiesStatementDatabricks;
import liquibase.ext.databricks.change.alterViewProperties.AlterViewPropertiesStatementDatabricks;
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 {
public class AlterTablePropertiesChangeDatabricks extends AbstractAlterPropertiesChangeDatabricks {

private final static String CHANGE_TYPE_SUBJECT = "Table";
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;
protected String getNoPropertiesErrorMessage() {
return applySubjectToErrorPattern(CHANGE_TYPE_SUBJECT);
}

@Override
public String getConfirmationMessage() {
return MessageFormat.format("{0}.{1}.{2} successfully altered.", getCatalogName(), getSchemaName(), getTableName());
return getConfirmationMessage(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;
return generateStatements(new AlterTablePropertiesStatementDatabricks(getCatalogName(), getSchemaName(), getTableName()));
}

@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
@@ -1,23 +1,19 @@
package liquibase.ext.databricks.change.alterTableProperties;

import liquibase.ext.databricks.change.AbstractAlterPropertiesStatementDatabricks;
import liquibase.statement.AbstractSqlStatement;
import lombok.Getter;
import lombok.Setter;

@Setter
@Getter
public class AlterTablePropertiesStatementDatabricks extends AbstractSqlStatement {
public class AlterTablePropertiesStatementDatabricks extends AbstractAlterPropertiesStatementDatabricks {

private String tableName;
private String catalogName;
private String schemaName;
private SetExtendedTableProperties setExtendedTableProperties;
private UnsetExtendedTableProperties unsetExtendedTableProperties;

public AlterTablePropertiesStatementDatabricks(String catalogName, String schemaName, String tableName) {
super(catalogName, schemaName);
this.tableName = tableName;
this.catalogName = catalogName;
this.schemaName = schemaName;
}

}
Original file line number Diff line number Diff line change
@@ -1,91 +1,44 @@
package liquibase.ext.databricks.change.alterViewProperties;

import liquibase.change.AbstractChange;
import liquibase.change.DatabaseChange;
import liquibase.change.DatabaseChangeProperty;
import liquibase.database.Database;
import liquibase.exception.ValidationErrors;
import liquibase.ext.databricks.change.alterTableProperties.SetExtendedTableProperties;
import liquibase.ext.databricks.change.alterTableProperties.UnsetExtendedTableProperties;
import liquibase.ext.databricks.change.AbstractAlterPropertiesChangeDatabricks;
import liquibase.ext.databricks.change.AbstractAlterPropertiesStatementDatabricks;
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 = "alterViewProperties", description = "Alter View Properties", priority = PrioritizedService.PRIORITY_DATABASE + 500)
public class AlterViewPropertiesChangeDatabricks extends AbstractChange {
public class AlterViewPropertiesChangeDatabricks extends AbstractAlterPropertiesChangeDatabricks {

private final static String CHANGE_TYPE_SUBJECT = "View";
private String viewName;
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 View Properties change require 'setExtendedTableProperties' or 'unsetExtendedTableProperties' element, please add at least one option.");
}
return validationErrors;
protected String getNoPropertiesErrorMessage() {
return applySubjectToErrorPattern(CHANGE_TYPE_SUBJECT);
}

@Override
public String getConfirmationMessage() {
return MessageFormat.format("{0}.{1}.{2} successfully altered.", getCatalogName(), getSchemaName(), getViewName());
return getConfirmationMessage(getViewName());
}

@Override
public SqlStatement[] generateStatements(Database database) {
AlterViewPropertiesStatementDatabricks statement = new AlterViewPropertiesStatementDatabricks(getCatalogName(), getSchemaName(), getViewName());

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;
return generateStatements(new AlterViewPropertiesStatementDatabricks(getCatalogName(), getSchemaName(), getViewName()));
}

@DatabaseChangeProperty
public String getViewName() {
return viewName;
}

@DatabaseChangeProperty
public SetExtendedTableProperties getSetExtendedTableProperties() {
return setExtendedTableProperties;
}

@DatabaseChangeProperty
public UnsetExtendedTableProperties getUnsetExtendedTableProperties() {
return unsetExtendedTableProperties;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package liquibase.ext.databricks.change.alterViewProperties;

import liquibase.ext.databricks.change.AbstractAlterPropertiesStatementDatabricks;
import liquibase.ext.databricks.change.alterTableProperties.SetExtendedTableProperties;
import liquibase.ext.databricks.change.alterTableProperties.UnsetExtendedTableProperties;
import liquibase.statement.AbstractSqlStatement;
Expand All @@ -8,18 +9,13 @@

@Setter
@Getter
public class AlterViewPropertiesStatementDatabricks extends AbstractSqlStatement {
public class AlterViewPropertiesStatementDatabricks extends AbstractAlterPropertiesStatementDatabricks {

private String viewName;
private String catalogName;
private String schemaName;
private SetExtendedTableProperties setExtendedTableProperties;
private UnsetExtendedTableProperties unsetExtendedTableProperties;

public AlterViewPropertiesStatementDatabricks(String catalogName, String schemaName, String viewName) {
super(catalogName, schemaName);
this.viewName = viewName;
this.catalogName = catalogName;
this.schemaName = schemaName;
}

}

0 comments on commit 96396f3

Please sign in to comment.