--
-- PostgreSQL database dump
--

SET statement_timeout = 0;
SET lock_timeout = 0;
SET client_encoding = 'UTF8';
SET standard_conforming_strings = on;
SET check_function_bodies = false;
SET client_min_messages = warning;

--
-- Name: plpgsql; Type: EXTENSION; Schema: -; 
--

CREATE EXTENSION IF NOT EXISTS plpgsql WITH SCHEMA pg_catalog;


--
-- Name: EXTENSION plpgsql; Type: COMMENT; Schema: -; 
--

COMMENT ON EXTENSION plpgsql IS 'PL/pgSQL procedural language';


--
-- Name: unaccent; Type: EXTENSION; Schema: -; 
--

CREATE EXTENSION IF NOT EXISTS unaccent WITH SCHEMA public;


--
-- Name: EXTENSION unaccent; Type: COMMENT; Schema: -; 
--

COMMENT ON EXTENSION unaccent IS 'text search dictionary that removes accents';


--
-- Name: uuid-ossp; Type: EXTENSION; Schema: -; 
--

CREATE EXTENSION IF NOT EXISTS "uuid-ossp" WITH SCHEMA public;


--
-- Name: EXTENSION "uuid-ossp"; Type: COMMENT; Schema: -; 
--

COMMENT ON EXTENSION "uuid-ossp" IS 'generate universally unique identifiers (UUIDs)';


SET search_path = public, pg_catalog;

--
-- Name: get_uuid(); Type: FUNCTION; Schema: public; 
--

CREATE FUNCTION get_uuid() RETURNS uuid
    LANGUAGE sql
    AS $$SELECT * FROM uuid_generate_v1()$$;

--
-- Name: lu_on_row_delete(); Type: FUNCTION; Schema: public; 
--

CREATE FUNCTION lu_on_row_delete() RETURNS trigger
    LANGUAGE plpgsql
    AS $$BEGIN
INSERT INTO lu_entities_history ("table", "data") VALUES(TG_TABLE_NAME, to_json(OLD));
RETURN OLD;
END$$;

--
-- Name: lu_on_row_update(); Type: FUNCTION; Schema: public; 
--

CREATE FUNCTION lu_on_row_update() RETURNS trigger
    LANGUAGE plpgsql
    AS $$BEGIN
NEW.updated_at := now();
INSERT INTO lu_entities_history ("table", "data") VALUES(TG_TABLE_NAME, to_json(OLD));
return NEW;
END$$;

--
-- Name: lu_text_match_prepare(text); Type: FUNCTION; Schema: public; Owner: dev
--

CREATE FUNCTION lu_text_match_prepare(data text) RETURNS text
    LANGUAGE sql
        AS $$SELECT regexp_replace(unaccent(data), '[^a-zA-Z0-9]+', ' ', 'g')$$;

--
-- Name: lu_text_match(text, text); Type: FUNCTION; Schema: public; 
--

CREATE FUNCTION lu_text_match(query text, data text) RETURNS boolean
    LANGUAGE sql
    AS $$SELECT lu_text_match_prepare(data) ILIKE  ('%' || lu_text_match_prepare(query) || '%') AS result$$;

--
-- Name: lu_texts_match(text, text[]); Type: FUNCTION; Schema: public; 
--

CREATE FUNCTION lu_texts_match(query text, VARIADIC data text[]) RETURNS boolean
    LANGUAGE sql
    AS $$
WITH q AS (
SELECT unnest(data) AS d UNION SELECT array_to_string(data, ' ')
)
SELECT lu_text_match(query, d.d) AS res FROM q d ORDER BY res DESC LIMIT 1
$$;

SET default_tablespace = '';

SET default_with_oids = false;

--
-- Name: lu_entities_history; Type: TABLE; Schema: public; ; Tablespace:
--

CREATE TABLE lu_entities_history (
    id uuid DEFAULT get_uuid() NOT NULL,
    "table" character varying(128) NOT NULL,
    data json,
    date timestamp with time zone DEFAULT now() NOT NULL
);

--
-- Name: lu_entities_history_pkey; Type: CONSTRAINT; Schema: public; ; Tablespace:
--

ALTER TABLE ONLY lu_entities_history
    ADD CONSTRAINT lu_entities_history_pkey PRIMARY KEY (id);

--
-- PostgreSQL database dump complete
--