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

Support escaped keyspace names #26 #27

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
9 changes: 7 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,12 @@
<groupId>com.datastax.cassandra</groupId>
<artifactId>cassandra-driver-core</artifactId>
<version>3.0.0</version>
<exclusions>
<exclusion>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>junit</groupId>
Expand Down Expand Up @@ -124,8 +130,7 @@
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>18.0</version>
<scope>test</scope>
<version>19.0</version>
</dependency>
</dependencies>

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.contrastsecurity.cassandra.migration;

import com.contrastsecurity.cassandra.migration.action.Clear;
import com.contrastsecurity.cassandra.migration.action.Initialize;
import com.contrastsecurity.cassandra.migration.action.Migrate;
import com.contrastsecurity.cassandra.migration.action.Validate;
Expand Down Expand Up @@ -108,6 +109,15 @@ public String execute(Session session) {
throw new CassandraMigrationException("Validation failed. " + validationError);
}
}

public Boolean clear() {
return execute(new Action<Boolean>() {
public Boolean execute(Session session) {
Clear clear = new Clear(session, keyspace);
return clear.run();
}
});
}

public void baseline() {
//TODO
Expand Down Expand Up @@ -162,8 +172,9 @@ <T> T execute(Action<T> action) {
throw new IllegalArgumentException("Keyspace not specified.");
List<KeyspaceMetadata> keyspaces = metadata.getKeyspaces();
boolean keyspaceExists = false;
String keyspaceName = keyspace.getName().replace("\"", ""); //remove quotation marks
for (KeyspaceMetadata keyspaceMetadata : keyspaces) {
if (keyspaceMetadata.getName().equalsIgnoreCase(keyspace.getName()))
if (keyspaceMetadata.getName().equalsIgnoreCase(keyspaceName))
keyspaceExists = true;
}
if (keyspaceExists)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ public class CommandLine {
*/
public static final String VALIDATE = "validate";

/**
* command to trigger clear action
*/
public static final String CLEAR = "clear";

/**
* logging support
*/
Expand All @@ -45,10 +50,16 @@ public static void main(String[] args) {
CassandraMigration cm = new CassandraMigration();
Keyspace ks = new Keyspace();
cm.setKeyspace(ks);
if (MIGRATE.equalsIgnoreCase(operation)) {
cm.migrate();
} else if (VALIDATE.equalsIgnoreCase(operation)) {
cm.validate();
switch (operation.toLowerCase()) {
case MIGRATE:
cm.migrate();
break;
case VALIDATE:
cm.validate();
break;
case CLEAR:
cm.clear();
break;
}
}

Expand Down Expand Up @@ -92,6 +103,7 @@ private static void printUsage() {
LOG.info("========");
LOG.info("migrate : Migrates the database");
LOG.info("validate : Validates the applied migrations against the available ones");
LOG.info("clear : Clears the whole database");
LOG.info("");
LOG.info("Add -X to print debug output");
LOG.info("Add -q to suppress all output, except for errors and warnings");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package com.contrastsecurity.cassandra.migration.action;

import com.contrastsecurity.cassandra.migration.config.Keyspace;
import com.contrastsecurity.cassandra.migration.logging.Log;
import com.contrastsecurity.cassandra.migration.logging.LogFactory;
import com.contrastsecurity.cassandra.migration.utils.StopWatch;
import com.datastax.driver.core.ResultSet;
import com.datastax.driver.core.Row;
import com.datastax.driver.core.Session;
import com.datastax.driver.core.querybuilder.QueryBuilder;
import com.datastax.driver.core.querybuilder.Select;

import static com.datastax.driver.core.querybuilder.QueryBuilder.eq;

public class Clear {
private static final Log LOG = LogFactory.getLog(Clear.class);

private final Session session;
private final Keyspace keyspace;

public Clear(Session session, Keyspace keyspace) {
this.session = session;
this.keyspace = keyspace;
}

public boolean run() {
StopWatch stopWatch = new StopWatch();
stopWatch.start();

for (ObjectType objectType : ObjectType.values()) {
clearObjects(objectType);
}

stopWatch.stop();
LOG.info("CLEARED ALL OBJECTS");
LOG.info(String.format("CLEARING TOOK %d ms", stopWatch.getTotalTimeMillis()));

return true;
}

public void clearObjects(ObjectType objectType) {
Select.Where objectsQuery = QueryBuilder.select(objectType.getSchemaColumnName()).from("system_schema", objectType.getSchemaTable()).where(eq("keyspace_name", keyspace.getName()));
ResultSet objects = session.execute(objectsQuery);
for (Row object : objects) {
LOG.info(String.format("Clearing %s of type %s", object.getString(objectType.getSchemaColumnName()), objectType.queryFormat()));
session.execute(String.format("DROP %s IF EXISTS %s",
objectType.queryFormat(),
object.getString(objectType.getSchemaColumnName())));
}

}

public enum ObjectType {
MATERIALIZED_VIEW("views", "view_name"),
TABLE("tables", "table_name");

private final String schemaTable;
private final String schemaColumnName;

ObjectType(String schemaTable, String schemaColumnName) {
this.schemaTable = schemaTable;
this.schemaColumnName = schemaColumnName;
}

public String queryFormat() {
return name().replace("_", " ").toUpperCase();
}

public String getSchemaTable() {
return schemaTable;
}

public String getSchemaColumnName() {
return schemaColumnName;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ private int calculateInstalledRank() {
Statement statement = new SimpleStatement(
"UPDATE " + keyspace.getName() + "." + tableName + COUNTS_TABLE_NAME_SUFFIX +
" SET count = count + 1" +
"WHERE name = 'installed_rank';");
" WHERE name = 'installed_rank';");
session.execute(statement);
Select select = QueryBuilder
.select("count")
Expand Down