Skip to content

Commit

Permalink
Merge pull request #325 from liquibase/DAT-17773-epick
Browse files Browse the repository at this point in the history
[DAT-17773] Epic branch for moving functionality from liquibase core
  • Loading branch information
vitaliimak authored Jun 7, 2024
2 parents 5bd52fc + fa1dde3 commit 9513092
Show file tree
Hide file tree
Showing 77 changed files with 1,378 additions and 225 deletions.
4 changes: 4 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,10 @@
<version>1.5.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package liquibase.ext.bigquery.change;

import liquibase.change.AddColumnConfig;
import liquibase.change.ChangeMetaData;
import liquibase.change.DatabaseChange;
import liquibase.change.core.AddColumnChange;
import liquibase.change.core.DropColumnChange;
import liquibase.change.core.MergeColumnChange;
import liquibase.database.Database;
import liquibase.ext.bigquery.database.BigqueryDatabase;
import liquibase.ext.bigquery.database.BigQueryDatabase;
import liquibase.servicelocator.PrioritizedService;
import liquibase.statement.SqlStatement;
import liquibase.statement.core.RawSqlStatement;
import liquibase.structure.core.Column;
Expand All @@ -18,12 +18,12 @@

@DatabaseChange(name="mergeColumns",
description = "Concatenates the values in two columns, joins them by with string, and stores the resulting value in a new column.",
priority = ChangeMetaData.PRIORITY_DATABASE)
priority = PrioritizedService.PRIORITY_DATABASE)
public class BigQueryMergeColumnChange extends MergeColumnChange {

@Override
public boolean supports(Database database) {
return database instanceof BigqueryDatabase;
return database instanceof BigQueryDatabase;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,20 @@
import java.util.Optional;
import java.util.Properties;

import static liquibase.ext.bigquery.database.BigqueryDatabase.BIGQUERY_PRIORITY_DATABASE;
import static liquibase.ext.bigquery.database.BigQueryDatabase.BIGQUERY_PRIORITY_DATABASE;

/**
* A Bigquery specific Delegate that removes the calls to autocommit
*/

public class BigqueryConnection extends JdbcConnection {
public class BigQueryConnection extends JdbcConnection {
private static final String LOCATION = "Location";
private S42Connection con;

public BigqueryConnection() {
public BigQueryConnection() {
}

public BigqueryConnection(Connection conn) throws SQLException {
public BigQueryConnection(Connection conn) throws SQLException {
this.con = (S42Connection) conn;
String url = conn.getMetaData().getURL();
Scope.getCurrentScope().getLog(this.getClass()).fine(String.format("Setting connection to %s Location=%s", url, getUnderlyingBQConnectionLocation()));
Expand Down Expand Up @@ -85,9 +86,9 @@ public Connection getUnderlyingConnection() {

@Override
public void open(String url, Driver driverObject, Properties driverProperties) throws DatabaseException {
if (driverProperties.stringPropertyNames().contains("Location")) {
String locationValue = getUrlParamValue(url, "Location");
driverProperties.setProperty("Location", locationValue);
if (driverProperties.stringPropertyNames().contains(LOCATION)) {
String locationValue = getUrlParamValue(url, LOCATION);
driverProperties.setProperty(LOCATION, locationValue);
}

Scope.getCurrentScope().getLog(this.getClass()).fine(String.format("Opening connection to %s driverProperties=%s", url, driverProperties));
Expand Down Expand Up @@ -124,6 +125,7 @@ public boolean getAutoCommit() throws DatabaseException {

@Override
public void setAutoCommit(boolean autoCommit) throws DatabaseException {
// not supported by BigQuery
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,22 @@
import liquibase.structure.DatabaseObject;
import liquibase.structure.core.Catalog;
import liquibase.structure.core.Schema;
import liquibase.structure.core.Sequence;
import liquibase.structure.core.Table;

import java.util.HashSet;
import java.util.Set;
import java.util.regex.Pattern;

public class BigqueryDatabase extends AbstractJdbcDatabase {
public class BigQueryDatabase extends AbstractJdbcDatabase {

public static final String PRODUCT_NAME = BQDriver.DATABASE_NAME;
public static final int BIGQUERY_PRIORITY_DATABASE = 510;
private String liquibaseSchemaName;

private static final Pattern CREATE_VIEW_AS_PATTERN = Pattern.compile("^CREATE\\s+.*?VIEW\\s+.*?AS\\s+", 34);

public BigqueryDatabase() {
public BigQueryDatabase() {
this.setCurrentDateTimeFunction("CURRENT_DATETIME()");
this.unquotedObjectsAreUppercased = false;
this.addReservedWords(getDefaultReservedWords());
Expand All @@ -53,6 +54,7 @@ public int getPriority() {
return BIGQUERY_PRIORITY_DATABASE;
}

@Override
public boolean supportsDatabaseChangeLogHistory() {
return true;
}
Expand Down Expand Up @@ -93,6 +95,14 @@ public int getDatabaseMinorVersion() {
return BQDriver.DRIVER_MINOR_VERSION;
}

@Override
public boolean supports(Class<? extends DatabaseObject> object) {
if (Sequence.class.isAssignableFrom(object)) {
return false;
}
return super.supports(object);
}

@Override
public boolean supportsInitiallyDeferrableColumns() {
return false;
Expand Down Expand Up @@ -141,7 +151,7 @@ public String getDefaultDriver(String url) {

@Override
public boolean supportsSequences() {
return false;
return this.supports(Sequence.class);
}

@Override
Expand All @@ -164,7 +174,7 @@ private String getDefaultDataset() {
if (connection == null) {
return null;
}
return BigqueryConnection.getUrlParamValue(connection.getURL(), "DefaultDataset");
return BigQueryConnection.getUrlParamValue(connection.getURL(), "DefaultDataset");
}

@Override
Expand Down Expand Up @@ -222,7 +232,7 @@ public String getJdbcCatalogName(final CatalogAndSchema schema) {
@Override
public String getViewDefinition(CatalogAndSchema schema, String viewName) throws DatabaseException {
schema = schema.customize(this);
String definition = (String) ((ExecutorService) Scope.getCurrentScope().getSingleton(ExecutorService.class))
String definition = (Scope.getCurrentScope().getSingleton(ExecutorService.class))
.getExecutor("jdbc", this)
.queryForObject(new GetViewDefinitionStatement(schema.getCatalogName(), schema.getSchemaName(), viewName), String.class);
Scope.getCurrentScope().getLog(this.getClass()).info("getViewDefinition "+definition);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,34 +5,41 @@
import liquibase.datatype.DataTypeInfo;
import liquibase.datatype.DatabaseDataType;
import liquibase.datatype.LiquibaseDataType;
import liquibase.ext.bigquery.database.BigqueryDatabase;
import liquibase.ext.bigquery.database.BigQueryDatabase;
import liquibase.servicelocator.PrioritizedService;


@DataTypeInfo(
name = "bignumeric",
minParameters = 0,
maxParameters = 0,
priority = 1
priority = PrioritizedService.PRIORITY_DATABASE
)
public class BignumericDataTypeBigQuery extends LiquibaseDataType {

private final static String BIGNUMERIC = "BIGNUMERIC";
private static final String BIGNUMERIC = "BIGNUMERIC";

public BignumericDataTypeBigQuery() {
}

@Override
public boolean supports(Database database) {
return database instanceof BigqueryDatabase;
return database instanceof BigQueryDatabase;
}

@Override
public DatabaseDataType toDatabaseDataType(Database database) {
if (database instanceof BigqueryDatabase) {
if (database instanceof BigQueryDatabase) {

DatabaseDataType type = new DatabaseDataType(BIGNUMERIC, this.getParameters());
if (this.getParameters().length > 0) {
String firstParameter = String.valueOf(this.getParameters()[0]);
int typePrecision = Integer.parseInt(firstParameter);
if (typePrecision == 77) {
try {
int typePrecision = Integer.parseInt(firstParameter);
if (typePrecision == 77) {
type.setType(BIGNUMERIC);
}
} catch (NumberFormatException e) {
type.setType(BIGNUMERIC);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,28 @@
import liquibase.datatype.DataTypeInfo;
import liquibase.datatype.DatabaseDataType;
import liquibase.datatype.LiquibaseDataType;
import liquibase.ext.bigquery.database.BigqueryDatabase;

import static liquibase.ext.bigquery.database.BigqueryDatabase.BIGQUERY_PRIORITY_DATABASE;
import liquibase.ext.bigquery.database.BigQueryDatabase;
import liquibase.servicelocator.PrioritizedService;


@DataTypeInfo(
name = "boolean",
minParameters = 0,
maxParameters = 0,
priority = BIGQUERY_PRIORITY_DATABASE
priority = PrioritizedService.PRIORITY_DATABASE
)
public class BoolDataTypeBigQuery extends LiquibaseDataType {
public BoolDataTypeBigQuery() {
}

@Override
public boolean supports(Database database) {
return database instanceof BigqueryDatabase;
return database instanceof BigQueryDatabase;
}

@Override
public DatabaseDataType toDatabaseDataType(Database database) {
if (database instanceof BigqueryDatabase) {
if (database instanceof BigQueryDatabase) {

DatabaseDataType type = new DatabaseDataType("BOOL", this.getParameters());
type.setType("BOOL");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,28 @@
import liquibase.datatype.DataTypeInfo;
import liquibase.datatype.DatabaseDataType;
import liquibase.datatype.LiquibaseDataType;
import liquibase.ext.bigquery.database.BigqueryDatabase;
import liquibase.ext.bigquery.database.BigQueryDatabase;
import liquibase.servicelocator.PrioritizedService;

import static liquibase.ext.bigquery.database.BigqueryDatabase.BIGQUERY_PRIORITY_DATABASE;

@DataTypeInfo(
name = "float64",
minParameters = 0,
maxParameters = 0,
priority = BIGQUERY_PRIORITY_DATABASE
priority = PrioritizedService.PRIORITY_DATABASE
)
public class Float64DataTypeBigQuery extends LiquibaseDataType {
public Float64DataTypeBigQuery() {
}

@Override
public boolean supports(Database database) {
return database instanceof BigqueryDatabase;
return database instanceof BigQueryDatabase;
}

@Override
public DatabaseDataType toDatabaseDataType(Database database) {
if (database instanceof BigqueryDatabase) {
if (database instanceof BigQueryDatabase) {

DatabaseDataType type = new DatabaseDataType("FLOAT64", this.getParameters());
type.setType("FLOAT64");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,28 @@
import liquibase.datatype.DataTypeInfo;
import liquibase.datatype.DatabaseDataType;
import liquibase.datatype.LiquibaseDataType;
import liquibase.ext.bigquery.database.BigqueryDatabase;

import static liquibase.ext.bigquery.database.BigqueryDatabase.BIGQUERY_PRIORITY_DATABASE;
import liquibase.ext.bigquery.database.BigQueryDatabase;
import liquibase.servicelocator.PrioritizedService;


@DataTypeInfo(
name = "geography",
minParameters = 0,
maxParameters = 0,
priority = BIGQUERY_PRIORITY_DATABASE
priority = PrioritizedService.PRIORITY_DATABASE
)
public class GeographyDataTypeBigQuery extends LiquibaseDataType {
public GeographyDataTypeBigQuery() {
}

@Override
public boolean supports(Database database) {
return database instanceof BigqueryDatabase;
return database instanceof BigQueryDatabase;
}

@Override
public DatabaseDataType toDatabaseDataType(Database database) {
if (database instanceof BigqueryDatabase) {
if (database instanceof BigQueryDatabase) {

DatabaseDataType type = new DatabaseDataType("GEOGRAPHY", this.getParameters());
type.setType("GEOGRAPHY");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,28 @@
import liquibase.datatype.DataTypeInfo;
import liquibase.datatype.DatabaseDataType;
import liquibase.datatype.LiquibaseDataType;
import liquibase.ext.bigquery.database.BigqueryDatabase;

import static liquibase.ext.bigquery.database.BigqueryDatabase.BIGQUERY_PRIORITY_DATABASE;
import liquibase.ext.bigquery.database.BigQueryDatabase;
import liquibase.servicelocator.PrioritizedService;


@DataTypeInfo(
name = "int64",
minParameters = 0,
maxParameters = 0,
priority = 1
priority = PrioritizedService.PRIORITY_DATABASE
)
public class Int64DataTypeBigQuery extends LiquibaseDataType {
public Int64DataTypeBigQuery() {
}

@Override
public boolean supports(Database database) {
return database instanceof BigqueryDatabase;
return database instanceof BigQueryDatabase;
}

@Override
public DatabaseDataType toDatabaseDataType(Database database) {
if (database instanceof BigqueryDatabase) {
if (database instanceof BigQueryDatabase) {

DatabaseDataType type = new DatabaseDataType("INT64", this.getParameters());
type.setType("INT64");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,28 @@
import liquibase.datatype.DataTypeInfo;
import liquibase.datatype.DatabaseDataType;
import liquibase.datatype.LiquibaseDataType;
import liquibase.ext.bigquery.database.BigqueryDatabase;
import liquibase.ext.bigquery.database.BigQueryDatabase;
import liquibase.servicelocator.PrioritizedService;


@DataTypeInfo(
name = "numeric",
minParameters = 0,
maxParameters = 0,
priority = 1
priority = PrioritizedService.PRIORITY_DATABASE
)
public class NumberDataTypeBigQuery extends LiquibaseDataType {
public NumberDataTypeBigQuery() {
public class NumericDataTypeBigQuery extends LiquibaseDataType {
public NumericDataTypeBigQuery() {
}

@Override
public boolean supports(Database database) {
return database instanceof BigqueryDatabase;
return database instanceof BigQueryDatabase;
}

@Override
public DatabaseDataType toDatabaseDataType(Database database) {
if (database instanceof BigqueryDatabase) {
if (database instanceof BigQueryDatabase) {

return new DatabaseDataType("NUMERIC", this.getParameters());
} else {
Expand Down
Loading

0 comments on commit 9513092

Please sign in to comment.