Skip to content

Databricks Support #26

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

Merged
merged 13 commits into from
May 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 61 additions & 0 deletions flyway-database-databricks/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--

Copyright (C) Red Gate Software Ltd 2010-2024

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

-->
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.flywaydb</groupId>
<artifactId>flyway-community-db-support</artifactId>
<version>10.11.0</version>
</parent>

<artifactId>flyway-database-databricks</artifactId>
<name>${project.artifactId}</name>

<dependencies>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>flyway-core</artifactId>
</dependency>
<dependency>
<groupId>com.databricks</groupId>
<artifactId>databricks-jdbc</artifactId>
<version>2.6.33</version>
<optional>true</optional>
</dependency>
</dependencies>

<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
<plugins>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Copyright (C) Red Gate Software Ltd 2010-2024
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.flywaydb.community.database;

import org.flywaydb.core.api.FlywayException;
import org.flywaydb.core.extensibility.PluginMetadata;
import org.flywaydb.core.internal.util.FileUtils;

import java.io.IOException;
import java.nio.charset.StandardCharsets;

public class DatabricksDatabaseExtension implements PluginMetadata {
public String getDescription() {
return "Community-contributed Databricks database support extension " + readVersion() + " by Redgate";
}

public static String readVersion() {
try {
return FileUtils.copyToString(
DatabricksDatabaseExtension.class.getClassLoader().getResourceAsStream("org/flywaydb/community/database/databricks/version.txt"),
StandardCharsets.UTF_8);
} catch (IOException e) {
throw new FlywayException("Unable to read extension version: " + e.getMessage(), e);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package org.flywaydb.community.database.databricks;

import org.flywaydb.core.api.FlywayException;
import org.flywaydb.core.internal.database.base.Connection;
import org.flywaydb.core.internal.database.base.Schema;
import org.flywaydb.core.internal.util.StringUtils;

import java.sql.SQLException;

public class DatabricksConnection extends Connection<DatabricksDatabase> {
protected DatabricksConnection(DatabricksDatabase database, java.sql.Connection connection) {
super(database, connection);
}

@Override
protected String getCurrentSchemaNameOrSearchPath() throws SQLException {
String defaultSchema = "default";
String currentSchema = jdbcTemplate.queryForString("SELECT current_schema();");
return (currentSchema != null) ? currentSchema : defaultSchema;
}

@Override
public void doChangeCurrentSchemaOrSearchPathTo(String schema) throws SQLException {
String sql = "USE SCHEMA" + database.doQuote(schema) + ";";
jdbcTemplate.execute(sql);
}

@Override
public Schema doGetCurrentSchema() throws SQLException {
String currentSchema = getCurrentSchemaNameOrSearchPath();

if (!StringUtils.hasText(currentSchema)) {
throw new FlywayException("Unable to determine current schema as currentSchema is empty.");
}

return getSchema(currentSchema);
}

@Override
public Schema getSchema(String name) {
return new DatabricksSchema(jdbcTemplate, database, name);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
package org.flywaydb.community.database.databricks;

import org.flywaydb.core.api.configuration.Configuration;
import org.flywaydb.core.internal.database.base.Database;
import org.flywaydb.core.internal.database.base.Table;
import org.flywaydb.core.internal.jdbc.JdbcConnectionFactory;
import org.flywaydb.core.internal.jdbc.StatementInterceptor;
import org.flywaydb.core.internal.util.StringUtils;

import java.sql.Connection;
import java.sql.SQLException;

public class DatabricksDatabase extends Database<DatabricksConnection> {
public DatabricksDatabase(Configuration configuration, JdbcConnectionFactory jdbcConnectionFactory, StatementInterceptor statementInterceptor) {
super(configuration, jdbcConnectionFactory, statementInterceptor);
}

@Override
protected DatabricksConnection doGetConnection(Connection connection) {
return new DatabricksConnection(this, connection);
}

@Override
protected String doGetCurrentUser() throws SQLException {
return getMainConnection().getJdbcTemplate().queryForString("SELECT current_user() as user;");
}

@Override
public void ensureSupported(Configuration configuration) {
// Always latest Databricks version.
}

@Override
public boolean supportsDdlTransactions() {
// Databricks is non-transactional
return false;
}

@Override
public String getBooleanTrue() {
return "TRUE";
}

@Override
public String getBooleanFalse() {
return "FALSE";
}

@Override
public boolean catalogIsSchema() {
return false;
}

@Override
public boolean supportsMultiStatementTransactions() {
return false;
}

@Override
public boolean useSingleConnection() {
return true;
}

@Override
public String doQuote(String identifier) {
return getOpenQuote() + StringUtils.replaceAll(identifier, getCloseQuote(), getEscapedQuote()) + getCloseQuote();
}

@Override
protected String getOpenQuote() {
return "`";
}

@Override
protected String getCloseQuote() {
return "`";
}

@Override
public String getEscapedQuote() {
return "\\`";
}

@Override
public String getRawCreateScript(Table table, boolean baseline) {
String sql = "CREATE TABLE " + table + " (\n" +
" `installed_rank` INT NOT NULL,\n" +
" `version` STRING,\n" +
" `description` STRING NOT NULL,\n" +
" `type` STRING NOT NULL,\n" +
" `script` STRING NOT NULL,\n" +
" `checksum` INT,\n" +
" `installed_by` STRING NOT NULL,\n" +
" `installed_on` TIMESTAMP NOT NULL,\n" +
" `execution_time` INT NOT NULL,\n" +
" `success` BOOLEAN NOT NULL\n" +
");\n" +
(baseline ? getBaselineStatement(table) + ";\n" : "");
return sql;
}

@Override
public String getInsertStatement(Table table) {
// Explicitly set installed_on to CURRENT_TIMESTAMP().
return "INSERT INTO " + table
+ " (" + quote("installed_rank")
+ ", " + quote("version")
+ ", " + quote("description")
+ ", " + quote("type")
+ ", " + quote("script")
+ ", " + quote("checksum")
+ ", " + quote("installed_by")
+ ", " + quote("installed_on")
+ ", " + quote("execution_time")
+ ", " + quote("success")
+ ")"
+ " VALUES (?, ?, ?, ?, ?, ?, ?, CURRENT_TIMESTAMP(), ?, ?)";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package org.flywaydb.community.database.databricks;

import org.flywaydb.core.api.ResourceProvider;
import org.flywaydb.core.api.configuration.Configuration;
import org.flywaydb.core.internal.database.base.BaseDatabaseType;
import org.flywaydb.core.internal.database.base.CommunityDatabaseType;
import org.flywaydb.core.internal.database.base.Database;
import org.flywaydb.core.internal.jdbc.JdbcConnectionFactory;
import org.flywaydb.core.internal.jdbc.StatementInterceptor;
import org.flywaydb.core.internal.parser.Parser;
import org.flywaydb.core.internal.parser.ParsingContext;
import org.flywaydb.core.internal.util.ClassUtils;

import java.sql.Connection;
import java.sql.Types;
import java.util.Properties;

public class DatabricksDatabaseType extends BaseDatabaseType implements CommunityDatabaseType {
private static final String DATABRICKS_JDBC_DRIVER = "com.databricks.client.jdbc.Driver";
private static final String DATABRICKS_JDBC41_DRIVER = "com.databricks.client.jdbc41.Driver";

@Override
public String getName() {
return "Databricks";
}

@Override
public int getNullType() {
return Types.VARCHAR;
}

@Override
public boolean handlesJDBCUrl(String url) {
return url.startsWith("jdbc:databricks:");
}

@Override
public String getDriverClass(String url, ClassLoader classLoader) {
return "com.databricks.client.jdbc42.Driver";
}

@Override
public String getBackupDriverClass(String url, ClassLoader classLoader) {
if (ClassUtils.isPresent(DATABRICKS_JDBC41_DRIVER, classLoader)) {
return DATABRICKS_JDBC41_DRIVER;
}
return DATABRICKS_JDBC_DRIVER;
}

@Override
public boolean handlesDatabaseProductNameAndVersion(String databaseProductName, String databaseProductVersion, Connection connection) {
return databaseProductName.startsWith("SparkSQL");
}

@Override
public Database createDatabase(Configuration configuration, JdbcConnectionFactory jdbcConnectionFactory, StatementInterceptor statementInterceptor) {
return new DatabricksDatabase(configuration, jdbcConnectionFactory, statementInterceptor);
}

@Override
public Parser createParser(Configuration configuration, ResourceProvider resourceProvider, ParsingContext parsingContext) {
return new DatabricksParser(configuration, parsingContext);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package org.flywaydb.community.database.databricks;

import org.flywaydb.core.api.configuration.Configuration;
import org.flywaydb.core.internal.parser.Parser;
import org.flywaydb.core.internal.parser.ParsingContext;

public class DatabricksParser extends Parser {
protected DatabricksParser(Configuration configuration, ParsingContext parsingContext) {
super(configuration, parsingContext, 3);
}
}
Loading
Loading