Skip to content

Commit

Permalink
Add ability to load include scripts from non-classpath locations by p…
Browse files Browse the repository at this point in the history
…roviding a baseScriptsPath
  • Loading branch information
fromanator committed Apr 18, 2017
1 parent 3d95676 commit 4521bea
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 8 deletions.
4 changes: 3 additions & 1 deletion README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 9 additions & 2 deletions src/main/groovy/org/cassalog/core/CassalogBuilder.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,25 @@ class CassalogBuilder {

private Session session

private String baseScriptsPath

CassalogBuilder withKeyspace(String keyspace) {
this.keyspace = keyspace
return this
}

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)
}

}
9 changes: 8 additions & 1 deletion src/main/groovy/org/cassalog/core/CassalogImpl.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ class CassalogImpl implements Cassalog {

Session session

String baseScriptsPath

int bucketSize = DEFAULT_BUCKET_SIZE

PreparedStatement insertSchemaChange
Expand Down Expand Up @@ -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
Expand Down
78 changes: 74 additions & 4 deletions src/test/groovy/org/cassalog/core/IncludeTest.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -45,7 +45,7 @@ class IncludeTest extends CassalogBaseTest {
}

@Test
void includeScriptBeforeChanges() {
void includeScriptBeforeChangesClasspath() {
def keyspace = 'include_before'
resetSchema(keyspace)

Expand All @@ -61,7 +61,7 @@ class IncludeTest extends CassalogBaseTest {
}

@Test
void multipleIncludes() {
void multipleIncludesClasspath() {
def keyspace = 'multiple_includes'
resetSchema(keyspace)

Expand All @@ -77,7 +77,7 @@ class IncludeTest extends CassalogBaseTest {
}

@Test
void nestedIncludes() {
void nestedIncludesClasspath() {
def keyspace = 'nested_includes'
resetSchema(keyspace)

Expand All @@ -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'))
}
}

0 comments on commit 4521bea

Please sign in to comment.