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);
}
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);
}
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());
}
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));
}
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));
}
Aggregations