generated from liquibase/liquibase-extension-example
-
Notifications
You must be signed in to change notification settings - Fork 7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[DAT-18260] Add tblProperties to createView #184
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
b1387c3
feat: add tblProperties support to Databricks createView
filipelautert 662ddd6
chore: create SQL in the expected order
filipelautert 79d7a9a
chore: create SQL in the expected order
filipelautert 55d10ba
chore: improve tests/add harness
filipelautert 60d0622
chore: adding XSD changes
filipelautert File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
36 changes: 36 additions & 0 deletions
36
src/main/java/liquibase/ext/databricks/change/createView/CreateViewChangeDatabricks.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,36 @@ | ||
package liquibase.ext.databricks.change.createView; | ||
|
||
import liquibase.change.DatabaseChange; | ||
import liquibase.change.DatabaseChangeProperty; | ||
import liquibase.change.core.CreateViewChange; | ||
import liquibase.database.Database; | ||
import liquibase.ext.databricks.database.DatabricksDatabase; | ||
import liquibase.servicelocator.PrioritizedService; | ||
import liquibase.statement.core.CreateViewStatement; | ||
import lombok.Setter; | ||
|
||
|
||
@DatabaseChange(name = "createView", description = "Create View", priority = PrioritizedService.PRIORITY_DATABASE) | ||
@Setter | ||
public class CreateViewChangeDatabricks extends CreateViewChange { | ||
private String tblProperties; | ||
|
||
@Override | ||
public boolean supports(Database database) { | ||
return database instanceof DatabricksDatabase; | ||
} | ||
|
||
@Override | ||
protected CreateViewStatement createViewStatement(String catalogName, String schemaName, String viewName, String selectQuery, boolean replaceIfExists) { | ||
CreateViewStatementDatabricks cvsd = new CreateViewStatementDatabricks(catalogName, schemaName, viewName, selectQuery, replaceIfExists); | ||
cvsd.setTblProperties(this.getTblProperties()); | ||
return cvsd; | ||
} | ||
|
||
|
||
@DatabaseChangeProperty | ||
public String getTblProperties() { | ||
return tblProperties; | ||
} | ||
|
||
} |
16 changes: 16 additions & 0 deletions
16
src/main/java/liquibase/ext/databricks/change/createView/CreateViewStatementDatabricks.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,16 @@ | ||
package liquibase.ext.databricks.change.createView; | ||
|
||
import liquibase.statement.core.CreateViewStatement; | ||
import lombok.Getter; | ||
import lombok.Setter; | ||
|
||
@Getter | ||
@Setter | ||
public class CreateViewStatementDatabricks extends CreateViewStatement { | ||
|
||
private String tblProperties; | ||
|
||
public CreateViewStatementDatabricks(String catalogName, String schemaName, String viewName, String selectQuery, boolean replaceIfExists) { | ||
super(catalogName, schemaName, viewName, selectQuery, replaceIfExists); | ||
} | ||
} |
64 changes: 64 additions & 0 deletions
64
src/main/java/liquibase/ext/databricks/sqlgenerator/CreateViewGeneratorDatabricks.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,64 @@ | ||
package liquibase.ext.databricks.sqlgenerator; | ||
|
||
import liquibase.Scope; | ||
import liquibase.database.Database; | ||
import liquibase.ext.databricks.change.createView.CreateViewStatementDatabricks; | ||
import liquibase.ext.databricks.database.DatabricksDatabase; | ||
import liquibase.parser.LiquibaseSqlParser; | ||
import liquibase.parser.SqlParserFactory; | ||
import liquibase.sql.Sql; | ||
import liquibase.sql.UnparsedSql; | ||
import liquibase.sqlgenerator.SqlGeneratorChain; | ||
import liquibase.sqlgenerator.core.CreateViewGenerator; | ||
import liquibase.statement.core.CreateViewStatement; | ||
import org.apache.commons.lang3.StringUtils; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class CreateViewGeneratorDatabricks extends CreateViewGenerator { | ||
|
||
@Override | ||
public int getPriority() { | ||
return PRIORITY_DATABASE; | ||
} | ||
|
||
@Override | ||
public boolean supports(CreateViewStatement statement, Database database) { | ||
return super.supports(statement, database) && (database instanceof DatabricksDatabase); | ||
} | ||
|
||
@Override | ||
public Sql[] generateSql(CreateViewStatement statement, Database database, SqlGeneratorChain sqlGeneratorChain) { | ||
List<Sql> sql = new ArrayList<>(); | ||
|
||
SqlParserFactory sqlParserFactory = Scope.getCurrentScope().getSingleton(SqlParserFactory.class); | ||
LiquibaseSqlParser sqlParser = sqlParserFactory.getSqlParser(); | ||
String viewDefinition = sqlParser.parse(statement.getSelectQuery(), true, true).toString(); | ||
|
||
if (!statement.isFullDefinition()) { | ||
viewDefinition = "CREATE VIEW " + | ||
database.escapeViewName(statement.getCatalogName(), statement.getSchemaName(), statement.getViewName()) + | ||
addTblProperties(statement) + | ||
" AS " + viewDefinition; | ||
} | ||
|
||
if (statement.isReplaceIfExists() && !statement.getSelectQuery().toUpperCase().contains("OR REPLACE")) { | ||
viewDefinition = viewDefinition.replace("CREATE", "CREATE OR REPLACE"); | ||
} | ||
|
||
sql.add(new UnparsedSql(viewDefinition, getAffectedView(statement))); | ||
return sql.toArray(EMPTY_SQL); | ||
} | ||
|
||
private String addTblProperties(CreateViewStatement statement) { | ||
if (statement instanceof CreateViewStatementDatabricks) { | ||
CreateViewStatementDatabricks thisStatement = (CreateViewStatementDatabricks) statement; | ||
|
||
if (StringUtils.isNotEmpty(thisStatement.getTblProperties()) && !statement.getSelectQuery().toUpperCase().contains("TBLPROPERTIES")) { | ||
return " TBLPROPERTIES (" + thisStatement.getTblProperties() + ")"; | ||
} | ||
} | ||
return ""; | ||
} | ||
} |
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
1 change: 1 addition & 0 deletions
1
src/main/resources/META-INF/services/liquibase.sqlgenerator.SqlGenerator
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
47 changes: 47 additions & 0 deletions
47
...est/groovy/liquibase/ext/databricks/sqlgenerator/CreateViewGeneratorDatabricksTest.groovy
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,47 @@ | ||
package liquibase.ext.databricks.sqlgenerator | ||
|
||
import liquibase.ext.databricks.change.createView.CreateViewStatementDatabricks | ||
import liquibase.ext.databricks.database.DatabricksDatabase | ||
import liquibase.sqlgenerator.SqlGeneratorFactory | ||
import spock.lang.Specification | ||
|
||
class CreateViewGeneratorDatabricksTest extends Specification { | ||
|
||
def "creates a view from a sql"() { | ||
when: | ||
def selectQuery = "SELECT SYSDATE FROM DUAL" | ||
def statement = new CreateViewStatementDatabricks("PUBLIC", "schema", "my_view", selectQuery, false) | ||
def generators = SqlGeneratorFactory.instance.getGenerators(statement, new DatabricksDatabase()) | ||
|
||
then: | ||
generators.size() > 0 | ||
generators[0] instanceof CreateViewGeneratorDatabricks | ||
|
||
when: | ||
def sql = SqlGeneratorFactory.instance.generateSql(statement, new DatabricksDatabase()) | ||
|
||
then: | ||
sql.length == 1 | ||
sql[0].toString() == "CREATE VIEW PUBLIC.schema.my_view AS " + selectQuery + ";" | ||
} | ||
|
||
def "creates a view with tblProperties"() { | ||
when: | ||
def selectQuery = "SELECT * FROM mytable" | ||
def tblProperties = "'external.location'='s3://mybucket/mytable','this.is.my.key'=12,'this.is.my.key2'=true" | ||
def statement = new CreateViewStatementDatabricks("main", "schema", "my_view", selectQuery, false) | ||
statement.tblProperties = tblProperties | ||
def sqla = SqlGeneratorFactory.instance.generateSql(statement, new DatabricksDatabase()) | ||
|
||
then: | ||
sqla.length == 1 | ||
|
||
when: | ||
def sql = sqla[0].toString() | ||
|
||
then: | ||
sql == "CREATE VIEW main.schema.my_view TBLPROPERTIES (" + tblProperties + ") AS " + selectQuery + ";" | ||
} | ||
|
||
} | ||
|
17 changes: 17 additions & 0 deletions
17
src/test/resources/liquibase/harness/change/changelogs/databricks/createView.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,17 @@ | ||
<?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="2" author="fl"> | ||
<databricks:createView viewName="test_view" tblProperties="'external.location'='s3://mybucket/myview','this.is.my.key'=12,'this.is.my.key2'=true" > | ||
select id, first_name, last_name, email from authors | ||
</databricks:createView> | ||
</changeSet> | ||
</databaseChangeLog> |
2 changes: 1 addition & 1 deletion
2
src/test/resources/liquibase/harness/change/expectedSql/databricks/createView.sql
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 +1 @@ | ||
CREATE VIEW main.liquibase_harness_test_ds.test_view AS select id, first_name, last_name, email from authors | ||
CREATE VIEW main.liquibase_harness_test_ds.test_view TBLPROPERTIES ('external.location'='s3://mybucket/myview','this.is.my.key'=12,'this.is.my.key2'=true) AS select id, first_name, last_name, email from authors |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We might check instanceof first, because boolean statements with double signs like '&&' are "fail fast" and if DB is inapropriate there won't be a call to super.
I'm not sure if my propose makes sence in readability perspective though.