HEX
Server: Apache/2.4.63 (Unix)
System: Linux Synopilou92 4.4.302+ #72806 SMP Mon Jul 21 23:16:00 CST 2025 x86_64
User: pilou92 (1026)
PHP: 8.0.30
Disabled: NONE
Upload Files
File: /volume1/@appstore/SynologyPhotos/etc/sql/145.sql
DO $$
DECLARE
  i INT;
  version_time_table_name TEXT;
BEGIN
  FOR i IN 0..13 LOOP
    version_time_table_name := format('version_time_%s', i);
    -- check table exists
    IF NOT EXISTS (
      SELECT 1
      FROM information_schema.tables
      WHERE table_schema = 'public'
      AND table_name = version_time_table_name
    ) THEN
      CONTINUE;
    END IF;
    -- Drop existing indexes in version_time table
    EXECUTE format('ALTER TABLE public.version_time_%s DROP CONSTRAINT IF EXISTS version_id_%s_primary',
        i,
        i
    );
    EXECUTE format('DROP INDEX IF EXISTS version_time_%s_idx', i);
    EXECUTE format('DROP INDEX IF EXISTS time_%s_idx', i);
    -- Add a unique index on version_time table
    EXECUTE format('CREATE UNIQUE INDEX version_time_%s_idx ON public.version_time_%s USING btree (version, modified_time ASC NULLS LAST)',
        i,
        i
    );
  END LOOP;
END $$;
CREATE OR REPLACE FUNCTION public.create_version_time_by_id_user_func (id_user integer)
  RETURNS void
  LANGUAGE plpgsql
  VOLATILE
  CALLED ON NULL INPUT
  SECURITY INVOKER
  COST 1
  AS $$
DECLARE
  new_version_time_table text;
  new_version_time_table_name text;
  trigger_name text;
  pk_name text;
  idx_name text;
  new_sequence_name text;
  id_user_bucket integer;
BEGIN
  id_user_bucket := CASE WHEN id_user = 0 THEN 0 ELSE (id_user - 1) % 13 + 1 END;
  new_version_time_table = 'public.version_time_' || id_user_bucket;
  new_version_time_table_name = 'version_time_' || id_user_bucket;
  pk_name = 'version_id_' || id_user_bucket || '_primary';
  idx_name = 'version_time_' || id_user_bucket || '_idx';
  new_sequence_name = 'public.version_time_' || id_user_bucket || '_version_seq';
  trigger_name = 'delete_expired_version_time_' || id_user_bucket || '_data_trigger';
  -- check if the sequence and table already exists
  RAISE NOTICE 'Checking if sequence % and table % exist', new_sequence_name, new_version_time_table;
  IF EXISTS (
    SELECT 1
    FROM information_schema.tables
    WHERE table_schema = 'public'
    AND table_name = new_version_time_table_name
  ) THEN
    RETURN;
  END IF;
  EXECUTE format('CREATE SEQUENCE %s START WITH 1', new_sequence_name);
  EXECUTE format('CREATE TABLE %s (version BIGINT NOT NULL DEFAULT nextval(''%s''), modified_time BIGINT NOT NULL)',
    new_version_time_table, new_sequence_name, pk_name);
  EXECUTE format('CREATE UNIQUE INDEX %s ON %s USING btree (version, modified_time ASC NULLS LAST)', idx_name, new_version_time_table);
  EXECUTE format('CREATE TRIGGER %s BEFORE INSERT ON %s FOR EACH ROW EXECUTE PROCEDURE public.delete_expired_version_time_data_func()',
    trigger_name, new_version_time_table);
END;
$$;
CREATE OR REPLACE FUNCTION public.create_user_event_history_table_by_id_user_func (id_user integer)
  RETURNS void
  LANGUAGE plpgsql
  VOLATILE
  CALLED ON NULL INPUT
  SECURITY INVOKER
  COST 1
  AS $$
DECLARE
  id_user_bucket INT;
  user_event_history_table text;
  user_event_history_table_name text;
BEGIN
  id_user_bucket := CASE WHEN id_user = 0 THEN 0 ELSE (id_user - 1) % 13 + 1 END;
  user_event_history_table = 'public.user_event_history_' || id_user_bucket;
  user_event_history_table_name = 'user_event_history_' || id_user_bucket;
  IF EXISTS (
    SELECT 1
    FROM information_schema.tables
    WHERE table_schema = 'public'
    AND table_name = user_event_history_table_name
  ) THEN
    RETURN;
  END IF;
  EXECUTE format('CREATE TABLE %s (
    version BIGINT PRIMARY KEY,
    id_user INTEGER NOT NULL,
    target_type SMALLINT NOT NULL,
    target_id INTEGER[] NOT NULL,
    target_id_user INTEGER NOT NULL,
    trigger_id_user INTEGER,
    event_type TEXT NOT NULL,
    event_detail JSON)', user_event_history_table
  );
  EXECUTE format('CREATE INDEX IF NOT EXISTS user_event_history_%s_version_id_user_idx ON %s (id_user, version);', id_user_bucket, user_event_history_table);
END
$$;