34
34
35
35
import static com .datastax .driver .core .querybuilder .QueryBuilder .eq ;
36
36
37
+ /**
38
+ * Schema migrations table Data Access Object.
39
+ */
37
40
// TODO: Convert to Kotlin code... Some challenges with Mockito mocking :)
38
41
public class SchemaVersionDAO {
39
42
@@ -46,14 +49,22 @@ public class SchemaVersionDAO {
46
49
private CachePrepareStatement cachePs ;
47
50
private ConsistencyLevel consistencyLevel ;
48
51
52
+ /**
53
+ * Creates a new schema version DAO.
54
+ *
55
+ * @param session The Cassandra session connection to use to execute the migration.
56
+ * @param keyspace The Cassandra keyspace to connect to.
57
+ * @param tableName The Cassandra migration version table name.
58
+ */
49
59
public SchemaVersionDAO (Session session , Keyspace keyspace , String tableName ) {
50
60
this .session = session ;
51
61
this .keyspace = keyspace ;
52
62
this .tableName = tableName ;
53
63
this .cachePs = new CachePrepareStatement (session );
54
- //If running on a single host, don't force ConsistencyLevel.ALL
55
- this .consistencyLevel =
56
- session .getCluster ().getMetadata ().getAllHosts ().size () > 1 ? ConsistencyLevel .ALL : ConsistencyLevel .ONE ;
64
+
65
+ // If running on a single host, don't force ConsistencyLevel.ALL
66
+ boolean isClustered = session .getCluster ().getMetadata ().getAllHosts ().size () > 1 ;
67
+ this .consistencyLevel = isClustered ? ConsistencyLevel .ALL : ConsistencyLevel .ONE ;
57
68
}
58
69
59
70
public String getTableName () {
@@ -64,6 +75,9 @@ public Keyspace getKeyspace() {
64
75
return this .keyspace ;
65
76
}
66
77
78
+ /**
79
+ * Create schema migration version table if it does not exists.
80
+ */
67
81
public void createTablesIfNotExist () {
68
82
if (tablesExist ()) {
69
83
return ;
@@ -97,6 +111,11 @@ public void createTablesIfNotExist() {
97
111
session .execute (statement );
98
112
}
99
113
114
+ /**
115
+ * Check if schema migration version table has already been created.
116
+ *
117
+ * @return {@code true} if schema migration version table exists in the keyspace.
118
+ */
100
119
public boolean tablesExist () {
101
120
boolean schemaVersionTableExists = false ;
102
121
boolean schemaVersionCountsTableExists = false ;
@@ -135,18 +154,29 @@ public boolean tablesExist() {
135
154
return schemaVersionTableExists && schemaVersionCountsTableExists ;
136
155
}
137
156
157
+ /**
158
+ * Add applied migration record into the schema migration version table.
159
+ *
160
+ * @param appliedMigration The applied migration.
161
+ */
138
162
public void addAppliedMigration (AppliedMigration appliedMigration ) {
139
163
createTablesIfNotExist ();
140
164
141
165
MigrationVersion version = appliedMigration .getVersion ();
142
166
143
167
int versionRank = calculateVersionRank (version );
144
168
PreparedStatement statement = cachePs .prepare (
145
- "INSERT INTO " + keyspace .getName () + "." + tableName +
146
- " (version_rank, installed_rank, version, description, type, script, checksum, installed_on," +
147
- " installed_by, execution_time, success)" +
148
- " VALUES" +
149
- " (?, ?, ?, ?, ?, ?, ?, dateOf(now()), ?, ?, ?);"
169
+ "INSERT INTO " + keyspace .getName () + "." + tableName + "(" +
170
+ " version_rank, installed_rank, version," +
171
+ " description, type, script," +
172
+ " checksum, installed_on, installed_by," +
173
+ " execution_time, success" +
174
+ ") VALUES (" +
175
+ " ?, ?, ?," +
176
+ " ?, ?, ?," +
177
+ " ?, dateOf(now()), ?," +
178
+ " ?, ?" +
179
+ ");"
150
180
);
151
181
152
182
statement .setConsistencyLevel (this .consistencyLevel );
@@ -166,7 +196,7 @@ public void addAppliedMigration(AppliedMigration appliedMigration) {
166
196
}
167
197
168
198
/**
169
- * Retrieve the applied migrations from the metadata table.
199
+ * Retrieve the applied migrations from the schema migration version table.
170
200
*
171
201
* @return The applied migrations.
172
202
*/
@@ -209,8 +239,8 @@ public List<AppliedMigration> findAppliedMigrations() {
209
239
));
210
240
}
211
241
212
- //order by version_rank not necessary here as it eventually gets saved in TreeMap that uses natural ordering
213
-
242
+ // NOTE: Order by ` version_rank` not necessary here, as it eventually gets saved in TreeMap
243
+ // that uses natural ordering
214
244
return resultsList ;
215
245
}
216
246
@@ -263,11 +293,16 @@ public List<AppliedMigration> findAppliedMigrations(MigrationType... migrationTy
263
293
}
264
294
}
265
295
266
- //order by version_rank not necessary here as it eventually gets saved in TreeMap that uses natural ordering
267
-
296
+ // NOTE: Order by ` version_rank` not necessary here, as it eventually gets saved in TreeMap
297
+ // that uses natural ordering
268
298
return resultsList ;
269
299
}
270
300
301
+ /**
302
+ * Check if the keyspace has applied migrations.
303
+ *
304
+ * @return {@code true} if the keyspace has applied migrations.
305
+ */
271
306
public boolean hasAppliedMigrations () {
272
307
if (!tablesExist ()) {
273
308
return false ;
@@ -284,6 +319,13 @@ public boolean hasAppliedMigrations() {
284
319
return !filteredMigrations .isEmpty ();
285
320
}
286
321
322
+ /**
323
+ * Add a baseline version marker.
324
+ *
325
+ * @param baselineVersion The baseline version.
326
+ * @param baselineDescription the baseline version description.
327
+ * @param user The user's username executing the baselining.
328
+ */
287
329
public void addBaselineMarker (final MigrationVersion baselineVersion , final String baselineDescription , final String user ) {
288
330
addAppliedMigration (
289
331
new AppliedMigration (
@@ -299,16 +341,28 @@ public void addBaselineMarker(final MigrationVersion baselineVersion, final Stri
299
341
);
300
342
}
301
343
344
+ /**
345
+ * Get the baseline marker's applied migration.
346
+ *
347
+ * @return The baseline marker's applied migration.
348
+ */
302
349
public AppliedMigration getBaselineMarker () {
303
350
List <AppliedMigration > appliedMigrations = findAppliedMigrations (MigrationType .BASELINE );
304
351
return appliedMigrations .isEmpty () ? null : appliedMigrations .get (0 );
305
352
}
306
353
354
+ /**
355
+ * Check if schema migration version table has a baseline marker.
356
+ *
357
+ * @return {@code true} if the schema migration version table has a baseline marker.
358
+ */
307
359
public boolean hasBaselineMarker () {
308
360
if (!tablesExist ()) {
309
361
return false ;
310
362
}
363
+
311
364
createTablesIfNotExist ();
365
+
312
366
return !findAppliedMigrations (MigrationType .BASELINE ).isEmpty ();
313
367
}
314
368
@@ -321,14 +375,19 @@ private int calculateInstalledRank() {
321
375
Statement statement = new SimpleStatement (
322
376
"UPDATE " + keyspace .getName () + "." + tableName + COUNTS_TABLE_NAME_SUFFIX +
323
377
" SET count = count + 1" +
324
- "WHERE name = 'installed_rank';" );
378
+ " WHERE name = 'installed_rank'" +
379
+ ";" );
380
+
325
381
session .execute (statement );
382
+
326
383
Select select = QueryBuilder
327
384
.select ("count" )
328
385
.from (tableName + COUNTS_TABLE_NAME_SUFFIX );
329
386
select .where (eq ("name" , "installed_rank" ));
387
+
330
388
select .setConsistencyLevel (this .consistencyLevel );
331
389
ResultSet result = session .execute (select );
390
+
332
391
return (int ) result .one ().getLong ("count" );
333
392
}
334
393
@@ -344,6 +403,7 @@ private int calculateVersionRank(MigrationVersion version) {
344
403
.column ("version" )
345
404
.column ("version_rank" )
346
405
.from (keyspace .getName (), tableName );
406
+
347
407
statement .setConsistencyLevel (this .consistencyLevel );
348
408
ResultSet versionRows = session .execute (statement );
349
409
@@ -362,7 +422,8 @@ private int calculateVersionRank(MigrationVersion version) {
362
422
PreparedStatement preparedStatement = cachePs .prepare (
363
423
"UPDATE " + keyspace .getName () + "." + tableName +
364
424
" SET version_rank = ?" +
365
- " WHERE version = ?;" );
425
+ " WHERE version = ?" +
426
+ ";" );
366
427
367
428
for (int i = 0 ; i < migrationVersions .size (); i ++) {
368
429
if (version .compareTo (migrationVersions .get (i )) < 0 ) {
@@ -381,6 +442,9 @@ private int calculateVersionRank(MigrationVersion version) {
381
442
return migrationVersions .size () + 1 ;
382
443
}
383
444
445
+ /**
446
+ * Schema migration (transient) metadata.
447
+ */
384
448
class MigrationMetaHolder {
385
449
private int versionRank ;
386
450
0 commit comments