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

Commit 8c93c7f

Browse files
committed
Added some comments and tidy-up SchemaVersionDAO class
1 parent 712fe98 commit 8c93c7f

File tree

1 file changed

+79
-15
lines changed

1 file changed

+79
-15
lines changed

src/main/java/com/builtamont/cassandra/migration/internal/dbsupport/SchemaVersionDAO.java

Lines changed: 79 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@
3434

3535
import static com.datastax.driver.core.querybuilder.QueryBuilder.eq;
3636

37+
/**
38+
* Schema migrations table Data Access Object.
39+
*/
3740
// TODO: Convert to Kotlin code... Some challenges with Mockito mocking :)
3841
public class SchemaVersionDAO {
3942

@@ -46,14 +49,22 @@ public class SchemaVersionDAO {
4649
private CachePrepareStatement cachePs;
4750
private ConsistencyLevel consistencyLevel;
4851

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+
*/
4959
public SchemaVersionDAO(Session session, Keyspace keyspace, String tableName) {
5060
this.session = session;
5161
this.keyspace = keyspace;
5262
this.tableName = tableName;
5363
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;
5768
}
5869

5970
public String getTableName() {
@@ -64,6 +75,9 @@ public Keyspace getKeyspace() {
6475
return this.keyspace;
6576
}
6677

78+
/**
79+
* Create schema migration version table if it does not exists.
80+
*/
6781
public void createTablesIfNotExist() {
6882
if (tablesExist()) {
6983
return;
@@ -97,6 +111,11 @@ public void createTablesIfNotExist() {
97111
session.execute(statement);
98112
}
99113

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+
*/
100119
public boolean tablesExist() {
101120
boolean schemaVersionTableExists = false;
102121
boolean schemaVersionCountsTableExists = false;
@@ -135,18 +154,29 @@ public boolean tablesExist() {
135154
return schemaVersionTableExists && schemaVersionCountsTableExists;
136155
}
137156

157+
/**
158+
* Add applied migration record into the schema migration version table.
159+
*
160+
* @param appliedMigration The applied migration.
161+
*/
138162
public void addAppliedMigration(AppliedMigration appliedMigration) {
139163
createTablesIfNotExist();
140164

141165
MigrationVersion version = appliedMigration.getVersion();
142166

143167
int versionRank = calculateVersionRank(version);
144168
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+
");"
150180
);
151181

152182
statement.setConsistencyLevel(this.consistencyLevel);
@@ -166,7 +196,7 @@ public void addAppliedMigration(AppliedMigration appliedMigration) {
166196
}
167197

168198
/**
169-
* Retrieve the applied migrations from the metadata table.
199+
* Retrieve the applied migrations from the schema migration version table.
170200
*
171201
* @return The applied migrations.
172202
*/
@@ -209,8 +239,8 @@ public List<AppliedMigration> findAppliedMigrations() {
209239
));
210240
}
211241

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
214244
return resultsList;
215245
}
216246

@@ -263,11 +293,16 @@ public List<AppliedMigration> findAppliedMigrations(MigrationType... migrationTy
263293
}
264294
}
265295

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
268298
return resultsList;
269299
}
270300

301+
/**
302+
* Check if the keyspace has applied migrations.
303+
*
304+
* @return {@code true} if the keyspace has applied migrations.
305+
*/
271306
public boolean hasAppliedMigrations() {
272307
if (!tablesExist()) {
273308
return false;
@@ -284,6 +319,13 @@ public boolean hasAppliedMigrations() {
284319
return !filteredMigrations.isEmpty();
285320
}
286321

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+
*/
287329
public void addBaselineMarker(final MigrationVersion baselineVersion, final String baselineDescription, final String user) {
288330
addAppliedMigration(
289331
new AppliedMigration(
@@ -299,16 +341,28 @@ public void addBaselineMarker(final MigrationVersion baselineVersion, final Stri
299341
);
300342
}
301343

344+
/**
345+
* Get the baseline marker's applied migration.
346+
*
347+
* @return The baseline marker's applied migration.
348+
*/
302349
public AppliedMigration getBaselineMarker() {
303350
List<AppliedMigration> appliedMigrations = findAppliedMigrations(MigrationType.BASELINE);
304351
return appliedMigrations.isEmpty() ? null : appliedMigrations.get(0);
305352
}
306353

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+
*/
307359
public boolean hasBaselineMarker() {
308360
if (!tablesExist()) {
309361
return false;
310362
}
363+
311364
createTablesIfNotExist();
365+
312366
return !findAppliedMigrations(MigrationType.BASELINE).isEmpty();
313367
}
314368

@@ -321,14 +375,19 @@ private int calculateInstalledRank() {
321375
Statement statement = new SimpleStatement(
322376
"UPDATE " + keyspace.getName() + "." + tableName + COUNTS_TABLE_NAME_SUFFIX +
323377
" SET count = count + 1" +
324-
"WHERE name = 'installed_rank';");
378+
" WHERE name = 'installed_rank'" +
379+
";");
380+
325381
session.execute(statement);
382+
326383
Select select = QueryBuilder
327384
.select("count")
328385
.from(tableName + COUNTS_TABLE_NAME_SUFFIX);
329386
select.where(eq("name", "installed_rank"));
387+
330388
select.setConsistencyLevel(this.consistencyLevel);
331389
ResultSet result = session.execute(select);
390+
332391
return (int) result.one().getLong("count");
333392
}
334393

@@ -344,6 +403,7 @@ private int calculateVersionRank(MigrationVersion version) {
344403
.column("version")
345404
.column("version_rank")
346405
.from(keyspace.getName(), tableName);
406+
347407
statement.setConsistencyLevel(this.consistencyLevel);
348408
ResultSet versionRows = session.execute(statement);
349409

@@ -362,7 +422,8 @@ private int calculateVersionRank(MigrationVersion version) {
362422
PreparedStatement preparedStatement = cachePs.prepare(
363423
"UPDATE " + keyspace.getName() + "." + tableName +
364424
" SET version_rank = ?" +
365-
" WHERE version = ?;");
425+
" WHERE version = ?" +
426+
";");
366427

367428
for (int i = 0; i < migrationVersions.size(); i++) {
368429
if (version.compareTo(migrationVersions.get(i)) < 0) {
@@ -381,6 +442,9 @@ private int calculateVersionRank(MigrationVersion version) {
381442
return migrationVersions.size() + 1;
382443
}
383444

445+
/**
446+
* Schema migration (transient) metadata.
447+
*/
384448
class MigrationMetaHolder {
385449
private int versionRank;
386450

0 commit comments

Comments
 (0)