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 #51 from builtamont-oss/feature/user_defined_confi…
Browse files Browse the repository at this point in the history
…guration_file

Feature - User defined configuration file
  • Loading branch information
hhandoko authored Mar 4, 2017
2 parents 55eeabe + 63063a3 commit c312aea
Show file tree
Hide file tree
Showing 12 changed files with 464 additions and 90 deletions.
29 changes: 21 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ Import this library as a dependency:
</repositories>
```

***NOTE:** A Vagrant script is provided to enable quick integration testing against different or older Cassandra distributions.*
*NOTE: A Vagrant script is provided to enable quick integration testing against different or older Cassandra distributions.*

### Migration version table

Expand Down Expand Up @@ -121,6 +121,13 @@ cm.migrate();
#### Command line
Logging level can be set by passing the following arguments:
* INFO: This is the default
* DEBUG: `-X`
* WARNING: `-q`
##### With system properties JVM args
``` shell
java -jar \
-Dcassandra.migration.scripts.locations=filesystem:target/test-classes/migration/integ \
Expand All @@ -134,14 +141,18 @@ java -jar \
target/*-jar-with-dependencies.jar migrate
```
Logging level can be set by passing the following arguments:
* INFO: This is the default
* DEBUG: `-X`
* WARNING: `-q`
##### With application configuration file
``` shell
java -jar \
-Dconfig.file=src/test/application.test.conf \
target/*-jar-with-dependencies.jar migrate
```
### VM Options
Options can be set either programmatically with API or via Java VM options.
Options can be set either programmatically with API, Java VM options, or configuration file.
Refer to [Typesafe Config] library documentation or refer to [reference.conf] for a reference configuration.
Migration:
* `cassandra.migration.scripts.locations`: Locations of the migration scripts in CSV format. Scripts are scanned in the specified folder recursively. (default=`db/migration`)
Expand Down Expand Up @@ -200,7 +211,7 @@ Run `mvn test` to run the unit tests.
Run `mvn verify` to run the integration tests.
***NOTE:** The integration test might complain about some missing SIGAR binaries, this can be safely ignored. If you wish, you can download the missing binaries and set `java.library.path` parameter to point to the containing folder (e.g. `mvn verify -Djava.library.path=lib` where `lib` is the `/lib` folder relative to the project root).*
*NOTE: The integration test might complain about some missing SIGAR binaries, this can be safely ignored. If you wish, you can download the missing binaries and set `java.library.path` parameter to point to the containing folder (e.g. `mvn verify -Djava.library.path=lib` where `lib` is the `/lib` folder relative to the project root).*
### Travis CI Integration Test Matrix
Expand Down Expand Up @@ -251,4 +262,6 @@ https://github.com/builtamont/cassandra-migration/releases
[Flyway's project license page]: https://github.com/flyway/flyway/blob/master/LICENSE
[fork-and-pull]: https://help.github.com/articles/using-pull-requests
[LICENSE]: LICENSE
[SIGAR]: https://support.hyperic.com/display/SIGAR/Home
[reference.conf]: https://github.com/builtamont-oss/cassandra-migration/blob/master/src/main/resources/reference.conf
[SIGAR]: https://support.hyperic.com/display/SIGAR/Home
[Typesafe Config]: https://github.com/typesafehub/config
18 changes: 18 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,13 @@
<maven.compiler.target>1.7</maven.compiler.target>
</properties>

<repositories>
<repository>
<id>jcenter</id>
<url>https://jcenter.bintray.com/</url>
</repository>
</repositories>

<dependencies>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
Expand Down Expand Up @@ -93,6 +100,16 @@
<artifactId>cassandra-driver-core</artifactId>
<version>3.0.6</version>
</dependency>
<dependency>
<groupId>com.typesafe</groupId>
<artifactId>config</artifactId>
<version>1.2.1</version>
</dependency>
<dependency>
<groupId>io.github.config4k</groupId>
<artifactId>config4k</artifactId>
<version>0.3.0</version>
</dependency>

<!-- Test-specific dependencies -->
<dependency>
Expand Down Expand Up @@ -263,6 +280,7 @@
</plugin>
</plugins>
</build>

<profiles>
<profile>
<id>release</id>
Expand Down
7 changes: 6 additions & 1 deletion scripts/verify.sh
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,9 @@
# limitations under the License.
###

mvn verify -Dcassandra.migration.cluster.contactpoints=127.0.0.1 -Dcassandra.migration.cluster.port=9042 -Dcassandra.migration.disable_embedded=true
mvn verify \
-Dit.config.file=src/test/resources/application.it-test.conf \
-Dcassandra.migration.cluster.contactpoints=127.0.0.1 \
-Dcassandra.migration.cluster.port=9042 \
-Dcassandra.migration.disable_embedded=true

Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ import com.datastax.driver.core.NettySSLOptions
import com.datastax.driver.core.Session
import com.datastax.driver.core.policies.DCAwareRoundRobinPolicy
import com.datastax.driver.core.policies.TokenAwarePolicy
import com.typesafe.config.ConfigFactory
import io.github.config4k.extract
import io.netty.handler.ssl.SslContextBuilder
import io.netty.handler.ssl.SslProvider
import java.io.FileInputStream
Expand All @@ -58,7 +60,7 @@ class CassandraMigration : CassandraMigrationConfiguration {
/**
* The Cassandra keyspace configuration.
*/
lateinit var keyspaceConfig: KeyspaceConfiguration
var keyspaceConfig: KeyspaceConfiguration

/**
* The ClassLoader to use for resolving migrations on the classpath.
Expand Down Expand Up @@ -120,29 +122,40 @@ class CassandraMigration : CassandraMigrationConfiguration {
init {
this.keyspaceConfig = KeyspaceConfiguration()

val targetVersionProp = System.getProperty(ConfigurationProperty.TARGET_VERSION.namespace)
if (!targetVersionProp.isNullOrBlank()) target = MigrationVersion.fromVersion(targetVersionProp)
ConfigFactory.invalidateCaches()
ConfigFactory.load().let {
it.extract<String?>(ConfigurationProperty.TARGET_VERSION.namespace)?.let {
this.target = MigrationVersion.fromVersion(it.trim().toUpperCase())
}

val baselineVersionProp = System.getProperty(ConfigurationProperty.BASELINE_VERSION.namespace)
if (!baselineVersionProp.isNullOrBlank()) baselineVersion = MigrationVersion.fromVersion(baselineVersionProp.trim())
it.extract<String?>(ConfigurationProperty.BASELINE_VERSION.namespace)?.let {
this.baselineVersion = MigrationVersion.fromVersion(it.trim().toUpperCase())
}

val baselineDescriptionProp = System.getProperty(ConfigurationProperty.BASELINE_DESCRIPTION.namespace)
if (!baselineDescriptionProp.isNullOrBlank()) baselineDescription = baselineDescriptionProp.trim()
it.extract<String?>(ConfigurationProperty.BASELINE_DESCRIPTION.namespace)?.let {
this.baselineDescription = it.trim()
}

val encodingProp = System.getProperty(ConfigurationProperty.SCRIPTS_ENCODING.namespace)
if (!encodingProp.isNullOrBlank()) encoding = encodingProp.trim()
it.extract<String?>(ConfigurationProperty.SCRIPTS_ENCODING.namespace)?.let {
this.encoding = it.trim()
}

val locationsProp = System.getProperty(ConfigurationProperty.SCRIPTS_LOCATIONS.namespace)
if (!locationsProp.isNullOrBlank()) locations = StringUtils.tokenizeToStringArray(locationsProp, ",")
it.extract<String?>(ConfigurationProperty.SCRIPTS_LOCATIONS.namespace)?.let {
this.locations = StringUtils.tokenizeToStringArray(it, ",")
}

val timeoutProp = System.getProperty(ConfigurationProperty.SCRIPTS_TIMEOUT.namespace)
if (!timeoutProp.isNullOrBlank() && Regex("""^\d+$""").matches(timeoutProp)) timeout = timeoutProp.toInt()
it.extract<Int?>(ConfigurationProperty.SCRIPTS_TIMEOUT.namespace)?.let {
this.timeout = it
}

val allowOutOfOrderProp = System.getProperty(ConfigurationProperty.ALLOW_OUT_OF_ORDER.namespace)
if (!allowOutOfOrderProp.isNullOrBlank()) allowOutOfOrder = allowOutOfOrderProp.toBoolean()
it.extract<Boolean?>(ConfigurationProperty.ALLOW_OUT_OF_ORDER.namespace)?.let {
this.allowOutOfOrder = it
}

val tablePrefixProp = System.getProperty(ConfigurationProperty.TABLE_PREFIX.namespace)
if (!tablePrefixProp.isNullOrBlank()) tablePrefix = tablePrefixProp.trim()
it.extract<String?>(ConfigurationProperty.TABLE_PREFIX.namespace)?.let {
this.tablePrefix = it.trim()
}
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -221,9 +221,17 @@ class MigrationVersion : Comparable<MigrationVersion?> {
return "current".equals(version, ignoreCase = true)
}

/**
* @return {@code true} if version is "LATEST".
*/
fun isLatest(): Boolean {
return "latest".equals(version, ignoreCase = true)
}

return when {
version == null -> EMPTY
isCurrent() -> CURRENT
isLatest() -> LATEST
LATEST.version == version -> LATEST
else -> MigrationVersion(version)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ package com.builtamont.cassandra.migration.api.configuration
import java.nio.file.Path;
import java.nio.file.Paths;
import com.builtamont.cassandra.migration.internal.util.StringUtils
import com.typesafe.config.ConfigFactory
import io.github.config4k.extract

/**
* Cluster configuration.
Expand Down Expand Up @@ -81,29 +83,40 @@ class ClusterConfiguration {
* ClusterConfiguration initialization.
*/
init {
val contactpointsProp = System.getProperty(ConfigurationProperty.CONTACT_POINTS.namespace)
if (!contactpointsProp.isNullOrBlank()) this.contactpoints = StringUtils.tokenizeToStringArray(contactpointsProp, ",")

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

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

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

val truststoreProp = System.getProperty(ConfigurationProperty.TRUSTSTORE.namespace)
if (!truststoreProp.isNullOrBlank()) this.truststore = Paths.get(truststoreProp.trim())

val truststorePasswordProp = System.getProperty(ConfigurationProperty.TRUSTSTORE_PASSWORD.namespace)
if (!truststorePasswordProp.isNullOrBlank()) this.truststorePassword = truststorePasswordProp.trim()

val keystoreProp = System.getProperty(ConfigurationProperty.KEYSTORE.namespace)
if (!keystoreProp.isNullOrBlank()) this.keystore = Paths.get(keystoreProp.trim())

val keystorePasswordProp = System.getProperty(ConfigurationProperty.KEYSTORE_PASSWORD.namespace)
if (!keystorePasswordProp.isNullOrBlank()) this.keystorePassword = keystorePasswordProp.trim()
ConfigFactory.invalidateCaches()
ConfigFactory.load().let {
it.extract<String?>(ConfigurationProperty.CONTACT_POINTS.namespace)?.let {
this.contactpoints = StringUtils.tokenizeToStringArray(it, ",")
}

it.extract<Int?>(ConfigurationProperty.PORT.namespace)?.let {
this.port = it
}

it.extract<String?>(ConfigurationProperty.USERNAME.namespace)?.let {
this.username = it.trim()
}

it.extract<String?>(ConfigurationProperty.PASSWORD.namespace)?.let {
this.password = it.trim()
}

it.extract<String?>(ConfigurationProperty.TRUSTSTORE.namespace)?.let {
this.truststore = Paths.get(it.trim())
}

it.extract<String?>(ConfigurationProperty.TRUSTSTORE_PASSWORD.namespace)?.let {
this.truststorePassword = it.trim()
}

it.extract<String?>(ConfigurationProperty.KEYSTORE.namespace)?.let {
this.keystore = Paths.get(it.trim())
}

it.extract<String?>(ConfigurationProperty.KEYSTORE_PASSWORD.namespace)?.let {
this.keystorePassword = it.trim()
}
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
package com.builtamont.cassandra.migration.api.configuration

import com.datastax.driver.core.ConsistencyLevel
import com.typesafe.config.ConfigFactory
import io.github.config4k.extract

/**
* Keyspace configuration.
Expand Down Expand Up @@ -46,11 +48,16 @@ class KeyspaceConfiguration {
* KeyspaceConfiguration initialization.
*/
init {
val keyspaceProp = System.getProperty(ConfigurationProperty.KEYSPACE_NAME.namespace)
if (!keyspaceProp.isNullOrBlank()) this.name = keyspaceProp.trim()

val consistencyProp = System.getProperty(ConfigurationProperty.CONSISTENCY_LEVEL.namespace)
if (!consistencyProp.isNullOrBlank()) this.consistency = ConsistencyLevel.valueOf(consistencyProp.trim().toUpperCase())
ConfigFactory.invalidateCaches()
ConfigFactory.load().let {
it.extract<String?>(ConfigurationProperty.KEYSPACE_NAME.namespace)?.let {
this.name = it.trim()
}

it.extract<String?>(ConfigurationProperty.CONSISTENCY_LEVEL.namespace)?.let {
this.consistency = ConsistencyLevel.valueOf(it.trim().toUpperCase())
}
}
}

}
Loading

0 comments on commit c312aea

Please sign in to comment.