@@ -24,13 +24,17 @@ import com.builtamont.cassandra.migration.api.MigrationVersion
24
24
import com.builtamont.cassandra.migration.api.configuration.CassandraMigrationConfiguration
25
25
import com.builtamont.cassandra.migration.api.configuration.MigrationConfigs
26
26
import com.builtamont.cassandra.migration.api.resolver.MigrationResolver
27
+ import com.builtamont.cassandra.migration.config.Keyspace
27
28
import com.builtamont.cassandra.migration.internal.command.Initialize
28
29
import com.builtamont.cassandra.migration.internal.command.Migrate
29
30
import com.builtamont.cassandra.migration.internal.command.Validate
31
+ import com.builtamont.cassandra.migration.internal.dbsupport.SchemaVersionDAO
30
32
import com.builtamont.cassandra.migration.internal.info.MigrationInfoServiceImpl
31
33
import com.builtamont.cassandra.migration.internal.resolver.CompositeMigrationResolver
32
34
import com.builtamont.cassandra.migration.internal.util.ScriptsLocations
35
+ import com.builtamont.cassandra.migration.internal.util.VersionPrinter
33
36
import com.builtamont.cassandra.migration.internal.util.logging.LogFactory
37
+ import com.datastax.driver.core.Cluster
34
38
import com.datastax.driver.core.Metadata
35
39
import com.datastax.driver.core.Session
36
40
import sun.reflect.generics.reflectiveObjects.NotImplementedException
@@ -50,7 +54,7 @@ class CassandraMigration : CassandraMigrationConfiguration {
50
54
/* *
51
55
* The Cassandra keyspace to connect to.
52
56
*/
53
- var keyspace: com.builtamont.cassandra.migration.config. Keyspace ? = null
57
+ lateinit var keyspace: Keyspace
54
58
55
59
/* *
56
60
* The Cassandra migration configuration.
@@ -61,7 +65,7 @@ class CassandraMigration : CassandraMigrationConfiguration {
61
65
* CassandraMigration initialization.
62
66
*/
63
67
init {
64
- this .keyspace = com.builtamont.cassandra.migration.config. Keyspace ()
68
+ this .keyspace = Keyspace ()
65
69
this .configs = MigrationConfigs ()
66
70
}
67
71
@@ -74,12 +78,18 @@ class CassandraMigration : CassandraMigrationConfiguration {
74
78
fun migrate (): Int {
75
79
return execute(object : Action <Int > {
76
80
override fun execute (session : Session ): Int {
77
- Initialize ().run (session, keyspace!! , MigrationVersion .CURRENT .table)
81
+ Initialize ().run (session, keyspace, MigrationVersion .CURRENT .table)
78
82
79
83
val migrationResolver = createMigrationResolver()
80
- val schemaVersionDAO = com.builtamont.cassandra.migration.internal.dbsupport.SchemaVersionDAO (session, keyspace, MigrationVersion .CURRENT .table)
81
- val migrate = Migrate (migrationResolver, configs.target, schemaVersionDAO, session,
82
- keyspace!! .cluster.username, configs.isAllowOutOfOrder)
84
+ val schemaVersionDAO = SchemaVersionDAO (session, keyspace, MigrationVersion .CURRENT .table)
85
+ val migrate = Migrate (
86
+ migrationResolver,
87
+ configs.target,
88
+ schemaVersionDAO,
89
+ session,
90
+ keyspace.cluster.username,
91
+ configs.isAllowOutOfOrder
92
+ )
83
93
84
94
return migrate.run ()
85
95
}
@@ -96,7 +106,7 @@ class CassandraMigration : CassandraMigrationConfiguration {
96
106
return execute(object : Action <MigrationInfoService > {
97
107
override fun execute (session : Session ): MigrationInfoService ? {
98
108
val migrationResolver = createMigrationResolver()
99
- val schemaVersionDAO = com.builtamont.cassandra.migration.internal.dbsupport. SchemaVersionDAO (session, keyspace, MigrationVersion .CURRENT .table)
109
+ val schemaVersionDAO = SchemaVersionDAO (session, keyspace, MigrationVersion .CURRENT .table)
100
110
val migrationInfoService = MigrationInfoServiceImpl (migrationResolver, schemaVersionDAO, configs.target, false , true )
101
111
migrationInfoService.refresh()
102
112
@@ -118,7 +128,7 @@ class CassandraMigration : CassandraMigrationConfiguration {
118
128
val validationError = execute(object : Action <String ?> {
119
129
override fun execute (session : Session ): String? {
120
130
val migrationResolver = createMigrationResolver()
121
- val schemaVersionDao = com.builtamont.cassandra.migration.internal.dbsupport. SchemaVersionDAO (session, keyspace, MigrationVersion .CURRENT .table)
131
+ val schemaVersionDao = SchemaVersionDAO (session, keyspace, MigrationVersion .CURRENT .table)
122
132
val validate = Validate (migrationResolver, configs.target, schemaVersionDao, true , false )
123
133
return validate.run ()
124
134
}
@@ -147,23 +157,22 @@ class CassandraMigration : CassandraMigrationConfiguration {
147
157
internal fun <T > execute (action : Action <T >): T {
148
158
val result: T
149
159
150
- com.builtamont.cassandra.migration.internal.util. VersionPrinter .printVersion(classLoader)
160
+ VersionPrinter .printVersion(classLoader)
151
161
152
- var cluster: com.datastax.driver.core. Cluster ? = null
162
+ var cluster: Cluster ? = null
153
163
var session: Session ? = null
154
164
try {
155
165
if (null == keyspace)
156
166
throw IllegalArgumentException (" Unable to establish Cassandra session. Keyspace is not configured." )
157
167
158
- if (null == keyspace!! .cluster)
168
+ if (null == keyspace.cluster)
159
169
throw IllegalArgumentException (" Unable to establish Cassandra session. Cluster is not configured." )
160
170
161
- val builder = com.datastax.driver.core.Cluster .Builder ()
162
- builder.addContactPoints(* keyspace!! .cluster.contactpoints).withPort(keyspace!! .cluster.port)
163
- if (null != keyspace!! .cluster.username && ! keyspace!! .cluster.username.trim { it <= ' ' }.isEmpty()) {
164
- if (null != keyspace!! .cluster.password && ! keyspace!! .cluster.password.trim { it <= ' ' }.isEmpty()) {
165
- builder.withCredentials(keyspace!! .cluster.username,
166
- keyspace!! .cluster.password)
171
+ val builder = Cluster .Builder ()
172
+ builder.addContactPoints(* keyspace.cluster.contactpoints).withPort(keyspace.cluster.port)
173
+ if (null != keyspace.cluster.username && ! keyspace.cluster.username.trim { it <= ' ' }.isEmpty()) {
174
+ if (null != keyspace.cluster.password && ! keyspace.cluster.password.trim { it <= ' ' }.isEmpty()) {
175
+ builder.withCredentials(keyspace.cluster.username, keyspace.cluster.password)
167
176
} else {
168
177
throw IllegalArgumentException (" Password must be provided with username." )
169
178
}
@@ -174,18 +183,19 @@ class CassandraMigration : CassandraMigrationConfiguration {
174
183
LOG .info(getConnectionInfo(metadata))
175
184
176
185
session = cluster.newSession()
177
- if (null == keyspace!! .name || keyspace!! .name.trim { it <= ' ' }.length == 0 )
186
+ if (null == keyspace.name || keyspace.name.trim { it <= ' ' }.length == 0 )
178
187
throw IllegalArgumentException (" Keyspace not specified." )
188
+
179
189
val keyspaces = metadata.keyspaces
180
190
var keyspaceExists = false
181
191
for (keyspaceMetadata in keyspaces) {
182
- if (keyspaceMetadata.name.equals(keyspace!! .name, ignoreCase = true ))
192
+ if (keyspaceMetadata.name.equals(keyspace.name, ignoreCase = true ))
183
193
keyspaceExists = true
184
194
}
185
195
if (keyspaceExists)
186
- session!! .execute(" USE " + keyspace!! .name)
196
+ session!! .execute(" USE " + keyspace.name)
187
197
else
188
- throw CassandraMigrationException (" Keyspace: " + keyspace!! .name + " does not exist." )
198
+ throw CassandraMigrationException (" Keyspace: " + keyspace.name + " does not exist." )
189
199
190
200
result = action.execute(session)!!
191
201
} finally {
0 commit comments