 DO $$
   BEGIN
     BEGIN
-      CREATE SCHEMA integration;
-
+      CREATE SCHEMA IF NOT EXISTS integration;
     END;
   END;
 $$;


GRANT SELECT, INSERT, UPDATE ON "integration"."channel_integration" TO "social";

CREATE INDEX  "channel_integration_token_idx" ON integration.channel_integration USING btree(token DESC NULLS LAST);


--


GRANT USAGE ON SEQUENCE "integration"."channel_integration_id_seq" TO "social";

-- Create "integration"."integration_type_constant_enum"
--
-- All of this to create a type if it does not exist
CREATE OR REPLACE FUNCTION create_integration_integration_type_constant_enum_type() RETURNS integer AS $$
DECLARE v_exists INTEGER;
BEGIN
    SELECT into v_exists (SELECT 1 FROM pg_type WHERE typname = 'integration_type_constant_enum');
    IF v_exists IS NULL THEN
      CREATE TYPE "integration"."integration_type_constant_enum" AS ENUM (
        'incoming',
        'outgoing'
      );
    END IF;
    RETURN v_exists;
END;
$$ LANGUAGE plpgsql;

-- Call the function
SELECT create_integration_integration_type_constant_enum_type();
-- Remove the function
DROP function create_integration_integration_type_constant_enum_type();


--
-- create integration table for storing general purpose integration definitions
--
