Skip to content

Commit

Permalink
db: added new file with func, more data and not nulls in columns
Browse files Browse the repository at this point in the history
  • Loading branch information
rogacky11 committed Mar 24, 2024
1 parent cc1e52d commit 1c48514
Show file tree
Hide file tree
Showing 5 changed files with 174 additions and 77 deletions.
2 changes: 0 additions & 2 deletions .env.sample
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,9 @@ POSTGRES_DB=reasn

POSTGRES_ADMIN_USER=admindba
POSTGRES_ADMIN_PASSWORD=sql
POSTGRES_DB=reasn

POSTGRES_CLIENT_USER=userdba
POSTGRES_CLIENT_PASSWORD=sql
POSTGRES_DB=reasn

# PGADMIN
PGADMIN_LISTEN_PORT=5050
Expand Down
124 changes: 108 additions & 16 deletions Database/init-dev-data.sql
Original file line number Diff line number Diff line change
@@ -1,46 +1,138 @@
INSERT INTO users_schema.address ("id", "city", "country", "street", "state", "zip_code") VALUES
(1, 'Kalisz', 'Polska', 'Stawiszyńska', 'Wielkopolskie', '62-800'),
(2, 'Wrocław', 'Polska', 'Legnicka', 'Dolnośląskie', '51-702');
(1, 'Stawiszyn', 'Polska', 'Pleszewska 2', 'Wielkopolskie', '62-820'),
(2, 'Kalisz', 'Polska', 'Stawiszyńska 4', 'Wielkopolskie', '62-800'),
(3, 'Bydgoszcz', 'Polska', 'Jagielońska 12', 'Kujawsko-Pomorskie', '85-097'),
(4, 'Poznań', 'Polska', 'Bułgarska 17', 'Wielkopolskie', '60-320'),
(5, 'Koźminek', 'Polska', 'Mikołaja Kopernika', 'Wielkopolskie', '62-840'),
(6, 'Kalisz', 'Polska', 'Górnośląska 2', 'Wielkopolskie', '62-800'),
(7, 'Warszawa', 'Polska', 'Aleje Jerozolimskie', 'Mazowieckie', '00-001'),
(8, 'Kraków', 'Polska', 'ul. Floriańska 7', 'Małopolskie', '30-001'),
(9, 'Gdańsk', 'Polska', 'ul. Długa 69', 'Pomorskie', '80-001'),
(10, 'Wrocław', 'Polska', 'Legnicka 420', 'Dolnośląskie', '51-702');

INSERT INTO users_schema.user ("id", "name", "surname", "username", "password", "created_at", "updated_at", "role_id", "email", "is_active", "address_id", "phone") VALUES
(1, 'Kamil', 'Owczarski', 'bilimigus', 'password', '2023-03-20 08:00:00', '2023-03-20 08:00:00', 1, '[email protected]', B'1', 1, '123456789'),
(2, 'Kamil', 'Owczarzyński', 'bilililimigosu', '12345', '2023-03-21 09:00:00', '2023-03-21 09:00:00', 2, '[email protected]', B'1', 2, '696969691');
(2, 'Kamil', 'Owczarzyński', 'bilililimigosu', '12345', '2023-03-21 09:00:00', '2023-03-21 09:00:00', 2, '[email protected]', B'1', 2, '696969691'),
(3, 'Jan', 'Kowalski', 'jkowalski', 'hasło123', '2022-03-21 16:00:00', '2023-1-21 09:21:14', 1, '[email protected]', B'0', 1, '123456789'),
(4, 'Adam', 'Nowak', 'anowak', 'haslo123', '2022-01-05 10:30:00', '2022-02-15 14:20:45', 2, '[email protected]', B'0', 2, '987654321'),
(5, 'Ewa', 'Kowalska', 'ekowalska', 'tajnehaslo', '2022-02-10 08:15:00', '2022-03-25 12:45:30', 1, '[email protected]', B'0', 3, '654321987'),
(6, 'Piotr', 'Wiśniewski', 'pwiśniewski', 'password123', '2022-03-15 11:45:00', '2022-04-30 16:10:20', 3, '[email protected]', B'0', 4, '321654987'),
(7, 'Anna', 'Lis', 'alis', '123456', '2022-04-20 13:20:00', '2022-05-10 09:55:15', 2, '[email protected]', B'1', 5, '789456123'),
(8, 'Tomasz', 'Zieliński', 'tzielinski', 'qwerty', '2022-05-25 15:00:00', '2022-06-20 11:30:10', 1, '[email protected]', B'1', 6, '456123789'),
(9, 'Magdalena', 'Kowalczyk', 'mkowalczyk', 'mojehaslo', '2022-06-30 17:10:00', '2022-07-15 13:15:25', 3, '[email protected]', B'1', 7, '987654123'),
(10, 'Marcin', 'Szymański', 'mszymanski', 'password', '2022-07-05 09:00:00', '2022-08-05 08:20:35', 2, '[email protected]', B'1', 8, '321789654');


INSERT INTO events_schema.event ("id", "name", "address_id", "description", "organizer_id", "start_at", "end_at", "created_at", "updated_at", "slug", "status_id") VALUES
(1, 'Tech Conference', 1, 'Annual tech conference', 1, '2023-10-01 09:00:00', '2023-10-02 17:00:00', '2023-09-01 08:00:00', '2023-09-01 08:00:00', 'tech-conference', 1),
(2, 'Health Symposium', 2, 'Health and wellness symposium', 2, '2023-11-05 09:00:00', '2023-11-06 17:00:00', '2023-10-05 08:00:00', '2023-10-05 08:00:00', 'health-symposium', 2);
(2, 'Health Symposium', 2, 'Health and wellness symposium', 2, '2023-11-05 09:00:00', '2023-11-06 17:00:00', '2023-10-05 08:00:00', '2023-10-05 08:00:00', 'health-symposium', 2),
(3, 'Koncert Rockowy', 3, 'Występ ulubionych zespołów rockowych', 2, CURRENT_TIMESTAMP - '1 day'::INTERVAL, CURRENT_TIMESTAMP + '4 hours'::INTERVAL, '2023-09-01 08:00:00', '2023-09-01 08:00:00', 'koncert-rockowy', 1),
(4, 'Konferencja IT', 4, 'Coroczna konferencja technologiczna', 1, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP + '3 hours'::INTERVAL, '2023-10-02 17:00:00', '2023-10-02 18:00:00', 'konferencja-it', 1),
(5, 'Mecz Piłki Nożnej', 5, 'Mecz drużynowej rywalizacji w piłce nożnej', 3, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP + '3 hours'::INTERVAL, '2022-12-01 08:00:00', '2023-09-01 08:30:00', 'mecz-pilki-noznej', 1),
(6, 'Festiwal Elektroniczny', 6, 'Największe hity muzyki elektronicznej', 4, CURRENT_TIMESTAMP - '4 day'::INTERVAL, CURRENT_TIMESTAMP - '2 days'::INTERVAL, '2023-09-01 08:00:00', '2023-09-02 08:00:00', 'festiwal-elektroniczny', 1),
(7, 'Koncert Hip-Hopowy', 7, 'Najnowsze hity hip-hopu w wykonaniu gwiazd', 6, CURRENT_TIMESTAMP + '2 days'::INTERVAL, CURRENT_TIMESTAMP + '2 days'::INTERVAL + '4 hours'::INTERVAL, '2023-10-08 17:00:00', '2023-11-01 09:19:22', 'koncert-hip-hopowy', 1),
(8, 'Wieczór Jazzowy', 8, 'Relaksujące dźwięki jazzu w kameralnej atmosferze', 8, CURRENT_TIMESTAMP + '1 day'::INTERVAL, CURRENT_TIMESTAMP + '1 day'::INTERVAL + '5 hours'::INTERVAL, '2023-09-01 08:00:00', '2023-09-01 08:00:00', 'wieczor-jazzowy', 1),
(9, 'Koncert Klasyczny', 9, 'Muzyka klasyczna w wykonaniu renomowanych artystów', 10, CURRENT_TIMESTAMP + '1 day'::INTERVAL, CURRENT_TIMESTAMP + '1 day'::INTERVAL + '3 hours'::INTERVAL, '2023-01-01 15:00:00', '2023-02-01 08:00:00', 'koncert-klasyczny', 1),
(10, 'Turniej w League of Legends', 10, 'Turniej LAN dla miłośnikow esportu i gry League of Legends', 10, CURRENT_TIMESTAMP - '1 day'::INTERVAL, CURRENT_TIMESTAMP - '3 hours'::INTERVAL, '2023-01-01 15:00:00', '2023-02-01 08:00:00', 'turniej-lol', 1);


INSERT INTO events_schema.event_tag ("event_id", "tag_id") VALUES
(1, 1),
(2, 2);
(2, 10),
(3, 5),
(3, 4),
(4, 1),
(5, 2),
(5, 3),
(6, 4),
(7, 7),
(8, 4),
(9, 4),
(10, 8),
(10, 9);


INSERT INTO events_schema.participant ("id", "event_id", "user_id", "status_id") VALUES
(1, 1, 1, 1),
(2, 2, 2, 2);
(1, 1, 1, 2),
(2, 2, 2, 2),
(3, 3, 3, 2),
(4, 4, 4, 2),
(5, 5, 5, 2),
(6, 6, 6, 2),
(7, 7, 7, 2),
(8, 7, 8, 2),
(9, 9, 9, 2),
(10, 10, 10, 2);

INSERT INTO events_schema.parameter ("id", "key", "value") VALUES
(1, 'Location', 'Virtual'),
(2, 'SpeakerCount', '5');
(1, 'Liczba Uczestników', '1000'),
(2, 'Długość Trasy', '10 km'),
(3, 'Temperatura', '25°C'),
(4, 'Czas Trwania', '2 godziny'),
(5, 'Poziom Trudności', 'Średni'),
(6, 'Rozdawane Nagrody', '500 USD'),
(7, 'Typ Wydarzenia', 'Bezpośrednie'),
(8, 'Czas Trwania', '3 dni'),
(9, 'Czas Trwania', '10 dni'),
(10, 'Miejsce Wydarzenia', 'Plenerowe');

INSERT INTO events_schema.event_parameter ("parameter_id", "event_id") VALUES
(1, 1),
(2, 2);
(2, 2),
(3, 3),
(4, 4),
(5, 5),
(6, 6),
(7, 7),
(8, 8),
(9, 9),
(10, 10);

INSERT INTO users_schema.comment ("id", "event_id", "content", "created_at", "user_id") VALUES
(1, 1, 'Looking forward to this!', '2023-09-10', 1),
(2, 2, 'Boring...', '2023-10-15', 2);
(1, 1, 'Nie mogę się doczekać!', '2023-09-10', 1),
(2, 2, 'Trochę nudne...', '2023-10-15', 2),
(3, 3, 'Super wydarzenie!', '2023-11-20', 3),
(4, 4, 'Fantastyczne doświadczenie!', '2023-12-05', 4),
(5, 5, 'Zapowiada się świetnie!', '2024-01-02', 5),
(6, 6, 'Jestem podekscytowany!', '2024-02-14', 6),
(7, 7, 'Bardzo ciekawe!', '2024-03-21', 7),
(8, 8, 'Jestem zachwycony!', '2024-04-30', 8),
(9, 9, 'To będzie świetna zabawa!', '2024-05-18', 9),
(10, 10, 'jd orka', '2024-06-25', 10);

INSERT INTO users_schema.interest ("id", "name", "level") VALUES
(1, 'Programming', 5),
(2, 'Reading', 4);
(1, 'Programowanie', 5),
(2, 'Sport', 4),
(3, 'Muzyka', 3),
(4, 'Film', 3),
(5, 'Taniec', 4),
(6, 'Fitness', 3),
(7, 'Sztuka', 4),
(8, 'Kulinaria', 3),
(9, 'Podróże', 4),
(10, 'Gry komputerowe', 5);

INSERT INTO users_schema.user_interest ("user_id", "interest_id") VALUES
(1, 1),
(2, 2);
(2, 2),
(3, 3),
(4, 4),
(5, 5),
(6, 6),
(7, 7),
(8, 8),
(9, 9),
(10, 10);

INSERT INTO general_schema.image (image_data, object_type_id, object_id)
VALUES
(('path to image'), 1, 1),
(('path to image2'), 2, 2);
(('path to image2'), 2, 2),
(('path to image3'), 1, 3),
(('path to image4'), 2, 4),
(('path to image5'), 1, 5),
(('path to image6'), 2, 6),
(('path to image7'), 1, 7),
(('path to image8'), 2, 8),
(('path to image9'), 1, 9),
(('path to image10'), 2, 10);
14 changes: 14 additions & 0 deletions Database/init-func-object-id.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
CREATE OR REPLACE FUNCTION
general_schema.check_fk_exists(object_id INT, object_type_id INT)
RETURNS BOOLEAN AS $$
BEGIN
IF object_type_id = 1 THEN
RETURN EXISTS (SELECT 1 FROM users_schema.user WHERE "id" = object_id);
ELSIF object_type_id = 2 THEN RETURN EXISTS (SELECT 1 FROM events_schema.event WHERE "id" = object_id);
ELSE RETURN FALSE;
END IF;
END;
$$ LANGUAGE plpgsql STABLE;

ALTER TABLE general_schema.image ADD CONSTRAINT chech_object_fk
CHECK (general_schema.check_fk_exists(object_id, object_type_id));
108 changes: 50 additions & 58 deletions Database/init-tables-values.sql
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ CREATE SCHEMA general_schema;

CREATE TABLE IF NOT EXISTS events_schema.event (
"id" SERIAL PRIMARY KEY,
"name" varchar,
"name" varchar NOT NULL,
"address_id" integer,
"description" text,
"description" text NOT NULL,
"organizer_id" integer,
"start_at" timestamp,
"end_at" timestamp,
"created_at" timestamp,
"updated_at" timestamp,
"start_at" timestamptz,
"end_at" timestamptz,
"created_at" timestamptz,
"updated_at" timestamptz,
"slug" varchar,
"status_id" integer
);
Expand All @@ -21,93 +21,93 @@ CREATE TABLE IF NOT EXISTS events_schema.participant (
"id" SERIAL PRIMARY KEY,
"event_id" integer,
"user_id" integer,
"status_id" int
"status_id" integer
);

CREATE TABLE IF NOT EXISTS general_schema.status (
"id" SERIAL PRIMARY KEY,
"name" varchar(255),
"name" varchar(255) NOT NULL,
"object_type_id" integer
);

CREATE TABLE IF NOT EXISTS events_schema.tag (
"id" SERIAL PRIMARY KEY,
"name" varchar(255)
"name" varchar(255) NOT NULL
);

CREATE TABLE IF NOT EXISTS events_schema.event_tag (
"event_id" integer,
"tag_id" integer
"event_id" integer NOT NULL,
"tag_id" integer NOT NULL
);

CREATE TABLE IF NOT EXISTS users_schema.user (
"id" SERIAL PRIMARY KEY,
"name" varchar(255),
"surname" varchar(255),
"username" varchar(255),
"password" varchar(255),
"name" varchar(255) NOT NULL,
"surname" varchar(255) NOT NULL,
"username" varchar(255) NOT NULL,
"password" varchar(255) NOT NULL,
"created_at" timestamp,
"updated_at" timestamp,
"role_id" integer,
"email" varchar(255),
"is_active" bit,
"is_active" bit NOT NULL,
"address_id" integer,
"phone" varchar(255)
);

CREATE TABLE IF NOT EXISTS events_schema.parameter (
"id" SERIAL PRIMARY KEY,
"key" varchar(255),
"value" varchar(255)
"key" varchar(255) NOT NULL,
"value" varchar(255) NOT NULL
);

CREATE TABLE IF NOT EXISTS events_schema.event_parameter (
"parameter_id" integer,
"event_id" integer
"parameter_id" integer NOT NULL,
"event_id" integer NOT NULL
);

CREATE TABLE IF NOT EXISTS users_schema.role (
"id" SERIAL PRIMARY KEY,
"role" varchar(255)
"id" SERIAL PRIMARY KEY NOT NULL,
"role" varchar(255) NOT NULL
);

CREATE TABLE IF NOT EXISTS users_schema.comment (
"id" SERIAL PRIMARY KEY,
"event_id" integer,
"content" varchar(255),
"content" varchar(255) NOT NULL,
"created_at" date,
"user_id" integer
);

CREATE TABLE IF NOT EXISTS users_schema.address (
"id" SERIAL PRIMARY KEY,
"city" varchar(255),
"country" varchar(255),
"street" varchar(255),
"state" varchar(255),
"city" varchar(255) NOT NULL,
"country" varchar(255) NOT NULL,
"street" varchar(255) NOT NULL,
"state" varchar(255) NOT NULL,
"zip_code" varchar(255)
);

CREATE TABLE IF NOT EXISTS general_schema.image (
"id" SERIAL PRIMARY KEY,
"image_data" bytea,
"image_data" bytea NOT NULL,
"object_type_id" integer,
"object_id" integer
);

CREATE TABLE IF NOT EXISTS general_schema.object_type (
"id" SERIAL PRIMARY KEY,
"name" varchar(255)
"name" varchar(255) NOT NULL
);

CREATE TABLE IF NOT EXISTS users_schema.user_interest (
"user_id" integer,
"interest_id" integer
"user_id" integer NOT NULL,
"interest_id" integer NOT NULL
);

CREATE TABLE IF NOT EXISTS users_schema.interest (
"id" SERIAL PRIMARY KEY,
"name" varchar(255),
"name" varchar(255) NOT NULL,
"level" integer
);

Expand Down Expand Up @@ -148,39 +148,31 @@ ALTER TABLE events_schema.event ADD FOREIGN KEY ("status_id") REFERENCES general

ALTER TABLE users_schema.user_interest ADD FOREIGN KEY ("interest_id") REFERENCES users_schema.interest ("id");

CREATE OR REPLACE FUNCTION
general_schema.check_fk_exists(object_id INT, object_type_id INT)
RETURNS BOOLEAN AS $$
BEGIN
IF object_type_id = 1 THEN
RETURN EXISTS (SELECT 1 FROM users_schema.user WHERE "id" = object_id);
ELSIF object_type_id = 2 THEN RETURN EXISTS (SELECT 1 FROM events_schema.event WHERE "id" = object_id);
ELSE RETURN FALSE;
END IF;
END;
$$ LANGUAGE plpgsql STABLE;

ALTER TABLE general_schema.image ADD CONSTRAINT chech_object_fk
CHECK (general_schema.check_fk_exists(object_id, object_type_id));



INSERT INTO users_schema.role ("id", "role") VALUES
(1, 'User'),
(2, 'Organizer'),
(1, 'Użytkownik'),
(2, 'Organizator'),
(3, 'Admin');

INSERT INTO general_schema.object_type ("id", "name") VALUES (1, 'Event'),
(2, 'User');
INSERT INTO general_schema.object_type ("id", "name") VALUES (1, 'Wydarzenie'),
(2, 'Użytkownik');

INSERT INTO general_schema.status ("id", "name", "object_type_id") VALUES
(1, 'Interested', 2),
(2, 'Participant', 2),
(3, 'Archived', 1),
(4, 'In progress', 1);
(1, 'Zainteresoway', 2),
(2, 'Wezmę udział', 2),
(3, 'Zrealizowano', 1),
(4, 'W trakcie realizacji', 1);

INSERT INTO events_schema.tag ("id", "name") VALUES
(1, 'Technology'),
(2, 'Health');
(1, 'Technologia'),
(2, 'Sport'),
(3, 'Piłka nożna'),
(4, 'Muzyka'),
(5, 'Rock'),
(6, 'Koszykówka'),
(7, 'Hip-Hop'),
(8, 'Esport'),
(9, 'League of Legends'),
(10, 'Zdrowie');


3 changes: 2 additions & 1 deletion docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ services:
volumes:
- postgres-data:/var/lib/postgresql/data
- ./Database/init-tables-values.sql:/docker-entrypoint-initdb.d/01_init-tables-values.sql
- ./Database/init-user-db.sh:/docker-entrypoint-initdb.d/02_init-user-db.sh
- ./Database/init-func-object-id.sql:/docker-entrypoint-initdb.d/02_init-func-object-id.sql
- ./Database/init-user-db.sh:/docker-entrypoint-initdb.d/03_init-user-db.sh
env_file:
- .env
- .env.sample
Expand Down

0 comments on commit 1c48514

Please sign in to comment.