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-18263: refactored alter properties change and statements for bett…
…er code reuse.
- Loading branch information
Mykhailo Savchenko
committed
Sep 27, 2024
1 parent
a63bfca
commit 96396f3
Showing
6 changed files
with
57 additions
and
138 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
23 changes: 23 additions & 0 deletions
23
...main/java/liquibase/ext/databricks/change/AbstractAlterPropertiesStatementDatabricks.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; | ||
|
||
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; | ||
} | ||
|
||
} |
62 changes: 9 additions & 53 deletions
62
...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 |
---|---|---|
@@ -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; | ||
} | ||
} |
10 changes: 3 additions & 7 deletions
10
...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 |
---|---|---|
@@ -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; | ||
} | ||
|
||
} |
63 changes: 8 additions & 55 deletions
63
...uibase/ext/databricks/change/alterViewProperties/AlterViewPropertiesChangeDatabricks.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 |
---|---|---|
@@ -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; | ||
} | ||
} |
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