generated from GoodforGod/java-library-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Cassandra Keyspace by default support added Cassandra Cognitor migration library support added Cassandra Migration#dropMode TRUNCATE/DROP support added
- Loading branch information
1 parent
97142fa
commit 2fa3d80
Showing
27 changed files
with
369 additions
and
139 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
.../goodforgod/testcontainers/extensions/cassandra/AbstractDropCassandraMigrationEngine.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package io.goodforgod.testcontainers.extensions.cassandra; | ||
|
||
import java.util.*; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
abstract class AbstractDropCassandraMigrationEngine implements CassandraMigrationEngine { | ||
|
||
private static final Set<String> SYSTEM_KEYSPACES = Set.of("system", "system_auth", "system_schema", "system_distributed", | ||
"system_traces"); | ||
|
||
protected final Logger logger = LoggerFactory.getLogger(getClass()); | ||
|
||
private static class Table { | ||
|
||
private final String keyspace; | ||
private final String name; | ||
|
||
private Table(String keyspace, String name) { | ||
this.keyspace = keyspace; | ||
this.name = name; | ||
} | ||
|
||
public String keyspace() { | ||
return keyspace; | ||
} | ||
|
||
public String name() { | ||
return name; | ||
} | ||
} | ||
|
||
protected final CassandraConnection connection; | ||
|
||
public AbstractDropCassandraMigrationEngine(CassandraConnection connection) { | ||
this.connection = connection; | ||
} | ||
|
||
@Override | ||
public void drop(@NotNull List<String> locations, Migration.DropMode mode) { | ||
if (locations.isEmpty()) { | ||
logger.warn("Empty locations for schema migration for engine '{}' for connection: {}", | ||
getClass().getSimpleName(), connection); | ||
return; | ||
} | ||
|
||
logger.debug("Starting schema dropping for engine '{}' for connection: {}", | ||
getClass().getSimpleName(), connection); | ||
|
||
var tables = connection.queryMany( | ||
"SELECT keyspace_name, table_name FROM system_schema.tables;", | ||
r -> new Table(r.getString(0), r.getString(1))); | ||
|
||
for (Table table : tables) { | ||
if (table.keyspace().equals(connection.params().keyspace()) && !SYSTEM_KEYSPACES.contains(table.keyspace())) { | ||
// always try to use TRUNCATE cause DROP is SUPER slow, drop keyspace is even slower | ||
if (mode == Migration.DropMode.TRUNCATE) { | ||
connection.execute("TRUNCATE TABLE " + table.keyspace() + "." + table.name()); | ||
} else { | ||
connection.execute("DROP TABLE " + table.keyspace() + "." + table.name()); | ||
} | ||
} | ||
} | ||
|
||
logger.info("Finished schema dropping for engine '{}' for connection: {}", | ||
getClass().getSimpleName(), connection); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.