Skip to content

Commit a0fe57f

Browse files
authored
Merge pull request #39 from flyway/improve-databricks
Improve databricks
2 parents 35ebd48 + 76072c8 commit a0fe57f

File tree

1 file changed

+25
-10
lines changed

1 file changed

+25
-10
lines changed

flyway-database-databricks/src/main/java/org/flywaydb/community/database/databricks/DatabricksSchema.java

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import java.util.ArrayList;
1010
import java.util.List;
1111
import java.util.Map;
12+
import java.util.Objects;
1213

1314
public class DatabricksSchema extends Schema<DatabricksDatabase, DatabricksTable> {
1415
/**
@@ -20,25 +21,40 @@ public DatabricksSchema(JdbcTemplate jdbcTemplate, DatabricksDatabase database,
2021
super(jdbcTemplate, database, name);
2122
}
2223

23-
private List<String> fetchAllObjs(String obj) throws SQLException {
24+
private List<String> fetchAllObjs(String obj, String column) throws SQLException {
2425
List<Map<String, String>> tableInfos = jdbcTemplate.queryForList(
2526
"show " + obj + "s from " + database.quote(name)
2627
);
2728
List<String> tableNames = new ArrayList<String>();
2829
for (Map<String, String> tableInfo : tableInfos) {
29-
tableNames.add(tableInfo.get("tableName"));
30+
tableNames.add(tableInfo.get(column));
3031
}
3132
return tableNames;
3233
}
34+
35+
private List<String> fetchAllSchemas() throws SQLException {
36+
List<Map<String, String>> schemaInfos = jdbcTemplate.queryForList("show schemas");
37+
List<String> schemaNames = new ArrayList<>();
38+
for (Map<String, String> schemaInfo : schemaInfos) {
39+
schemaNames.add(schemaInfo.get("databaseName"));
40+
}
41+
return schemaNames;
42+
}
43+
44+
private List<String> fetchAllTables() throws SQLException {
45+
var tables = fetchAllObjs("table", "tableName");
46+
var views = fetchAllObjs("view", "viewName");
47+
return tables.stream().filter(t -> !views.contains(t)).toList();
48+
}
3349

3450
@Override
3551
protected boolean doExists() throws SQLException {
36-
return fetchAllObjs("table").size() > 0;
52+
return fetchAllSchemas().stream().anyMatch(schema -> Objects.equals(schema, name));
3753
}
3854

3955
@Override
4056
protected boolean doEmpty() throws SQLException {
41-
return fetchAllObjs("table").size() == 0;
57+
return fetchAllTables().size() == 0;
4258
}
4359

4460
@Override
@@ -53,19 +69,18 @@ protected void doDrop() throws SQLException {
5369

5470
@Override
5571
protected void doClean() throws SQLException {
56-
for (String statement : generateDropStatements("TABLE")) {
72+
for (String statement : generateDropStatements("TABLE", fetchAllTables())) {
5773
jdbcTemplate.execute(statement);
5874
}
59-
for (String statement : generateDropStatements("VIEW")) {
75+
for (String statement : generateDropStatements("VIEW", fetchAllObjs("VIEW", "viewName"))) {
6076
jdbcTemplate.execute(statement);
6177
}
62-
for (String statement : generateDropStatements("FUNCTION")) {
78+
for (String statement : generateDropStatements("FUNCTION",fetchAllObjs("USER FUNCTION", "function"))) {
6379
jdbcTemplate.execute(statement);
6480
}
6581
}
6682

67-
private List<String> generateDropStatements(String objType) throws SQLException {
68-
List<String> names = fetchAllObjs(objType);
83+
private List<String> generateDropStatements(String objType, List<String> names) throws SQLException {
6984
List<String> statements = new ArrayList<>();
7085
for (String domainName : names) {
7186
statements.add("drop " + objType + " if exists " + database.quote(name, domainName) + ";");
@@ -76,7 +91,7 @@ private List<String> generateDropStatements(String objType) throws SQLException
7691

7792
@Override
7893
protected DatabricksTable[] doAllTables() throws SQLException {
79-
List<String> tableNames = fetchAllObjs("TABLE");
94+
List<String> tableNames = fetchAllTables();
8095
DatabricksTable[] tables = new DatabricksTable[tableNames.size()];
8196
for (int i = 0; i < tableNames.size(); i++) {
8297
tables[i] = new DatabricksTable(jdbcTemplate, database, this, tableNames.get(i));

0 commit comments

Comments
 (0)