|
| 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.foreignKey; |
| 9 | +import static org.jooq.impl.DSL.primaryKey; |
| 10 | + |
| 11 | +import java.time.OffsetDateTime; |
| 12 | +import java.util.UUID; |
| 13 | +import org.flywaydb.core.api.migration.BaseJavaMigration; |
| 14 | +import org.flywaydb.core.api.migration.Context; |
| 15 | +import org.jooq.DSLContext; |
| 16 | +import org.jooq.Field; |
| 17 | +import org.jooq.impl.DSL; |
| 18 | +import org.jooq.impl.SQLDataType; |
| 19 | +import org.slf4j.Logger; |
| 20 | +import org.slf4j.LoggerFactory; |
| 21 | + |
| 22 | +public class V0_40_3_001__CreateSyncStats extends BaseJavaMigration { |
| 23 | + |
| 24 | + private static final Logger LOGGER = LoggerFactory.getLogger(V0_40_3_001__CreateSyncStats.class); |
| 25 | + |
| 26 | + @Override |
| 27 | + public void migrate(final Context context) throws Exception { |
| 28 | + LOGGER.info("Running migration: {}", this.getClass().getSimpleName()); |
| 29 | + final DSLContext ctx = DSL.using(context.getConnection()); |
| 30 | + createSyncStatsTable(ctx); |
| 31 | + } |
| 32 | + |
| 33 | + private static void createSyncStatsTable(final DSLContext ctx) { |
| 34 | + final Field<UUID> id = DSL.field("id", SQLDataType.UUID.nullable(false)); |
| 35 | + final Field<Integer> attemptId = DSL.field("attempt_id", SQLDataType.INTEGER.nullable(false)); |
| 36 | + final Field<Long> recordsEmitted = DSL.field("records_emitted", SQLDataType.BIGINT.nullable(true)); |
| 37 | + final Field<Long> bytesEmitted = DSL.field("bytes_emitted", SQLDataType.BIGINT.nullable(true)); |
| 38 | + final Field<Long> sourceStateMessagesEmitted = DSL.field("source_state_messages_emitted", SQLDataType.BIGINT.nullable(true)); |
| 39 | + final Field<Long> destinationStateMessagesEmitted = DSL.field("destination_state_messages_emitted", SQLDataType.BIGINT.nullable(true)); |
| 40 | + final Field<Long> recordsCommitted = DSL.field("records_committed", SQLDataType.BIGINT.nullable(true)); |
| 41 | + final Field<Long> meanSecondsBeforeSourceStateMessageEmitted = |
| 42 | + DSL.field("mean_seconds_before_source_state_message_emitted", SQLDataType.BIGINT.nullable(true)); |
| 43 | + final Field<Long> maxSecondsBeforeSourceStateMessageEmitted = |
| 44 | + DSL.field("max_seconds_before_source_state_message_emitted", SQLDataType.BIGINT.nullable(true)); |
| 45 | + final Field<Long> meanSecondsBetweenStateMessageEmittedandCommitted = |
| 46 | + DSL.field("mean_seconds_between_state_message_emitted_and_committed", SQLDataType.BIGINT.nullable(true)); |
| 47 | + final Field<Long> maxSecondsBetweenStateMessageEmittedandCommitted = |
| 48 | + DSL.field("max_seconds_between_state_message_emitted_and_committed", SQLDataType.BIGINT.nullable(true)); |
| 49 | + final Field<OffsetDateTime> createdAt = |
| 50 | + DSL.field("created_at", SQLDataType.TIMESTAMPWITHTIMEZONE.nullable(false).defaultValue(currentOffsetDateTime())); |
| 51 | + final Field<OffsetDateTime> updatedAt = |
| 52 | + DSL.field("updated_at", SQLDataType.TIMESTAMPWITHTIMEZONE.nullable(false).defaultValue(currentOffsetDateTime())); |
| 53 | + |
| 54 | + ctx.createTableIfNotExists("sync_stats") |
| 55 | + .columns(id, attemptId, recordsEmitted, bytesEmitted, sourceStateMessagesEmitted, destinationStateMessagesEmitted, recordsCommitted, |
| 56 | + meanSecondsBeforeSourceStateMessageEmitted, maxSecondsBeforeSourceStateMessageEmitted, meanSecondsBetweenStateMessageEmittedandCommitted, |
| 57 | + maxSecondsBetweenStateMessageEmittedandCommitted, createdAt, updatedAt) |
| 58 | + .constraints(primaryKey(id), foreignKey(attemptId).references("attempts", "id").onDeleteCascade()) |
| 59 | + .execute(); |
| 60 | + |
| 61 | + ctx.createIndex("attempt_id_idx").on("sync_stats", "attempt_id").execute(); |
| 62 | + } |
| 63 | + |
| 64 | +} |
0 commit comments