Skip to content
This repository has been archived by the owner on Jul 22, 2020. It is now read-only.

Commit

Permalink
Merge pull request #18 from james-hu/feature/configurable_migration_t…
Browse files Browse the repository at this point in the history
…able_names

Allow a configurable prefix to be prepended to `cassandra_migration_version*` table names
  • Loading branch information
hhandoko authored Sep 30, 2016
2 parents 4f31660 + 9a2c65b commit 32077c8
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 2 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ keyspaceConfig.getClusterConfig().setPassword(CASSANDRA_PASSWORD);
CassandraMigration cm = new CassandraMigration();
cm.setLocations(scriptsLocations);
cm.setKeyspaceConfig(keyspaceConfig);
cm.setTablePrefix("my_app_");
cm.migrate();
```
Expand All @@ -117,6 +118,7 @@ cm.migrate();
``` shell
java -jar \
-Dcassandra.migration.scripts.locations=filesystem:target/test-classes/migration/integ \
-Dcassandra.migration.table.prefix=another_app_ \
-Dcassandra.migration.cluster.contactpoints=localhost \
-Dcassandra.migration.cluster.port=9147 \
-Dcassandra.migration.cluster.username=cassandra \
Expand All @@ -139,6 +141,7 @@ Migration:
* `cassandra.migration.scripts.encoding`: The encoding of CQL scripts (default=UTF-8)
* `cassandra.migration.scripts.allowoutoforder`: Allow out of order migration (default=false)
* `cassandra.migration.version.target`: The target version. Migrations with a higher version number will be ignored. (default=latest)
* `cassandra.migration.table.prefix`: The prefix to be prepended to `cassandra_migration_version*` table names.
Cluster:
* `cassandra.migration.cluster.contactpoints`: Comma separated values of node IP addresses (default=localhost)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,12 @@ class CassandraMigration : CassandraMigrationConfiguration {
*/
override var locations = arrayOf("db/migration")

/**
* The prefix to be prepended to `cassandra_migration_version*` table names.
* (default: "")
*/
override var tablePrefix = ""

/**
* Allow out of order migrations.
* (default: false)
Expand All @@ -108,6 +114,9 @@ class CassandraMigration : CassandraMigrationConfiguration {
val locationsProp = System.getProperty(ConfigurationProperty.SCRIPTS_LOCATIONS.namespace)
if (!locationsProp.isNullOrBlank()) locations = StringUtils.tokenizeToStringArray(locationsProp, ",")

val tablePrefixProp = System.getProperty(ConfigurationProperty.TABLE_PREFIX.namespace)
if (!tablePrefixProp.isNullOrBlank()) tablePrefix = tablePrefixProp.trim()

val allowOutOfOrderProp = System.getProperty(ConfigurationProperty.ALLOW_OUT_OF_ORDER.namespace)
if (!allowOutOfOrderProp.isNullOrBlank()) allowOutOfOrder = allowOutOfOrderProp.toBoolean()
}
Expand Down Expand Up @@ -313,13 +322,17 @@ class CassandraMigration : CassandraMigrationConfiguration {
return CompositeMigrationResolver(classLoader, ScriptsLocations(*locations), encoding)
}

private fun migrationTableName(): String{
return tablePrefix.orEmpty() + MigrationVersion.CURRENT.table
}

/**
* Creates the SchemaVersionDAO.
*
* @return A configured SchemaVersionDAO instance.
*/
private fun createSchemaVersionDAO(session: Session): SchemaVersionDAO {
return SchemaVersionDAO(session, keyspaceConfig, MigrationVersion.CURRENT.table)
return SchemaVersionDAO(session, keyspaceConfig, migrationTableName())
}

/**
Expand All @@ -328,7 +341,7 @@ class CassandraMigration : CassandraMigrationConfiguration {
private fun migrateAction(): Action<Int> {
return object: Action<Int> {
override fun execute(session: Session): Int {
Initialize().run(session, keyspaceConfig, MigrationVersion.CURRENT.table)
Initialize().run(session, keyspaceConfig, migrationTableName())

val migrationResolver = createMigrationResolver()
val schemaVersionDAO = createSchemaVersionDAO(session)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -226,4 +226,15 @@ interface CassandraMigrationConfiguration {
*/
val locations: Array<String>

/**
* Retrieves the migration table prefix.
*
* The prefix will be prepended to `cassandra_migration_version*` table names.
*
* By providing the prefix, multiple applications can have their own migration tables tracking the migration of their
* own cassandra database assets without interfering with each other.
*
* @return the prefix to be prepended to `cassandra_migration_version*` table names
*/
val tablePrefix: String
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ enum class ConfigurationProperty(val namespace: String, val description: String)
"Locations of the migration scripts in CSV format"
),

TABLE_PREFIX(
"cassandra.migration.table.prefix",
"Prefix to be prepended to cassandra_migration_version* table names"
),

ALLOW_OUT_OF_ORDER(
"cassandra.migration.scripts.allowoutoforder",
"Allow out of order migration"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,20 @@ public void testBaseLine() {
assertThat(baselineMarker.getVersion(), is(MigrationVersion.Companion.fromVersion("1")));
}

@Test
public void testBaseLineWithTablePrefix() {
String[] scriptsLocations = {"migration/integ", "migration/integ/java"};
CassandraMigration cm = new CassandraMigration();
cm.setLocations(scriptsLocations);
cm.setKeyspaceConfig(getKeyspace());
cm.setTablePrefix("test1_");
cm.baseline();

SchemaVersionDAO schemaVersionDAO = new SchemaVersionDAO(getSession(), getKeyspace(), cm.getTablePrefix() + MigrationVersion.Companion.getCURRENT().getTable());
AppliedMigration baselineMarker = schemaVersionDAO.getBaselineMarker();
assertThat(baselineMarker.getVersion(), is(MigrationVersion.Companion.fromVersion("1")));
}

@Test
public void testBaseLineWithSession() {
String[] scriptsLocations = {"migration/integ", "migration/integ/java"};
Expand All @@ -261,6 +275,21 @@ public void testBaseLineWithSession() {
assertThat(baselineMarker.getVersion(), is(MigrationVersion.Companion.fromVersion("1")));
}

@Test
public void testBaseLineWithSessionAndTablePrefix() {
String[] scriptsLocations = {"migration/integ", "migration/integ/java"};
Session session = getSession();
CassandraMigration cm = new CassandraMigration();
cm.setLocations(scriptsLocations);
cm.setKeyspaceConfig(getKeyspace());
cm.setTablePrefix("test2_");
cm.baseline(session);

SchemaVersionDAO schemaVersionDAO = new SchemaVersionDAO(getSession(), getKeyspace(), cm.getTablePrefix() + MigrationVersion.Companion.getCURRENT().getTable());
AppliedMigration baselineMarker = schemaVersionDAO.getBaselineMarker();
assertThat(baselineMarker.getVersion(), is(MigrationVersion.Companion.fromVersion("1")));
}

@Test(expected = CassandraMigrationException.class)
public void testBaseLineWithMigrations() {
String[] scriptsLocations = { "migration/integ", "migration/integ/java" };
Expand Down

0 comments on commit 32077c8

Please sign in to comment.