|
| 1 | +/* |
| 2 | + * Copyright (c) 2024 Airbyte, Inc., all rights reserved. |
| 3 | + */ |
| 4 | + |
| 5 | +package io.airbyte.cdk.integrations.destination.jdbc |
| 6 | + |
| 7 | +import io.airbyte.cdk.db.jdbc.JdbcDatabase |
| 8 | +import io.airbyte.cdk.db.jdbc.JdbcUtils |
| 9 | +import io.airbyte.cdk.integrations.destination.NamingConventionTransformer |
| 10 | +import io.airbyte.cdk.integrations.destination.async.model.PartialAirbyteMessage |
| 11 | +import io.airbyte.cdk.integrations.destination.async.model.PartialAirbyteRecordMessage |
| 12 | +import io.airbyte.commons.exceptions.ConnectionErrorException |
| 13 | +import io.airbyte.commons.json.Jsons |
| 14 | +import java.sql.Connection |
| 15 | +import java.sql.ResultSet |
| 16 | +import java.sql.SQLException |
| 17 | +import java.util.* |
| 18 | + |
| 19 | +object JdbcCheckOperations { |
| 20 | + |
| 21 | + /** |
| 22 | + * Verifies if provided creds has enough permissions. Steps are: 1. Create schema if not exists. |
| 23 | + * 2. Create test table. 3. Insert dummy record to newly created table if "attemptInsert" set to |
| 24 | + * true. |
| 25 | + * 4. Delete table created on step 2. |
| 26 | + * |
| 27 | + * @param outputSchema |
| 28 | + * - schema to tests against. |
| 29 | + * @param database |
| 30 | + * - database to tests against. |
| 31 | + * @param namingResolver |
| 32 | + * - naming resolver. |
| 33 | + * @param sqlOps |
| 34 | + * - SqlOperations object |
| 35 | + * @param attemptInsert |
| 36 | + * - set true if need to make attempt to insert dummy records to newly created table. Set false |
| 37 | + * to skip insert step. |
| 38 | + */ |
| 39 | + @JvmStatic |
| 40 | + @Throws(Exception::class) |
| 41 | + fun attemptTableOperations( |
| 42 | + outputSchema: String?, |
| 43 | + database: JdbcDatabase, |
| 44 | + namingResolver: NamingConventionTransformer, |
| 45 | + sqlOps: SqlOperations, |
| 46 | + attemptInsert: Boolean |
| 47 | + ) { |
| 48 | + // verify we have write permissions on the target schema by creating a table with a |
| 49 | + // random name, |
| 50 | + // then dropping that table |
| 51 | + try { |
| 52 | + // Get metadata from the database to see whether connection is possible |
| 53 | + database.bufferedResultSetQuery( |
| 54 | + { conn: Connection -> conn.metaData.catalogs }, |
| 55 | + { queryContext: ResultSet? -> |
| 56 | + JdbcUtils.defaultSourceOperations.rowToJson(queryContext!!) |
| 57 | + }, |
| 58 | + ) |
| 59 | + |
| 60 | + // verify we have write permissions on the target schema by creating a table with a |
| 61 | + // random name, |
| 62 | + // then dropping that table |
| 63 | + val outputTableName = |
| 64 | + namingResolver.getIdentifier( |
| 65 | + "_airbyte_connection_test_" + |
| 66 | + UUID.randomUUID().toString().replace("-".toRegex(), ""), |
| 67 | + ) |
| 68 | + sqlOps.createSchemaIfNotExists(database, outputSchema) |
| 69 | + sqlOps.createTableIfNotExists(database, outputSchema, outputTableName) |
| 70 | + // verify if user has permission to make SQL INSERT queries |
| 71 | + try { |
| 72 | + if (attemptInsert) { |
| 73 | + sqlOps.insertRecords( |
| 74 | + database, |
| 75 | + listOf(dummyRecord), |
| 76 | + outputSchema, |
| 77 | + outputTableName, |
| 78 | + ) |
| 79 | + } |
| 80 | + } finally { |
| 81 | + sqlOps.dropTableIfExists(database, outputSchema, outputTableName) |
| 82 | + } |
| 83 | + } catch (e: SQLException) { |
| 84 | + if (Objects.isNull(e.cause) || e.cause !is SQLException) { |
| 85 | + throw ConnectionErrorException(e.sqlState, e.errorCode, e.message, e) |
| 86 | + } else { |
| 87 | + val cause = e.cause as SQLException? |
| 88 | + throw ConnectionErrorException(e.sqlState, cause!!.errorCode, cause.message, e) |
| 89 | + } |
| 90 | + } catch (e: Exception) { |
| 91 | + throw Exception(e) |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + private val dummyRecord: PartialAirbyteMessage |
| 96 | + /** |
| 97 | + * Generates a dummy AirbyteRecordMessage with random values. |
| 98 | + * |
| 99 | + * @return AirbyteRecordMessage object with dummy values that may be used to test insert |
| 100 | + * permission. |
| 101 | + */ |
| 102 | + get() { |
| 103 | + val dummyDataToInsert = Jsons.deserialize("{ \"field1\": true }") |
| 104 | + return PartialAirbyteMessage() |
| 105 | + .withRecord( |
| 106 | + PartialAirbyteRecordMessage() |
| 107 | + .withStream("stream1") |
| 108 | + .withEmittedAt(1602637589000L), |
| 109 | + ) |
| 110 | + .withSerialized(dummyDataToInsert.toString()) |
| 111 | + } |
| 112 | +} |
0 commit comments