Skip to content
This repository was archived by the owner on Jul 22, 2020. It is now read-only.

Commit 9a54113

Browse files
committed
Removed casting on MigrationInfoService filters collections
1 parent 5f899f8 commit 9a54113

File tree

4 files changed

+29
-29
lines changed

4 files changed

+29
-29
lines changed

src/main/java/com/builtamont/cassandra/migration/CassandraMigration.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ class CassandraMigration : CassandraMigrationConfiguration {
5959
/**
6060
* The Cassandra migration configuration.
6161
*/
62-
val configs: MigrationConfigs
62+
lateinit var configs: MigrationConfigs
6363

6464
/**
6565
* CassandraMigration initialization.

src/main/java/com/builtamont/cassandra/migration/internal/command/Migrate.kt

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,7 @@ class Migrate(
7272

7373
// Initialise `firstRun` and `currentSchemaVersion` variables
7474
val firstRun = migrationSuccessCount == 0
75-
var currentSchemaVersion = MigrationVersion.EMPTY
76-
if (infoService.current() != null) {
77-
currentSchemaVersion = infoService.current()!!.version
78-
}
75+
val currentSchemaVersion = infoService.current()?.version ?: MigrationVersion.CURRENT
7976

8077
// First run only
8178
// ~~~~~
@@ -93,7 +90,7 @@ class Migrate(
9390
// Log future migrations and warn users if there are no resolved migrations, or
9491
// if there current version migration is newer than what is available
9592
val future = infoService.future()
96-
if (future.size > 0) {
93+
if (future.isNotEmpty()) {
9794
val resolvedLogMsg = "Keyspace $keyspaceName has version $currentSchemaVersion"
9895
val resolved = infoService.resolved()
9996
if (resolved.size == 0) {
@@ -108,7 +105,7 @@ class Migrate(
108105
// ~~~~~
109106
// Log failed future migrations and throw `CassandraMigrationException` for everything else
110107
val failed = infoService.failed()
111-
if (failed.size > 0) {
108+
if (failed.isNotEmpty()) {
112109
val isFutureFailed = failed[0].state === MigrationState.FUTURE_FAILED
113110
val failedVersion = failed[0].version
114111
if (failed.size == 1 && isFutureFailed) {
@@ -122,14 +119,17 @@ class Migrate(
122119

123120
// Pending migrations
124121
// ~~~~~
125-
// Early return if there are no pending migrations, otherwise
126-
// apply pending migrations and exit when error is thrown
127-
val pendingMigrations = infoService.pending() as Array<MigrationInfoImpl>
128-
if (pendingMigrations.size == 0) {
122+
// Apply pending migrations
123+
val pendingMigrations = infoService.pending()
124+
if (pendingMigrations.isNotEmpty()) {
125+
if (pendingMigrations[0] is MigrationInfoImpl) {
126+
val isOutOfOrder = pendingMigrations[0].version.compareTo(currentSchemaVersion) < 0
127+
applyMigration(pendingMigrations[0] as MigrationInfoImpl, isOutOfOrder) ?: break
128+
}
129+
} else {
130+
// Exit if there are no more pending migrations
129131
break
130132
}
131-
val isOutOfOrder = pendingMigrations[0].version.compareTo(currentSchemaVersion) < 0
132-
applyMigration(pendingMigrations[0], isOutOfOrder) ?: break
133133

134134
migrationSuccessCount++
135135
}
@@ -184,9 +184,9 @@ class Migrate(
184184
stopWatch.start()
185185

186186
var isMigrationSuccess = false
187-
val migrationExecutor = migration.resolvedMigration?.executor
188187
try {
189-
migrationExecutor!!.execute(session)
188+
val executor = migration.resolvedMigration!!.executor!!
189+
executor.execute(session)
190190
isMigrationSuccess = true
191191
LOG.debug("$logMsg success!")
192192
} catch (e: Exception) {

src/main/java/com/builtamont/cassandra/migration/internal/info/MigrationInfoImpl.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,9 @@ import java.util.*
3434
* @param context The current context.
3535
*/
3636
class MigrationInfoImpl(
37-
val resolvedMigration: ResolvedMigration?,
38-
val appliedMigration: AppliedMigration?,
39-
private val context: MigrationInfoContext
37+
val resolvedMigration: ResolvedMigration?,
38+
val appliedMigration: AppliedMigration?,
39+
private val context: MigrationInfoContext
4040
) : MigrationInfo {
4141

4242
/**

src/main/java/com/builtamont/cassandra/migration/internal/info/MigrationInfoServiceImpl.kt

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ class MigrationInfoServiceImpl(
4848
/**
4949
* The migrations infos calculated at the last refresh.
5050
*/
51-
private var migrationInfos: List<MigrationInfoImpl>? = null
51+
private var migrationInfos: List<MigrationInfoImpl> = emptyList()
5252

5353
/**
5454
* Refreshes the info about all known migrations from both the classpath and the DB.
@@ -60,7 +60,7 @@ class MigrationInfoServiceImpl(
6060
migrationInfos = mergeAvailableAndAppliedMigrations(availableMigrations, appliedMigrations)
6161

6262
if (MigrationVersion.CURRENT === target) {
63-
target = current()!!.version
63+
target = current()?.version
6464
}
6565
}
6666

@@ -70,7 +70,7 @@ class MigrationInfoServiceImpl(
7070
* @return The error message, or `null` if everything is fine.
7171
*/
7272
override fun validate(): String? {
73-
migrationInfos?.forEach { it.validate()?.let { return it } }
73+
migrationInfos.forEach { it.validate()?.let { return it } }
7474
return null
7575
}
7676

@@ -80,14 +80,14 @@ class MigrationInfoServiceImpl(
8080
* @return The migrations.
8181
*/
8282
override fun all(): Array<MigrationInfo> {
83-
return migrationInfos?.toTypedArray<MigrationInfo>() ?: emptyArray()
83+
return migrationInfos.toTypedArray()
8484
}
8585

8686
/**
8787
* @return Current migration to be run.
8888
*/
8989
override fun current(): MigrationInfo? {
90-
return migrationInfos?.lastOrNull { it.state.isApplied }
90+
return migrationInfos.lastOrNull { it.state.isApplied }
9191
}
9292

9393
/**
@@ -96,7 +96,7 @@ class MigrationInfoServiceImpl(
9696
* @return The pending migrations. An empty array if none.
9797
*/
9898
override fun pending(): Array<MigrationInfo> {
99-
return migrationInfos?.filter { it.state === MigrationState.PENDING }.orEmpty<MigrationInfo>().toTypedArray()
99+
return migrationInfos.filter { it.state === MigrationState.PENDING }.orEmpty().toTypedArray()
100100
}
101101

102102
/**
@@ -105,7 +105,7 @@ class MigrationInfoServiceImpl(
105105
* @return The applied migrations. An empty array if none.
106106
*/
107107
override fun applied(): Array<MigrationInfo> {
108-
return migrationInfos?.filter { it.state.isApplied }.orEmpty<MigrationInfo>().toTypedArray()
108+
return migrationInfos.filter { it.state.isApplied }.orEmpty().toTypedArray()
109109
}
110110

111111
/**
@@ -114,7 +114,7 @@ class MigrationInfoServiceImpl(
114114
* @return The resolved migrations. An empty array if none.
115115
*/
116116
override fun resolved(): Array<MigrationInfo> {
117-
return migrationInfos?.filter { it.state.isResolved }.orEmpty<MigrationInfo>().toTypedArray()
117+
return migrationInfos.filter { it.state.isResolved }.orEmpty().toTypedArray()
118118
}
119119

120120
/**
@@ -123,7 +123,7 @@ class MigrationInfoServiceImpl(
123123
* @return The failed migrations. An empty array if none.
124124
*/
125125
override fun failed(): Array<MigrationInfo> {
126-
return migrationInfos?.filter { it.state.isFailed }.orEmpty<MigrationInfo>().toTypedArray()
126+
return migrationInfos.filter { it.state.isFailed }.orEmpty().toTypedArray()
127127
}
128128

129129
/**
@@ -132,7 +132,7 @@ class MigrationInfoServiceImpl(
132132
* @return The future migrations. An empty array if none.
133133
*/
134134
override fun future(): Array<MigrationInfo> {
135-
return migrationInfos?.filter { it.state === MigrationState.FUTURE_SUCCESS }.orEmpty<MigrationInfo>().toTypedArray()
135+
return migrationInfos.filter { it.state === MigrationState.FUTURE_SUCCESS }.orEmpty().toTypedArray()
136136
}
137137

138138
/**
@@ -141,7 +141,7 @@ class MigrationInfoServiceImpl(
141141
* @return The out of order migrations. An empty array if none.
142142
*/
143143
override fun outOfOrder(): Array<MigrationInfo> {
144-
return migrationInfos?.filter { it.state === MigrationState.OUT_OF_ORDER }.orEmpty<MigrationInfo>().toTypedArray()
144+
return migrationInfos.filter { it.state === MigrationState.OUT_OF_ORDER }.orEmpty().toTypedArray()
145145
}
146146

147147
/**

0 commit comments

Comments
 (0)