ahmedabu98 commented on code in PR #36034:
URL: https://github.com/apache/beam/pull/36034#discussion_r2323477097


##########
sdks/java/io/jdbc/src/main/java/org/apache/beam/sdk/io/jdbc/providers/WriteToPostgresSchemaTransformProvider.java:
##########
@@ -43,4 +53,31 @@ public String description() {
   protected String jdbcType() {
     return POSTGRES;
   }
+
+  @Override
+  public @UnknownKeyFor @NonNull @Initialized SchemaTransform from(
+      JdbcWriteSchemaTransformConfiguration configuration) {
+    String jdbcType = configuration.getJdbcType();
+    if (jdbcType != null && !jdbcType.equals(jdbcType())) {
+      throw new IllegalArgumentException(
+          String.format("Wrong JDBC type. Expected '%s' but got '%s'", 
jdbcType(), jdbcType));
+    }
+
+    List<@org.checkerframework.checker.nullness.qual.Nullable String> 
connectionInitSql =
+        configuration.getConnectionInitSql();
+    if (connectionInitSql != null && !connectionInitSql.isEmpty()) {
+      LOG.warn("Postgres does not support connectionInitSql, ignoring.");
+    }
+
+    // Override "connectionInitSql" for postgres
+    configuration = 
configuration.toBuilder().setConnectionInitSql(Collections.emptyList()).build();
+    return new PostgresWriteSchemaTransform(configuration);
+  }
+
+  public static class PostgresWriteSchemaTransform extends 
JdbcWriteSchemaTransform {
+    public PostgresWriteSchemaTransform(JdbcWriteSchemaTransformConfiguration 
config) {
+      super(config, POSTGRES);
+      config.validate(POSTGRES);

Review Comment:
   nit: config.validate(str) should go inside 
`JdbcWriteSchemaTransformConfiguration`



##########
sdks/java/io/jdbc/src/test/java/org/apache/beam/sdk/io/jdbc/JdbcIOPostgresIT.java:
##########
@@ -0,0 +1,178 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.beam.sdk.io.jdbc;
+
+import static 
org.apache.beam.sdk.io.common.IOITHelper.readIOTestPipelineOptions;
+
+import java.sql.SQLException;
+import java.util.Arrays;
+import java.util.List;
+import java.util.Map;
+import org.apache.beam.sdk.io.common.DatabaseTestHelper;
+import org.apache.beam.sdk.io.common.PostgresIOTestPipelineOptions;
+import 
org.apache.beam.sdk.io.jdbc.providers.ReadFromPostgresSchemaTransformProvider;
+import 
org.apache.beam.sdk.io.jdbc.providers.WriteToPostgresSchemaTransformProvider;
+import org.apache.beam.sdk.managed.Managed;
+import org.apache.beam.sdk.schemas.Schema;
+import org.apache.beam.sdk.testing.PAssert;
+import org.apache.beam.sdk.testing.TestPipeline;
+import org.apache.beam.sdk.transforms.Create;
+import org.apache.beam.sdk.values.PCollection;
+import org.apache.beam.sdk.values.PCollectionRowTuple;
+import org.apache.beam.sdk.values.Row;
+import 
org.apache.beam.vendor.guava.v32_1_2_jre.com.google.common.collect.ImmutableMap;
+import org.junit.BeforeClass;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.JUnit4;
+import org.postgresql.ds.PGSimpleDataSource;
+
+/**
+ * A test of {@link org.apache.beam.sdk.io.jdbc.JdbcIO} on an independent 
Postgres instance.
+ *
+ * <p>Similar to JdbcIOIT, this test requires a running instance of Postgres. 
Pass in connection
+ * information using PipelineOptions:
+ *
+ * <pre>
+ *  ./gradlew integrationTest -p sdks/java/io/jdbc 
-DintegrationTestPipelineOptions='[
+ *  "--postgresServerName=1.2.3.4",
+ *  "--postgresUsername=postgres",
+ *  "--postgresDatabaseName=myfancydb",
+ *  "--postgresPassword=mypass",
+ *  "--postgresSsl=false" ]'
+ *  --tests org.apache.beam.sdk.io.jdbc.JdbcIOPostgresIT
+ *  -DintegrationTestRunner=direct
+ * </pre>
+ */
+@RunWith(JUnit4.class)
+public class JdbcIOPostgresIT {
+  private static final Schema INPUT_SCHEMA =
+      Schema.of(
+          Schema.Field.of("id", Schema.FieldType.INT32),
+          Schema.Field.of("name", Schema.FieldType.STRING));
+
+  private static final List<Row> ROWS =
+      Arrays.asList(
+          Row.withSchema(INPUT_SCHEMA)
+              .withFieldValue("id", 1)
+              .withFieldValue("name", "foo")
+              .build(),
+          Row.withSchema(INPUT_SCHEMA)
+              .withFieldValue("id", 2)
+              .withFieldValue("name", "bar")
+              .build(),
+          Row.withSchema(INPUT_SCHEMA)
+              .withFieldValue("id", 3)
+              .withFieldValue("name", "baz")
+              .build());
+
+  private static PGSimpleDataSource dataSource;
+  private static String jdbcUrl;
+
+  @Rule public TestPipeline writePipeline = TestPipeline.create();
+  @Rule public TestPipeline readPipeline = TestPipeline.create();
+
+  @BeforeClass
+  public static void setup() {
+    PostgresIOTestPipelineOptions options;
+    try {
+      options = readIOTestPipelineOptions(PostgresIOTestPipelineOptions.class);
+    } catch (IllegalArgumentException e) {
+      options = null;
+    }
+    org.junit.Assume.assumeNotNull(options);
+    dataSource = DatabaseTestHelper.getPostgresDataSource(options);
+    jdbcUrl = DatabaseTestHelper.getPostgresDBUrl(options);
+  }
+
+  @Test
+  public void testWriteThenRead() throws SQLException {
+    String tableName = DatabaseTestHelper.getTestTableName("JdbcIOPostgresIT");
+    DatabaseTestHelper.createTable(dataSource, tableName);
+
+    
WriteToPostgresSchemaTransformProvider.JdbcWriteSchemaTransformConfiguration 
writeConfig =

Review Comment:
   nit:
   This confused me a little. It would be more correct to reference 
`JdbcWriteSchemaTransformProvider.JdbcWriteSchemaTransformConfiguration` right?
   
   same with the other configurations



##########
sdks/java/io/jdbc/src/main/java/org/apache/beam/sdk/io/jdbc/providers/ReadFromPostgresSchemaTransformProvider.java:
##########
@@ -43,4 +53,41 @@ public String description() {
   protected String jdbcType() {
     return POSTGRES;
   }
+
+  @Override
+  public @UnknownKeyFor @NonNull @Initialized SchemaTransform from(
+      JdbcReadSchemaTransformConfiguration configuration) {
+    String jdbcType = configuration.getJdbcType();
+    if (jdbcType != null && !jdbcType.equals(jdbcType())) {
+      throw new IllegalArgumentException(
+          String.format("Wrong JDBC type. Expected '%s' but got '%s'", 
jdbcType(), jdbcType));
+    }
+
+    List<@org.checkerframework.checker.nullness.qual.Nullable String> 
connectionInitSql =
+        configuration.getConnectionInitSql();
+    if (connectionInitSql != null && !connectionInitSql.isEmpty()) {
+      LOG.warn("Postgres does not support connectionInitSql, ignoring.");
+    }
+
+    Boolean disableAutoCommit = configuration.getDisableAutoCommit();
+    if (disableAutoCommit != null && !disableAutoCommit) {
+      LOG.warn("Postgres reads require disableAutoCommit to be true, 
overriding to true.");
+    }
+
+    // Override "connectionInitSql" and "disableAutoCommit" for postgres
+    configuration =
+        configuration
+            .toBuilder()
+            .setConnectionInitSql(Collections.emptyList())
+            .setDisableAutoCommit(true)
+            .build();
+    return new PostgresReadSchemaTransform(configuration);
+  }
+
+  public static class PostgresReadSchemaTransform extends 
JdbcReadSchemaTransform {
+    public PostgresReadSchemaTransform(JdbcReadSchemaTransformConfiguration 
config) {
+      super(config, POSTGRES);
+      config.validate(POSTGRES);

Review Comment:
   nit: config.validate(str) should go inside 
`JdbcReadSchemaTransformConfiguration`



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to