mirror of
https://github.com/ente-io/ente.git
synced 2025-08-13 01:27:17 +00:00
[server] Add DB triggers for validating row sanity
This commit is contained in:
@@ -1,6 +1,5 @@
|
|||||||
|
|
||||||
DROP INDEX IF EXISTS idx_file_data_user_id_data_type;
|
DROP INDEX IF EXISTS idx_file_data_user_type_deleted;
|
||||||
DROP INDEX IF EXISTS idx_file_data_user_id_updated_at;
|
|
||||||
|
|
||||||
DROP TABLE IF EXISTS file_data;
|
DROP TABLE IF EXISTS file_data;
|
||||||
|
|
||||||
|
@@ -1,24 +1,52 @@
|
|||||||
-- Create the derived table
|
-- Create the derived table
|
||||||
CREATE TABLE file_data (
|
CREATE TABLE file_data
|
||||||
file_id BIGINT NOT NULL,
|
(
|
||||||
user_id BIGINT NOT NULL,
|
file_id BIGINT NOT NULL,
|
||||||
data_type OBJECT_TYPE NOT NULL,
|
user_id BIGINT NOT NULL,
|
||||||
size BIGINT NOT NULL,
|
data_type OBJECT_TYPE NOT NULL,
|
||||||
latest_bucket s3region NOT NULL,
|
size BIGINT NOT NULL,
|
||||||
replicated_buckets s3region[] NOT NULL,
|
latest_bucket 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
|
replicated_buckets s3region[] NOT NULL DEFAULT '{}',
|
||||||
delete_from_buckets s3region[] NOT NULL DEFAULT '{}',
|
-- 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
|
||||||
pending_sync BOOLEAN NOT NULL DEFAULT false,
|
delete_from_buckets s3region[] NOT NULL DEFAULT '{}',
|
||||||
is_deleted BOOLEAN NOT NULL DEFAULT false,
|
pending_sync BOOLEAN NOT NULL DEFAULT false,
|
||||||
last_sync_time BIGINT NOT NULL DEFAULT 0,
|
is_deleted BOOLEAN NOT NULL DEFAULT false,
|
||||||
created_at BIGINT NOT NULL DEFAULT now_utc_micro_seconds(),
|
last_sync_time BIGINT NOT NULL DEFAULT 0,
|
||||||
updated_at BIGINT NOT NULL DEFAULT now_utc_micro_seconds(),
|
created_at BIGINT NOT NULL DEFAULT now_utc_micro_seconds(),
|
||||||
PRIMARY KEY (file_id, data_type)
|
updated_at BIGINT NOT NULL DEFAULT now_utc_micro_seconds(),
|
||||||
|
PRIMARY KEY (file_id, data_type)
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
||||||
-- Add index for user_id and data_type
|
-- Add index for user_id and data_type for efficient querying
|
||||||
CREATE INDEX idx_file_data_user_id_data_type ON file_data (user_id, data_type);
|
CREATE INDEX idx_file_data_user_type_deleted ON file_data (user_id, data_type, is_deleted) INCLUDE (file_id, size);
|
||||||
|
|
||||||
|
CREATE OR REPLACE FUNCTION ensure_no_common_entries()
|
||||||
|
RETURNS TRIGGER AS $$
|
||||||
|
BEGIN
|
||||||
|
-- Check for common entries between latest_bucket and replicated_buckets
|
||||||
|
IF NEW.latest_bucket = ANY(NEW.replicated_buckets) THEN
|
||||||
|
RAISE EXCEPTION 'latest_bucket and replicated_buckets have common entries';
|
||||||
|
END IF;
|
||||||
|
|
||||||
|
-- Check for common entries between latest_bucket and delete_from_buckets
|
||||||
|
IF NEW.latest_bucket = ANY(NEW.delete_from_buckets) THEN
|
||||||
|
RAISE EXCEPTION 'latest_bucket and delete_from_buckets have common entries';
|
||||||
|
END IF;
|
||||||
|
|
||||||
|
-- Check for common entries between replicated_buckets and delete_from_buckets
|
||||||
|
IF EXISTS (
|
||||||
|
SELECT 1 FROM unnest(NEW.replicated_buckets) AS rb
|
||||||
|
WHERE rb = ANY(NEW.delete_from_buckets)
|
||||||
|
) THEN
|
||||||
|
RAISE EXCEPTION 'replicated_buckets and delete_from_buckets have common entries';
|
||||||
|
END IF;
|
||||||
|
|
||||||
|
RETURN NEW;
|
||||||
|
END;
|
||||||
|
$$ LANGUAGE plpgsql;
|
||||||
|
|
||||||
|
CREATE TRIGGER check_no_common_entries
|
||||||
|
BEFORE INSERT OR UPDATE ON file_data
|
||||||
|
FOR EACH ROW EXECUTE FUNCTION ensure_no_common_entries();
|
||||||
|
|
||||||
-- Add index for user_id and updated_at for efficient querying
|
|
||||||
CREATE INDEX idx_file_data_user_id_updated_at ON file_data (user_id, updated_at);
|
|
Reference in New Issue
Block a user