SET statement_timeout = 0;
SET lock_timeout = 0;
SET idle_in_transaction_session_timeout = 0;
SET client_encoding = 'UTF8';
SET standard_conforming_strings = on;
SELECT pg_catalog.set_config('search_path', '', false);
SET check_function_bodies = false;
SET xmloption = content;
SET client_min_messages = warning;
SET row_security = off;

-- event テーブルに欠落している app.record.index.delete.submit を追加します。
DO $$
DECLARE
    max_id INTEGER;
BEGIN
    SELECT MAX(id) INTO max_id FROM public."event";
    PERFORM pg_catalog.setval('public.event_id_seq', max_id, true);

	INSERT INTO public."event" (create_time, update_time, category, "type", eventid, "function", mobile)
	VALUES(NOW(), NOW(), 'Kintone', 'レコード一覧画面', 'app.record.index.delete.submit', 'レコードを削除するとき', true)
	ON CONFLICT (eventid) DO NOTHING;
END $$;


-- eventaction テーブル
DO $$
DECLARE
    max_id INTEGER;
BEGIN
	-- constraint: unique_eventid_actionid
	IF NOT EXISTS (
	    SELECT 1
	    FROM pg_constraint
	    WHERE conrelid = 'public.eventaction'::regclass
	      AND conname = 'unique_eventid_actionid'
	) THEN
	    ALTER TABLE public.eventaction
	    ADD CONSTRAINT unique_eventid_actionid UNIQUE (eventid, actionid);
	END IF;

    SELECT MAX(id) INTO max_id FROM public.eventaction;
    PERFORM pg_catalog.setval('public.eventaction_id_seq', max_id, true);

	-- /must-input.ts
	INSERT INTO public.eventaction (create_time, update_time, eventid, actionid)
	VALUES(NOW(), NOW(), 'app.record.detail.delete.submit', 1)
	    , (NOW(), NOW(), 'app.record.index.delete.submit', 1)
		ON CONFLICT (eventid, actionid) DO NOTHING;

END $$;

