Skip to content
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

CrateDB Support #37

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
43 changes: 43 additions & 0 deletions flyway-database-crate/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?xml version="1.0" encoding="UTF-8"?>
<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-crate</artifactId>
<name>${project.artifactId}</name>

<dependencies>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>flyway-core</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<scope>provided</scope>
</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,24 @@
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 CrateDatabaseExtension implements PluginMetadata {
public String getDescription() {
return "Community-contributed Crate database support extension " + readVersion() + " by JaVol";
}

public static String readVersion() {
try {
return FileUtils.copyToString(
CrateDatabaseExtension.class.getClassLoader().getResourceAsStream("org/flywaydb/community/database/crate/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,22 @@
package org.flywaydb.community.database.crate;

import org.flywaydb.core.internal.database.base.Connection;
import org.flywaydb.core.internal.database.base.Schema;

import java.sql.SQLException;

public class CrateConnection extends Connection<CrateDatabase> {
public CrateConnection(CrateDatabase database, java.sql.Connection connection) {
super(database, connection);
}

@Override
protected String getCurrentSchemaNameOrSearchPath() throws SQLException {
return jdbcTemplate.queryForString("SELECT current_schema");
}

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

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 java.sql.Connection;

public class CrateDatabase extends Database<CrateConnection> {

public CrateDatabase(Configuration configuration, JdbcConnectionFactory jdbcConnectionFactory, StatementInterceptor statementInterceptor) {
super(configuration, jdbcConnectionFactory, statementInterceptor);
}

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

@Override
public void ensureSupported(Configuration configuration) {
// NOOP
}

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

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

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

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

@Override
public String getRawCreateScript(Table table, boolean baseline) {
return "CREATE TABLE " + table + """
(
installed_rank INT NOT NULL PRIMARY KEY,
version VARCHAR(50),
description VARCHAR(200) NOT NULL,
type VARCHAR(20) NOT NULL,
script VARCHAR(1000) NOT NULL,
checksum INTEGER,
installed_by varchar(100) NOT NULL,
installed_on TIMESTAMP NOT NULL DEFAULT now(),
execution_time INTEGER NOT NULL,
success BOOLEAN NOT NULL
);
""" + (baseline ? getBaselineStatement(table) + ";\n" : "");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package org.flywaydb.community.database.crate;

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 java.sql.Connection;
import java.sql.Types;

public class CrateDatabaseType extends BaseDatabaseType implements CommunityDatabaseType {
@Override
public String getName() {
return "CrateDB";
}

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

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

@Override
public String getDriverClass(String url, ClassLoader classLoader) {
return "org.postgresql.Driver";
}

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

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

@Override
public Parser createParser(Configuration configuration, ResourceProvider resourceProvider, ParsingContext parsingContext) {
return new CrateParser(configuration, parsingContext);
}

@Override
public int getPriority() {
// have a precedence over standard PostgreSQL
return 1;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package org.flywaydb.community.database.crate;

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

public class CrateParser extends Parser {
public CrateParser(Configuration configuration, ParsingContext parsingContext) {
super(configuration, parsingContext, 3);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package org.flywaydb.community.database.crate;

import org.flywaydb.core.internal.database.base.Schema;
import org.flywaydb.core.internal.database.base.Table;
import org.flywaydb.core.internal.jdbc.JdbcTemplate;

import java.sql.SQLException;
import java.util.List;

public class CrateSchema extends Schema<CrateDatabase, CrateTable> {
public CrateSchema(JdbcTemplate jdbcTemplate, CrateDatabase database, String name) {
super(jdbcTemplate, database, name);
}

@Override
protected boolean doExists() throws SQLException {
return jdbcTemplate.queryForInt("SELECT COUNT() FROM information_schema.schemata WHERE schema_name = ?",
name) > 0;
}

@Override
protected boolean doEmpty() throws SQLException {
return jdbcTemplate.queryForInt("""
SELECT COUNT() FROM (
SELECT table_name FROM information_schema.tables WHERE table_type='BASE TABLE' AND table_schema = ? UNION
SELECT table_name FROM information_schema.views WHERE table_schema = ? UNION
SELECT routine_name FROM information_schema.routines WHERE routine_schema = ?
) objs""",
name, name, name) == 0;
}

@Override
protected void doCreate() throws SQLException {
// schema cannot be explicitly created
// NOOP
}

@Override
protected void doDrop() throws SQLException {
// schema cannot be explicitly removed
// NOOP
}

@Override
protected void doClean() throws SQLException {
// drop views
List<String> dropViewStatements = jdbcTemplate.queryForStringList(
"SELECT table_name FROM information_schema.views WHERE table_schema = ?", name)
.stream().map(viewName -> "DROP VIEW IF EXISTS "+database.quote(name, viewName)).toList();
for (String stmt : dropViewStatements) {
jdbcTemplate.execute(stmt);
}

// drop tables
List<String> dropTableStatements = jdbcTemplate.queryForStringList(
"SELECT table_name FROM information_schema.tables WHERE table_type='BASE TABLE' AND table_schema = ?", name)
.stream().map(tableName -> "DROP TABLE IF EXISTS "+database.quote(name, tableName)).toList();
for (String stmt : dropTableStatements) {
jdbcTemplate.execute(stmt);
}

// drop functions
List<String> dropFunctionStatements = jdbcTemplate.queryForList(
"SELECT routine_name, specific_name FROM information_schema.routines WHERE routine_schema = ?", name)
.stream().map(result -> {
String fnName = result.get("routine_name");
String fnArgs = result.get("specific_name").substring(fnName.length());
return "DROP FUNCTION IF EXISTS " + database.quote(name, fnName) + fnArgs;
}).toList();
for (String stmt : dropFunctionStatements) {
jdbcTemplate.execute(stmt);
}
}

@Override
protected CrateTable[] doAllTables() throws SQLException {
return jdbcTemplate
.queryForStringList("SELECT table_name FROM information_schema.tables WHERE table_schema = ?",
name)
.stream()
.map( tableName -> new CrateTable(jdbcTemplate, database, this, tableName) )
.toArray(CrateTable[]::new);
}

@Override
public Table getTable(String tableName) {
return new CrateTable(jdbcTemplate, database, this, tableName);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package org.flywaydb.community.database.crate;

import org.flywaydb.core.internal.database.base.Table;
import org.flywaydb.core.internal.jdbc.JdbcTemplate;

import java.sql.SQLException;

public class CrateTable extends Table<CrateDatabase, CrateSchema> {
public CrateTable(JdbcTemplate jdbcTemplate, CrateDatabase database, CrateSchema schema, String name) {
super(jdbcTemplate, database, schema, name);
}

@Override
protected void doDrop() throws SQLException {
jdbcTemplate.execute("DROP TABLE " + this);
}

@Override
protected boolean doExists() throws SQLException {
return jdbcTemplate.queryForInt(
"SELECT COUNT() FROM information_schema.tables WHERE table_schema = ? AND table_name = ?",
schema.getName(), name) > 0;
}

@Override
protected void doLock() throws SQLException {
// NOOP - There is no support for locking in CrateDB
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
org.flywaydb.community.database.crate.CrateDatabaseType
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
${pom.version}
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
<module>flyway-database-yugabytedb</module>
<module>flyway-database-clickhouse</module>
<module>flyway-database-oceanbase</module>
<module>flyway-database-crate</module>
</modules>

<properties>
Expand Down