BEGIN;

--
-- Add new rights for actions on transfers
--

DO $$
BEGIN
    BEGIN
        ALTER TABLE rights ADD COLUMN show_order int;
    EXCEPTION
        WHEN duplicate_column THEN
        -- do nothing
    END;
END;
$$;

ALTER TABLE rights DROP CONSTRAINT IF EXISTS rights_show_order_unique;


DO $$
BEGIN
    BEGIN
        INSERT INTO rights VALUES (13, 'monitoring', 'restart', 'monitoring', false, 20003);
    EXCEPTION
        WHEN unique_violation THEN
        -- do nothing
    END;
END;
$$;

DO $$
BEGIN
    BEGIN
        INSERT INTO rights VALUES (14, 'monitoring', 'stop', 'monitoring', false, 20001);
    EXCEPTION
        WHEN unique_violation THEN
        -- do nothing
    END;
END;
$$;

DO $$
BEGIN
    BEGIN
        INSERT INTO rights VALUES (19, 'monitoring', 'cancel', 'monitoring', false, 20002);
    EXCEPTION
        WHEN unique_violation THEN
        -- do nothing
    END;
END;
$$;

DO $$
BEGIN
    BEGIN
        INSERT INTO rights VALUES (20, 'monitoring', 'hide', 'monitoring', false, 20004);
    EXCEPTION
        WHEN unique_violation THEN
        -- do nothing
    END;
END;
$$;

DO $$
BEGIN
    BEGIN
        INSERT INTO rights VALUES (21, 'administration', 'audit', 'administration', false, 40001);
    EXCEPTION
        WHEN unique_violation THEN
        -- do nothing
    END;
END;
$$;

UPDATE rights SET show_order=10000 WHERE target='site' and action='view';
UPDATE rights SET show_order=10001 WHERE target='site' and action='edit';
UPDATE rights SET show_order=10002 WHERE target='site' and action='delete';
UPDATE rights SET show_order=10100 WHERE target='route' and action='view';
UPDATE rights SET show_order=10101 WHERE target='route' and action='edit';
UPDATE rights SET show_order=10102 WHERE target='route' and action='delete';
UPDATE rights SET show_order=10200 WHERE target='partner' and action='view';
UPDATE rights SET show_order=10201 WHERE target='partner' and action='edit';
UPDATE rights SET show_order=10202 WHERE target='partner' and action='delete';
UPDATE rights SET show_order=10300 WHERE target= 'flow' and action='view';
UPDATE rights SET show_order=10301 WHERE target= 'flow' and action='edit';
UPDATE rights SET show_order=10302 WHERE target= 'flow' and action='delete';
UPDATE rights SET show_order=20003 WHERE target= 'monitoring' and action='restart';
UPDATE rights SET show_order=20001 WHERE target= 'monitoring' and action='stop';
UPDATE rights SET show_order=20000 WHERE target= 'monitoring' and action='access';
UPDATE rights SET show_order=40000 WHERE target= 'administration' and action='access';
UPDATE rights SET show_order=50000 WHERE target= 'installer' and action='install';
UPDATE rights SET show_order=30000 WHERE target= 'reporting' and action='access';
UPDATE rights SET show_order=20002 WHERE target= 'monitoring' and action='cancel';
UPDATE rights SET show_order=20004 WHERE target= 'monitoring' and action='hide';

ALTER TABLE rights ALTER COLUMN show_order SET NOT NULL;

DO $$
BEGIN
    BEGIN
        ALTER TABLE rights ADD CONSTRAINT rights_show_order_unique UNIQUE (show_order);
    EXCEPTION
        WHEN unique_violation THEN
        RAISE 'Cannot set UNIQUE constraint on rights.show_order because of duplicated values';
    END;
END;
$$;

--
-- Keep more information on transfers
--

DO $$
BEGIN
    BEGIN
        ALTER TABLE transfer_log ADD COLUMN transfer_mode varchar(255) DEFAULT 'unknown';
    EXCEPTION
        WHEN duplicate_column THEN
        -- do nothing
    END;
END;
$$;

DO $$
BEGIN
    BEGIN
        ALTER TABLE transfer_log ADD COLUMN hidden boolean DEFAULT false;
    EXCEPTION
        WHEN duplicate_column THEN
        -- do nothing
    END;
END;
$$;

DO $$
BEGIN
    BEGIN
        ALTER TABLE transfer_log ADD COLUMN fileinfo json NOT NULL DEFAULT '{}';
    EXCEPTION
        WHEN duplicate_column THEN
        -- do nothing
    END;
END;
$$;

--
-- Add params to flows
--

DO $$
BEGIN
    BEGIN
        ALTER TABLE flows ADD COLUMN params json NOT NULL DEFAULT '{}';
    EXCEPTION
        WHEN duplicate_column THEN
        -- do nothing
    END;
END;
$$;

UPDATE flows SET params = '{"hash_algo":"MD5"}' WHERE params->>'hash_algo' = '';
UPDATE flows SET params = '{"filetype_enabled":"true","filetype_mode":"extension","filetypes":"zip","hash_algo":"MD5"}' where template = 'déploiement_config';


--
-- Add distant directories to FTP Server partners
--

DO $$
BEGIN
    BEGIN
        ALTER TABLE partners ADD COLUMN ftp_server_directory text DEFAULT '/';
    EXCEPTION
        WHEN duplicate_column THEN
        -- do nothing
    END;
END;
$$;


--
-- Add index on transfer_log.filename
--


DROP INDEX IF EXISTS transfer_log_filename_idx;
CREATE INDEX transfer_log_filename_idx ON transfer_log(filename);

COMMIT;