--
-- PostgreSQL database dump
--

-- Dumped from database version 16.2
-- Dumped by pg_dump version 16.2

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: public; Type: SCHEMA; Schema: -; Owner: postgres
--

-- *not* creating schema, since initdb creates it


ALTER SCHEMA public OWNER TO postgres;

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

CREATE EXTENSION IF NOT EXISTS unaccent WITH SCHEMA public;


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

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


--
-- Name: delete_unused_tags(); Type: FUNCTION; Schema: public; Owner: f0ck
--

CREATE FUNCTION public.delete_unused_tags() RETURNS trigger
    LANGUAGE plpgsql
    AS $$
begin
  delete from tags
  where
    tags.id not in (select tag_id from tags_assign) and
    tags.id = OLD.tag_id and
    tags.tag != 'sfw' and
    tags.tag != 'nsfw';
  return OLD;
end $$;


ALTER FUNCTION public.delete_unused_tags() OWNER TO f0ck;

--
-- Name: fill_normalized(); Type: FUNCTION; Schema: public; Owner: f0ck
--

CREATE FUNCTION public.fill_normalized() RETURNS trigger
    LANGUAGE plpgsql
    AS $$
begin
  NEW.normalized = slugify(NEW.tag);
  return NEW;
end$$;


ALTER FUNCTION public.fill_normalized() OWNER TO f0ck;

--
-- Name: slugify(text); Type: FUNCTION; Schema: public; Owner: f0ck
--

CREATE FUNCTION public.slugify(v text) RETURNS text
    LANGUAGE plpgsql
    AS $$
BEGIN
  RETURN trim(BOTH '-' FROM regexp_replace(lower(unaccent(trim(v))), '[\u0000-\u002f \u003a-\u0040\u005b-\u0060\u007b-\u00bf]+', '', 'gi'));
END;
$$;


ALTER FUNCTION public.slugify(v text) OWNER TO f0ck;

--
-- Name: unaccent_text(text); Type: FUNCTION; Schema: public; Owner: f0ck
--

CREATE FUNCTION public.unaccent_text(text) RETURNS text
    LANGUAGE sql IMMUTABLE COST 1
    AS $_$
  -- unaccent is STABLE, but the indexes must use IMMUTABLE functions.
  -- comment this line out when calling pg_dump.
  SELECT unaccent($1);

  -- Uncomment this line when calling pg_dump.
  --SELECT ''::text;
$_$;


ALTER FUNCTION public.unaccent_text(text) OWNER TO f0ck;

SET default_tablespace = '';

SET default_table_access_method = heap;

--
-- Name: favorites; Type: TABLE; Schema: public; Owner: f0ck
--

CREATE TABLE public.favorites (
    user_id integer NOT NULL,
    item_id integer NOT NULL
);


ALTER TABLE public.favorites OWNER TO f0ck;

--
-- Name: items_id_seq; Type: SEQUENCE; Schema: public; Owner: f0ck
--

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


ALTER SEQUENCE public.items_id_seq OWNER TO f0ck;

--
-- Name: items; Type: TABLE; Schema: public; Owner: f0ck
--

CREATE TABLE public.items (
    id integer DEFAULT nextval('public.items_id_seq'::regclass) NOT NULL,
    src character varying(255) NOT NULL,
    dest character varying(40) NOT NULL,
    mime character varying(100) NOT NULL,
    size integer NOT NULL,
    checksum character varying(255) NOT NULL,
    username character varying(40) NOT NULL,
    userchannel character varying(255) NOT NULL,
    usernetwork character varying(40) NOT NULL,
    stamp integer NOT NULL,
    active boolean NOT NULL,
    thumb character varying(100)
);


ALTER TABLE public.items OWNER TO f0ck;

--
-- Name: COLUMN items.src; Type: COMMENT; Schema: public; Owner: f0ck
--

COMMENT ON COLUMN public.items.src IS 'src-Link';


--
-- Name: COLUMN items.dest; Type: COMMENT; Schema: public; Owner: f0ck
--

COMMENT ON COLUMN public.items.dest IS 'filename';


--
-- Name: items_li; Type: VIEW; Schema: public; Owner: f0ck
--

CREATE VIEW public.items_li AS
SELECT
    NULL::integer AS id,
    NULL::character varying(255) AS src,
    NULL::character varying(40) AS dest,
    NULL::character varying(100) AS mime,
    NULL::integer AS size,
    NULL::character varying(255) AS checksum,
    NULL::character varying(40) AS username,
    NULL::character varying(255) AS userchannel,
    NULL::character varying(40) AS usernetwork,
    NULL::integer AS stamp;


ALTER VIEW public.items_li OWNER TO f0ck;

--
-- Name: tags_assign; Type: TABLE; Schema: public; Owner: f0ck
--

CREATE TABLE public.tags_assign (
    item_id integer NOT NULL,
    tag_id integer NOT NULL,
    user_id integer DEFAULT 10 NOT NULL
);


ALTER TABLE public.tags_assign OWNER TO f0ck;

--
-- Name: tags_nsfp; Type: TABLE; Schema: public; Owner: f0ck
--

CREATE TABLE public.tags_nsfp (
    id integer NOT NULL
);


ALTER TABLE public.tags_nsfp OWNER TO f0ck;

--
-- Name: items_sfw; Type: VIEW; Schema: public; Owner: f0ck
--

CREATE VIEW public.items_sfw AS
 SELECT ( SELECT
                CASE
                    WHEN (tags_assign.tag_id > 0) THEN tags_assign.tag_id
                    ELSE 0
                END AS "case"
           FROM public.tags_assign
          WHERE ((tags_assign.tag_id = ANY (ARRAY[1, 2])) AND (tags_assign.item_id = items.id))) AS sfw,
    ( SELECT
                CASE
                    WHEN (tags_assign.tag_id > 0) THEN 1
                    ELSE 0
                END AS "case"
           FROM public.tags_assign
          WHERE ((tags_assign.tag_id IN ( SELECT tags_nsfp.id
                   FROM public.tags_nsfp)) AND (tags_assign.item_id = items.id))
         LIMIT 1) AS nsfp,
    id,
    src,
    dest,
    mime,
    size,
    checksum,
    username,
    userchannel,
    usernetwork,
    stamp,
    active
   FROM public.items;


ALTER VIEW public.items_sfw OWNER TO f0ck;

--
-- Name: tags_id_seq; Type: SEQUENCE; Schema: public; Owner: f0ck
--

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


ALTER SEQUENCE public.tags_id_seq OWNER TO f0ck;

--
-- Name: tags; Type: TABLE; Schema: public; Owner: f0ck
--

CREATE TABLE public.tags (
    id integer DEFAULT nextval('public.tags_id_seq'::regclass) NOT NULL,
    tag character varying(70) NOT NULL,
    normalized character varying(70) NOT NULL
);


ALTER TABLE public.tags OWNER TO f0ck;

--
-- Name: user_id_seq; Type: SEQUENCE; Schema: public; Owner: f0ck
--

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


ALTER SEQUENCE public.user_id_seq OWNER TO f0ck;

--
-- Name: user; Type: TABLE; Schema: public; Owner: f0ck
--

CREATE TABLE public."user" (
    id integer DEFAULT nextval('public.user_id_seq'::regclass) NOT NULL,
    login character varying(255) NOT NULL,
    "user" character varying(255) NOT NULL,
    password character varying(167) NOT NULL,
    admin boolean NOT NULL,
    created_at timestamp without time zone DEFAULT now() NOT NULL
);


ALTER TABLE public."user" OWNER TO f0ck;

--
-- Name: items_tags; Type: VIEW; Schema: public; Owner: f0ck
--

CREATE VIEW public.items_tags AS
 SELECT ( SELECT
                CASE
                    WHEN (tags_assign.tag_id > 0) THEN tags_assign.tag_id
                    ELSE 0
                END AS "case"
           FROM public.tags_assign
          WHERE ((tags_assign.tag_id = ANY (ARRAY[1, 2])) AND (tags_assign.item_id = items.id))) AS sfw,
    ( SELECT
                CASE
                    WHEN (tags_assign.tag_id > 0) THEN 1
                    ELSE 0
                END AS "case"
           FROM public.tags_assign
          WHERE ((tags_assign.tag_id IN ( SELECT tags_nsfp.id
                   FROM public.tags_nsfp)) AND (tags_assign.item_id = items.id))
         LIMIT 1) AS nsfp,
    ( SELECT jsonb_agg(jsonb_build_object('id', tags.id, 'normalized', tags.normalized)) AS jsonb_agg
           FROM (public.tags_assign
             LEFT JOIN public.tags ON ((tags.id = tags_assign.tag_id)))
          WHERE (tags_assign.item_id = items.id)) AS tags,
    ( SELECT jsonb_agg(jsonb_build_object('id', favorites.user_id, 'user', "user"."user")) AS jsonb_agg
           FROM (public.favorites
             LEFT JOIN public."user" ON (("user".id = favorites.user_id)))
          WHERE (favorites.item_id = items.id)) AS favs,
    id,
    src,
    dest,
    mime,
    size,
    checksum,
    username,
    userchannel,
    usernetwork,
    stamp,
    active
   FROM public.items;


ALTER VIEW public.items_tags OWNER TO f0ck;

--
-- Name: tags_alias; Type: TABLE; Schema: public; Owner: f0ck
--

CREATE TABLE public.tags_alias (
    tag_orig_id integer NOT NULL,
    tag_alias character varying NOT NULL
);


ALTER TABLE public.tags_alias OWNER TO f0ck;

--
-- Name: user_alias; Type: TABLE; Schema: public; Owner: f0ck
--

CREATE TABLE public.user_alias (
    userid integer NOT NULL,
    alias character varying(255) NOT NULL
);

ALTER TABLE ONLY public.user_alias REPLICA IDENTITY FULL;


ALTER TABLE public.user_alias OWNER TO f0ck;

--
-- Name: user_options; Type: TABLE; Schema: public; Owner: f0ck
--

CREATE TABLE public.user_options (
    user_id integer NOT NULL,
    mode integer NOT NULL,
    theme character varying(50) NOT NULL,
    avatar integer DEFAULT 56660 NOT NULL,
    fullscreen smallint DEFAULT '0'::smallint NOT NULL
);


ALTER TABLE public.user_options OWNER TO f0ck;

--
-- Name: user_sessions_id_seq; Type: SEQUENCE; Schema: public; Owner: f0ck
--

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


ALTER SEQUENCE public.user_sessions_id_seq OWNER TO f0ck;

--
-- Name: user_sessions; Type: TABLE; Schema: public; Owner: f0ck
--

CREATE TABLE public.user_sessions (
    id integer DEFAULT nextval('public.user_sessions_id_seq'::regclass) NOT NULL,
    user_id integer NOT NULL,
    session character varying(32) NOT NULL,
    browser character varying(255) NOT NULL,
    created_at integer NOT NULL,
    last_used integer NOT NULL,
    last_action character varying(255) NOT NULL,
    kmsi smallint DEFAULT '0'::smallint NOT NULL
);


ALTER TABLE public.user_sessions OWNER TO f0ck;

--
-- Name: favorites idx_16521_primary; Type: CONSTRAINT; Schema: public; Owner: f0ck
--

ALTER TABLE ONLY public.favorites
    ADD CONSTRAINT idx_16521_primary PRIMARY KEY (user_id, item_id);


--
-- Name: items idx_16526_primary; Type: CONSTRAINT; Schema: public; Owner: f0ck
--

ALTER TABLE ONLY public.items
    ADD CONSTRAINT idx_16526_primary PRIMARY KEY (id);


--
-- Name: user idx_16554_primary; Type: CONSTRAINT; Schema: public; Owner: f0ck
--

ALTER TABLE ONLY public."user"
    ADD CONSTRAINT idx_16554_primary PRIMARY KEY (id);


--
-- Name: user_options idx_16567_user_id; Type: CONSTRAINT; Schema: public; Owner: f0ck
--

ALTER TABLE ONLY public.user_options
    ADD CONSTRAINT idx_16567_user_id UNIQUE (user_id);


--
-- Name: user_sessions idx_16572_primary; Type: CONSTRAINT; Schema: public; Owner: f0ck
--

ALTER TABLE ONLY public.user_sessions
    ADD CONSTRAINT idx_16572_primary PRIMARY KEY (id);


--
-- Name: items items_checksum; Type: CONSTRAINT; Schema: public; Owner: f0ck
--

ALTER TABLE ONLY public.items
    ADD CONSTRAINT items_checksum UNIQUE (checksum);


--
-- Name: tags_alias tags_alias_tag_alias_tag_orig_id; Type: CONSTRAINT; Schema: public; Owner: f0ck
--

ALTER TABLE ONLY public.tags_alias
    ADD CONSTRAINT tags_alias_tag_alias_tag_orig_id UNIQUE (tag_alias, tag_orig_id);


--
-- Name: tags_alias tags_alias_tag_orig_id; Type: CONSTRAINT; Schema: public; Owner: f0ck
--

ALTER TABLE ONLY public.tags_alias
    ADD CONSTRAINT tags_alias_tag_orig_id PRIMARY KEY (tag_orig_id);


--
-- Name: tags_assign tags_assign_item_id_tag_id_primary; Type: CONSTRAINT; Schema: public; Owner: f0ck
--

ALTER TABLE ONLY public.tags_assign
    ADD CONSTRAINT tags_assign_item_id_tag_id_primary PRIMARY KEY (item_id, tag_id);


--
-- Name: tags_assign tags_assign_item_id_tag_id_unique; Type: CONSTRAINT; Schema: public; Owner: f0ck
--

ALTER TABLE ONLY public.tags_assign
    ADD CONSTRAINT tags_assign_item_id_tag_id_unique UNIQUE (item_id, tag_id);


--
-- Name: tags tags_id; Type: CONSTRAINT; Schema: public; Owner: f0ck
--

ALTER TABLE ONLY public.tags
    ADD CONSTRAINT tags_id PRIMARY KEY (id);


--
-- Name: tags tags_normalized; Type: CONSTRAINT; Schema: public; Owner: f0ck
--

ALTER TABLE ONLY public.tags
    ADD CONSTRAINT tags_normalized UNIQUE (normalized);


--
-- Name: tags tags_tag; Type: CONSTRAINT; Schema: public; Owner: f0ck
--

ALTER TABLE ONLY public.tags
    ADD CONSTRAINT tags_tag UNIQUE (tag);


--
-- Name: user_options user_options_user_id; Type: CONSTRAINT; Schema: public; Owner: f0ck
--

ALTER TABLE ONLY public.user_options
    ADD CONSTRAINT user_options_user_id PRIMARY KEY (user_id);


--
-- Name: items_li _RETURN; Type: RULE; Schema: public; Owner: f0ck
--

CREATE OR REPLACE VIEW public.items_li AS
 SELECT items.id,
    items.src,
    items.dest,
    items.mime,
    items.size,
    items.checksum,
    items.username,
    items.userchannel,
    items.usernetwork,
    items.stamp
   FROM ((public.items
     JOIN public.tags_assign ta1 ON (((ta1.tag_id = 1) AND (ta1.item_id = items.id))))
     JOIN public.tags_assign ta2 ON (((NOT (ta2.tag_id IN ( SELECT tags_nsfp.id
           FROM public.tags_nsfp))) AND (ta2.item_id = items.id))))
  WHERE items.active
  GROUP BY items.id;


--
-- Name: tags_assign tags_assign_ad; Type: TRIGGER; Schema: public; Owner: f0ck
--

CREATE TRIGGER tags_assign_ad AFTER DELETE ON public.tags_assign FOR EACH ROW EXECUTE FUNCTION public.delete_unused_tags();


--
-- Name: tags tags_bi; Type: TRIGGER; Schema: public; Owner: f0ck
--

CREATE TRIGGER tags_bi BEFORE INSERT ON public.tags FOR EACH ROW EXECUTE FUNCTION public.fill_normalized();


--
-- Name: tags tags_bu; Type: TRIGGER; Schema: public; Owner: f0ck
--

CREATE TRIGGER tags_bu BEFORE UPDATE ON public.tags FOR EACH ROW EXECUTE FUNCTION public.fill_normalized();


--
-- Name: favorites favorites_item_id_fkey; Type: FK CONSTRAINT; Schema: public; Owner: f0ck
--

ALTER TABLE ONLY public.favorites
    ADD CONSTRAINT favorites_item_id_fkey FOREIGN KEY (item_id) REFERENCES public.items(id) ON DELETE CASCADE;


--
-- Name: favorites favorites_user_id_fkey; Type: FK CONSTRAINT; Schema: public; Owner: f0ck
--

ALTER TABLE ONLY public.favorites
    ADD CONSTRAINT favorites_user_id_fkey FOREIGN KEY (user_id) REFERENCES public."user"(id) ON DELETE CASCADE;


--
-- Name: tags_alias tags_alias_tag_orig_id_fkey; Type: FK CONSTRAINT; Schema: public; Owner: f0ck
--

ALTER TABLE ONLY public.tags_alias
    ADD CONSTRAINT tags_alias_tag_orig_id_fkey FOREIGN KEY (tag_orig_id) REFERENCES public.tags(id) ON DELETE CASCADE;


--
-- Name: tags_assign tags_assign_item_id_fkey; Type: FK CONSTRAINT; Schema: public; Owner: f0ck
--

ALTER TABLE ONLY public.tags_assign
    ADD CONSTRAINT tags_assign_item_id_fkey FOREIGN KEY (item_id) REFERENCES public.items(id) ON DELETE CASCADE;


--
-- Name: tags_assign tags_assign_tag_id_fkey; Type: FK CONSTRAINT; Schema: public; Owner: f0ck
--

ALTER TABLE ONLY public.tags_assign
    ADD CONSTRAINT tags_assign_tag_id_fkey FOREIGN KEY (tag_id) REFERENCES public.tags(id) ON DELETE CASCADE;


--
-- Name: tags_assign tags_assign_user_id_fkey; Type: FK CONSTRAINT; Schema: public; Owner: f0ck
--

ALTER TABLE ONLY public.tags_assign
    ADD CONSTRAINT tags_assign_user_id_fkey FOREIGN KEY (user_id) REFERENCES public."user"(id) ON DELETE SET DEFAULT;


--
-- Name: user_options user_options_avatar_fkey; Type: FK CONSTRAINT; Schema: public; Owner: f0ck
--

ALTER TABLE ONLY public.user_options
    ADD CONSTRAINT user_options_avatar_fkey FOREIGN KEY (avatar) REFERENCES public.items(id) ON DELETE SET DEFAULT;


--
-- Name: user_options user_options_user_id_fkey; Type: FK CONSTRAINT; Schema: public; Owner: f0ck
--

ALTER TABLE ONLY public.user_options
    ADD CONSTRAINT user_options_user_id_fkey FOREIGN KEY (user_id) REFERENCES public."user"(id) ON DELETE CASCADE;


--
-- Name: user_sessions user_sessions_user_id_fkey; Type: FK CONSTRAINT; Schema: public; Owner: f0ck
--

ALTER TABLE ONLY public.user_sessions
    ADD CONSTRAINT user_sessions_user_id_fkey FOREIGN KEY (user_id) REFERENCES public."user"(id) ON DELETE CASCADE;


--
-- Name: alltables; Type: PUBLICATION; Schema: -; Owner: postgres
--

CREATE PUBLICATION alltables FOR ALL TABLES WITH (publish = 'insert, update, delete, truncate');


ALTER PUBLICATION alltables OWNER TO postgres;

--
-- Name: SCHEMA public; Type: ACL; Schema: -; Owner: postgres
--

REVOKE USAGE ON SCHEMA public FROM PUBLIC;
GRANT ALL ON SCHEMA public TO PUBLIC;


--
-- PostgreSQL database dump complete
--

