cargagep-v2/db/schema.sql
2026-08-23 17:47:31 +02:00

392 lines
8.9 KiB
SQL

\restrict dbmate
-- Dumped from database version 18.6
-- Dumped by pg_dump version 18.6
SET statement_timeout = 0;
SET lock_timeout = 0;
SET idle_in_transaction_session_timeout = 0;
SET transaction_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: bike_status; Type: TYPE; Schema: public; Owner: -
--
CREATE TYPE public.bike_status AS ENUM (
'in_service',
'out_of_service'
);
--
-- Name: reservation_status; Type: TYPE; Schema: public; Owner: -
--
CREATE TYPE public.reservation_status AS ENUM (
'requested',
'refused',
'approved',
'cancelled',
'ongoing',
'archived'
);
SET default_tablespace = '';
SET default_table_access_method = heap;
--
-- Name: bikes; Type: TABLE; Schema: public; Owner: -
--
CREATE TABLE public.bikes (
id integer NOT NULL,
name text NOT NULL,
key_number text,
key_quantity integer DEFAULT 0 NOT NULL,
drivetrain text,
battery text,
status public.bike_status DEFAULT 'in_service'::public.bike_status NOT NULL,
CONSTRAINT bikes_key_quantity_positive CHECK ((key_quantity >= 0))
);
--
-- Name: bikes_id_seq; Type: SEQUENCE; Schema: public; Owner: -
--
CREATE SEQUENCE public.bikes_id_seq
AS integer
START WITH 1
INCREMENT BY 1
NO MINVALUE
NO MAXVALUE
CACHE 1;
--
-- Name: bikes_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: -
--
ALTER SEQUENCE public.bikes_id_seq OWNED BY public.bikes.id;
--
-- Name: reservations; Type: TABLE; Schema: public; Owner: -
--
CREATE TABLE public.reservations (
id integer NOT NULL,
unit text NOT NULL,
start_time timestamp with time zone NOT NULL,
end_time timestamp with time zone NOT NULL,
requester_id integer NOT NULL,
telegram text NOT NULL,
description text DEFAULT ''::text NOT NULL,
status public.reservation_status DEFAULT 'requested'::public.reservation_status NOT NULL,
CONSTRAINT reservations_telegram_handle CHECK ((telegram ~ '^@[A-Za-z][A-Za-z0-9_]{4,31}$'::text)),
CONSTRAINT reservations_time_order CHECK ((end_time > start_time))
);
--
-- Name: reservations_bikes; Type: TABLE; Schema: public; Owner: -
--
CREATE TABLE public.reservations_bikes (
reservation_id integer NOT NULL,
bike_id integer NOT NULL
);
--
-- Name: reservations_id_seq; Type: SEQUENCE; Schema: public; Owner: -
--
CREATE SEQUENCE public.reservations_id_seq
AS integer
START WITH 1
INCREMENT BY 1
NO MINVALUE
NO MAXVALUE
CACHE 1;
--
-- Name: reservations_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: -
--
ALTER SEQUENCE public.reservations_id_seq OWNED BY public.reservations.id;
--
-- Name: reservations_users; Type: TABLE; Schema: public; Owner: -
--
CREATE TABLE public.reservations_users (
reservation_id integer NOT NULL,
user_id integer NOT NULL
);
--
-- Name: schema_migrations; Type: TABLE; Schema: public; Owner: -
--
CREATE TABLE public.schema_migrations (
version character varying NOT NULL
);
--
-- Name: units_users; Type: TABLE; Schema: public; Owner: -
--
CREATE TABLE public.units_users (
user_id integer NOT NULL,
unit_name text NOT NULL
);
--
-- Name: users; Type: TABLE; Schema: public; Owner: -
--
CREATE TABLE public.users (
id integer NOT NULL,
external_id text,
firstname text NOT NULL,
name text NOT NULL,
email text NOT NULL,
oidc_sub text NOT NULL,
admin boolean DEFAULT false NOT NULL
);
--
-- Name: users_id_seq; Type: SEQUENCE; Schema: public; Owner: -
--
CREATE SEQUENCE public.users_id_seq
AS integer
START WITH 1
INCREMENT BY 1
NO MINVALUE
NO MAXVALUE
CACHE 1;
--
-- Name: users_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: -
--
ALTER SEQUENCE public.users_id_seq OWNED BY public.users.id;
--
-- Name: bikes id; Type: DEFAULT; Schema: public; Owner: -
--
ALTER TABLE ONLY public.bikes ALTER COLUMN id SET DEFAULT nextval('public.bikes_id_seq'::regclass);
--
-- Name: reservations id; Type: DEFAULT; Schema: public; Owner: -
--
ALTER TABLE ONLY public.reservations ALTER COLUMN id SET DEFAULT nextval('public.reservations_id_seq'::regclass);
--
-- Name: users id; Type: DEFAULT; Schema: public; Owner: -
--
ALTER TABLE ONLY public.users ALTER COLUMN id SET DEFAULT nextval('public.users_id_seq'::regclass);
--
-- Name: bikes bikes_pkey; Type: CONSTRAINT; Schema: public; Owner: -
--
ALTER TABLE ONLY public.bikes
ADD CONSTRAINT bikes_pkey PRIMARY KEY (id);
--
-- Name: reservations_bikes reservations_bikes_pkey; Type: CONSTRAINT; Schema: public; Owner: -
--
ALTER TABLE ONLY public.reservations_bikes
ADD CONSTRAINT reservations_bikes_pkey PRIMARY KEY (reservation_id, bike_id);
--
-- Name: reservations reservations_pkey; Type: CONSTRAINT; Schema: public; Owner: -
--
ALTER TABLE ONLY public.reservations
ADD CONSTRAINT reservations_pkey PRIMARY KEY (id);
--
-- Name: reservations_users reservations_users_pkey; Type: CONSTRAINT; Schema: public; Owner: -
--
ALTER TABLE ONLY public.reservations_users
ADD CONSTRAINT reservations_users_pkey PRIMARY KEY (reservation_id, user_id);
--
-- Name: schema_migrations schema_migrations_pkey; Type: CONSTRAINT; Schema: public; Owner: -
--
ALTER TABLE ONLY public.schema_migrations
ADD CONSTRAINT schema_migrations_pkey PRIMARY KEY (version);
--
-- Name: units_users units_users_pkey; Type: CONSTRAINT; Schema: public; Owner: -
--
ALTER TABLE ONLY public.units_users
ADD CONSTRAINT units_users_pkey PRIMARY KEY (user_id, unit_name);
--
-- Name: users users_email_key; Type: CONSTRAINT; Schema: public; Owner: -
--
ALTER TABLE ONLY public.users
ADD CONSTRAINT users_email_key UNIQUE (email);
--
-- Name: users users_external_id_key; Type: CONSTRAINT; Schema: public; Owner: -
--
ALTER TABLE ONLY public.users
ADD CONSTRAINT users_external_id_key UNIQUE (external_id);
--
-- Name: users users_oidc_sub_key; Type: CONSTRAINT; Schema: public; Owner: -
--
ALTER TABLE ONLY public.users
ADD CONSTRAINT users_oidc_sub_key UNIQUE (oidc_sub);
--
-- Name: users users_pkey; Type: CONSTRAINT; Schema: public; Owner: -
--
ALTER TABLE ONLY public.users
ADD CONSTRAINT users_pkey PRIMARY KEY (id);
--
-- Name: reservations_bikes_bike_id_idx; Type: INDEX; Schema: public; Owner: -
--
CREATE INDEX reservations_bikes_bike_id_idx ON public.reservations_bikes USING btree (bike_id);
--
-- Name: reservations_period_idx; Type: INDEX; Schema: public; Owner: -
--
CREATE INDEX reservations_period_idx ON public.reservations USING btree (start_time, end_time);
--
-- Name: reservations_status_idx; Type: INDEX; Schema: public; Owner: -
--
CREATE INDEX reservations_status_idx ON public.reservations USING btree (status);
--
-- Name: reservations_unit_idx; Type: INDEX; Schema: public; Owner: -
--
CREATE INDEX reservations_unit_idx ON public.reservations USING btree (unit);
--
-- Name: units_users_unit_name_idx; Type: INDEX; Schema: public; Owner: -
--
CREATE INDEX units_users_unit_name_idx ON public.units_users USING btree (unit_name);
--
-- Name: reservations_bikes reservations_bikes_bike_id_fkey; Type: FK CONSTRAINT; Schema: public; Owner: -
--
ALTER TABLE ONLY public.reservations_bikes
ADD CONSTRAINT reservations_bikes_bike_id_fkey FOREIGN KEY (bike_id) REFERENCES public.bikes(id) ON DELETE RESTRICT;
--
-- Name: reservations_bikes reservations_bikes_reservation_id_fkey; Type: FK CONSTRAINT; Schema: public; Owner: -
--
ALTER TABLE ONLY public.reservations_bikes
ADD CONSTRAINT reservations_bikes_reservation_id_fkey FOREIGN KEY (reservation_id) REFERENCES public.reservations(id) ON DELETE CASCADE;
--
-- Name: reservations reservations_requester_id_fkey; Type: FK CONSTRAINT; Schema: public; Owner: -
--
ALTER TABLE ONLY public.reservations
ADD CONSTRAINT reservations_requester_id_fkey FOREIGN KEY (requester_id) REFERENCES public.users(id);
--
-- Name: reservations_users reservations_users_reservation_id_fkey; Type: FK CONSTRAINT; Schema: public; Owner: -
--
ALTER TABLE ONLY public.reservations_users
ADD CONSTRAINT reservations_users_reservation_id_fkey FOREIGN KEY (reservation_id) REFERENCES public.reservations(id) ON DELETE CASCADE;
--
-- Name: reservations_users reservations_users_user_id_fkey; Type: FK CONSTRAINT; Schema: public; Owner: -
--
ALTER TABLE ONLY public.reservations_users
ADD CONSTRAINT reservations_users_user_id_fkey FOREIGN KEY (user_id) REFERENCES public.users(id) ON DELETE CASCADE;
--
-- Name: units_users units_users_user_id_fkey; Type: FK CONSTRAINT; Schema: public; Owner: -
--
ALTER TABLE ONLY public.units_users
ADD CONSTRAINT units_users_user_id_fkey FOREIGN KEY (user_id) REFERENCES public.users(id) ON DELETE CASCADE;
--
-- PostgreSQL database dump complete
--
\unrestrict dbmate
--
-- Dbmate schema migrations
--
INSERT INTO public.schema_migrations (version) VALUES
('20260823153300'),
('20260823153310'),
('20260823153320');