From 4521bea4d295fc8c0449238c77068ad7b87a7485 Mon Sep 17 00:00:00 2001 From: AndrewFrom Date: Mon, 17 Apr 2017 15:45:44 -0500 Subject: [PATCH] Add ability to load include scripts from non-classpath locations by providing a baseScriptsPath --- README.adoc | 4 +- .../org/cassalog/core/CassalogBuilder.groovy | 11 ++- .../org/cassalog/core/CassalogImpl.groovy | 9 ++- .../org/cassalog/core/IncludeTest.groovy | 78 ++++++++++++++++++- 4 files changed, 94 insertions(+), 8 deletions(-) diff --git a/README.adoc b/README.adoc index d52a6cd..30ba47a 100644 --- a/README.adoc +++ b/README.adoc @@ -192,7 +192,9 @@ include '/dbchanges/seed_data.groovy' ---- The `include` function currently takes a single string argument that should -specify the absolute path of a script on the classpath. +specify the absolute path of a script on the classpath or from the configured `baseScriptsPath`. + +`baseScriptsPath` is an absolute path to where the other include scripts are located e.g. `/Users/john/cassalog/scripts`. === Schema change detection Cassalog does not store the CQL code associated with each schema change. It diff --git a/src/main/groovy/org/cassalog/core/CassalogBuilder.groovy b/src/main/groovy/org/cassalog/core/CassalogBuilder.groovy index 7e95a8f..82102be 100644 --- a/src/main/groovy/org/cassalog/core/CassalogBuilder.groovy +++ b/src/main/groovy/org/cassalog/core/CassalogBuilder.groovy @@ -27,6 +27,8 @@ class CassalogBuilder { private Session session + private String baseScriptsPath + CassalogBuilder withKeyspace(String keyspace) { this.keyspace = keyspace return this @@ -34,11 +36,16 @@ class CassalogBuilder { CassalogBuilder withSession(Session session) { this.session = session - return this; + return this + } + + CassalogBuilder withBaseScriptsPath(String baseScriptsPath) { + this.baseScriptsPath = baseScriptsPath + return this } Cassalog build() { - return new CassalogImpl(keyspace: keyspace, session: session) + return new CassalogImpl(keyspace: keyspace, session: session, baseScriptsPath: baseScriptsPath) } } diff --git a/src/main/groovy/org/cassalog/core/CassalogImpl.groovy b/src/main/groovy/org/cassalog/core/CassalogImpl.groovy index cb283e5..45b6b07 100644 --- a/src/main/groovy/org/cassalog/core/CassalogImpl.groovy +++ b/src/main/groovy/org/cassalog/core/CassalogImpl.groovy @@ -39,6 +39,8 @@ class CassalogImpl implements Cassalog { Session session + String baseScriptsPath + int bucketSize = DEFAULT_BUCKET_SIZE PreparedStatement insertSchemaChange @@ -196,7 +198,12 @@ class CassalogImpl implements Cassalog { scriptVars.putAll(createScriptHelperFunctions()) def shell = new GroovyShell(binding) - def scriptURI = getClass().getResource(script).toURI() + def scriptURI + if (baseScriptsPath != null && baseScriptsPath != "") { + scriptURI = new File(baseScriptsPath + script).toURI() + } else { + scriptURI = getClass().getResource(script).toURI() + } shell.evaluate(scriptURI) return dbchanges diff --git a/src/test/groovy/org/cassalog/core/IncludeTest.groovy b/src/test/groovy/org/cassalog/core/IncludeTest.groovy index 2fcc4d8..e42a36c 100644 --- a/src/test/groovy/org/cassalog/core/IncludeTest.groovy +++ b/src/test/groovy/org/cassalog/core/IncludeTest.groovy @@ -26,7 +26,7 @@ import static org.testng.Assert.assertEquals class IncludeTest extends CassalogBaseTest { @Test - void includeScriptAfterChanges() { + void includeScriptAfterChangesClasspath() { def keyspace = 'include_after' resetSchema(keyspace) @@ -45,7 +45,7 @@ class IncludeTest extends CassalogBaseTest { } @Test - void includeScriptBeforeChanges() { + void includeScriptBeforeChangesClasspath() { def keyspace = 'include_before' resetSchema(keyspace) @@ -61,7 +61,7 @@ class IncludeTest extends CassalogBaseTest { } @Test - void multipleIncludes() { + void multipleIncludesClasspath() { def keyspace = 'multiple_includes' resetSchema(keyspace) @@ -77,7 +77,7 @@ class IncludeTest extends CassalogBaseTest { } @Test - void nestedIncludes() { + void nestedIncludesClasspath() { def keyspace = 'nested_includes' resetSchema(keyspace) @@ -91,5 +91,75 @@ class IncludeTest extends CassalogBaseTest { assertChangeSetEquals(rows[0], new ChangeSet(version: 'second-table')) assertChangeSetEquals(rows[1], new ChangeSet(version: 'third-table')) } + @Test + void includeScriptAfterChangesBasePath() { + def keyspace = 'include_after' + resetSchema(keyspace) + + def script = getClass().getResource('/include/script1.groovy').toURI() + + def baseScriptsPath = new File(getClass().getResource('/').toURI()).absolutePath + + def cassalog = new CassalogImpl(keyspace: keyspace, session: session, baseScriptsPath: baseScriptsPath) + cassalog.execute(script, [keyspace: keyspace]) + + assertTableExists(keyspace, 'test1') + assertTableExists(keyspace, 'test2') + + def rows = findChangeSets(keyspace, 0) + assertEquals(rows.size(), 2) + assertChangeSetEquals(rows[0], new ChangeSet(version: 'first-table')) + assertChangeSetEquals(rows[1], new ChangeSet(version: 'second-table')) + } + + @Test + void includeScriptBeforeChangesBasePath() { + def keyspace = 'include_before' + resetSchema(keyspace) + + def script = getClass().getResource('/include/script3.groovy').toURI() + def baseScriptsPath = new File(getClass().getResource('/').toURI()).absolutePath + + def cassalog = new CassalogImpl(keyspace: keyspace, session: session, baseScriptsPath: baseScriptsPath) + cassalog.execute(script, [keyspace: keyspace]) + + def rows = findChangeSets(keyspace, 0) + assertEquals(rows.size(), 2) + assertChangeSetEquals(rows[0], new ChangeSet(version: 'second-table')) + assertChangeSetEquals(rows[1], new ChangeSet(version: 'third-table')) + } + + @Test + void multipleIncludesBasePath() { + def keyspace = 'multiple_includes' + resetSchema(keyspace) + + def script = getClass().getResource('/include/multiple_includes.groovy').toURI() + def baseScriptsPath = new File(getClass().getResource('/').toURI()).absolutePath + def cassalog = new CassalogImpl(keyspace: keyspace, session: session, baseScriptsPath: baseScriptsPath) + cassalog.execute(script, [keyspace: keyspace]) + + def rows = findChangeSets(keyspace, 0) + assertEquals(rows.size(), 2) + assertChangeSetEquals(rows[0], new ChangeSet(version: 'second-table')) + assertChangeSetEquals(rows[1], new ChangeSet(version: 'fourth-table')) + } + + @Test + void nestedIncludesBasePath() { + def keyspace = 'nested_includes' + resetSchema(keyspace) + + def script = getClass().getResource('/include/nested.groovy').toURI() + def baseScriptsPath = new File(getClass().getResource('/').toURI()).absolutePath + + def cassalog = new CassalogImpl(keyspace: keyspace, session: session, baseScriptsPath: baseScriptsPath) + cassalog.execute(script, [keyspace: keyspace]) + + def rows = findChangeSets(keyspace, 0) + assertEquals(rows.size(), 2) + assertChangeSetEquals(rows[0], new ChangeSet(version: 'second-table')) + assertChangeSetEquals(rows[1], new ChangeSet(version: 'third-table')) + } }