|
| 1 | +/* |
| 2 | + * Copyright (c) 2022 Airbyte, Inc., all rights reserved. |
| 3 | + */ |
| 4 | + |
| 5 | +package io.airbyte.db.instance.jobs.migrations; |
| 6 | + |
| 7 | +import static org.jooq.impl.DSL.currentOffsetDateTime; |
| 8 | +import static org.jooq.impl.DSL.field; |
| 9 | +import static org.jooq.impl.DSL.foreignKey; |
| 10 | +import static org.jooq.impl.DSL.primaryKey; |
| 11 | +import static org.jooq.impl.DSL.unique; |
| 12 | + |
| 13 | +import java.time.OffsetDateTime; |
| 14 | +import java.util.UUID; |
| 15 | +import org.flywaydb.core.api.migration.BaseJavaMigration; |
| 16 | +import org.flywaydb.core.api.migration.Context; |
| 17 | +import org.jooq.DSLContext; |
| 18 | +import org.jooq.Field; |
| 19 | +import org.jooq.impl.DSL; |
| 20 | +import org.jooq.impl.SQLDataType; |
| 21 | +import org.slf4j.Logger; |
| 22 | +import org.slf4j.LoggerFactory; |
| 23 | + |
| 24 | +/** |
| 25 | + * The estimated columns contains the overall estimated records and bytes for an attempt. |
| 26 | + * <p> |
| 27 | + * The new stream_stats table contains the estimated and emitted records/bytes for an attempt at the |
| 28 | + * per-stream level. This lets us track per-stream stats as an attempt is in progress. |
| 29 | + */ |
| 30 | +public class V0_40_18_002__AddProgressBarStats extends BaseJavaMigration { |
| 31 | + |
| 32 | + private static final Logger LOGGER = LoggerFactory.getLogger(V0_40_18_002__AddProgressBarStats.class); |
| 33 | + |
| 34 | + @Override |
| 35 | + public void migrate(final Context context) throws Exception { |
| 36 | + LOGGER.info("Running migration: {}", this.getClass().getSimpleName()); |
| 37 | + |
| 38 | + // Warning: please do not use any jOOQ generated code to write a migration. |
| 39 | + // As database schema changes, the generated jOOQ code can be deprecated. So |
| 40 | + // old migration may not compile if there is any generated code. |
| 41 | + try (final DSLContext ctx = DSL.using(context.getConnection())) { |
| 42 | + addEstimatedColumnsToSyncStats(ctx); |
| 43 | + addStreamStatsTable(ctx); |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + private static void addEstimatedColumnsToSyncStats(final DSLContext ctx) { |
| 48 | + ctx.alterTable("sync_stats") |
| 49 | + .add( |
| 50 | + field("estimated_records", SQLDataType.BIGINT.nullable(true)), |
| 51 | + field("estimated_bytes", SQLDataType.BIGINT.nullable(true))) |
| 52 | + .execute(); |
| 53 | + } |
| 54 | + |
| 55 | + private static void addStreamStatsTable(final DSLContext ctx) { |
| 56 | + // Metadata Columns |
| 57 | + final Field<UUID> id = field("id", SQLDataType.UUID.nullable(false)); |
| 58 | + final Field<Integer> attemptId = field("attempt_id", SQLDataType.INTEGER.nullable(false)); |
| 59 | + final Field<String> streamNamespace = field("stream_namespace", SQLDataType.VARCHAR.nullable(false)); |
| 60 | + final Field<String> streamName = field("stream_name", SQLDataType.VARCHAR.nullable(false)); |
| 61 | + |
| 62 | + // Stats Columns |
| 63 | + final Field<Long> recordsEmitted = field("records_emitted", SQLDataType.BIGINT.nullable(true)); |
| 64 | + final Field<Long> bytesEmitted = field("bytes_emitted", SQLDataType.BIGINT.nullable(true)); |
| 65 | + final Field<Long> estimatedRecords = field("estimated_records", SQLDataType.BIGINT.nullable(true)); |
| 66 | + final Field<Long> estimatedBytes = field("estimated_bytes", SQLDataType.BIGINT.nullable(true)); |
| 67 | + |
| 68 | + // Time Columns |
| 69 | + final Field<OffsetDateTime> createdAt = |
| 70 | + field("created_at", SQLDataType.TIMESTAMPWITHTIMEZONE.nullable(false).defaultValue(currentOffsetDateTime())); |
| 71 | + final Field<OffsetDateTime> updatedAt = |
| 72 | + field("updated_at", SQLDataType.TIMESTAMPWITHTIMEZONE.nullable(false).defaultValue(currentOffsetDateTime())); |
| 73 | + |
| 74 | + ctx.createTableIfNotExists("stream_stats") |
| 75 | + .columns( |
| 76 | + id, attemptId, streamNamespace, streamName, recordsEmitted, bytesEmitted, estimatedRecords, estimatedBytes, createdAt, updatedAt) |
| 77 | + .constraints( |
| 78 | + primaryKey(id), |
| 79 | + foreignKey(attemptId).references("attempts", "id").onDeleteCascade(), |
| 80 | + // Prevent duplicate stat records of the same stream and attempt. |
| 81 | + unique("attempt_id", "stream_name")) |
| 82 | + .execute(); |
| 83 | + |
| 84 | + // Create an index on attempt_id, since all read queries on this table as of this migration will be |
| 85 | + // WHERE clauses on the attempt id. |
| 86 | + ctx.createIndex("index").on("stream_stats", "attempt_id").execute(); |
| 87 | + |
| 88 | + } |
| 89 | + |
| 90 | +} |
0 commit comments