Designing the database using soda migration

This commit is contained in:
vinchent 2024-07-06 16:51:08 +02:00
parent 9fc6c05d38
commit a497798d2f
23 changed files with 494 additions and 0 deletions

1
.gitignore vendored
View File

@ -1,3 +1,4 @@
**/c.out
**/c.html
bookings
database.yml

14
database-template.yml Normal file
View File

@ -0,0 +1,14 @@
development:
dialect: postgres
database: bookings
user:
password:
host: 127.0.0.1
pool: 5
test:
url: {{envOr "TEST_DATABASE_URL" "postgres://postgres:postgres@127.0.0.1:5432/myapp_test"}}
production:
url: {{envOr "DATABASE_URL" "postgres://postgres:postgres@127.0.0.1:5432/myapp_production"}}

View File

@ -0,0 +1 @@
sql("DROP TABLE users")

View File

@ -0,0 +1,8 @@
create_table("users") {
t.Column("id", "integer", {primary: true})
t.Column("first_name", "string", {"default": ""})
t.Column("last_name", "string", {"default": ""})
t.Column("email", "string", {})
t.Column("password", "string", {"size": 60})
t.Column("access_level", "integer", {"default": 1})
}

View File

@ -0,0 +1 @@
drop_table("reservations")

View File

@ -0,0 +1,11 @@
create_table("reservations") {
t.Column("id", "integer", {primary: true})
t.Column("first_name", "string", {"default": ""})
t.Column("last_name", "string", {"default": ""})
t.Column("email", "string", {})
t.Column("phone", "string", {"default": ""})
t.Column("start_date", "date", {})
t.Column("end_date", "date", {})
t.Column("room_id", "integer", {})
}

View File

@ -0,0 +1 @@
drop_table("rooms")

View File

@ -0,0 +1,5 @@
create_table("rooms") {
t.Column("id", "integer", {primary: true})
t.Column("room_name", "string", {})
}

View File

@ -0,0 +1 @@
drop_table("restrictions")

View File

@ -0,0 +1,5 @@
create_table("restrictions") {
t.Column("id", "integer", {primary: true})
t.Column("restriction_name", "string", {})
}

View File

@ -0,0 +1,2 @@
drop_table("room_restrictions")

View File

@ -0,0 +1,9 @@
create_table("room_restrictions") {
t.Column("id", "integer", {primary: true})
t.Column("start_date", "date", {})
t.Column("end_date", "date", {})
t.Column("room_id", "integer", {})
t.Column("reservation_id", "integer", {})
t.Column("restriction_id", "integer", {})
}

View File

@ -0,0 +1 @@
drop_foreign_key("reservations", "reservations_rooms_id_fk")

View File

@ -0,0 +1,4 @@
add_foreign_key("reservations", "room_id", {"rooms": ["id"]}, {
"on_delete": "cascade",
"on_update": "cascade",
})

View File

@ -0,0 +1,3 @@
drop_foreign_key("room_restrictions", "room_restrictions_rooms_id_fk")
drop_foreign_key("room_restrictions", "room_restrictions_restrictions_id_fk")

View File

@ -0,0 +1,9 @@
add_foreign_key("room_restrictions", "room_id", {"rooms": ["id"]}, {
"on_delete": "cascade",
"on_update": "cascade",
})
add_foreign_key("room_restrictions", "restriction_id", {"restrictions": ["id"]}, {
"on_delete": "cascade",
"on_update": "cascade",
})

View File

@ -0,0 +1,2 @@
drop_index("users", "users_email_idx")

View File

@ -0,0 +1 @@
add_index("users", "email", {"unique": true})

View File

@ -0,0 +1,4 @@
drop_index("room_restrictions", "room_restrictions_reservation_id_idx")
drop_index("room_restrictions", "room_restrictions_room_id_idx")
drop_index("room_restrictions", "room_restrictions_start_date_end_date_idx")

View File

@ -0,0 +1,3 @@
add_index("room_restrictions", ["start_date", "end_date"], {})
add_index("room_restrictions", "room_id", {})
add_index("room_restrictions", "reservation_id", {})

View File

@ -0,0 +1,4 @@
drop_foreign_key("room_restrictions", "room_restrictions_reservations_id_fk")
drop_index("room_restrictions", "reservations_email_idx")
drop_index("room_restrictions", "reservations_last_name_idx")

View File

@ -0,0 +1,9 @@
add_foreign_key("room_restrictions", "reservation_id", {"reservations": ["id"]}, {
"on_delete": "cascade",
"on_update": "cascade",
})
add_index("reservations", "email", {})
add_index("reservations", "last_name", {})

395
migrations/schema.sql Normal file
View File

@ -0,0 +1,395 @@
--
-- PostgreSQL database dump
--
-- Dumped from database version 16.3 (Ubuntu 16.3-0ubuntu0.24.04.1)
-- Dumped by pg_dump version 16.3 (Ubuntu 16.3-0ubuntu0.24.04.1)
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;
SET default_tablespace = '';
SET default_table_access_method = heap;
--
-- Name: reservations; Type: TABLE; Schema: public; Owner: jing
--
CREATE TABLE public.reservations (
id integer NOT NULL,
first_name character varying(255) DEFAULT ''::character varying NOT NULL,
last_name character varying(255) DEFAULT ''::character varying NOT NULL,
email character varying(255) NOT NULL,
phone character varying(255) DEFAULT ''::character varying NOT NULL,
start_date date NOT NULL,
end_date date NOT NULL,
room_id integer NOT NULL,
created_at timestamp without time zone NOT NULL,
updated_at timestamp without time zone NOT NULL
);
ALTER TABLE public.reservations OWNER TO jing;
--
-- Name: reservations_id_seq; Type: SEQUENCE; Schema: public; Owner: jing
--
CREATE SEQUENCE public.reservations_id_seq
AS integer
START WITH 1
INCREMENT BY 1
NO MINVALUE
NO MAXVALUE
CACHE 1;
ALTER SEQUENCE public.reservations_id_seq OWNER TO jing;
--
-- Name: reservations_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: jing
--
ALTER SEQUENCE public.reservations_id_seq OWNED BY public.reservations.id;
--
-- Name: restrictions; Type: TABLE; Schema: public; Owner: jing
--
CREATE TABLE public.restrictions (
id integer NOT NULL,
restriction_name character varying(255) NOT NULL,
created_at timestamp without time zone NOT NULL,
updated_at timestamp without time zone NOT NULL
);
ALTER TABLE public.restrictions OWNER TO jing;
--
-- Name: restrictions_id_seq; Type: SEQUENCE; Schema: public; Owner: jing
--
CREATE SEQUENCE public.restrictions_id_seq
AS integer
START WITH 1
INCREMENT BY 1
NO MINVALUE
NO MAXVALUE
CACHE 1;
ALTER SEQUENCE public.restrictions_id_seq OWNER TO jing;
--
-- Name: restrictions_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: jing
--
ALTER SEQUENCE public.restrictions_id_seq OWNED BY public.restrictions.id;
--
-- Name: room_restrictions; Type: TABLE; Schema: public; Owner: jing
--
CREATE TABLE public.room_restrictions (
id integer NOT NULL,
start_date date NOT NULL,
end_date date NOT NULL,
room_id integer NOT NULL,
reservation_id integer NOT NULL,
restriction_id integer NOT NULL,
created_at timestamp without time zone NOT NULL,
updated_at timestamp without time zone NOT NULL
);
ALTER TABLE public.room_restrictions OWNER TO jing;
--
-- Name: room_restrictions_id_seq; Type: SEQUENCE; Schema: public; Owner: jing
--
CREATE SEQUENCE public.room_restrictions_id_seq
AS integer
START WITH 1
INCREMENT BY 1
NO MINVALUE
NO MAXVALUE
CACHE 1;
ALTER SEQUENCE public.room_restrictions_id_seq OWNER TO jing;
--
-- Name: room_restrictions_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: jing
--
ALTER SEQUENCE public.room_restrictions_id_seq OWNED BY public.room_restrictions.id;
--
-- Name: rooms; Type: TABLE; Schema: public; Owner: jing
--
CREATE TABLE public.rooms (
id integer NOT NULL,
room_name character varying(255) NOT NULL,
created_at timestamp without time zone NOT NULL,
updated_at timestamp without time zone NOT NULL
);
ALTER TABLE public.rooms OWNER TO jing;
--
-- Name: rooms_id_seq; Type: SEQUENCE; Schema: public; Owner: jing
--
CREATE SEQUENCE public.rooms_id_seq
AS integer
START WITH 1
INCREMENT BY 1
NO MINVALUE
NO MAXVALUE
CACHE 1;
ALTER SEQUENCE public.rooms_id_seq OWNER TO jing;
--
-- Name: rooms_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: jing
--
ALTER SEQUENCE public.rooms_id_seq OWNED BY public.rooms.id;
--
-- Name: schema_migration; Type: TABLE; Schema: public; Owner: jing
--
CREATE TABLE public.schema_migration (
version character varying(14) NOT NULL
);
ALTER TABLE public.schema_migration OWNER TO jing;
--
-- Name: users; Type: TABLE; Schema: public; Owner: jing
--
CREATE TABLE public.users (
id integer NOT NULL,
first_name character varying(255) DEFAULT ''::character varying NOT NULL,
last_name character varying(255) DEFAULT ''::character varying NOT NULL,
email character varying(255) NOT NULL,
password character varying(60) NOT NULL,
access_level integer DEFAULT 1 NOT NULL,
created_at timestamp without time zone NOT NULL,
updated_at timestamp without time zone NOT NULL
);
ALTER TABLE public.users OWNER TO jing;
--
-- Name: users_id_seq; Type: SEQUENCE; Schema: public; Owner: jing
--
CREATE SEQUENCE public.users_id_seq
AS integer
START WITH 1
INCREMENT BY 1
NO MINVALUE
NO MAXVALUE
CACHE 1;
ALTER SEQUENCE public.users_id_seq OWNER TO jing;
--
-- Name: users_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: jing
--
ALTER SEQUENCE public.users_id_seq OWNED BY public.users.id;
--
-- Name: reservations id; Type: DEFAULT; Schema: public; Owner: jing
--
ALTER TABLE ONLY public.reservations ALTER COLUMN id SET DEFAULT nextval('public.reservations_id_seq'::regclass);
--
-- Name: restrictions id; Type: DEFAULT; Schema: public; Owner: jing
--
ALTER TABLE ONLY public.restrictions ALTER COLUMN id SET DEFAULT nextval('public.restrictions_id_seq'::regclass);
--
-- Name: room_restrictions id; Type: DEFAULT; Schema: public; Owner: jing
--
ALTER TABLE ONLY public.room_restrictions ALTER COLUMN id SET DEFAULT nextval('public.room_restrictions_id_seq'::regclass);
--
-- Name: rooms id; Type: DEFAULT; Schema: public; Owner: jing
--
ALTER TABLE ONLY public.rooms ALTER COLUMN id SET DEFAULT nextval('public.rooms_id_seq'::regclass);
--
-- Name: users id; Type: DEFAULT; Schema: public; Owner: jing
--
ALTER TABLE ONLY public.users ALTER COLUMN id SET DEFAULT nextval('public.users_id_seq'::regclass);
--
-- Name: reservations reservations_pkey; Type: CONSTRAINT; Schema: public; Owner: jing
--
ALTER TABLE ONLY public.reservations
ADD CONSTRAINT reservations_pkey PRIMARY KEY (id);
--
-- Name: restrictions restrictions_pkey; Type: CONSTRAINT; Schema: public; Owner: jing
--
ALTER TABLE ONLY public.restrictions
ADD CONSTRAINT restrictions_pkey PRIMARY KEY (id);
--
-- Name: room_restrictions room_restrictions_pkey; Type: CONSTRAINT; Schema: public; Owner: jing
--
ALTER TABLE ONLY public.room_restrictions
ADD CONSTRAINT room_restrictions_pkey PRIMARY KEY (id);
--
-- Name: rooms rooms_pkey; Type: CONSTRAINT; Schema: public; Owner: jing
--
ALTER TABLE ONLY public.rooms
ADD CONSTRAINT rooms_pkey PRIMARY KEY (id);
--
-- Name: schema_migration schema_migration_pkey; Type: CONSTRAINT; Schema: public; Owner: jing
--
ALTER TABLE ONLY public.schema_migration
ADD CONSTRAINT schema_migration_pkey PRIMARY KEY (version);
--
-- Name: users users_pkey; Type: CONSTRAINT; Schema: public; Owner: jing
--
ALTER TABLE ONLY public.users
ADD CONSTRAINT users_pkey PRIMARY KEY (id);
--
-- Name: reservations_email_idx; Type: INDEX; Schema: public; Owner: jing
--
CREATE INDEX reservations_email_idx ON public.reservations USING btree (email);
--
-- Name: reservations_last_name_idx; Type: INDEX; Schema: public; Owner: jing
--
CREATE INDEX reservations_last_name_idx ON public.reservations USING btree (last_name);
--
-- Name: room_restrictions_reservation_id_idx; Type: INDEX; Schema: public; Owner: jing
--
CREATE INDEX room_restrictions_reservation_id_idx ON public.room_restrictions USING btree (reservation_id);
--
-- Name: room_restrictions_room_id_idx; Type: INDEX; Schema: public; Owner: jing
--
CREATE INDEX room_restrictions_room_id_idx ON public.room_restrictions USING btree (room_id);
--
-- Name: room_restrictions_start_date_end_date_idx; Type: INDEX; Schema: public; Owner: jing
--
CREATE INDEX room_restrictions_start_date_end_date_idx ON public.room_restrictions USING btree (start_date, end_date);
--
-- Name: schema_migration_version_idx; Type: INDEX; Schema: public; Owner: jing
--
CREATE UNIQUE INDEX schema_migration_version_idx ON public.schema_migration USING btree (version);
--
-- Name: users_email_idx; Type: INDEX; Schema: public; Owner: jing
--
CREATE UNIQUE INDEX users_email_idx ON public.users USING btree (email);
--
-- Name: reservations reservations_rooms_id_fk; Type: FK CONSTRAINT; Schema: public; Owner: jing
--
ALTER TABLE ONLY public.reservations
ADD CONSTRAINT reservations_rooms_id_fk FOREIGN KEY (room_id) REFERENCES public.rooms(id) ON UPDATE CASCADE ON DELETE CASCADE;
--
-- Name: room_restrictions room_restrictions_reservations_id_fk; Type: FK CONSTRAINT; Schema: public; Owner: jing
--
ALTER TABLE ONLY public.room_restrictions
ADD CONSTRAINT room_restrictions_reservations_id_fk FOREIGN KEY (reservation_id) REFERENCES public.reservations(id) ON UPDATE CASCADE ON DELETE CASCADE;
--
-- Name: room_restrictions room_restrictions_restrictions_id_fk; Type: FK CONSTRAINT; Schema: public; Owner: jing
--
ALTER TABLE ONLY public.room_restrictions
ADD CONSTRAINT room_restrictions_restrictions_id_fk FOREIGN KEY (restriction_id) REFERENCES public.restrictions(id) ON UPDATE CASCADE ON DELETE CASCADE;
--
-- Name: room_restrictions room_restrictions_rooms_id_fk; Type: FK CONSTRAINT; Schema: public; Owner: jing
--
ALTER TABLE ONLY public.room_restrictions
ADD CONSTRAINT room_restrictions_rooms_id_fk FOREIGN KEY (room_id) REFERENCES public.rooms(id) ON UPDATE CASCADE ON DELETE CASCADE;
--
-- PostgreSQL database dump complete
--