Skip to content

Commit

Permalink
Update TimeplusSchema to delete UDF and SCHEMA FORMAT on `flyway clea…
Browse files Browse the repository at this point in the history
…nup`
  • Loading branch information
jovezhong committed Dec 19, 2024
1 parent 3e447ad commit 2b759a5
Showing 1 changed file with 28 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
* 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.
Expand Down Expand Up @@ -39,6 +39,7 @@
import org.flywaydb.core.internal.util.StringUtils;

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

public class TimeplusSchema extends Schema<TimeplusDatabase, TimeplusTable> {
Expand All @@ -47,8 +48,8 @@ public class TimeplusSchema extends Schema<TimeplusDatabase, TimeplusTable> {

/**
* @param jdbcTemplate The Jdbc Template for communicating with the DB.
* @param database The database-specific support.
* @param name The name of the schema.
* @param database The database-specific support.
* @param name The name of the schema.
*/
public TimeplusSchema(JdbcTemplate jdbcTemplate, TimeplusDatabase database, String name) {
super(jdbcTemplate, database, name);
Expand All @@ -64,16 +65,17 @@ protected boolean doExists() throws SQLException {
@Override
protected boolean doEmpty() throws SQLException {
TimeplusConnection systemConnection = database.getSystemConnection();
int i = systemConnection.getJdbcTemplate().queryForInt("SELECT COUNT() FROM system.tables WHERE database = ?", name);
return i == 0;
int objectCount = systemConnection.getJdbcTemplate().queryForInt("SELECT COUNT() FROM system.tables WHERE database = ?", name) + getObjectCount("FUNCTION")
+ getObjectCount("FORMAT SCHEMA");
return objectCount == 0;
}

@Override
protected void doCreate() throws SQLException {
TimeplusConnection systemConnection = database.getSystemConnection();
String clusterName = database.getClusterName();
boolean isClustered = StringUtils.hasText(clusterName);
systemConnection.getJdbcTemplate().executeStatement("CREATE DATABASE " + database.quote(name) + (false&&isClustered ? (" ON CLUSTER " + clusterName) : ""));
systemConnection.getJdbcTemplate().executeStatement("CREATE DATABASE " + database.quote(name) + (false && isClustered ? (" ON CLUSTER " + clusterName) : ""));
}

@Override
Expand All @@ -88,9 +90,17 @@ protected void doDrop() throws SQLException {

@Override
protected void doClean() throws SQLException {
// TODO: this will drop streams, views, materialized views, when there are dependencies, DROP could fail.
for (TimeplusTable table : allTables()) {
table.drop();
}
for (String dropStatement : generateDropStatements("FORMAT SCHEMA")) {
jdbcTemplate.execute(dropStatement);
}

for (String dropStatement : generateDropStatements("FUNCTION")) {
jdbcTemplate.execute(dropStatement);
}
}

@Override
Expand All @@ -106,4 +116,15 @@ protected TimeplusTable[] doAllTables() throws SQLException {
public TimeplusTable getTable(String tableName) {
return new TimeplusTable(jdbcTemplate, database, this, tableName);
}

private int getObjectCount(String objectType) throws SQLException {
return jdbcTemplate.query("SHOW " + objectType + "S", rs -> 1).size();
}

private List<String> generateDropStatements(final String objectType) throws SQLException {
return jdbcTemplate.query("SHOW " + objectType + "S", rs -> {
String resName = rs.getString("name");
return "DROP " + objectType + " " + database.quote(resName);
});
}
}

0 comments on commit 2b759a5

Please sign in to comment.