Skip to content

Commit

Permalink
implemented more SQL scripts to fill the database with predetermined …
Browse files Browse the repository at this point in the history
…data
  • Loading branch information
MiguelPelegrina committed Apr 8, 2024
1 parent 126a9ef commit dd93298
Show file tree
Hide file tree
Showing 3 changed files with 148 additions and 14 deletions.
1 change: 1 addition & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ services:
image: postgres:latest
ports:
- "5432:5432"
restart: on-failure
volumes:
- ../FinalTask3/src/main/resources/sql:/docker-entrypoint-initdb.d
- ./src/main/resources/sql/create_tables.sql:/docker-entrypoint-initdb.d/create_tables.sql
Expand Down
104 changes: 91 additions & 13 deletions src/main/resources/sql/create_tables.sql
Original file line number Diff line number Diff line change
@@ -1,31 +1,29 @@
CREATE TABLE IF NOT EXISTS roles (
name VARCHAR(255) PRIMARY KEY
CREATE TABLE IF NOT EXISTS public.roles (
name VARCHAR(45) COLLATE pg_catalog."default" NOT NULL,
CONSTRAINT roles_pkey PRIMARY KEY (name)
);

CREATE TABLE IF NOT EXISTS public.countries
(
name character varying(45) COLLATE pg_catalog."default" NOT NULL,
CREATE TABLE IF NOT EXISTS public.countries (
name VARCHAR(45) COLLATE pg_catalog."default" NOT NULL,
is_active boolean NOT NULL,
CONSTRAINT countries_pkey PRIMARY KEY (name)
);

CREATE TABLE IF NOT EXISTS public.cities
(
name character varying(45) COLLATE pg_catalog."default" NOT NULL,
CREATE TABLE IF NOT EXISTS public.cities (
name VARCHAR(45) COLLATE pg_catalog."default" NOT NULL,
is_active boolean NOT NULL,
country_name character varying(45) COLLATE pg_catalog."default" NOT NULL,
country_name VARCHAR(45) COLLATE pg_catalog."default" NOT NULL,
CONSTRAINT cities_pkey PRIMARY KEY (name),
CONSTRAINT cities_country_name_fkey FOREIGN KEY (country_name)
REFERENCES public.countries (name) MATCH SIMPLE
ON UPDATE NO ACTION
ON DELETE NO ACTION
);

CREATE TABLE IF NOT EXISTS public.postal_codes
(
code character varying(45) COLLATE pg_catalog."default" NOT NULL,
CREATE TABLE IF NOT EXISTS public.postal_codes (
code VARCHAR(45) COLLATE pg_catalog."default" NOT NULL,
is_active boolean NOT NULL,
city_name character varying COLLATE pg_catalog."default",
city_name VARCHAR COLLATE pg_catalog."default",
CONSTRAINT postal_codes_pkey PRIMARY KEY (code),
CONSTRAINT postal_codes_city_name_fkey FOREIGN KEY (city_name)
REFERENCES public.cities (name) MATCH SIMPLE
Expand All @@ -34,4 +32,84 @@ CREATE TABLE IF NOT EXISTS public.postal_codes
NOT VALID
);

CREATE TABLE IF NOT EXISTS public.delivery_methods (
name VARCHAR(45) COLLATE pg_catalog."default" NOT NULL,
is_active boolean NOT NULL,
CONSTRAINT delivery_methods_pkey PRIMARY KEY (name)
);

CREATE TABLE IF NOT EXISTS public.order_statuses (
name VARCHAR(45) COLLATE pg_catalog."default" NOT NULL,
is_active boolean NOT NULL,
CONSTRAINT order_statuses_pkey PRIMARY KEY (name)
);

CREATE TABLE IF NOT EXISTS public.payment_methods (
name VARCHAR(45) COLLATE pg_catalog."default" NOT NULL,
is_active boolean NOT NULL,
CONSTRAINT payment_methods_pkey PRIMARY KEY (name)
);

CREATE TABLE IF NOT EXISTS public.payment_statuses (
name VARCHAR(45) COLLATE pg_catalog."default" NOT NULL,
is_active boolean NOT NULL,
CONSTRAINT payment_statuses_pkey PRIMARY KEY (name)
);

CREATE TABLE IF NOT EXISTS public.book_genres (
name VARCHAR(45) COLLATE pg_catalog."default" NOT NULL,
CONSTRAINT book_genres_pkey PRIMARY KEY (name)
);

CREATE TABLE IF NOT EXISTS public.book_parameter_formats (
name VARCHAR(45) COLLATE pg_catalog."default" NOT NULL,
CONSTRAINT book_parameter_formats_pkey PRIMARY KEY (name)
);

CREATE TABLE IF NOT EXISTS public.user_addresses (
id integer NOT NULL,
postal_code VARCHAR(45) COLLATE pg_catalog."default" NOT NULL,
street VARCHAR(60) COLLATE pg_catalog."default" NOT NULL,
number VARCHAR(255) COLLATE pg_catalog."default" NOT NULL,
is_active boolean NOT NULL,
CONSTRAINT user_addresses_pkey PRIMARY KEY (id),
CONSTRAINT user_addresses_postal_code_fkey FOREIGN KEY (postal_code)
REFERENCES public.postal_codes (code) MATCH SIMPLE
ON UPDATE NO ACTION
ON DELETE NO ACTION
);

CREATE TABLE IF NOT EXISTS public.users (
id integer NOT NULL,
name VARCHAR(45) COLLATE pg_catalog."default" NOT NULL,
surname VARCHAR(45) COLLATE pg_catalog."default" NOT NULL,
date_of_birth date NOT NULL,
email_address VARCHAR COLLATE pg_catalog."default" NOT NULL,
password VARCHAR COLLATE pg_catalog."default" NOT NULL,
is_active boolean NOT NULL,
phone_number VARCHAR(45) COLLATE pg_catalog."default" NOT NULL,
address_id integer,
CONSTRAINT users_pkey PRIMARY KEY (id),
CONSTRAINT users_email_address_key UNIQUE (email_address),
CONSTRAINT users_address_id_fkey FOREIGN KEY (address_id)
REFERENCES public.user_addresses (id) MATCH SIMPLE
ON UPDATE NO ACTION
ON DELETE NO ACTION
);

CREATE TABLE IF NOT EXISTS public.user_roles (
user_id integer NOT NULL,
role_id VARCHAR(45) COLLATE pg_catalog."default" NOT NULL,
assigned_date date NOT NULL,
id integer NOT NULL,
CONSTRAINT user_roles_pkey PRIMARY KEY (user_id, role_id),
CONSTRAINT user_roles_role_fkey FOREIGN KEY (role_id)
REFERENCES public.roles (name) MATCH SIMPLE
ON UPDATE NO ACTION
ON DELETE NO ACTION
NOT VALID,
CONSTRAINT user_roles_user_id_fkey FOREIGN KEY (user_id)
REFERENCES public.users (id) MATCH SIMPLE
ON UPDATE NO ACTION
ON DELETE NO ACTION
);
57 changes: 56 additions & 1 deletion src/main/resources/sql/fill_tables.sql
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,59 @@ VALUES
INSERT INTO public.postal_codes (code, is_active, city_name)
VALUES
('18014', true, 'Granada'),
('48143', true, 'Münster');
('48143', true, 'Münster');

INSERT INTO public.delivery_methods (name, is_active)
VALUES
('EXPRESS', true),
('STANDARD', true);

INSERT INTO public.order_statuses (name, is_active)
VALUES
('Pending payment', true),
('Pending shipment', true),
('Shipped', true),
('Delivered', true);

INSERT INTO public.payment_methods (name, is_active)
VALUES
('Cash', true),
('By card', true);

INSERT INTO public.payment_statuses (name, is_active)
VALUES
('Pending', true),
('Paid', true);

INSERT INTO public.book_genres (name)
VALUES
('Thriller'),
('Essay'),
('Fantasy');

INSERT INTO public.book_parameter_formats (name)
VALUES
('E-book'),
('Paperback'),
('Hardcover');

INSERT INTO public.user_addresses (
id, postal_code, street, number, is_active
)
VALUES (
1, '18014', 'Street', 1, true
);

INSERT INTO public.users (
id, name, surname, date_of_birth, email_address, password, is_active, phone_number, address_id
)
VALUES (
1, 'Admin', 'Admin', '01-01-2000', '[email protected]', '$2a$10$UXwOIA7tOQES3gO2shUsDO.AISCnWLy.aaxUzQd1te2Uki.0qPZpG', true, 123456789, 1
);

INSERT INTO public.user_roles (
user_id, role_id, assigned_date, id
)
VALUES (
1, 'ADMIN', '01-01-2000', 1
);

0 comments on commit dd93298

Please sign in to comment.