use of com.google.spanner.admin.database.v1.UpdateDatabaseDdlRequest in project pgadapter by GoogleCloudPlatform.
the class DdlTransactionModeAutocommitImplicitTest method testSelectAndDdlInBatch.
@Test
public void testSelectAndDdlInBatch() throws SQLException {
String sql = "SELECT 1; SELECT 2; CREATE TABLE foo (id bigint primary key);";
addDdlResponseToSpannerAdmin();
try (Connection connection = DriverManager.getConnection(createUrl())) {
try (Statement statement = connection.createStatement()) {
assertTrue(statement.execute(sql));
try (ResultSet resultSet = statement.getResultSet()) {
assertTrue(resultSet.next());
assertEquals(1L, resultSet.getLong(1));
assertFalse(resultSet.next());
}
assertTrue(statement.getMoreResults());
try (ResultSet resultSet = statement.getResultSet()) {
assertTrue(resultSet.next());
assertEquals(2L, resultSet.getLong(1));
assertFalse(resultSet.next());
}
assertFalse(statement.getMoreResults());
assertEquals(0, statement.getUpdateCount());
// getMoreResults() should now return false. We should also check getUpdateCount() as that
// method should return -1 to indicate that there is also no update count available.
assertFalse(statement.getMoreResults());
assertEquals(-1, statement.getUpdateCount());
}
}
List<UpdateDatabaseDdlRequest> updateDatabaseDdlRequests = mockDatabaseAdmin.getRequests().stream().filter(request -> request instanceof UpdateDatabaseDdlRequest).map(UpdateDatabaseDdlRequest.class::cast).collect(Collectors.toList());
assertEquals(1, updateDatabaseDdlRequests.size());
assertEquals(1, updateDatabaseDdlRequests.get(0).getStatementsCount());
assertEquals("CREATE TABLE foo (id bigint primary key)", updateDatabaseDdlRequests.get(0).getStatements(0));
// The first SELECT statement should start a read-only implicit transaction.
assertEquals(1, mockSpanner.getRequestsOfType(BeginTransactionRequest.class).size());
BeginTransactionRequest beginRequest = mockSpanner.getRequestsOfType(BeginTransactionRequest.class).get(0);
assertTrue(beginRequest.getOptions().hasReadOnly());
List<ExecuteSqlRequest> requests = mockSpanner.getRequestsOfType(ExecuteSqlRequest.class);
assertEquals(2, requests.size());
assertEquals(SELECT1.getSql(), requests.get(0).getSql());
assertTrue(requests.get(0).getTransaction().hasId());
assertEquals(SELECT2.getSql(), requests.get(1).getSql());
assertTrue(requests.get(1).getTransaction().hasId());
}
use of com.google.spanner.admin.database.v1.UpdateDatabaseDdlRequest in project pgadapter by GoogleCloudPlatform.
the class DdlTransactionModeAutocommitImplicitTest method testDdlAndSelectInBatch.
@Test
public void testDdlAndSelectInBatch() throws SQLException {
String sql = "CREATE TABLE foo (id bigint primary key); SELECT 1; SELECT 2;";
addDdlResponseToSpannerAdmin();
try (Connection connection = DriverManager.getConnection(createUrl())) {
try (Statement statement = connection.createStatement()) {
// Statement#execute(String) returns false if the result was either an update count or there
// was no result. Statement#getUpdateCount() returns 0 if there was no result.
assertFalse(statement.execute(sql));
assertEquals(0, statement.getUpdateCount());
assertTrue(statement.getMoreResults());
try (ResultSet resultSet = statement.getResultSet()) {
assertTrue(resultSet.next());
assertEquals(1L, resultSet.getLong(1));
assertFalse(resultSet.next());
}
assertTrue(statement.getMoreResults());
try (ResultSet resultSet = statement.getResultSet()) {
assertTrue(resultSet.next());
assertEquals(2L, resultSet.getLong(1));
assertFalse(resultSet.next());
}
// getMoreResults() should now return false. We should also check getUpdateCount() as that
// method should return -1 to indicate that there is also no update count available.
assertFalse(statement.getMoreResults());
assertEquals(-1, statement.getUpdateCount());
}
}
List<UpdateDatabaseDdlRequest> updateDatabaseDdlRequests = mockDatabaseAdmin.getRequests().stream().filter(request -> request instanceof UpdateDatabaseDdlRequest).map(UpdateDatabaseDdlRequest.class::cast).collect(Collectors.toList());
assertEquals(1, updateDatabaseDdlRequests.size());
assertEquals(1, updateDatabaseDdlRequests.get(0).getStatementsCount());
assertEquals("CREATE TABLE foo (id bigint primary key)", updateDatabaseDdlRequests.get(0).getStatements(0));
List<ExecuteSqlRequest> requests = mockSpanner.getRequestsOfType(ExecuteSqlRequest.class);
assertEquals(2, requests.size());
assertEquals(SELECT1.getSql(), requests.get(0).getSql());
// The first statement after the DDL statement should start an implicit transaction.
// That transaction will be a read-only transaction, and the begin of those are not inlined
// with the first statement.
assertTrue(requests.get(0).getTransaction().hasId());
assertEquals(SELECT2.getSql(), requests.get(1).getSql());
assertTrue(requests.get(1).getTransaction().hasId());
assertEquals(1, mockSpanner.getRequestsOfType(BeginTransactionRequest.class).size());
BeginTransactionRequest beginRequest = mockSpanner.getRequestsOfType(BeginTransactionRequest.class).get(0);
assertTrue(beginRequest.getOptions().hasReadOnly());
}
use of com.google.spanner.admin.database.v1.UpdateDatabaseDdlRequest in project pgadapter by GoogleCloudPlatform.
the class DdlTransactionModeNoneTest method testSingleDdlStatement.
@Test
public void testSingleDdlStatement() throws SQLException {
String sql = "CREATE TABLE foo (id bigint primary key)";
addDdlResponseToSpannerAdmin();
try (Connection connection = DriverManager.getConnection(createUrl())) {
try (Statement statement = connection.createStatement()) {
assertFalse(statement.execute(sql));
assertEquals(0, statement.getUpdateCount());
assertFalse(statement.getMoreResults());
assertEquals(-1, statement.getUpdateCount());
}
}
List<UpdateDatabaseDdlRequest> updateDatabaseDdlRequests = mockDatabaseAdmin.getRequests().stream().filter(request -> request instanceof UpdateDatabaseDdlRequest).map(UpdateDatabaseDdlRequest.class::cast).collect(Collectors.toList());
assertEquals(1, updateDatabaseDdlRequests.size());
assertEquals(1, updateDatabaseDdlRequests.get(0).getStatementsCount());
assertEquals("CREATE TABLE foo (id bigint primary key)", updateDatabaseDdlRequests.get(0).getStatements(0));
}
use of com.google.spanner.admin.database.v1.UpdateDatabaseDdlRequest in project java-spanner by googleapis.
the class MockDatabaseAdminServiceImpl method updateDatabaseDdl.
@Override
public void updateDatabaseDdl(UpdateDatabaseDdlRequest request, StreamObserver<Operation> responseObserver) {
requests.add(request);
MockDatabase db = databases.get(request.getDatabase());
if (db != null) {
db.ddl.addAll(request.getStatementsList());
UpdateDatabaseDdlMetadata metadata = UpdateDatabaseDdlMetadata.newBuilder().setDatabase(request.getDatabase()).addAllStatements(request.getStatementsList()).build();
Operation operation = Operation.newBuilder().setMetadata(Any.pack(metadata)).setResponse(Any.pack(Empty.getDefaultInstance())).setDone(true).setName(operations.generateOperationName(request.getDatabase())).build();
operations.addOperation(operation, new UpdateDatabaseDdlCallable(operation.getName()));
responseObserver.onNext(operation);
responseObserver.onCompleted();
} else {
responseObserver.onError(Status.NOT_FOUND.asRuntimeException());
}
}
use of com.google.spanner.admin.database.v1.UpdateDatabaseDdlRequest in project liquibase-spanner by cloudspannerecosystem.
the class MergeColumnsTest method testMergeSingersFirstNamdAndLastNameFromYaml.
@Test
void testMergeSingersFirstNamdAndLastNameFromYaml() throws Exception {
String[] expectedSql = new String[] { "ALTER TABLE Singers ADD COLUMN FullName STRING(500)", "ALTER TABLE Singers DROP COLUMN FirstName", "ALTER TABLE Singers DROP COLUMN LastName" };
for (String sql : expectedSql) {
addUpdateDdlStatementsResponse(sql);
}
for (String file : new String[] { "merge-singers-firstname-and-lastname.spanner.yaml" }) {
try (Connection con = createConnection();
Liquibase liquibase = getLiquibase(con, file)) {
liquibase.update(new Contexts("test"));
}
}
assertThat(mockAdmin.getRequests()).hasSize(expectedSql.length);
for (int i = 0; i < expectedSql.length; i++) {
assertThat(mockAdmin.getRequests().get(i)).isInstanceOf(UpdateDatabaseDdlRequest.class);
UpdateDatabaseDdlRequest request = (UpdateDatabaseDdlRequest) mockAdmin.getRequests().get(i);
assertThat(request.getStatementsList()).hasSize(1);
assertThat(request.getStatementsList().get(0)).isEqualTo(expectedSql[i]);
}
// Verify that the mock server received one BeginTransactionRequest for a PDML transaction.
assertThat(mockSpanner.getRequests().stream().filter(new Predicate<AbstractMessage>() {
@Override
public boolean test(AbstractMessage t) {
return t instanceof BeginTransactionRequest;
}
}).map(new Function<AbstractMessage, BeginTransactionRequest>() {
@Override
public BeginTransactionRequest apply(AbstractMessage t) {
return (BeginTransactionRequest) t;
}
}).filter(new Predicate<BeginTransactionRequest>() {
@Override
public boolean test(BeginTransactionRequest t) {
return t.hasOptions() && t.getOptions().hasPartitionedDml();
}
}).count()).isEqualTo(1L);
}
Aggregations