|
| 1 | +/* |
| 2 | + * Copyright (c) 2023 Airbyte, Inc., all rights reserved. |
| 3 | + */ |
| 4 | + |
| 5 | +package io.airbyte.integrations.base.destination.typing_deduping; |
| 6 | + |
| 7 | +import static io.airbyte.integrations.base.JavaBaseConstants.LEGACY_RAW_TABLE_COLUMNS; |
| 8 | +import static io.airbyte.integrations.base.JavaBaseConstants.V2_RAW_TABLE_COLUMN_NAMES; |
| 9 | + |
| 10 | +import io.airbyte.protocol.models.v0.DestinationSyncMode; |
| 11 | +import java.util.Collection; |
| 12 | +import java.util.Optional; |
| 13 | +import org.slf4j.Logger; |
| 14 | +import org.slf4j.LoggerFactory; |
| 15 | + |
| 16 | +public abstract class BaseDestinationV1V2Migrator<DialectTableDefinition> implements DestinationV1V2Migrator { |
| 17 | + |
| 18 | + Logger LOGGER = LoggerFactory.getLogger(BaseDestinationV1V2Migrator.class); |
| 19 | + |
| 20 | + @Override |
| 21 | + public void migrateIfNecessary( |
| 22 | + final SqlGenerator sqlGenerator, |
| 23 | + final DestinationHandler destinationHandler, |
| 24 | + final StreamConfig streamConfig) |
| 25 | + throws TableNotMigratedException, UnexpectedSchemaException { |
| 26 | + if (shouldMigrate(streamConfig)) { |
| 27 | + LOGGER.info("Starting v2 Migration for stream {}", streamConfig.id().finalName()); |
| 28 | + migrate(sqlGenerator, destinationHandler, streamConfig); |
| 29 | + } |
| 30 | + } |
| 31 | + |
| 32 | + /** |
| 33 | + * Determine whether a given stream needs to be migrated from v1 to v2 |
| 34 | + * |
| 35 | + * @param streamConfig the stream in question |
| 36 | + * @return whether to migrate the stream |
| 37 | + */ |
| 38 | + protected boolean shouldMigrate(final StreamConfig streamConfig) { |
| 39 | + final var v1RawTable = convertToV1RawName(streamConfig); |
| 40 | + return isMigrationRequiredForSyncMode(streamConfig.destinationSyncMode()) |
| 41 | + && !doesValidV2RawTableAlreadyExist(streamConfig) |
| 42 | + && doesValidV1RawTableExist(v1RawTable.namespace(), v1RawTable.tableName()); |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * Execute sql statements that converts a v1 raw table to a v2 raw table. Leaves the v1 raw table |
| 47 | + * intact |
| 48 | + * |
| 49 | + * @param sqlGenerator the class which generates dialect specific sql statements |
| 50 | + * @param destinationHandler the class which executes the sql statements |
| 51 | + * @param streamConfig the stream to migrate the raw table of |
| 52 | + */ |
| 53 | + public void migrate(final SqlGenerator<DialectTableDefinition> sqlGenerator, |
| 54 | + final DestinationHandler<DialectTableDefinition> destinationHandler, |
| 55 | + final StreamConfig streamConfig) |
| 56 | + throws TableNotMigratedException { |
| 57 | + final var namespacedTableName = convertToV1RawName(streamConfig); |
| 58 | + final var migrateAndReset = String.join("\n", |
| 59 | + sqlGenerator.migrateFromV1toV2(streamConfig.id(), namespacedTableName.namespace(), |
| 60 | + namespacedTableName.tableName()), |
| 61 | + sqlGenerator.softReset(streamConfig)); |
| 62 | + try { |
| 63 | + destinationHandler.execute(migrateAndReset); |
| 64 | + } catch (Exception e) { |
| 65 | + final var message = "Attempted and failed to migrate stream %s".formatted(streamConfig.id().finalName()); |
| 66 | + throw new TableNotMigratedException(message, e); |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * Checks the schema of the v1 raw table to ensure it matches the expected format |
| 72 | + * |
| 73 | + * @param existingV2AirbyteRawTable the v1 raw table |
| 74 | + * @return whether the schema is as expected |
| 75 | + */ |
| 76 | + private boolean doesV1RawTableMatchExpectedSchema(DialectTableDefinition existingV2AirbyteRawTable) { |
| 77 | + |
| 78 | + return schemaMatchesExpectation(existingV2AirbyteRawTable, LEGACY_RAW_TABLE_COLUMNS); |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * Checks the schema of the v2 raw table to ensure it matches the expected format |
| 83 | + * |
| 84 | + * @param existingV2AirbyteRawTable the v2 raw table |
| 85 | + */ |
| 86 | + private void validateAirbyteInternalNamespaceRawTableMatchExpectedV2Schema(DialectTableDefinition existingV2AirbyteRawTable) { |
| 87 | + if (!schemaMatchesExpectation(existingV2AirbyteRawTable, V2_RAW_TABLE_COLUMN_NAMES)) { |
| 88 | + throw new UnexpectedSchemaException("Destination V2 Raw Table does not match expected Schema"); |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * If the sync mode is a full refresh and we overwrite the table then there is no need to migrate |
| 94 | + * |
| 95 | + * @param destinationSyncMode destination sync mode |
| 96 | + * @return whether this is full refresh overwrite |
| 97 | + */ |
| 98 | + private boolean isMigrationRequiredForSyncMode(final DestinationSyncMode destinationSyncMode) { |
| 99 | + return !DestinationSyncMode.OVERWRITE.equals(destinationSyncMode); |
| 100 | + } |
| 101 | + |
| 102 | + /** |
| 103 | + * Checks if a valid destinations v2 raw table already exists |
| 104 | + * |
| 105 | + * @param streamConfig the raw table to check |
| 106 | + * @return whether it exists and is in the correct format |
| 107 | + */ |
| 108 | + private boolean doesValidV2RawTableAlreadyExist(final StreamConfig streamConfig) { |
| 109 | + if (doesAirbyteInternalNamespaceExist(streamConfig)) { |
| 110 | + final var existingV2Table = getTableIfExists(streamConfig.id().rawNamespace(), streamConfig.id().rawName()); |
| 111 | + existingV2Table.ifPresent(this::validateAirbyteInternalNamespaceRawTableMatchExpectedV2Schema); |
| 112 | + return existingV2Table.isPresent(); |
| 113 | + } |
| 114 | + return false; |
| 115 | + } |
| 116 | + |
| 117 | + /** |
| 118 | + * Checks if a valid v1 raw table already exists |
| 119 | + * |
| 120 | + * @param namespace |
| 121 | + * @param tableName |
| 122 | + * @return whether it exists and is in the correct format |
| 123 | + */ |
| 124 | + private boolean doesValidV1RawTableExist(final String namespace, final String tableName) { |
| 125 | + final var existingV1RawTable = getTableIfExists(namespace, tableName); |
| 126 | + return existingV1RawTable.isPresent() && doesV1RawTableMatchExpectedSchema(existingV1RawTable.get()); |
| 127 | + } |
| 128 | + |
| 129 | + /** |
| 130 | + * Checks to see if Airbyte's internal schema for destinations v2 exists |
| 131 | + * |
| 132 | + * @param streamConfig the stream to check |
| 133 | + * @return whether the schema exists |
| 134 | + */ |
| 135 | + abstract protected boolean doesAirbyteInternalNamespaceExist(StreamConfig streamConfig); |
| 136 | + |
| 137 | + /** |
| 138 | + * Checks a Table's schema and compares it to an expected schema to make sure it matches |
| 139 | + * |
| 140 | + * @param existingTable the table to check |
| 141 | + * @param columns the expected schema |
| 142 | + * @return whether the existing table schema matches the expectation |
| 143 | + */ |
| 144 | + abstract protected boolean schemaMatchesExpectation(DialectTableDefinition existingTable, Collection<String> columns); |
| 145 | + |
| 146 | + /** |
| 147 | + * Get a reference ta a table if it exists |
| 148 | + * |
| 149 | + * @param namespace |
| 150 | + * @param tableName |
| 151 | + * @return an optional potentially containing a reference to the table |
| 152 | + */ |
| 153 | + abstract protected Optional<DialectTableDefinition> getTableIfExists(String namespace, String tableName); |
| 154 | + |
| 155 | + /** |
| 156 | + * We use different naming conventions for raw table names in destinations v2, we need a way to map |
| 157 | + * that back to v1 names |
| 158 | + * |
| 159 | + * @param streamConfig the stream in question |
| 160 | + * @return the valid v1 name and namespace for the same stream |
| 161 | + */ |
| 162 | + abstract protected NamespacedTableName convertToV1RawName(StreamConfig streamConfig); |
| 163 | + |
| 164 | +} |
0 commit comments