From 198dab9f58092166353408d151d9029f2b4e6f2e Mon Sep 17 00:00:00 2001 From: Neeraj Gupta <254676+ua741@users.noreply.github.com> Date: Mon, 29 Jul 2024 10:37:13 +0530 Subject: [PATCH] [server] Add db script to store data --- .../migrations/89_derived_data_table.down.sql | 7 +++++ .../migrations/89_derived_data_table.up.sql | 27 +++++++++++++++++++ 2 files changed, 34 insertions(+) create mode 100644 server/migrations/89_derived_data_table.down.sql create mode 100644 server/migrations/89_derived_data_table.up.sql diff --git a/server/migrations/89_derived_data_table.down.sql b/server/migrations/89_derived_data_table.down.sql new file mode 100644 index 0000000000..c71f42880f --- /dev/null +++ b/server/migrations/89_derived_data_table.down.sql @@ -0,0 +1,7 @@ + +DROP INDEX IF EXISTS idx_derived_user_id_updated_at; +DROP INDEX IF EXISTS idx_derived_user_id_data_type; + +DROP TABLE IF EXISTS derived; + +DROP TYPE IF EXISTS derived_data_type; \ No newline at end of file diff --git a/server/migrations/89_derived_data_table.up.sql b/server/migrations/89_derived_data_table.up.sql new file mode 100644 index 0000000000..fbbb15146a --- /dev/null +++ b/server/migrations/89_derived_data_table.up.sql @@ -0,0 +1,27 @@ +-- Create the data_type enum +CREATE TYPE derived_data_type AS ENUM ('img_jpg_preview', 'vid_hls_preview', 'meta'); + +-- Create the derived table +CREATE TABLE derived ( + file_id BIGINT NOT NULL, + user_id BIGINT NOT NULL, + data_type derived_data_type NOT NULL, + size BIGINT NOT NULL, + latest_bucket s3region NOT NULL, + replicated_buckets s3region[] NOT NULL, +-- following field contains list of buckets from where we need to delete the data as the given data_type will not longer be persisted in that dc + delete_from_buckets s3region[] NOT NULL DEFAULT '{}', + pending_sync BOOLEAN NOT NULL DEFAULT false, + created_at BIGINT NOT NULL DEFAULT now_utc_micro_seconds(), + updated_at BIGINT NOT NULL DEFAULT now_utc_micro_seconds(), + PRIMARY KEY (file_id, data_type) +); + +-- Add primary key +ALTER TABLE derived ADD PRIMARY KEY (file_id, data_type); + +-- Add index for user_id and data_type +CREATE INDEX idx_derived_user_id_data_type ON derived (user_id, data_type); + +-- Add index for user_id and updated_at for efficient querying +CREATE INDEX idx_derived_user_id_updated_at ON derived (user_id, updated_at); \ No newline at end of file