--
-- PostgreSQL database dump
--

-- Dumped from database version 15.12 (Debian 15.12-0+deb12u2)
-- Dumped by pg_dump version 15.12 (Debian 15.12-0+deb12u2)

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;

--
-- Name: btree_gin; Type: EXTENSION; Schema: -; Owner: -
--

CREATE EXTENSION IF NOT EXISTS btree_gin WITH SCHEMA public;


--
-- Name: EXTENSION btree_gin; Type: COMMENT; Schema: -; Owner: 
--

COMMENT ON EXTENSION btree_gin IS 'support for indexing common datatypes in GIN';


--
-- Name: pg_trgm; Type: EXTENSION; Schema: -; Owner: -
--

CREATE EXTENSION IF NOT EXISTS pg_trgm WITH SCHEMA public;


--
-- Name: EXTENSION pg_trgm; Type: COMMENT; Schema: -; Owner: 
--

COMMENT ON EXTENSION pg_trgm IS 'text similarity measurement and index searching based on trigrams';


--
-- Name: archive_after_n_days(integer); Type: PROCEDURE; Schema: public; Owner: salt
--

CREATE PROCEDURE public.archive_after_n_days(IN days integer)
    LANGUAGE plpgsql
    AS $$
BEGIN
  INSERT INTO salt_events_archive (id, tag, data, alter_time, master_id) (SELECT id, tag, data, alter_time, master_id FROM salt_events WHERE alter_time < current_date - format('%s day', days)::interval);
  DELETE FROM salt_events WHERE alter_time < current_date - format('%s day', days)::interval;
END;$$;


ALTER PROCEDURE public.archive_after_n_days(IN days integer) OWNER TO salt;

--
-- Name: cleanup(integer); Type: PROCEDURE; Schema: public; Owner: salt
--

CREATE PROCEDURE public.cleanup(IN days integer)
    LANGUAGE plpgsql
    AS $$
BEGIN
  EXECUTE 'call archive_after_n_days('||days||');';
  EXECUTE 'call drop_archive_after_n_days('||days||')';
END;$$;


ALTER PROCEDURE public.cleanup(IN days integer) OWNER TO salt;

--
-- Name: delete_inc(text); Type: FUNCTION; Schema: public; Owner: salt
--

CREATE FUNCTION public.delete_inc(sdate text) RETURNS void
    LANGUAGE plpgsql
    AS $$
BEGIN
   DELETE FROM salt_events_archive WHERE alter_time < TO_DATE(sdate, "%Y-%m-%d %H:%i:%s");
END;
$$;


ALTER FUNCTION public.delete_inc(sdate text) OWNER TO salt;

--
-- Name: drop_archive_after_n_days(integer); Type: PROCEDURE; Schema: public; Owner: salt
--

CREATE PROCEDURE public.drop_archive_after_n_days(IN days integer)
    LANGUAGE plpgsql
    AS $$
BEGIN
  DELETE FROM salt_events_archive WHERE alter_time <= current_date - format('%s day', days)::interval;
END;$$;


ALTER PROCEDURE public.drop_archive_after_n_days(IN days integer) OWNER TO salt;

--
-- Name: seq_salt_events_id; Type: SEQUENCE; Schema: public; Owner: salt
--

CREATE SEQUENCE public.seq_salt_events_id
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;


ALTER TABLE public.seq_salt_events_id OWNER TO salt;

SET default_tablespace = '';

SET default_table_access_method = heap;

--
-- Name: salt_events; Type: TABLE; Schema: public; Owner: salt
--

CREATE TABLE public.salt_events (
    id bigint DEFAULT nextval('public.seq_salt_events_id'::regclass) NOT NULL,
    tag character varying(255) NOT NULL,
    data jsonb NOT NULL,
    alter_time timestamp with time zone DEFAULT now(),
    master_id character varying(255) NOT NULL
);


ALTER TABLE public.salt_events OWNER TO salt;

--
-- Name: auth; Type: VIEW; Schema: public; Owner: salt
--

CREATE VIEW public.auth AS
 SELECT salt_events.alter_time AS "time",
    json_extract_path_text((salt_events.data)::json, VARIADIC ARRAY['id'::text]) AS hostname
   FROM public.salt_events
  WHERE ((salt_events.tag)::text = 'salt/auth'::text)
  ORDER BY salt_events.alter_time DESC;


ALTER TABLE public.auth OWNER TO salt;

--
-- Name: highstate; Type: VIEW; Schema: public; Owner: salt
--

CREATE VIEW public.highstate AS
 SELECT se.id,
    se.tag,
    se.data,
    se.alter_time
   FROM public.salt_events se
  WHERE ((jsonb_extract_path_text(se.data, VARIADIC ARRAY['fun'::text]) = 'state.highstate'::text) OR (jsonb_extract_path_text(se.data, VARIADIC ARRAY['fun'::text]) = 'state.apply'::text))
  ORDER BY se.id DESC
 LIMIT 100;


ALTER TABLE public.highstate OWNER TO salt;

--
-- Name: highstate_errors; Type: VIEW; Schema: public; Owner: salt
--

CREATE VIEW public.highstate_errors AS
 SELECT se.id,
    se.tag,
    se.alter_time AS "time",
    (se.data ->> 'id'::text) AS hostname,
    split_part(dt.key, '_|-'::text, 2) AS salt_state,
    (dt.value ->> 'comment'::text) AS comment,
    ((dt.value ->> 'duration'::text))::double precision AS duration
   FROM public.salt_events se,
    LATERAL jsonb_each((se.data -> 'return'::text)) dt(key, value)
  WHERE ((((se.data ->> 'fun'::text) = 'state.highstate'::text) OR ((se.data ->> 'fun'::text) = 'state.apply'::text)) AND ((se.data ->> 'id'::text) IS NOT NULL) AND ((se.data ->> 'success'::text) IS NOT NULL) AND (NOT ((dt.value ->> 'result'::text))::boolean) AND (jsonb_typeof((se.data -> 'return'::text)) = 'object'::text) AND (se.alter_time > (CURRENT_DATE - '1 day'::interval)))
  ORDER BY se.alter_time DESC, dt.key DESC;


ALTER TABLE public.highstate_errors OWNER TO salt;

--
-- Name: jids; Type: TABLE; Schema: public; Owner: salt
--

CREATE TABLE public.jids (
    jid character varying(20) NOT NULL,
    load text NOT NULL
);


ALTER TABLE public.jids OWNER TO salt;

--
-- Name: pillar; Type: TABLE; Schema: public; Owner: salt
--

CREATE TABLE public.pillar (
    value1 character varying,
    minion_pattern character varying
);


ALTER TABLE public.pillar OWNER TO salt;

--
-- Name: salt_events_archive; Type: TABLE; Schema: public; Owner: salt
--

CREATE TABLE public.salt_events_archive (
    id bigint NOT NULL,
    tag character varying(255) NOT NULL,
    data jsonb NOT NULL,
    alter_time timestamp with time zone,
    master_id character varying(255) NOT NULL
);


ALTER TABLE public.salt_events_archive OWNER TO salt;

--
-- Name: salt_returns; Type: TABLE; Schema: public; Owner: salt
--

CREATE TABLE public.salt_returns (
    fun character varying(50) NOT NULL,
    jid character varying(255) NOT NULL,
    return text NOT NULL,
    full_ret text,
    id character varying(255) NOT NULL,
    success character varying(10) NOT NULL,
    alter_time timestamp with time zone DEFAULT now()
);


ALTER TABLE public.salt_returns OWNER TO salt;

--
-- Name: jids jids_pkey; Type: CONSTRAINT; Schema: public; Owner: salt
--

ALTER TABLE ONLY public.jids
    ADD CONSTRAINT jids_pkey PRIMARY KEY (jid);


--
-- Name: salt_events salt_events_id_key; Type: CONSTRAINT; Schema: public; Owner: salt
--

ALTER TABLE ONLY public.salt_events
    ADD CONSTRAINT salt_events_id_key UNIQUE (id);


--
-- Name: idx_alter_time; Type: INDEX; Schema: public; Owner: salt
--

CREATE INDEX idx_alter_time ON public.salt_events USING btree (alter_time);


--
-- Name: idx_fun; Type: INDEX; Schema: public; Owner: salt
--

CREATE INDEX idx_fun ON public.salt_events USING btree (((data ->> 'fun'::text)));


--
-- Name: idx_id; Type: INDEX; Schema: public; Owner: salt
--

CREATE INDEX idx_id ON public.salt_events USING btree (((data ->> 'id'::text)));


--
-- Name: idx_result; Type: INDEX; Schema: public; Owner: salt
--

CREATE INDEX idx_result ON public.salt_events USING btree (((data ->> 'result'::text)));


--
-- Name: idx_salt_events_tag; Type: INDEX; Schema: public; Owner: salt
--

CREATE INDEX idx_salt_events_tag ON public.salt_events USING btree (tag);


--
-- Name: idx_salt_returns_fun; Type: INDEX; Schema: public; Owner: salt
--

CREATE INDEX idx_salt_returns_fun ON public.salt_returns USING btree (fun);


--
-- Name: idx_salt_returns_id; Type: INDEX; Schema: public; Owner: salt
--

CREATE INDEX idx_salt_returns_id ON public.salt_returns USING btree (id);


--
-- Name: idx_salt_returns_jid; Type: INDEX; Schema: public; Owner: salt
--

CREATE INDEX idx_salt_returns_jid ON public.salt_returns USING btree (jid);


--
-- Name: idx_salt_returns_updated; Type: INDEX; Schema: public; Owner: salt
--

CREATE INDEX idx_salt_returns_updated ON public.salt_returns USING btree (alter_time);


--
-- Name: idx_success; Type: INDEX; Schema: public; Owner: salt
--

CREATE INDEX idx_success ON public.salt_events USING btree (((data ->> 'success'::text)));


--
-- PostgreSQL database dump complete
--

