Skip to content

Commit

Permalink
Merge pull request Contrast-Security-OSS#11 from builtamont-oss/chore…
Browse files Browse the repository at this point in the history
…/merge_migration_config

Chore - Merge migration config
  • Loading branch information
hhandoko authored Sep 13, 2016
2 parents 3264237 + 909afd4 commit ba66e83
Show file tree
Hide file tree
Showing 11 changed files with 212 additions and 268 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -198,8 +198,8 @@ https://github.com/builtamont/cassandra-migration/releases
## Version 0.9 Release Pending Actions
* ~~Replace `config.Cluster.java` and `config.Keyspace.java` to the one provided by DataStax Cassandra driver~~<br />***NOTE:** The classes are merely configuration objects, and has been updated for clarity.*
* ~~Add additional features from upstream open PRs~~<br />***DONE:** as per 8 September 2016 PRs.*
* ~~Replace `config.Cluster.java` and `config.Keyspace.java` to the one provided by DataStax Cassandra driver~~<br />*NOTE: The classes are merely configuration objects, and has been updated for clarity.*
* ~~Add additional features from upstream open PRs~~<br />*DONE: as per 8 September 2016 PRs.*
* Add standalone Cassandra (DataStax Community Edition) integration test
## Non-Critical Pending Actions
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import com.builtamont.cassandra.migration.api.CassandraMigrationException
import com.builtamont.cassandra.migration.api.MigrationInfoService
import com.builtamont.cassandra.migration.api.MigrationVersion
import com.builtamont.cassandra.migration.api.configuration.CassandraMigrationConfiguration
import com.builtamont.cassandra.migration.api.configuration.MigrationConfiguration
import com.builtamont.cassandra.migration.api.configuration.ConfigurationProperty
import com.builtamont.cassandra.migration.api.resolver.MigrationResolver
import com.builtamont.cassandra.migration.api.configuration.KeyspaceConfiguration
import com.builtamont.cassandra.migration.internal.command.Baseline
Expand All @@ -33,6 +33,7 @@ import com.builtamont.cassandra.migration.internal.dbsupport.SchemaVersionDAO
import com.builtamont.cassandra.migration.internal.info.MigrationInfoServiceImpl
import com.builtamont.cassandra.migration.internal.resolver.CompositeMigrationResolver
import com.builtamont.cassandra.migration.internal.util.ScriptsLocations
import com.builtamont.cassandra.migration.internal.util.StringUtils
import com.builtamont.cassandra.migration.internal.util.VersionPrinter
import com.builtamont.cassandra.migration.internal.util.logging.LogFactory
import com.datastax.driver.core.Cluster
Expand All @@ -45,38 +46,70 @@ import com.datastax.driver.core.Session
*/
class CassandraMigration : CassandraMigrationConfiguration {

/**
* The Cassandra keyspace configuration.
*/
lateinit var keyspaceConfig: KeyspaceConfiguration

/**
* The ClassLoader to use for resolving migrations on the classpath.
* (default: Thread.currentThread().getContextClassLoader())
*/
override var classLoader = Thread.currentThread().contextClassLoader

/**
* The Cassandra keyspace to connect to.
* The target version. Migrations with a higher version number will be ignored.
* (default: MigrationVersion.LATEST)
*/
lateinit var keyspaceConfig: KeyspaceConfiguration
override var target = MigrationVersion.LATEST

/**
* The Cassandra migration configuration.
* The baseline version.
* (default: MigrationVersion.fromVersion("1"))
*/
lateinit var migrationConfig: MigrationConfiguration
override var baselineVersion = MigrationVersion.fromVersion("1")

/**
* The baseline version.
* The baseline description.
* (default: "<< Cassandra Baseline >>")
*/
private val baselineVersion = MigrationVersion.Companion.fromVersion("1")
override var baselineDescription = "<< Cassandra Baseline >>"

/**
* The baseline description.
* The encoding of CQL migrations script encoding.
* (default: "UTF-8")
*/
private val baselineDescription = "<< Cassandra Baseline >>"
override var encoding = "UTF-8"

/**
* Locations to scan recursively for migrations.
* (default: new String[] {"db/migration"})
*/
override var locations = arrayOf("db/migration")

/**
* Allow out of order migrations.
* (default: false)
*/
var allowOutOfOrder = false

/**
* CassandraMigration initialization.
*/
init {
this.keyspaceConfig = KeyspaceConfiguration()
this.migrationConfig = MigrationConfiguration()

val targetVersionProp = System.getProperty(ConfigurationProperty.TARGET_VERSION.namespace)
if (!targetVersionProp.isNullOrBlank()) target = MigrationVersion.fromVersion(targetVersionProp)

val encodingProp = System.getProperty(ConfigurationProperty.SCRIPTS_ENCODING.namespace)
if (!encodingProp.isNullOrBlank()) encoding = encodingProp.trim()

val locationsProp = System.getProperty(ConfigurationProperty.SCRIPTS_LOCATIONS.namespace)
if (!locationsProp.isNullOrBlank()) locations = StringUtils.tokenizeToStringArray(locationsProp, ",")

val allowOutOfOrderProp = System.getProperty(ConfigurationProperty.ALLOW_OUT_OF_ORDER.namespace)
if (!allowOutOfOrderProp.isNullOrBlank()) allowOutOfOrder = allowOutOfOrderProp.toBoolean()
}

/**
Expand Down Expand Up @@ -277,7 +310,7 @@ class CassandraMigration : CassandraMigrationConfiguration {
* @return A new, fully configured, MigrationResolver instance.
*/
private fun createMigrationResolver(): MigrationResolver {
return CompositeMigrationResolver(classLoader, ScriptsLocations(*migrationConfig.scriptsLocations), migrationConfig.encoding)
return CompositeMigrationResolver(classLoader, ScriptsLocations(*locations), encoding)
}

/**
Expand All @@ -301,11 +334,11 @@ class CassandraMigration : CassandraMigrationConfiguration {
val schemaVersionDAO = createSchemaVersionDAO(session)
val migrate = Migrate(
migrationResolver,
migrationConfig.target,
target,
schemaVersionDAO,
session,
keyspaceConfig.clusterConfig.username ?: "",
migrationConfig.isAllowOutOfOrder
allowOutOfOrder
)

return migrate.run()
Expand All @@ -324,7 +357,7 @@ class CassandraMigration : CassandraMigrationConfiguration {
val migrationInfoService = MigrationInfoServiceImpl(
migrationResolver,
schemaVersionDAO,
migrationConfig.target,
target,
outOfOrder = false,
pendingOrFuture = true
)
Expand All @@ -345,7 +378,7 @@ class CassandraMigration : CassandraMigrationConfiguration {
val schemaVersionDAO = createSchemaVersionDAO(session)
val validate = Validate(
migrationResolver,
migrationConfig.target,
target,
schemaVersionDAO,
outOfOrder = true,
pendingOrFuture = false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
*/
package com.builtamont.cassandra.migration.api.configuration

import com.builtamont.cassandra.migration.api.MigrationVersion

/**
* Readonly interface for main Cassandra migration configuration.
* Can be used to provide configuration data to migrations and callbacks.
Expand All @@ -40,21 +42,21 @@ interface CassandraMigrationConfiguration {
// */
// val dataSource: DataSource

// /**
// * Retrieves the version to tag an existing schema with when executing baseline.
// *
// * @return The version to tag an existing schema with when executing baseline.
// * (default: 1)
// */
// val baselineVersion: MigrationVersion
/**
* Retrieves the version to tag an existing schema with when executing baseline.
*
* @return The version to tag an existing schema with when executing baseline.
* (default: 1)
*/
val baselineVersion: MigrationVersion

// /**
// * Retrieves the description to tag an existing schema with when executing baseline.
// *
// * @return The description to tag an existing schema with when executing baseline.
// * (default: << Cassandra Migration Baseline >>)
// */
// val baselineDescription: String
/**
* Retrieves the description to tag an existing schema with when executing baseline.
*
* @return The description to tag an existing schema with when executing baseline.
* (default: << Cassandra Baseline >>)
*/
val baselineDescription: String

// /**
// * Retrieves the the custom MigrationResolvers to be used in addition to the built-in ones for resolving migrations to apply.
Expand Down Expand Up @@ -164,16 +166,16 @@ interface CassandraMigrationConfiguration {
// */
// val placeholders: Map<String, String>

// /**
// * Retrieves the target version up to which Cassandra migration should consider migrations.
// * Migrations with a higher version number will be ignored.
// *
// * The special value `current` designates the current version of the schema.
// *
// * @return The target version up to which Cassandra migration should consider migrations.
// * (default: the latest version)
// */
// val target: MigrationVersion
/**
* Retrieves the target version up to which Cassandra migration should consider migrations.
* Migrations with a higher version number will be ignored.
*
* The special value `current` designates the current version of the schema.
*
* @return The target version up to which Cassandra migration should consider migrations.
* (default: the latest version)
*/
val target: MigrationVersion

// /**
// * Retrieves the name of the schema metadata table that will be used by Cassandra migration.
Expand All @@ -200,28 +202,28 @@ interface CassandraMigrationConfiguration {
// */
// val schemas: Array<String>

// /**
// * Retrieves the encoding of CQL migrations.
// *
// * @return The encoding of CQL migrations.
// * (default: UTF-8)
// */
// val encoding: String
/**
* Retrieves the encoding of CQL migrations.
*
* @return The encoding of CQL migrations.
* (default: UTF-8)
*/
val encoding: String

// /**
// * Retrieves the locations to scan recursively for migrations.
// *
// * The location type is determined by its prefix.
// *
// * Unprefixed locations or locations starting with `classpath:` point to a package on the classpath and may
// * contain both CQL and Java-based migrations.
// *
// * Locations starting with `filesystem:` point to a directory on the filesystem and may only contain sql
// * migrations.
// *
// * @return Locations to scan recursively for migrations.
// * (default: db/migration)
// */
// val locations: Array<String>
/**
* Retrieves the locations to scan recursively for migrations.
*
* The location type is determined by its prefix.
*
* Unprefixed locations or locations starting with `classpath:` point to a package on the classpath and may
* contain both CQL and Java-based migrations.
*
* Locations starting with `filesystem:` point to a directory on the filesystem and may only contain sql
* migrations.
*
* @return Locations to scan recursively for migrations.
* (default: db/migration)
*/
val locations: Array<String>

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,7 @@ import com.builtamont.cassandra.migration.internal.util.StringUtils
/**
* Cluster configuration.
*/
class ClusterConfiguration : Configuration() {

/**
* Cluster configuration properties.
*
* @param namespace The property namespace.
* @param description The property description.
*/
enum class ClusterProperty constructor(val namespace: String, val description: String) {
CONTACT_POINTS(PROPERTY_PREFIX + "contactpoints", "Comma separated values of node IP addresses"),
PORT(PROPERTY_PREFIX + "port", "CQL native transport port"),
USERNAME(PROPERTY_PREFIX + "username", "Username for password authenticator"),
PASSWORD(PROPERTY_PREFIX + "password", "Password for password authenticator")
}
class ClusterConfiguration {

/**
* Cluster node IP address(es).
Expand Down Expand Up @@ -68,24 +55,17 @@ class ClusterConfiguration : Configuration() {
* ClusterConfiguration initialization.
*/
init {
val contactpointsProp = System.getProperty(ClusterProperty.CONTACT_POINTS.namespace)
val contactpointsProp = System.getProperty(ConfigurationProperty.CONTACT_POINTS.namespace)
if (!contactpointsProp.isNullOrBlank()) this.contactpoints = StringUtils.tokenizeToStringArray(contactpointsProp, ",")

val portProp = System.getProperty(ClusterProperty.PORT.namespace)
val portProp = System.getProperty(ConfigurationProperty.PORT.namespace)
if (!portProp.isNullOrBlank()) this.port = Integer.parseInt(portProp)

val usernameProp = System.getProperty(ClusterProperty.USERNAME.namespace)
val usernameProp = System.getProperty(ConfigurationProperty.USERNAME.namespace)
if (!usernameProp.isNullOrBlank()) this.username = usernameProp.trim()

val passwordProp = System.getProperty(ClusterProperty.PASSWORD.namespace)
val passwordProp = System.getProperty(ConfigurationProperty.PASSWORD.namespace)
if (!passwordProp.isNullOrBlank()) this.password = passwordProp.trim()
}

/**
* ClusterConfiguration companion object.
*/
companion object {
private val PROPERTY_PREFIX = BASE_PREFIX + "cluster."
}

}

This file was deleted.

Loading

0 comments on commit ba66e83

Please sign in to comment.