BEGIN;

--
-- changes instance with partner
--

DO $$
BEGIN
    BEGIN
        ALTER TABLE instances RENAME TO partners;
    EXCEPTION
    WHEN undefined_table THEN
        -- do nothing
    END;
END;
$$;

DO $$
BEGIN
    BEGIN
        ALTER INDEX instances_pkey RENAME TO partners_pkey;
    EXCEPTION
        WHEN undefined_table THEN
        -- do nothing
    END;
END;
$$;

DO $$
BEGIN
    BEGIN
        ALTER INDEX instances_hostid_key RENAME TO partners_hostid_pkey;
    EXCEPTION
        WHEN undefined_table THEN
        -- do nothing
    END;
END;
$$;

DO $$
BEGIN
    BEGIN
        ALTER INDEX instances_hostidssl_key RENAME TO partner_hostidssl_pkey;
    EXCEPTION
        WHEN undefined_table THEN
        -- do nothing
    END;
END;
$$;

DO $$
BEGIN
    BEGIN
        ALTER TABLE instance_data RENAME TO partner_data;
    EXCEPTION
    WHEN undefined_table THEN
        -- do nothing
    END;
END;
$$;

DO $$
BEGIN
    BEGIN
        ALTER TABLE partner_data RENAME COLUMN instance_id TO partner_id;
    EXCEPTION
        WHEN undefined_column THEN
        -- do nothing
    END;
END;
$$;

DO $$
BEGIN
    BEGIN
        ALTER INDEX instance_data_pkey RENAME TO partner_data_pkey;
    EXCEPTION
        WHEN undefined_table THEN
        -- do nothing
    END;
END;
$$;

DO $$
BEGIN
    BEGIN
        ALTER TABLE flow_dest RENAME COLUMN instance_id TO partner_id;
    EXCEPTION
        WHEN undefined_column THEN
        -- do nothing
    END;
END;
$$;

DO $$
BEGIN
    BEGIN
        ALTER TABLE flow_log_dest RENAME COLUMN instance_id TO partner_id;
    EXCEPTION
        WHEN undefined_column THEN
        -- do nothing
    END;
END;
$$;

UPDATE trace_entry SET target='partner' WHERE target = 'instance';
UPDATE rights SET target='partner' WHERE target = 'instance';

--
-- Ajout des colonnes pour la gestion des passerelles par sites
--
DO $$
BEGIN
    BEGIN
        ALTER TABLE sites ADD COLUMN int_ptp boolean DEFAULT true;
    EXCEPTION
        WHEN duplicate_column THEN
        -- do nothing
    END;
END;
$$;

DO $$
BEGIN
    BEGIN
        ALTER TABLE sites ADD COLUMN int_gateway bigint DEFAULT null;
    EXCEPTION
        WHEN duplicate_column THEN
        -- do nothing
    END;
END;
$$;

DO $$
BEGIN
    BEGIN
        ALTER TABLE sites ADD COLUMN ext_ptp boolean DEFAULT true;
    EXCEPTION
        WHEN duplicate_column THEN
        -- do nothing
    END;
END;
$$;

DO $$
BEGIN
    BEGIN
        ALTER TABLE sites ADD COLUMN ext_gateway bigint DEFAULT null;
    EXCEPTION
        WHEN duplicate_column THEN
        -- do nothing
    END;
END;
$$;

DO $$
BEGIN
    BEGIN
        ALTER TABLE sites ADD COLUMN cc_relay bigint DEFAULT null;
    EXCEPTION
        WHEN duplicate_column THEN
        -- do nothing
    END;
END;
$$;

--
-- Changements dans la typologie des serveurs
--

DO $$
BEGIN
    BEGIN
        ALTER TABLE partners ADD COLUMN is_client boolean;
    EXCEPTION
        WHEN duplicate_column THEN
        -- do nothing
    END;
END;
$$;

DO $$
BEGIN
    BEGIN
        ALTER TABLE partners ADD COLUMN is_server boolean;
    EXCEPTION
        WHEN duplicate_column THEN
        -- do nothing
    END;
END;
$$;

-- convert old types
UPDATE partners SET type='r66', is_client=true, is_server=true where type='server';
UPDATE partners SET type='r66', is_client=true, is_server=false where type='client';

-- Add FTP fields in table partners
DO $$
BEGIN
    BEGIN
        ALTER TABLE partners ADD COLUMN "gwftp" bigint;
    EXCEPTION
        WHEN duplicate_column THEN
        -- do nothing
    END;
END;
$$;

DO $$
BEGIN
    BEGIN
        ALTER TABLE partners ADD COLUMN "ftp_client_username" varchar(255);
    EXCEPTION
        WHEN duplicate_column THEN
        -- do nothing
    END;
END;
$$;

DO $$
BEGIN
    BEGIN
        ALTER TABLE partners ADD COLUMN "ftp_client_password" varchar(255);
    EXCEPTION
        WHEN duplicate_column THEN
        -- do nothing
    END;
END;
$$;

DO $$
BEGIN
    BEGIN
        ALTER TABLE partners ADD COLUMN "ftp_server_port" integer;
    EXCEPTION
        WHEN duplicate_column THEN
        -- do nothing
    END;
END;
$$;

DO $$
BEGIN
    BEGIN
        ALTER TABLE partners ADD COLUMN "ftp_server_username" varchar(255);
    EXCEPTION
        WHEN duplicate_column THEN
        -- do nothing
    END;
END;
$$;

DO $$
BEGIN
    BEGIN
        ALTER TABLE partners ADD COLUMN "ftp_server_password" varchar(255);
    EXCEPTION
        WHEN duplicate_column THEN
        -- do nothing
    END;
END;
$$;

DO $$
BEGIN
    BEGIN
        ALTER TABLE partners ADD COLUMN "ftp_server_secure" varchar(30);
    EXCEPTION
        WHEN duplicate_column THEN
        -- do nothing
    END;
END;
$$;

DO $$
BEGIN
    BEGIN
        ALTER TABLE partners ADD COLUMN "ftp_server_mode" varchar(30);
    EXCEPTION
        WHEN duplicate_column THEN
        -- do nothing
    END;
END;
$$;

-- Set description not null
DO $$
BEGIN
    BEGIN
        ALTER TABLE partners ALTER COLUMN "description" SET DEFAULT '';
        UPDATE partners SET description='' WHERE description IS NULL;
        ALTER TABLE partners ALTER COLUMN "description" SET NOT NULL;
    EXCEPTION
        WHEN duplicate_column THEN
        -- do nothing
    END;
END;
$$;


--
-- Lock records created with an external referential
--

DO $$
BEGIN
    BEGIN
        ALTER TABLE sites ADD COLUMN autosync boolean not null default false;
    EXCEPTION
        WHEN duplicate_column THEN
        -- do nothing
    END;
END;
$$;

DO $$
BEGIN
    BEGIN
        ALTER TABLE partners ADD COLUMN autosync boolean not null default false;
    EXCEPTION
        WHEN duplicate_column THEN
        -- do nothing
    END;
END;
$$;

--
-- Add details to audit trail
--

DO $$
BEGIN
    BEGIN
        ALTER TABLE trace_entry ADD COLUMN details text not null default '';
    EXCEPTION
        WHEN duplicate_column THEN
        -- do nothing
    END;
END;
$$;

COMMIT;