Search in sources :

Example 21 with UpdateDatabaseDdlRequest

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());
}
Also used : ExecuteSqlRequest(com.google.spanner.v1.ExecuteSqlRequest) IntermediateStatement(com.google.cloud.spanner.pgadapter.statements.IntermediateStatement) Statement(java.sql.Statement) BeginTransactionRequest(com.google.spanner.v1.BeginTransactionRequest) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) UpdateDatabaseDdlRequest(com.google.spanner.admin.database.v1.UpdateDatabaseDdlRequest) Test(org.junit.Test)

Example 22 with UpdateDatabaseDdlRequest

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());
}
Also used : ExecuteSqlRequest(com.google.spanner.v1.ExecuteSqlRequest) IntermediateStatement(com.google.cloud.spanner.pgadapter.statements.IntermediateStatement) Statement(java.sql.Statement) BeginTransactionRequest(com.google.spanner.v1.BeginTransactionRequest) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) UpdateDatabaseDdlRequest(com.google.spanner.admin.database.v1.UpdateDatabaseDdlRequest) Test(org.junit.Test)

Example 23 with UpdateDatabaseDdlRequest

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));
}
Also used : Statement(java.sql.Statement) Connection(java.sql.Connection) UpdateDatabaseDdlRequest(com.google.spanner.admin.database.v1.UpdateDatabaseDdlRequest) Test(org.junit.Test)

Example 24 with UpdateDatabaseDdlRequest

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());
    }
}
Also used : UpdateDatabaseDdlMetadata(com.google.spanner.admin.database.v1.UpdateDatabaseDdlMetadata) Operation(com.google.longrunning.Operation)

Example 25 with UpdateDatabaseDdlRequest

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);
}
Also used : Liquibase(liquibase.Liquibase) AbstractMessage(com.google.protobuf.AbstractMessage) BeginTransactionRequest(com.google.spanner.v1.BeginTransactionRequest) Connection(java.sql.Connection) Contexts(liquibase.Contexts) UpdateDatabaseDdlRequest(com.google.spanner.admin.database.v1.UpdateDatabaseDdlRequest) Predicate(java.util.function.Predicate) Test(org.junit.jupiter.api.Test)

Aggregations

UpdateDatabaseDdlRequest (com.google.spanner.admin.database.v1.UpdateDatabaseDdlRequest)39 Connection (java.sql.Connection)35 Contexts (liquibase.Contexts)29 Liquibase (liquibase.Liquibase)29 Test (org.junit.jupiter.api.Test)29 Test (org.junit.Test)9 Statement (java.sql.Statement)5 AbstractMessage (com.google.protobuf.AbstractMessage)4 Empty (com.google.protobuf.Empty)4 BeginTransactionRequest (com.google.spanner.v1.BeginTransactionRequest)4 ExecuteSqlRequest (com.google.spanner.v1.ExecuteSqlRequest)4 IntermediateStatement (com.google.cloud.spanner.pgadapter.statements.IntermediateStatement)3 Operation (com.google.longrunning.Operation)3 UpdateDatabaseDdlMetadata (com.google.spanner.admin.database.v1.UpdateDatabaseDdlMetadata)3 ResultSet (java.sql.ResultSet)3 LabelExpression (liquibase.LabelExpression)3 AbstractMockServerTest (liquibase.ext.spanner.AbstractMockServerTest)3 SpannerException (com.google.cloud.spanner.SpannerException)2 ByteString (com.google.protobuf.ByteString)2 CommitRequest (com.google.spanner.v1.CommitRequest)2