@Test public void runMigration_shouldApplyMigrationAndLog() throws Exception { ResultSet migrationResultSet = mock(ResultSet.class); when(session.execute(anyString())).thenReturn(migrationResultSet); when(migrationResultSet.wasApplied()).thenReturn(true); client.runMigration(new Migration("001_initial_migration.cql", "create table foo (id uuid PRIMARY KEY)")); verify(session, times(1)).execute("create table foo (id uuid PRIMARY KEY)"); ArgumentCaptor<BuiltStatement> captor = ArgumentCaptor.forClass(BuiltStatement.class); verify(session, times(2)).execute(captor.capture()); assertThat(captor.getAllValues()) .hasSize(2) .extracting(BuiltStatement::toString) .has(containsSubstr("INSERT INTO test.migrations"), atIndex(0)) .has(containsSubstr("'001_initial_migration.cql',dateOf(now()),'APPLYING','create table foo (id uuid PRIMARY KEY)')"), atIndex(0)) .has(containsSubstr("UPDATE test.migrations SET status='APPLIED'"), atIndex(1)) .has(containsSubstr("IF status IN ('APPLYING','FAILED')"), atIndex(1)); }
@Test public void runMigration_shouldOverrideBrokenMigrations() throws Exception { ResultSet migrationResultSet = mock(ResultSet.class); when(session.execute(anyString())).thenReturn(migrationResultSet); when(migrationResultSet.wasApplied()).thenReturn(true); client.runMigration(new Migration("001_initial_migration.cql", "create table foo (id uuid PRIMARY KEY)")); verify(session, times(1)).execute("create table foo (id uuid PRIMARY KEY)"); ArgumentCaptor<BuiltStatement> captor = ArgumentCaptor.forClass(BuiltStatement.class); verify(session, times(2)).execute(captor.capture()); assertThat(captor.getAllValues()) .hasSize(2) .extracting(BuiltStatement::toString) .has(containsSubstr("INSERT INTO test.migrations"), atIndex(0)) .has(containsSubstr("IF NOT EXISTS"), atIndex(0)) .has(containsSubstr("UPDATE test.migrations SET status='APPLIED'"), atIndex(1)) .has(containsSubstr("IF status IN ('APPLYING','FAILED')"), atIndex(1)); }
@Test public void runMigration_shouldStopOnFailure() throws Exception { String invalidMigrationStatement = "invalid migration"; when(session.execute(invalidMigrationStatement)).thenThrow(new InvalidQueryException("unit test")); try { client.runMigration(new Migration("001_initial_migration.cql", invalidMigrationStatement)); fail("Excepted exception"); } catch (InvalidQueryException ignored) { } verify(session, times(1)).execute(invalidMigrationStatement); ArgumentCaptor<BuiltStatement> captor = ArgumentCaptor.forClass(BuiltStatement.class); verify(session, times(2)).execute(captor.capture()); assertThat(captor.getAllValues()) .hasSize(2) .extracting(BuiltStatement::toString) .has(containsSubstr("UPDATE test.migrations SET status='FAILED'"), atIndex(1)) .has(containsSubstr("reason='unit test'"), atIndex(1)) .has(containsSubstr("IF status IN ('APPLYING','FAILED')"), atIndex(1)); }
public List<Row> get() { Select.Where where = QueryBuilder.select().from(parent.getKeyspace(), parent.getTable()).where(); for (Clause clause : clauses) { where.and(clause); } if (searchBuilder != null) { where.and(QueryBuilder.eq(parent.getIndexColumn(), searchBuilder.refresh(refresh).toJson())); } BuiltStatement statement = limit == null ? where : where.limit(limit); String query = statement.toString(); query = query.substring(0, query.length() - 1); StringBuilder sb = new StringBuilder(query); for (String extra : extras) { sb.append(" "); sb.append(extra); sb.append(" "); } return parent.execute(sb, fetchSize); }
private static String processKeys(String[] columnNames, BuiltStatement delete) { BuiltStatement query = null; boolean isWhereNeeded = true; for (String columnName : columnNames) { if (isWhereNeeded) { if (delete instanceof Delete) { query = ((Delete) delete).where(QueryBuilder.eq(columnName, "?")); } else { query = ((Select) delete).where(QueryBuilder.eq(columnName, "?")); } isWhereNeeded = false; } else { if (delete instanceof Delete) { query = ((Delete.Where) query).and(QueryBuilder.eq(columnName, "?")); } else { query = ((Select.Where) query).and(QueryBuilder.eq(columnName, "?")); } } } return query != null ? query.getQueryString() : null; }
@Test public void releaseLock_shouldDeleteLease() throws Exception { client.releaseLock(); ArgumentCaptor<BuiltStatement> captor = ArgumentCaptor.forClass(BuiltStatement.class); verify(session, times(1)).execute(captor.capture()); assertThat(captor.getAllValues()) .hasSize(1) .extracting(BuiltStatement::toString) .containsOnly("DELETE FROM test.leases WHERE name='migration' IF owner='unit-test-runner';"); }
/** * @param statement the statement to prepare * @return the prepared statement future */ ListenableFuture<PreparedStatement> prepareAsync(final BuiltStatement statement) { return preparedStatementCache.prepareAsync(statement); }