|
| 1 | +/* |
| 2 | + * Copyright (c) 2021 Airbyte, Inc., all rights reserved. |
| 3 | + */ |
| 4 | + |
| 5 | +package io.airbyte.db.instance.configs.migrations; |
| 6 | + |
| 7 | +import static org.jooq.impl.DSL.constraint; |
| 8 | +import static org.jooq.impl.DSL.foreignKey; |
| 9 | +import static org.jooq.impl.DSL.primaryKey; |
| 10 | + |
| 11 | +import com.google.common.annotations.VisibleForTesting; |
| 12 | +import java.time.OffsetDateTime; |
| 13 | +import java.util.UUID; |
| 14 | +import org.flywaydb.core.api.migration.BaseJavaMigration; |
| 15 | +import org.flywaydb.core.api.migration.Context; |
| 16 | +import org.jooq.DSLContext; |
| 17 | +import org.jooq.Field; |
| 18 | +import org.jooq.JSONB; |
| 19 | +import org.jooq.impl.DSL; |
| 20 | +import org.jooq.impl.SQLDataType; |
| 21 | +import org.slf4j.Logger; |
| 22 | +import org.slf4j.LoggerFactory; |
| 23 | + |
| 24 | +public class V0_35_26_001__PersistDiscoveredCatalog extends BaseJavaMigration { |
| 25 | + |
| 26 | + private static final Logger LOGGER = LoggerFactory.getLogger(V0_35_26_001__PersistDiscoveredCatalog.class); |
| 27 | + |
| 28 | + @Override |
| 29 | + public void migrate(final Context context) throws Exception { |
| 30 | + LOGGER.info("Running migration: {}", this.getClass().getSimpleName()); |
| 31 | + |
| 32 | + // Warning: please do not use any jOOQ generated code to write a migration. |
| 33 | + // As database schema changes, the generated jOOQ code can be deprecated. So |
| 34 | + // old migration may not compile if there is any generated code. |
| 35 | + final DSLContext ctx = DSL.using(context.getConnection()); |
| 36 | + migrate(ctx); |
| 37 | + } |
| 38 | + |
| 39 | + @VisibleForTesting |
| 40 | + public static void migrate(final DSLContext ctx) { |
| 41 | + createActorCatalog(ctx); |
| 42 | + createCatalogFetchEvent(ctx); |
| 43 | + addConnectionTableForeignKey(ctx); |
| 44 | + } |
| 45 | + |
| 46 | + private static void createActorCatalog(final DSLContext ctx) { |
| 47 | + final Field<UUID> id = DSL.field("id", SQLDataType.UUID.nullable(false)); |
| 48 | + final Field<JSONB> catalog = DSL.field("catalog", SQLDataType.JSONB.nullable(false)); |
| 49 | + final Field<String> catalogHash = DSL.field("catalog_hash", SQLDataType.VARCHAR(32).nullable(false)); |
| 50 | + final Field<OffsetDateTime> createdAt = DSL.field("created_at", SQLDataType.TIMESTAMPWITHTIMEZONE.nullable(false)); |
| 51 | + ctx.createTableIfNotExists("actor_catalog") |
| 52 | + .columns(id, |
| 53 | + catalog, |
| 54 | + catalogHash, |
| 55 | + createdAt) |
| 56 | + .constraints(primaryKey(id)) |
| 57 | + .execute(); |
| 58 | + LOGGER.info("actor_catalog table created"); |
| 59 | + ctx.createIndexIfNotExists("actor_catalog_catalog_hash_id_idx").on("actor_catalog", "catalog_hash").execute(); |
| 60 | + } |
| 61 | + |
| 62 | + private static void createCatalogFetchEvent(final DSLContext ctx) { |
| 63 | + final Field<UUID> id = DSL.field("id", SQLDataType.UUID.nullable(false)); |
| 64 | + final Field<UUID> actorCatalogId = DSL.field("actor_catalog_id", SQLDataType.UUID.nullable(false)); |
| 65 | + final Field<UUID> actorId = DSL.field("actor_id", SQLDataType.UUID.nullable(false)); |
| 66 | + final Field<String> configHash = DSL.field("config_hash", SQLDataType.VARCHAR(32).nullable(false)); |
| 67 | + final Field<String> actorVersion = DSL.field("actor_version", SQLDataType.VARCHAR(256).nullable(false)); |
| 68 | + |
| 69 | + ctx.createTableIfNotExists("actor_catalog_fetch_event") |
| 70 | + .columns(id, |
| 71 | + actorCatalogId, |
| 72 | + actorId, |
| 73 | + configHash, |
| 74 | + actorVersion) |
| 75 | + .constraints(primaryKey(id), |
| 76 | + foreignKey(actorCatalogId).references("actor_catalog", "id").onDeleteCascade(), |
| 77 | + foreignKey(actorId).references("actor", "id").onDeleteCascade()) |
| 78 | + .execute(); |
| 79 | + LOGGER.info("actor_catalog_fetch_event table created"); |
| 80 | + ctx.createIndexIfNotExists("actor_catalog_fetch_event_actor_id_idx").on("actor_catalog_fetch_event", "actor_id").execute(); |
| 81 | + ctx.createIndexIfNotExists("actor_catalog_fetch_event_actor_catalog_id_idx").on("actor_catalog_fetch_event", "actor_catalog_id").execute(); |
| 82 | + } |
| 83 | + |
| 84 | + private static void addConnectionTableForeignKey(final DSLContext ctx) { |
| 85 | + final Field<UUID> sourceCatalogId = DSL.field("source_catalog_id", SQLDataType.UUID.nullable(true)); |
| 86 | + ctx.alterTable("connection") |
| 87 | + .addIfNotExists(sourceCatalogId).execute(); |
| 88 | + ctx.alterTable("connection") |
| 89 | + .dropConstraintIfExists("connection_actor_catalog_id_fk"); |
| 90 | + ctx.alterTable("connection") |
| 91 | + .add(constraint("connection_actor_catalog_id_fk").foreignKey(sourceCatalogId) |
| 92 | + .references("actor_catalog", "id").onDeleteCascade()) |
| 93 | + .execute(); |
| 94 | + } |
| 95 | + |
| 96 | +} |
0 commit comments