\connect "f0ck";

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;

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' and
    tags.tag != 'hentai' and
    tags.tag != 'audio';
  return OLD;
end $$;

ALTER FUNCTION public.delete_unused_tags() OWNER TO 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;

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;

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

ALTER TABLE public.favorites OWNER TO f0ck;

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

ALTER TABLE public.items_id_seq OWNER TO 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(100) 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;

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

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

ALTER TABLE public.tags_id_seq OWNER TO f0ck;

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

ALTER TABLE public.tags OWNER TO 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;

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

ALTER TABLE public.tags_assign OWNER TO f0ck;

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

ALTER TABLE public.user_id_seq OWNER TO 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,
    level integer NOT NULL
);

ALTER TABLE public."user" OWNER TO f0ck;

CREATE TABLE public.user_options (
    user_id integer NOT NULL,
    mode integer NOT NULL,
    theme character varying(50) NOT NULL,
    avatar integer DEFAULT 1 NOT NULL
);

ALTER TABLE public.user_options OWNER TO f0ck;

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

ALTER TABLE public.user_sessions_id_seq OWNER TO 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;

COPY public.items (id, src, dest, mime, size, checksum, username, userchannel, usernetwork, stamp, active, thumb) FROM stdin;
1	b761fa9339.png	b761fa9339.png	image/png	164	keinPlan	Flummi	#f0ck	n0xy	1471250800	t	
\.

COPY public.tags (id, tag, normalized) FROM stdin;
1	sfw	sfw
2	nsfw	nsfw
3	audio	audio
4	hentai	hentai
\.

COPY public.tags_assign (item_id, tag_id, user_id) FROM stdin;
1	1	1
\.

COPY public."user" (id, login, "user", password, level) FROM stdin;
1	autotagger	autotagger	f0ck you	0
2	deleted	deleted	f0ck you	0
\.

SELECT pg_catalog.setval('public.items_id_seq', 2, true);
SELECT pg_catalog.setval('public.tags_id_seq', 5, true);
SELECT pg_catalog.setval('public.user_id_seq', 3, true);
SELECT pg_catalog.setval('public.user_sessions_id_seq', 1, true);
ALTER TABLE ONLY public.favorites ADD CONSTRAINT idx_16521_primary PRIMARY KEY (user_id, item_id);
ALTER TABLE ONLY public.items ADD CONSTRAINT idx_16526_primary PRIMARY KEY (id);
ALTER TABLE ONLY public."user" ADD CONSTRAINT idx_16554_primary PRIMARY KEY (id);
ALTER TABLE ONLY public.user_options ADD CONSTRAINT idx_16567_user_id UNIQUE (user_id);
ALTER TABLE ONLY public.user_sessions ADD CONSTRAINT idx_16572_primary PRIMARY KEY (id);
ALTER TABLE ONLY public.items ADD CONSTRAINT items_checksum UNIQUE (checksum);
ALTER TABLE ONLY public.tags_alias ADD CONSTRAINT tags_alias_tag_alias_tag_orig_id UNIQUE (tag_alias, tag_orig_id);
ALTER TABLE ONLY public.tags_alias ADD CONSTRAINT tags_alias_tag_orig_id PRIMARY KEY (tag_orig_id);
ALTER TABLE ONLY public.tags_assign ADD CONSTRAINT tags_assign_item_id_tag_id_primary PRIMARY KEY (item_id, tag_id);
ALTER TABLE ONLY public.tags_assign ADD CONSTRAINT tags_assign_item_id_tag_id_unique UNIQUE (item_id, tag_id);
ALTER TABLE ONLY public.tags ADD CONSTRAINT tags_id PRIMARY KEY (id);
ALTER TABLE ONLY public.tags ADD CONSTRAINT tags_normalized UNIQUE (normalized);
ALTER TABLE ONLY public.tags ADD CONSTRAINT tags_tag UNIQUE (tag);
ALTER TABLE ONLY public.user_options ADD CONSTRAINT user_options_user_id PRIMARY KEY (user_id);
CREATE TRIGGER tags_assign_ad AFTER DELETE ON public.tags_assign FOR EACH ROW EXECUTE FUNCTION public.delete_unused_tags();
CREATE TRIGGER tags_bi BEFORE INSERT ON public.tags FOR EACH ROW EXECUTE FUNCTION public.fill_normalized();
CREATE TRIGGER tags_bu BEFORE UPDATE ON public.tags FOR EACH ROW EXECUTE FUNCTION public.fill_normalized();
ALTER TABLE ONLY public.favorites ADD CONSTRAINT favorites_item_id_fkey FOREIGN KEY (item_id) REFERENCES public.items(id) ON DELETE CASCADE;
ALTER TABLE ONLY public.favorites ADD CONSTRAINT favorites_user_id_fkey FOREIGN KEY (user_id) REFERENCES public."user"(id) ON DELETE CASCADE;
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;
ALTER TABLE ONLY public.tags_assign ADD CONSTRAINT tags_assign_item_id_fkey FOREIGN KEY (item_id) REFERENCES public.items(id) ON DELETE CASCADE;
ALTER TABLE ONLY public.tags_assign ADD CONSTRAINT tags_assign_tag_id_fkey FOREIGN KEY (tag_id) REFERENCES public.tags(id) ON DELETE CASCADE;
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;
ALTER TABLE ONLY public.user_options ADD CONSTRAINT user_options_avatar_fkey FOREIGN KEY (avatar) REFERENCES public.items(id) ON DELETE SET DEFAULT;
ALTER TABLE ONLY public.user_options ADD CONSTRAINT user_options_user_id_fkey FOREIGN KEY (user_id) REFERENCES public."user"(id) ON DELETE CASCADE;
ALTER TABLE ONLY public.user_sessions ADD CONSTRAINT user_sessions_user_id_fkey FOREIGN KEY (user_id) REFERENCES public."user"(id) ON DELETE CASCADE;
