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 #70 from liquibase/update_liquibase_version_and_sn…
…apshot Upgrade to Liquibase 4.25.0
- Loading branch information
Showing
7 changed files
with
125 additions
and
35 deletions.
There are no files selected for viewing
Binary file not shown.
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
85 changes: 85 additions & 0 deletions
85
src/main/java/liquibase/ext/databricks/snapshot/jvm/ViewSnapshotGeneratorDatabricks.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,85 @@ | ||
package liquibase.ext.databricks.snapshot.jvm; | ||
|
||
import liquibase.CatalogAndSchema; | ||
import liquibase.Scope; | ||
import liquibase.database.AbstractJdbcDatabase; | ||
import liquibase.database.Database; | ||
import liquibase.exception.DatabaseException; | ||
import liquibase.executor.ExecutorService; | ||
import liquibase.snapshot.DatabaseSnapshot; | ||
import liquibase.snapshot.jvm.ViewSnapshotGenerator; | ||
import liquibase.statement.core.RawSqlStatement; | ||
import liquibase.structure.DatabaseObject; | ||
import liquibase.structure.core.Schema; | ||
import liquibase.structure.core.View; | ||
import liquibase.util.StringUtil; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import liquibase.ext.databricks.database.DatabricksDatabase; | ||
public class ViewSnapshotGeneratorDatabricks extends ViewSnapshotGenerator { | ||
|
||
|
||
@Override | ||
public int getPriority(Class<? extends DatabaseObject> objectType, Database database) { | ||
int priority = super.getPriority(objectType, database); | ||
if (priority > PRIORITY_NONE && database instanceof DatabricksDatabase) { | ||
priority += DatabricksDatabase.PRIORITY_DATABASE; | ||
} | ||
return priority; | ||
} | ||
|
||
@Override | ||
protected DatabaseObject snapshotObject(DatabaseObject example, DatabaseSnapshot snapshot) throws DatabaseException { | ||
if (((View) example).getDefinition() != null) { | ||
return example; | ||
} else { | ||
Database database = snapshot.getDatabase(); | ||
Schema schema = example.getSchema(); | ||
|
||
CatalogAndSchema catalogAndSchema = (new CatalogAndSchema(schema.getCatalogName(), schema.getName())).customize(database); | ||
String jdbcSchemaName = database.correctObjectName(((AbstractJdbcDatabase) database).getJdbcSchemaName(catalogAndSchema), Schema.class); | ||
String query = String.format("SELECT view_definition FROM %s.%s.VIEWS WHERE table_name='%s' AND table_schema='%s' AND table_catalog='%s';", | ||
schema.getCatalogName(), database.getSystemSchema(), example.getName(), schema.getName(), schema.getCatalogName()); | ||
|
||
List<Map<String, ?>> viewsMetadataRs = Scope.getCurrentScope().getSingleton(ExecutorService.class) | ||
.getExecutor("jdbc", database).queryForList(new RawSqlStatement(query)); | ||
|
||
if (viewsMetadataRs.isEmpty()) { | ||
return null; | ||
} else { | ||
Map<String, ?> row = viewsMetadataRs.get(0); | ||
String rawViewName = example.getName(); | ||
String rawSchemaName = schema.getName(); | ||
String rawCatalogName = schema.getCatalogName(); | ||
|
||
View view = (new View()).setName(this.cleanNameFromDatabase(rawViewName, database)); | ||
CatalogAndSchema schemaFromJdbcInfo = ((AbstractJdbcDatabase) database).getSchemaFromJdbcInfo(rawCatalogName, rawSchemaName); | ||
view.setSchema(new Schema(schemaFromJdbcInfo.getCatalogName(), schemaFromJdbcInfo.getSchemaName())); | ||
|
||
String definition = (String) row.get("VIEW_DEFINITION"); | ||
if (definition.startsWith("FULL_DEFINITION: ")) { | ||
definition = definition.replaceFirst("^FULL_DEFINITION: ", ""); | ||
view.setContainsFullDefinition(true); | ||
} | ||
|
||
int length = definition.length(); | ||
if (length > 0 && definition.charAt(length - 1) == 0) { | ||
definition = definition.substring(0, length - 1); | ||
} | ||
|
||
definition = StringUtil.trimToNull(definition); | ||
if (definition == null) { | ||
definition = "[CANNOT READ VIEW DEFINITION]"; | ||
} | ||
|
||
view.setDefinition(definition); | ||
|
||
return view; | ||
} | ||
|
||
} | ||
} | ||
|
||
} |
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
2 changes: 0 additions & 2 deletions
2
...esources/liquibase/harness/generateChangelog/expectedChangeLog/databricks/createTable.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,3 +1 @@ | ||
-- liquibase formatted sql | ||
|
||
CREATE TABLE test_table_xml (test_column INT); |