From 7cc310764e22bdf7a1172bbc58a4a0a84bd0ce88 Mon Sep 17 00:00:00 2001 From: Brandon Thomas Date: Tue, 16 Jan 2024 19:10:23 -0500 Subject: [PATCH] new tables: idempotency_tokens, batch_generations; new column --- .../down.sql | 5 +++ .../up.sql | 19 +++++++++ .../down.sql | 5 +++ .../up.sql | 42 +++++++++++++++++++ .../down.sql | 5 +++ .../up.sql | 7 ++++ _sql/migrations_squashed/media_files.sql | 5 +++ 7 files changed, 88 insertions(+) create mode 100644 _sql/migrations/2024-01-16-231833_idempotency_tokens/down.sql create mode 100644 _sql/migrations/2024-01-16-231833_idempotency_tokens/up.sql create mode 100644 _sql/migrations/2024-01-16-231841_batch_generations/down.sql create mode 100644 _sql/migrations/2024-01-16-231841_batch_generations/up.sql create mode 100644 _sql/migrations/2024-01-16-231852_alter_media_files_add_batch_token/down.sql create mode 100644 _sql/migrations/2024-01-16-231852_alter_media_files_add_batch_token/up.sql diff --git a/_sql/migrations/2024-01-16-231833_idempotency_tokens/down.sql b/_sql/migrations/2024-01-16-231833_idempotency_tokens/down.sql new file mode 100644 index 0000000000..ab73e06165 --- /dev/null +++ b/_sql/migrations/2024-01-16-231833_idempotency_tokens/down.sql @@ -0,0 +1,5 @@ +-- noinspection SqlDialectInspectionForFile +-- noinspection SqlNoDataSourceInspectionForFile +-- noinspection SqlResolveForFile + +drop table idempotency_tokens; \ No newline at end of file diff --git a/_sql/migrations/2024-01-16-231833_idempotency_tokens/up.sql b/_sql/migrations/2024-01-16-231833_idempotency_tokens/up.sql new file mode 100644 index 0000000000..5667756d56 --- /dev/null +++ b/_sql/migrations/2024-01-16-231833_idempotency_tokens/up.sql @@ -0,0 +1,19 @@ +-- noinspection SqlDialectInspectionForFile +-- noinspection SqlNoDataSourceInspectionForFile +-- noinspection SqlResolveForFile + +-- A form of optimistic locking +CREATE TABLE idempotency_tokens ( + -- Not used for anything except replication. + id BIGINT(20) NOT NULL AUTO_INCREMENT, + + -- This can be a UUID or any entropic value. + idempotency_value VARCHAR(36) NOT NULL, + + created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, + + -- INDICES -- + PRIMARY KEY (id), + UNIQUE KEY (idempotency_value) + +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin; diff --git a/_sql/migrations/2024-01-16-231841_batch_generations/down.sql b/_sql/migrations/2024-01-16-231841_batch_generations/down.sql new file mode 100644 index 0000000000..419c98f550 --- /dev/null +++ b/_sql/migrations/2024-01-16-231841_batch_generations/down.sql @@ -0,0 +1,5 @@ +-- noinspection SqlDialectInspectionForFile +-- noinspection SqlNoDataSourceInspectionForFile +-- noinspection SqlResolveForFile + +drop table batch_generations; diff --git a/_sql/migrations/2024-01-16-231841_batch_generations/up.sql b/_sql/migrations/2024-01-16-231841_batch_generations/up.sql new file mode 100644 index 0000000000..d738abb9f5 --- /dev/null +++ b/_sql/migrations/2024-01-16-231841_batch_generations/up.sql @@ -0,0 +1,42 @@ +-- noinspection SqlDialectInspectionForFile +-- noinspection SqlNoDataSourceInspectionForFile +-- noinspection SqlResolveForFile + +-- Batch generations +CREATE TABLE batch_generations ( + -- Not used for anything except replication. + id BIGINT(20) NOT NULL AUTO_INCREMENT, + + -- The batch token + -- This is **NOT** a unique token. + token VARCHAR(32) NOT NULL, + + -- ========== ENTITY LINKAGE ========== + + -- The type of record this is attached to + -- Currently supported values: + -- * 'model_weight' + -- * 'media_file' + -- * 'comment' + entity_type VARCHAR(32) NOT NULL, + + -- The token for the record. + entity_token VARCHAR(32) NOT NULL, + + -- ========== TIMESTAMPS ========== + + created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, + updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, + + -- INDICES -- + PRIMARY KEY (id), + UNIQUE KEY (token, entity_type, entity_token), + KEY index_token (token), + KEY index_entity_token (entity_token) + + -- NB: Realistically, we probably don't need this index until we have a lot of entity types, and the entity_token + -- entropy is probably sufficient enough as-is. + -- KEY index_entity_type_and_token (entity_type, entity_token), + + +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin; diff --git a/_sql/migrations/2024-01-16-231852_alter_media_files_add_batch_token/down.sql b/_sql/migrations/2024-01-16-231852_alter_media_files_add_batch_token/down.sql new file mode 100644 index 0000000000..924a36d70c --- /dev/null +++ b/_sql/migrations/2024-01-16-231852_alter_media_files_add_batch_token/down.sql @@ -0,0 +1,5 @@ +-- noinspection SqlDialectInspectionForFile +-- noinspection SqlNoDataSourceInspectionForFile +-- noinspection SqlResolveForFile + +ALTER TABLE media_files DROP COLUMN is_batch_generated; diff --git a/_sql/migrations/2024-01-16-231852_alter_media_files_add_batch_token/up.sql b/_sql/migrations/2024-01-16-231852_alter_media_files_add_batch_token/up.sql new file mode 100644 index 0000000000..1cbc612de5 --- /dev/null +++ b/_sql/migrations/2024-01-16-231852_alter_media_files_add_batch_token/up.sql @@ -0,0 +1,7 @@ +-- noinspection SqlDialectInspectionForFile +-- noinspection SqlNoDataSourceInspectionForFile +-- noinspection SqlResolveForFile + +ALTER TABLE media_files + ADD COLUMN is_batch_generated BOOLEAN NOT NULL DEFAULT FALSE + AFTER maybe_origin_filename; diff --git a/_sql/migrations_squashed/media_files.sql b/_sql/migrations_squashed/media_files.sql index e04cb2de30..35d7334ca9 100644 --- a/_sql/migrations_squashed/media_files.sql +++ b/_sql/migrations_squashed/media_files.sql @@ -58,6 +58,11 @@ CREATE TABLE media_files ( -- The original filename of the media (if uploaded by a user) maybe_origin_filename VARCHAR(255) DEFAULT NULL, + -- Whether this media file was generated as part of a batch + -- If so, we can look up the batch in a separate query/call to the `batch_generations` table. + -- We won't hold the batch token in this table since it'll be very sparsely populated. + is_batch_generated BOOLEAN NOT NULL DEFAULT FALSE, + -- ========== METADATA ========== -- We can store text transcripts, etc. in other tables and refer back via foreign key.