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

YDB support #36

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
38 changes: 38 additions & 0 deletions flyway-database-ydb/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?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-ydb</artifactId>
<description>Support Flyway YDB Dialect</description>

<dependencies>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>flyway-core</artifactId>
</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,27 @@
package org.flywaydb.community.database.ydb;

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

/**
* @author Kirill Kurdyukov
*/
public class YdbConnection extends Connection<YdbDatabase> {

private static final String YDB_SCHEMA_NAME = "";

protected YdbConnection(YdbDatabase database, java.sql.Connection connection) {
super(database, connection);

this.jdbcTemplate = new YdbJdbcTemplate(connection, database.getDatabaseType());
}

@Override
protected String getCurrentSchemaNameOrSearchPath() {
return YDB_SCHEMA_NAME; // schema isn't supported
}

@Override
public YdbSchema getSchema(String name) {
return new YdbSchema(jdbcTemplate, database, YDB_SCHEMA_NAME);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
package org.flywaydb.community.database.ydb;

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

/**
* @author Kirill Kurdyukov
*/
public class YdbDatabase extends Database<YdbConnection> {

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

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

@Override
public void ensureSupported(Configuration configuration) {
}

@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 " + doQuote(table.getName()) + " (\n" +
" installed_rank INT32 NOT NULL,\n" +
" version TEXT,\n" +
" description TEXT,\n" +
" type TEXT,\n" +
" script TEXT,\n" +
" checksum INT32,\n" +
" installed_by TEXT,\n" +
" installed_on DATETIME,\n" +
" execution_time INT32,\n" +
" success BOOL,\n" +
" PRIMARY KEY (installed_rank)" +
");\n" +
(baseline ? getBaselineStatement(table) : "");
}

@Override
public String getSelectStatement(Table table) {
return "SELECT " + quote("installed_rank")
+ "," + quote("version")
+ "," + quote("description")
+ "," + quote("type")
+ "," + quote("script")
+ "," + quote("checksum")
+ "," + quote("installed_on")
+ "," + quote("installed_by")
+ "," + quote("execution_time")
+ "," + quote("success")
+ " FROM " + quote(table.getName())
+ " WHERE " + quote("installed_rank") + " > ?"
+ " ORDER BY " + quote("installed_rank");
}

@Override
public String getInsertStatement(Table table) {
return "INSERT INTO " + quote(table.getName())
+ " (" + quote("installed_rank")
+ ", " + quote("version")
+ ", " + quote("description")
+ ", " + quote("type")
+ ", " + quote("script")
+ ", " + quote("checksum")
+ ", " + quote("installed_by")
+ ", " + quote("execution_time")
+ ", " + quote("success")
+ ", " + quote("installed_on")
+ ")"
+ " VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, CurrentUtcDatetime())";
}

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

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

@Override
protected String getEscapedQuote() {
return "\\";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package org.flywaydb.community.database.ydb;

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

/**
* @author Kirill Kurdyukov
*/
public class YdbDatabaseType extends BaseDatabaseType {

private static final String YDB_NAME = "YDB";
private final static String DRIVER_NAME = "tech.ydb.jdbc.YdbDriver";

@Override
public String getName() {
return YDB_NAME;
}

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

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

@Override
public String getDriverClass(String url, ClassLoader classLoader) {
return DRIVER_NAME;
}

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

@Override
public Database<YdbConnection> createDatabase(
Configuration configuration,
JdbcConnectionFactory jdbcConnectionFactory,
StatementInterceptor statementInterceptor
) {
return new YdbDatabase(configuration, jdbcConnectionFactory, statementInterceptor);
}

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

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Types;
import org.flywaydb.core.internal.database.DatabaseType;
import static org.flywaydb.core.internal.jdbc.JdbcNullTypes.BooleanNull;
import static org.flywaydb.core.internal.jdbc.JdbcNullTypes.IntegerNull;
import static org.flywaydb.core.internal.jdbc.JdbcNullTypes.StringNull;
import org.flywaydb.core.internal.jdbc.JdbcTemplate;

/**
* @author Kirill Kurdyukov
*/
public class YdbJdbcTemplate extends JdbcTemplate {

public YdbJdbcTemplate(Connection connection, DatabaseType databaseType) {
super(connection, databaseType);
}

@Override
protected PreparedStatement prepareStatement(String sql, Object[] params) throws SQLException {
PreparedStatement statement = connection.prepareStatement(sql);
for (int i = 0; i < params.length; i++) {
if (params[i] == StringNull) {
statement.setNull(i + 1, Types.VARCHAR);
} else if (params[i] == IntegerNull) {
statement.setNull(i + 1, Types.INTEGER);
} else if (params[i] == BooleanNull) {
statement.setNull(i + 1, Types.BOOLEAN);
} else {
statement.setObject(i + 1, params[i]);
}
}

return statement;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package org.flywaydb.community.database.ydb;

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

/**
* @author Kirill Kurdyukov
*/
public class YdbParser extends Parser {

protected YdbParser(Configuration configuration, ParsingContext parsingContext) {
super(configuration, parsingContext, 3);
}
}
Loading