Search in sources :

Example 16 with UpdateDatabaseDdlRequest

use of com.google.spanner.admin.database.v1.UpdateDatabaseDdlRequest in project liquibase-spanner by cloudspannerecosystem.

the class CreateDropViewTest method testCreateViewFromYaml.

@Test
void testCreateViewFromYaml() throws Exception {
    String expectedSql = "CREATE VIEW V_Singers SQL SECURITY INVOKER AS SELECT * FROM Singers WHERE SingerId > 10";
    addUpdateDdlStatementsResponse(expectedSql);
    for (String file : new String[] { "create-view.spanner.yaml" }) {
        try (Connection con = createConnection();
            Liquibase liquibase = getLiquibase(con, file)) {
            liquibase.update(new Contexts("test"));
        }
    }
    assertThat(mockAdmin.getRequests()).hasSize(1);
    assertThat(mockAdmin.getRequests().get(0)).isInstanceOf(UpdateDatabaseDdlRequest.class);
    UpdateDatabaseDdlRequest request = (UpdateDatabaseDdlRequest) mockAdmin.getRequests().get(0);
    assertThat(request.getStatementsList()).hasSize(1);
    assertThat(request.getStatementsList().get(0)).isEqualTo(expectedSql);
}
Also used : Liquibase(liquibase.Liquibase) Connection(java.sql.Connection) Contexts(liquibase.Contexts) UpdateDatabaseDdlRequest(com.google.spanner.admin.database.v1.UpdateDatabaseDdlRequest) Test(org.junit.jupiter.api.Test) AbstractMockServerTest(liquibase.ext.spanner.AbstractMockServerTest)

Example 17 with UpdateDatabaseDdlRequest

use of com.google.spanner.admin.database.v1.UpdateDatabaseDdlRequest in project liquibase-spanner by cloudspannerecosystem.

the class CreateDropViewTest method testCreateOrReplaceViewFromYaml.

@Test
void testCreateOrReplaceViewFromYaml() throws Exception {
    String expectedSql = "CREATE OR REPLACE VIEW V_Singers SQL SECURITY INVOKER AS SELECT s.SingerId AS SingerId, s.FirstName AS FirstName, s.LastName AS LastName FROM Singers s ORDER BY s.LastName, s.FirstName, s.SingerId LIMIT 2";
    addUpdateDdlStatementsResponse(expectedSql);
    for (String file : new String[] { "create-or-replace-view.spanner.yaml" }) {
        try (Connection con = createConnection();
            Liquibase liquibase = getLiquibase(con, file)) {
            liquibase.update(new Contexts("test"));
        }
    }
    assertThat(mockAdmin.getRequests()).hasSize(1);
    assertThat(mockAdmin.getRequests().get(0)).isInstanceOf(UpdateDatabaseDdlRequest.class);
    UpdateDatabaseDdlRequest request = (UpdateDatabaseDdlRequest) mockAdmin.getRequests().get(0);
    assertThat(request.getStatementsList()).hasSize(1);
    assertThat(request.getStatementsList().get(0)).isEqualTo(expectedSql);
}
Also used : Liquibase(liquibase.Liquibase) Connection(java.sql.Connection) Contexts(liquibase.Contexts) UpdateDatabaseDdlRequest(com.google.spanner.admin.database.v1.UpdateDatabaseDdlRequest) Test(org.junit.jupiter.api.Test) AbstractMockServerTest(liquibase.ext.spanner.AbstractMockServerTest)

Example 18 with UpdateDatabaseDdlRequest

use of com.google.spanner.admin.database.v1.UpdateDatabaseDdlRequest in project pgadapter by GoogleCloudPlatform.

the class DdlTransactionModeAutocommitImplicitTest method testMixedBatch.

@Test
public void testMixedBatch() throws SQLException {
    String sql = "CREATE TABLE foo (id bigint primary key); INSERT INTO FOO VALUES (1); UPDATE FOO SET BAR=1 WHERE BAZ=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());
            // getMoreResults() returns false as the next result is an update count.
            assertFalse(statement.getMoreResults());
            assertEquals(1, statement.getUpdateCount());
            // getMoreResults() returns false as the next result is an update count.
            assertFalse(statement.getMoreResults());
            assertEquals(2, 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));
    // Verify that the DML statements were batched together by PgAdapter.
    List<ExecuteBatchDmlRequest> requests = mockSpanner.getRequestsOfType(ExecuteBatchDmlRequest.class);
    assertEquals(1, requests.size());
    ExecuteBatchDmlRequest request = requests.get(0);
    assertEquals(2, request.getStatementsCount());
    assertEquals(INSERT_STATEMENT.getSql(), request.getStatements(0).getSql());
    assertEquals(UPDATE_STATEMENT.getSql(), request.getStatements(1).getSql());
    List<CommitRequest> commitRequests = mockSpanner.getRequestsOfType(CommitRequest.class);
    assertEquals(1, commitRequests.size());
}
Also used : CommitRequest(com.google.spanner.v1.CommitRequest) IntermediateStatement(com.google.cloud.spanner.pgadapter.statements.IntermediateStatement) Statement(java.sql.Statement) Connection(java.sql.Connection) UpdateDatabaseDdlRequest(com.google.spanner.admin.database.v1.UpdateDatabaseDdlRequest) ExecuteBatchDmlRequest(com.google.spanner.v1.ExecuteBatchDmlRequest) Test(org.junit.Test)

Example 19 with UpdateDatabaseDdlRequest

use of com.google.spanner.admin.database.v1.UpdateDatabaseDdlRequest in project pgadapter by GoogleCloudPlatform.

the class JdbcMockServerTest method testDdl.

@Test
public void testDdl() throws SQLException {
    String sql = "CREATE TABLE foo (id bigint primary key)";
    addDdlResponseToSpannerAdmin();
    try (Connection connection = DriverManager.getConnection(createUrl())) {
        try (java.sql.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());
        }
    }
    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(sql, updateDatabaseDdlRequests.get(0).getStatements(0));
}
Also used : Connection(java.sql.Connection) UpdateDatabaseDdlRequest(com.google.spanner.admin.database.v1.UpdateDatabaseDdlRequest) Test(org.junit.Test)

Example 20 with UpdateDatabaseDdlRequest

use of com.google.spanner.admin.database.v1.UpdateDatabaseDdlRequest in project pgadapter by GoogleCloudPlatform.

the class JdbcSimpleModeMockServerTest method testTwoDdlStatements.

@Test
public void testTwoDdlStatements() throws SQLException {
    addDdlResponseToSpannerAdmin();
    addDdlResponseToSpannerAdmin();
    try (Connection connection = DriverManager.getConnection(createUrl())) {
        try (java.sql.Statement statement = connection.createStatement()) {
            // execute returns false if the result is an update count or no result.
            assertFalse(statement.execute(String.format("%s; %s;", "CREATE TABLE FOO (id bigint primary key)", "CREATE TABLE BAR (id bigint primary key)")));
            assertEquals(0, statement.getUpdateCount());
            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());
        }
    }
    // Verify that the two DDL statements are sent as one batch.
    List<UpdateDatabaseDdlRequest> requests = mockDatabaseAdmin.getRequests().stream().filter(r -> r instanceof UpdateDatabaseDdlRequest).map(r -> (UpdateDatabaseDdlRequest) r).collect(Collectors.toList());
    assertEquals(1, requests.size());
    UpdateDatabaseDdlRequest request = requests.get(0);
    assertEquals(2, request.getStatementsCount());
    assertEquals("CREATE TABLE FOO (id bigint primary key)", request.getStatements(0));
    assertEquals("CREATE TABLE BAR (id bigint primary key)", request.getStatements(1));
}
Also used : OptionsMetadata(com.google.cloud.spanner.pgadapter.metadata.OptionsMetadata) Connection(java.sql.Connection) BeforeClass(org.junit.BeforeClass) Dialect(com.google.cloud.spanner.Dialect) Assert.assertThrows(org.junit.Assert.assertThrows) LocalDateTime(java.time.LocalDateTime) RunWith(org.junit.runner.RunWith) Parameters(org.junit.runners.Parameterized.Parameters) UpdateDatabaseDdlRequest(com.google.spanner.admin.database.v1.UpdateDatabaseDdlRequest) StatementResult(com.google.cloud.spanner.MockSpannerServiceImpl.StatementResult) CommitRequest(com.google.spanner.v1.CommitRequest) BigDecimal(java.math.BigDecimal) SQLException(java.sql.SQLException) ResultSet(java.sql.ResultSet) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) ZoneOffset(java.time.ZoneOffset) TimestampUtils(org.postgresql.jdbc.TimestampUtils) BeginTransactionRequest(com.google.spanner.v1.BeginTransactionRequest) Parameterized(org.junit.runners.Parameterized) TRANSACTION_ABORTED_ERROR(com.google.cloud.spanner.pgadapter.statements.IntermediateStatement.TRANSACTION_ABORTED_ERROR) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) TimeZone(java.util.TimeZone) Parameter(org.junit.runners.Parameterized.Parameter) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) PreparedStatement(java.sql.PreparedStatement) Instant(java.time.Instant) Collectors(java.util.stream.Collectors) RollbackRequest(com.google.spanner.v1.RollbackRequest) SpannerException(com.google.cloud.spanner.SpannerException) List(java.util.List) OffsetDateTime(java.time.OffsetDateTime) Ignore(org.junit.Ignore) Assert.assertFalse(org.junit.Assert.assertFalse) ExecuteSqlRequest(com.google.spanner.v1.ExecuteSqlRequest) Statement(java.sql.Statement) QueryMode(com.google.spanner.v1.ExecuteSqlRequest.QueryMode) ExecuteBatchDmlRequest(com.google.spanner.v1.ExecuteBatchDmlRequest) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) DriverManager(java.sql.DriverManager) Connection(java.sql.Connection) Statement(java.sql.Statement) UpdateDatabaseDdlRequest(com.google.spanner.admin.database.v1.UpdateDatabaseDdlRequest) Test(org.junit.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