From 31464419cbb184a8ebf9e4273b044d0a8e6d3a10 Mon Sep 17 00:00:00 2001 From: KinTrae Date: Tue, 27 Aug 2024 19:22:04 +0200 Subject: [PATCH] feature: devDb on docker --- db/scripts/ddl/MeowHub_Database_create.sql | 236 ++++++++-------- db/scripts/ddl/MeowHub_Database_create_h.sql | 262 ++++++++++-------- db/scripts/ddl/MeowHub_Database_drop.sql | 57 ++-- db/scripts/ddl/MeowHub_Database_drop_h.sql | 3 + .../ddl/prepare_db_create_user_meowhub.sql | 24 ++ db/scripts/dml/insert_demo.sql | 108 ++++---- db/scripts/start.sql | 13 + db/scripts/triggers/tf_aud_info.sql | 203 +++++++++----- docker-compose.yaml | 11 +- 9 files changed, 518 insertions(+), 399 deletions(-) create mode 100644 db/scripts/ddl/prepare_db_create_user_meowhub.sql create mode 100644 db/scripts/start.sql diff --git a/db/scripts/ddl/MeowHub_Database_create.sql b/db/scripts/ddl/MeowHub_Database_create.sql index e2cdb89..ed86b45 100644 --- a/db/scripts/ddl/MeowHub_Database_create.sql +++ b/db/scripts/ddl/MeowHub_Database_create.sql @@ -1,157 +1,137 @@ -- Table: user_roles -CREATE TABLE user_roles ( - id SERIAL PRIMARY KEY, - name VARCHAR(30) NOT NULL, - description VARCHAR(250), - create_user_id INT NOT NULL, - create_date TIMESTAMP NOT NULL, - mod_user_id INT, - mod_date TIMESTAMP +CREATE TABLE user_roles +( + id NUMBER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY, + name VARCHAR2(30) NOT NULL, + description VARCHAR2(250), + create_user_id NUMBER NOT NULL, + create_date TIMESTAMP NOT NULL, + mod_user_id NUMBER, + mod_date TIMESTAMP ); --- Table: status -CREATE TABLE status ( - id SERIAL PRIMARY KEY, - name VARCHAR(50) NOT NULL, - description VARCHAR(250), - create_user_id INT NOT NULL, - create_date TIMESTAMP NOT NULL, - mod_user_id INT, - mod_date TIMESTAMP +-- Table: user_status (renamed from status) +CREATE TABLE user_status +( + id NUMBER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY, + name VARCHAR2(50) NOT NULL, + description VARCHAR2(250), + create_user_id NUMBER NOT NULL, + create_date TIMESTAMP NOT NULL, + mod_user_id NUMBER, + mod_date TIMESTAMP ); -- Table: images -CREATE TABLE images ( - id SERIAL PRIMARY KEY, - image BYTEA NOT NULL, - create_user_id INT NOT NULL, - create_date TIMESTAMP NOT NULL, - mod_user_id INT, - mod_date TIMESTAMP +CREATE TABLE images--TODO - do czego podłączyć??? +( + id NUMBER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY, + image BLOB NOT NULL, + create_user_id NUMBER NOT NULL, + create_date TIMESTAMP NOT NULL, + mod_user_id NUMBER, + mod_date TIMESTAMP ); -- Table: groups -CREATE TABLE groups ( - id SERIAL PRIMARY KEY, - name VARCHAR(25) NOT NULL, - description VARCHAR(250), - images_id INT, - create_user_id INT NOT NULL, - create_date TIMESTAMP NOT NULL, - mod_user_id INT, - mod_date TIMESTAMP, - CONSTRAINT fk_images_id FOREIGN KEY (images_id) REFERENCES images(id) +CREATE TABLE groups +( + id NUMBER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY, + name VARCHAR2(25) NOT NULL, + description VARCHAR2(250), + images_id NUMBER, + create_user_id NUMBER NOT NULL, + create_date TIMESTAMP NOT NULL, + mod_user_id NUMBER, + mod_date TIMESTAMP, + CONSTRAINT fk_images_id FOREIGN KEY (images_id) REFERENCES images (id) ); -- Table: tags -CREATE TABLE tags ( - id SERIAL PRIMARY KEY, - name VARCHAR(50) NOT NULL, - description VARCHAR(250), - create_user_id INT NOT NULL, - create_date TIMESTAMP NOT NULL, - mod_user_id INT, - mod_date TIMESTAMP, +CREATE TABLE tags +( + id NUMBER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY, + name VARCHAR2(50) NOT NULL, + description VARCHAR2(250), + create_user_id NUMBER NOT NULL, + create_date TIMESTAMP NOT NULL, + mod_user_id NUMBER, + mod_date TIMESTAMP, CONSTRAINT tags_name_uq UNIQUE (name) ); -- Table: users -CREATE TABLE users ( - id SERIAL PRIMARY KEY, - login VARCHAR(10) NOT NULL, - name VARCHAR(20) NOT NULL, - second_name VARCHAR(20), - lastname VARCHAR(20) NOT NULL, - password VARCHAR(40) NOT NULL, - salt VARCHAR(40) NOT NULL, - refresh_token INT, - role INT NOT NULL, - birthdate DATE NOT NULL, - email VARCHAR(40) NOT NULL, - status INT NOT NULL, - create_user_id INT NOT NULL, - create_date TIMESTAMP NOT NULL, - mod_user_id INT, - mod_date TIMESTAMP, +CREATE TABLE users +( + id NUMBER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY, + login VARCHAR2(10) NOT NULL, + name VARCHAR2(20) NOT NULL, + second_name VARCHAR2(20), + lastname VARCHAR2(20) NOT NULL, + password VARCHAR2(40) NOT NULL, + salt VARCHAR2(40) NOT NULL, + role NUMBER NOT NULL, + birthdate DATE NOT NULL, + email VARCHAR2(40) NOT NULL, + status NUMBER NOT NULL, + create_user_id NUMBER NOT NULL, + create_date TIMESTAMP NOT NULL, + mod_user_id NUMBER, + mod_date TIMESTAMP, CONSTRAINT users_login_uq UNIQUE (login), CONSTRAINT users_email_uq UNIQUE (email), - CONSTRAINT fk_role FOREIGN KEY (role) REFERENCES user_roles(id), - CONSTRAINT fk_status FOREIGN KEY (status) REFERENCES status(id) + CONSTRAINT fk_role FOREIGN KEY (role) REFERENCES user_roles (id), + CONSTRAINT fk_status FOREIGN KEY (status) REFERENCES user_status (id) +); + +-- Table: users_sessions +CREATE TABLE users_sessions +( + user_id NUMBER PRIMARY KEY, + token NUMBER, + refresh_token NUMBER, + CONSTRAINT fk_users FOREIGN KEY (user_id) REFERENCES users (id) ); -- Table: posts -CREATE TABLE posts ( - id SERIAL PRIMARY KEY, - content VARCHAR(5000) NOT NULL, - date INT NOT NULL, - users_id INT NOT NULL, - create_user_id INT NOT NULL, - create_date TIMESTAMP NOT NULL, - mod_user_id INT, - mod_date TIMESTAMP, - CONSTRAINT fk_users_id FOREIGN KEY (users_id) REFERENCES users(id) +CREATE TABLE posts +( + id NUMBER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY, + content VARCHAR2(4000) NOT NULL, + post_date NUMBER NOT NULL, + user_id NUMBER NOT NULL, + create_user_id NUMBER NOT NULL, + create_date TIMESTAMP NOT NULL, + mod_user_id NUMBER, + mod_date TIMESTAMP, + CONSTRAINT fk_users_id FOREIGN KEY (user_id) REFERENCES users (id) ); -- Table: comments -CREATE TABLE comments ( - idi SERIAL PRIMARY KEY, - comment VARCHAR(2000) NOT NULL, - users_id INT NOT NULL, - posts_id INT NOT NULL, - create_user_id INT NOT NULL, - create_date TIMESTAMP NOT NULL, - mod_user_id INT, - mod_date TIMESTAMP, - CONSTRAINT fk_comments_users FOREIGN KEY (users_id) REFERENCES users(id), - CONSTRAINT fk_comments_posts FOREIGN KEY (posts_id) REFERENCES posts(id) +CREATE TABLE comments +( + id NUMBER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY, + comment_value VARCHAR2(2000) NOT NULL, + users_id NUMBER NOT NULL, + posts_id NUMBER NOT NULL, + create_user_id NUMBER NOT NULL, + create_date TIMESTAMP NOT NULL, + mod_user_id NUMBER, + mod_date TIMESTAMP, + CONSTRAINT fk_comments_users FOREIGN KEY (users_id) REFERENCES users (id), + CONSTRAINT fk_comments_posts FOREIGN KEY (posts_id) REFERENCES posts (id) ); -- Table: group_users -CREATE TABLE group_users ( - groups_id INT NOT NULL, - users_id INT NOT NULL, - create_user_id INT NOT NULL, - create_date TIMESTAMP NOT NULL, - mod_user_id INT, - mod_date TIMESTAMP, +CREATE TABLE group_users +( + groups_id NUMBER NOT NULL, + users_id NUMBER NOT NULL, + create_user_id NUMBER NOT NULL, + create_date TIMESTAMP NOT NULL, + mod_user_id NUMBER, + mod_date TIMESTAMP, CONSTRAINT group_users_pk PRIMARY KEY (groups_id, users_id), - CONSTRAINT fk_group_users_groups FOREIGN KEY (groups_id) REFERENCES groups(id), - CONSTRAINT fk_group_users_users FOREIGN KEY (users_id) REFERENCES users(id) -); - --- Table: posts_images -CREATE TABLE posts_images ( - posts_id INT NOT NULL, - images_id INT NOT NULL, - create_user_id INT NOT NULL, - create_date TIMESTAMP NOT NULL, - mod_user_id INT, - mod_date TIMESTAMP, - CONSTRAINT posts_images_pk PRIMARY KEY (posts_id, images_id), - CONSTRAINT fk_posts_images_posts FOREIGN KEY (posts_id) REFERENCES posts(id), - CONSTRAINT fk_posts_images_images FOREIGN KEY (images_id) REFERENCES images(id) + CONSTRAINT fk_group_users_groups FOREIGN KEY (groups_id) REFERENCES groups (id), + CONSTRAINT fk_group_users_users FOREIGN KEY (users_id) REFERENCES users ); - --- Table: posts_tags -CREATE TABLE posts_tags ( - posts_id INT NOT NULL, - tags_id INT NOT NULL, - create_user_id INT NOT NULL, - create_date TIMESTAMP NOT NULL, - mod_user_id INT, - mod_date TIMESTAMP, - CONSTRAINT posts_tags_pk PRIMARY KEY (posts_id, tags_id), - CONSTRAINT fk_posts_tags_posts FOREIGN KEY (posts_id) REFERENCES posts(id), - CONSTRAINT fk_posts_tags_tags FOREIGN KEY (tags_id) REFERENCES tags(id) -); - --- Create Dual table: -CREATE TABLE DUAL ( - DUMMY VARCHAR(1) -); - -INSERT INTO DUAL (DUMMY) VALUES ('X'); - - - - diff --git a/db/scripts/ddl/MeowHub_Database_create_h.sql b/db/scripts/ddl/MeowHub_Database_create_h.sql index 082c74b..31e9c86 100644 --- a/db/scripts/ddl/MeowHub_Database_create_h.sql +++ b/db/scripts/ddl/MeowHub_Database_create_h.sql @@ -1,151 +1,173 @@ -- Table: h_user_roles -CREATE TABLE h_user_roles ( - h_id SERIAL PRIMARY KEY, - id INT, - h_oper VARCHAR(1), - name VARCHAR(30) NOT NULL, - description VARCHAR(250), - create_user_id INT NOT NULL, - create_date TIMESTAMP NOT NULL, - mod_user_id INT, - mod_date TIMESTAMP +CREATE TABLE h_user_roles +( + h_id NUMBER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY, + id NUMBER, + h_oper VARCHAR2(1), + name VARCHAR2(30) NOT NULL, + description VARCHAR2(250), + create_user_id NUMBER NOT NULL, + create_date TIMESTAMP NOT NULL, + mod_user_id NUMBER, + mod_date TIMESTAMP ); --- Table: h_status -CREATE TABLE h_status ( - h_id SERIAL PRIMARY KEY, - h_oper VARCHAR(1), - id INT, - name VARCHAR(50) NOT NULL, - description VARCHAR(250), - create_user_id INT NOT NULL, - create_date TIMESTAMP NOT NULL, - mod_user_id INT, - mod_date TIMESTAMP +-- Table: h_user_status (renamed from h_status) +CREATE TABLE h_user_status +( + h_id NUMBER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY, + h_oper VARCHAR2(1), + id NUMBER, + name VARCHAR2(50) NOT NULL, + description VARCHAR2(250), + create_user_id NUMBER NOT NULL, + create_date TIMESTAMP NOT NULL, + mod_user_id NUMBER, + mod_date TIMESTAMP ); -- Table: h_images -CREATE TABLE h_images ( - h_id SERIAL PRIMARY KEY, - h_oper VARCHAR(1), - id INT, - image BYTEA NOT NULL, - create_user_id INT NOT NULL, - create_date TIMESTAMP NOT NULL, - mod_user_id INT, - mod_date TIMESTAMP +CREATE TABLE h_images +( + h_id NUMBER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY, + h_oper VARCHAR2(1), + id NUMBER, + image BLOB NOT NULL, + create_user_id NUMBER NOT NULL, + create_date TIMESTAMP NOT NULL, + mod_user_id NUMBER, + mod_date TIMESTAMP ); -- Table: h_groups -CREATE TABLE h_groups ( - h_id SERIAL PRIMARY KEY, - h_oper VARCHAR(1), - id INT, - name VARCHAR(25) NOT NULL, - description VARCHAR(250), - images_id INT, - create_user_id INT NOT NULL, - create_date TIMESTAMP NOT NULL, - mod_user_id INT, - mod_date TIMESTAMP +CREATE TABLE h_groups +( + h_id NUMBER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY, + h_oper VARCHAR2(1), + id NUMBER, + name VARCHAR2(25) NOT NULL, + description VARCHAR2(250), + images_id NUMBER, + create_user_id NUMBER NOT NULL, + create_date TIMESTAMP NOT NULL, + mod_user_id NUMBER, + mod_date TIMESTAMP ); -- Table: h_tags -CREATE TABLE h_tags ( - h_id SERIAL PRIMARY KEY, - h_oper VARCHAR(1), - id INT, - name VARCHAR(50) NOT NULL, - description VARCHAR(250), - create_user_id INT NOT NULL, - create_date TIMESTAMP NOT NULL, - mod_user_id INT, - mod_date TIMESTAMP +CREATE TABLE h_tags +( + h_id NUMBER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY, + h_oper VARCHAR2(1), + id NUMBER, + name VARCHAR2(50) NOT NULL, + description VARCHAR2(250), + create_user_id NUMBER NOT NULL, + create_date TIMESTAMP NOT NULL, + mod_user_id NUMBER, + mod_date TIMESTAMP ); -- Table: h_users -CREATE TABLE h_users ( - h_id SERIAL PRIMARY KEY, - h_oper VARCHAR(1), - id INT, - login VARCHAR(10) NOT NULL, - name VARCHAR(20) NOT NULL, - second_name VARCHAR(20), - lastname VARCHAR(20) NOT NULL, - password VARCHAR(40) NOT NULL, - salt VARCHAR(40) NOT NULL, - refresh_token INT, - role INT NOT NULL, - birthdate DATE NOT NULL, - email VARCHAR(40) NOT NULL, - status INT NOT NULL, - create_user_id INT NOT NULL, - create_date TIMESTAMP NOT NULL, - mod_user_id INT, - mod_date TIMESTAMP +CREATE TABLE h_users +( + h_id NUMBER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY, + h_oper VARCHAR2(1), + id NUMBER, + login VARCHAR2(10) NOT NULL, + name VARCHAR2(20) NOT NULL, + second_name VARCHAR2(20), + lastname VARCHAR2(20) NOT NULL, + password VARCHAR2(40) NOT NULL, + salt VARCHAR2(40) NOT NULL, + refresh_token NUMBER, + role NUMBER NOT NULL, + birthdate DATE NOT NULL, + email VARCHAR2(40) NOT NULL, + status NUMBER NOT NULL, + create_user_id NUMBER NOT NULL, + create_date TIMESTAMP NOT NULL, + mod_user_id NUMBER, + mod_date TIMESTAMP +); + +-- Table: h_users_sessions +CREATE TABLE h_users_sessions +( + user_id NUMBER PRIMARY KEY, + token NUMBER, + refresh_token NUMBER, + CONSTRAINT fk_h_users FOREIGN KEY (user_id) REFERENCES users (id) ); -- Table: h_posts -CREATE TABLE h_posts ( - h_id SERIAL PRIMARY KEY, - h_oper VARCHAR(1), - id INT, - content VARCHAR(5000) NOT NULL, - date INT NOT NULL, - users_id INT NOT NULL, - create_user_id INT NOT NULL, - create_date TIMESTAMP NOT NULL, - mod_user_id INT, - mod_date TIMESTAMP, - CONSTRAINT fk_users_id FOREIGN KEY (users_id) REFERENCES h_users(h_id) +CREATE TABLE h_posts +( + h_id NUMBER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY, + h_oper VARCHAR2(1), + id NUMBER, + content VARCHAR2(4000) NOT NULL, + post_date NUMBER NOT NULL, + user_id NUMBER NOT NULL, + create_user_id NUMBER NOT NULL, + create_date TIMESTAMP NOT NULL, + mod_user_id NUMBER, + mod_date TIMESTAMP, + CONSTRAINT fk_h_users_id FOREIGN KEY (user_id) REFERENCES h_users (h_id) ); -- Table: h_comments -CREATE TABLE h_comments ( - h_id SERIAL PRIMARY KEY, - h_oper VARCHAR(1), - id INT, - comment VARCHAR(2000) NOT NULL, - users_id INT NOT NULL, - posts_id INT NOT NULL, - create_user_id INT NOT NULL, - create_date TIMESTAMP NOT NULL, - mod_user_id INT, - mod_date TIMESTAMP +CREATE TABLE h_comments +( + h_id NUMBER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY, + h_oper VARCHAR2(1), + id NUMBER, + comment_value VARCHAR2(2000) NOT NULL, + users_id NUMBER NOT NULL, + posts_id NUMBER NOT NULL, + create_user_id NUMBER NOT NULL, + create_date TIMESTAMP NOT NULL, + mod_user_id NUMBER, + mod_date TIMESTAMP ); -- Table: h_group_users -CREATE TABLE h_group_users ( - h_id SERIAL PRIMARY KEY, - h_oper VARCHAR(1), - groups_id INT NOT NULL, - users_id INT NOT NULL, - create_user_id INT NOT NULL, - create_date TIMESTAMP NOT NULL, - mod_user_id INT, - mod_date TIMESTAMP +CREATE TABLE h_group_users +( + h_id NUMBER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY, + h_oper VARCHAR2(1), + groups_id NUMBER NOT NULL, + users_id NUMBER NOT NULL, + create_user_id NUMBER NOT NULL, + create_date TIMESTAMP NOT NULL, + mod_user_id NUMBER, + mod_date TIMESTAMP ); -- Table: h_posts_images -CREATE TABLE h_posts_images ( - h_id SERIAL PRIMARY KEY, - h_oper VARCHAR(1), - posts_id INT NOT NULL, - images_id INT NOT NULL, - create_user_id INT NOT NULL, - create_date TIMESTAMP NOT NULL, - mod_user_id INT, - mod_date TIMESTAMP +CREATE TABLE h_posts_images +( + h_id NUMBER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY, + h_oper VARCHAR2(1), + posts_id NUMBER NOT NULL, + images_id NUMBER NOT NULL, + create_user_id NUMBER NOT NULL, + create_date TIMESTAMP NOT NULL, + mod_user_id NUMBER, + mod_date TIMESTAMP ); -- Table: h_posts_tags -CREATE TABLE h_posts_tags ( - h_id SERIAL PRIMARY KEY, - h_oper VARCHAR(1), - posts_id INT NOT NULL, - tags_id INT NOT NULL, - create_user_id INT NOT NULL, - create_date TIMESTAMP NOT NULL, - mod_user_id INT, - mod_date TIMESTAMP +CREATE TABLE h_posts_tags +( + h_id NUMBER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY, + h_oper VARCHAR2(1), + posts_id NUMBER NOT NULL, + tags_id NUMBER NOT NULL, + create_user_id NUMBER NOT NULL, + create_date TIMESTAMP NOT NULL, + mod_user_id NUMBER, + mod_date TIMESTAMP ); + +COMMIT; diff --git a/db/scripts/ddl/MeowHub_Database_drop.sql b/db/scripts/ddl/MeowHub_Database_drop.sql index a3adf2c..e9f5926 100644 --- a/db/scripts/ddl/MeowHub_Database_drop.sql +++ b/db/scripts/ddl/MeowHub_Database_drop.sql @@ -1,26 +1,31 @@ --- Drop constraints -ALTER TABLE users DROP CONSTRAINT IF EXISTS fk_role; -ALTER TABLE users DROP CONSTRAINT IF EXISTS fk_status; -ALTER TABLE groups DROP CONSTRAINT IF EXISTS fk_images_id; -ALTER TABLE comments DROP CONSTRAINT IF EXISTS fk_comments_users; -ALTER TABLE comments DROP CONSTRAINT IF EXISTS fk_comments_posts; -ALTER TABLE group_users DROP CONSTRAINT IF EXISTS fk_group_users_groups; -ALTER TABLE group_users DROP CONSTRAINT IF EXISTS fk_group_users_users; -ALTER TABLE posts_images DROP CONSTRAINT IF EXISTS fk_posts_images_posts; -ALTER TABLE posts_images DROP CONSTRAINT IF EXISTS fk_posts_images_images; -ALTER TABLE posts_tags DROP CONSTRAINT IF EXISTS fk_posts_tags_posts; -ALTER TABLE posts_tags DROP CONSTRAINT IF EXISTS fk_posts_tags_tags; - --- Drop tables -DROP TABLE IF EXISTS posts_tags; -DROP TABLE IF EXISTS posts_images; -DROP TABLE IF EXISTS group_users; -DROP TABLE IF EXISTS comments; -DROP TABLE IF EXISTS posts; -DROP TABLE IF EXISTS users; -DROP TABLE IF EXISTS tags; -DROP TABLE IF EXISTS groups; -DROP TABLE IF EXISTS images; -DROP TABLE IF EXISTS status; -DROP TABLE IF EXISTS user_roles; -DROP TABLE IF EXISTS DUAL; \ No newline at end of file +-- Usunięcie tabel, zaczynając od tabel z zależnościami + +-- Drop Table: group_users +DROP TABLE group_users CASCADE CONSTRAINTS; + +-- Drop Table: comments +DROP TABLE comments CASCADE CONSTRAINTS; + +-- Drop Table: posts +DROP TABLE posts CASCADE CONSTRAINTS; + +--DROP Table: users_sessions +DROP TABLE users_sessions CASCADE CONSTRAINTS; + +-- Drop Table: users +DROP TABLE users CASCADE CONSTRAINTS; + +-- Drop Table: tags +DROP TABLE tags CASCADE CONSTRAINTS; + +-- Drop Table: groups +DROP TABLE groups CASCADE CONSTRAINTS; + +-- Drop Table: images +DROP TABLE images CASCADE CONSTRAINTS; + +-- Drop Table: user_status +DROP TABLE user_status CASCADE CONSTRAINTS; + +-- Drop Table: user_roles +DROP TABLE user_roles CASCADE CONSTRAINTS; \ No newline at end of file diff --git a/db/scripts/ddl/MeowHub_Database_drop_h.sql b/db/scripts/ddl/MeowHub_Database_drop_h.sql index 4030ba9..12f2f91 100644 --- a/db/scripts/ddl/MeowHub_Database_drop_h.sql +++ b/db/scripts/ddl/MeowHub_Database_drop_h.sql @@ -16,6 +16,9 @@ DROP TABLE IF EXISTS h_posts; -- Drop Table: h_users DROP TABLE IF EXISTS h_users; +--DROP Table: h_users_sessions +DROP TABLE h_users_sessions CASCADE CONSTRAINTS; + -- Drop Table: h_tags DROP TABLE IF EXISTS h_tags; diff --git a/db/scripts/ddl/prepare_db_create_user_meowhub.sql b/db/scripts/ddl/prepare_db_create_user_meowhub.sql new file mode 100644 index 0000000..2fca93d --- /dev/null +++ b/db/scripts/ddl/prepare_db_create_user_meowhub.sql @@ -0,0 +1,24 @@ +--dodatkowe (pomocnicze zapytania) + +--sprawdzenie, na jakiej instancji jest uruchomiona nasza baza danych (chcemy PWD) +SHOW CON_NAME; + +--sprawdzenie na jakiej wersji bazy działamy, np. "Oracle Database 21c Express Edition Release 21.0.0.0.0 - Production" +SELECT banner FROM v$version WHERE ROWNUM = 1; + +--sprawdzenie dostępnych kontenerów +show pdbs; + +--sprawdzenie na jakim użytkowniku jesteśmy zalogowani +show user; + +--stworzenie użytkownika globalnego dla CDB - istnieje dla wszystkich dziedziczącyh po nim PDB +--CREATE USER C##app_user IDENTIFIED BY app_user; + +--Zmiana z CBD na PDB +ALTER SESSION SET CONTAINER = XEPDB1; + +--stworzenie user'a na kontenerze +CREATE USER meowhub identified by meowhub; +GRANT ALL PRIVILEGES TO meowhub; + diff --git a/db/scripts/dml/insert_demo.sql b/db/scripts/dml/insert_demo.sql index 1dfd241..9c668b4 100644 --- a/db/scripts/dml/insert_demo.sql +++ b/db/scripts/dml/insert_demo.sql @@ -1,55 +1,53 @@ --- Table: user_roles -INSERT INTO user_roles (id, name, description, create_user_id, create_date, mod_user_id, mod_date) VALUES -(1, 'Admin', 'Administrator role', 1, current_timestamp, NULL, NULL), -(2, 'User', 'Regular user role', 1, current_timestamp, NULL, NULL); - --- Table: status -INSERT INTO status (id, name, description, create_user_id, create_date, mod_user_id, mod_date) VALUES -(1, 'Active', 'Active status', 1, current_timestamp, NULL, NULL), -(2, 'Inactive', 'Inactive status', 1, current_timestamp, NULL, NULL); - --- Table: images -INSERT INTO images (id, image, create_user_id, create_date, mod_user_id, mod_date) VALUES -(1, E'\\x', 1, current_timestamp, NULL, NULL), -(2, E'\\x', 1, current_timestamp, NULL, NULL); - --- Table: groups -INSERT INTO groups (id, name, description, images_id, create_user_id, create_date, mod_user_id, mod_date) VALUES -(1, 'Group A', 'Description for Group A', 1, 1, current_timestamp, NULL, NULL), -(2, 'Group B', 'Description for Group B', 1, 1, current_timestamp, NULL, NULL); - --- Table: tags -INSERT INTO tags (id, name, description, create_user_id, create_date, mod_user_id, mod_date) VALUES -(1, 'Tag1', 'Description for Tag1', 1, current_timestamp, NULL, NULL), -(2, 'Tag2', 'Description for Tag2', 1, current_timestamp, NULL, NULL); - --- Table: users -INSERT INTO users (id, login, name, second_name, lastname, password, salt, refresh_token, role, birthdate, email, status, create_user_id, create_date, mod_user_id, mod_date) VALUES -(1, 'user1', 'John', NULL, 'Doe', 'password1', 'salt1', NULL, 1, '1990-01-01', 'john.doe@example.com', 1, 1, current_timestamp, NULL, NULL), -(2, 'user2', 'Jane', NULL, 'Smith', 'password2', 'salt2', NULL, 2, '1991-02-02', 'jane.smith@example.com', 1, 1, current_timestamp, NULL, NULL); - --- Table: posts -INSERT INTO posts (id, content, date, users_id, create_user_id, create_date, mod_user_id, mod_date) VALUES -(1, 'Post content 1', 1234567890, 1, 1, current_timestamp, NULL, NULL), -(2, 'Post content 2', 1234567890, 2, 1, current_timestamp, NULL, NULL); - --- Table: comments -INSERT INTO comments (idi, comment, users_id, posts_id, create_user_id, create_date, mod_user_id, mod_date) VALUES -(1, 'Comment 1', 1, 1, 1, current_timestamp, NULL, NULL), -(2, 'Comment 2', 2, 2, 1, current_timestamp, NULL, NULL); - --- Table: group_users -INSERT INTO group_users (groups_id, users_id, create_user_id, create_date, mod_user_id, mod_date) VALUES -(1, 1, 1, current_timestamp, NULL, NULL), -(2, 2, 1, current_timestamp, NULL, NULL); - --- Table: posts_images -INSERT INTO posts_images (posts_id, images_id, create_user_id, create_date, mod_user_id, mod_date) VALUES -(1, 1, 1, current_timestamp, NULL, NULL), -(2, 2, 1, current_timestamp, NULL, NULL); - --- Table: posts_tags -INSERT INTO posts_tags (posts_id, tags_id, create_user_id, create_date, mod_user_id, mod_date) VALUES -(1, 1, 1, current_timestamp, NULL, NULL), -(2, 2, 1, current_timestamp, NULL, NULL); - +-- Insert into user_roles +INSERT INTO user_roles (name, description) +VALUES ('Admin', 'Administrator role'); +INSERT INTO user_roles (name, description) +VALUES ('User', 'Regular user role'); + +-- Insert into user_status +INSERT INTO user_status (name, description) +VALUES ('Active', 'Active status'); +INSERT INTO user_status (name, description) +VALUES ('Inactive', 'Inactive status'); + +-- Insert into images +INSERT INTO images (image) +VALUES (EMPTY_BLOB()); +INSERT INTO images (image) +VALUES (EMPTY_BLOB()); + +-- Insert into groups +INSERT INTO groups (name, description, images_id) +VALUES ('Group1', 'First group', 1); +INSERT INTO groups (name, description, images_id) +VALUES ('Group2', 'Second group', 2); + +-- Insert into tags +INSERT INTO tags (name, description) +VALUES ('Technology', 'Tech related posts'); +INSERT INTO tags (name, description) +VALUES ('Lifestyle', 'Lifestyle related posts'); + +-- Insert into users +INSERT INTO users (login, name, second_name, lastname, password, salt, role, birthdate, email, status) +VALUES ('admin', 'admin', '', 'lastname', 'admin', 'salt1', 1, TO_DATE('1990-01-01', 'YYYY-MM-DD'), 'jdoe@example.com', 1); +INSERT INTO users (login, name, second_name, lastname, password, salt, role, birthdate, email, status) +VALUES ('user', 'user', 'B', 'Smith', 'user', 'salt2', 2, TO_DATE('1985-05-10', 'YYYY-MM-DD'), 'asmith@example.com', 1); + +-- Insert into posts +INSERT INTO posts (content, post_date, user_id) +VALUES ('This is the first post.', 1, 1); +INSERT INTO posts (content, post_date, user_id) +VALUES ('This is the second post.', 2, 2); + +-- Insert into comments +INSERT INTO comments (comment_value, users_id, posts_id) +VALUES ('Great post!', 1, 1); +INSERT INTO comments (comment_value, users_id, posts_id) +VALUES ('Thanks for sharing.', 2, 2); + +-- Insert into group_users +INSERT INTO group_users (groups_id, users_id) +VALUES (1, 1); +INSERT INTO group_users (groups_id, users_id) +VALUES (2, 2); diff --git a/db/scripts/start.sql b/db/scripts/start.sql new file mode 100644 index 0000000..b5d812c --- /dev/null +++ b/db/scripts/start.sql @@ -0,0 +1,13 @@ +-- Prepare DB and Create User +@/opt/oracle/scripts/startup/ddl/prepare_db_create_user_meowhub.sql + +-- Disconnect from SYS and reconnect as meowhub +connect meowhub/meowhub@XEPDB1 + +-- Run the rest of the scripts as meowhub +@/opt/oracle/scripts/startup/ddl/MeowHub_Database_drop.sql +@/opt/oracle/scripts/startup/ddl/MeowHub_Database_drop_h.sql +@/opt/oracle/scripts/startup/ddl/MeowHub_Database_create.sql +@/opt/oracle/scripts/startup/ddl/MeowHub_Database_create_h.sql +@/opt/oracle/scripts/startup/triggers/tf_aud_info.sql +@/opt/oracle/scripts/startup/dml/insert_demo.sql \ No newline at end of file diff --git a/db/scripts/triggers/tf_aud_info.sql b/db/scripts/triggers/tf_aud_info.sql index cd73f7e..17c00c0 100644 --- a/db/scripts/triggers/tf_aud_info.sql +++ b/db/scripts/triggers/tf_aud_info.sql @@ -1,6 +1,6 @@ -- funkcja uniwersalna wykorzystywana dla trigger'ów audytowych before insert /* -trigger audytowy: +trigger audytowy: - uzupełnienie 4 kolumn audytowe - liczba modyfikacji danego rekordu (?) - t_nazwa_tabeli_aud/def @@ -8,81 +8,154 @@ trigger audytowy: - nie odpowiada za delete - before insert/update - blokada UPDATE na create_date i create_user - - zastanowić się, czy token, który jest często odświeżany powinien być w tabelce users, (bo wtedy będzie mnóstwo rekordów w h_users), możnaby stworzyć tabelkę 1 do 1 np users_sessions z id_user, refresh_token itd + - zastanowić się, czy token, który jest często odświeżany powinien być w tabelce users, (bo wtedy będzie mnóstwo rekordów w h_users), możnaby stworzyć tabelkę 1 do 1 np users_sessions z id_user, refresh_token itd */ -CREATE OR REPLACE FUNCTION tf_aud_info() -RETURNS TRIGGER AS $$ +-- TRIGGER dla tabeli user_roles +CREATE OR REPLACE TRIGGER trg_user_roles_aud_info + BEFORE INSERT OR UPDATE ON user_roles + FOR EACH ROW BEGIN - -- Set create_user_id and create_date on INSERT - IF TG_OP = 'INSERT' THEN - NEW.create_user_id := -1; -- Set create_user_id to the current user - NEW.create_date := current_timestamp; -- Set create_date to the current timestamp - END IF; + IF INSERTING THEN + :NEW.create_user_id := -1; -- Ustaw ID użytkownika na bieżącego + :NEW.create_date := SYSTIMESTAMP; -- Ustaw datę utworzenia na bieżący timestamp + ELSIF UPDATING THEN + :NEW.mod_user_id := -1; -- Ustaw ID użytkownika na bieżącego + :NEW.mod_date := SYSTIMESTAMP; -- Ustaw datę modyfikacji na bieżący timestamp - -- Set mod_user_id and mod_date on UPDATE - IF TG_OP = 'UPDATE' THEN - NEW.mod_user_id := -1; -- Set mod_user_id to the current user - NEW.mod_date := current_timestamp; -- Set mod_date to the current timestamp - END IF; - - RETURN NEW; + -- Zablokowanie aktualizacji kolumn create_date i create_user_id + :NEW.create_user_id := :OLD.create_user_id; + :NEW.create_date := :OLD.create_date; +END IF; END; -$$ LANGUAGE PLPGSQL; - ---User_Roles -CREATE TRIGGER t_user_roles_aud_info -BEFORE INSERT OR UPDATE ON user_roles -FOR EACH ROW EXECUTE FUNCTION tf_aud_info(); - ---Users -CREATE TRIGGER t_users_aud_info -BEFORE INSERT OR UPDATE ON users -FOR EACH ROW EXECUTE FUNCTION tf_aud_info(); +/ ---Status -CREATE TRIGGER t_status_aud_info -BEFORE INSERT OR UPDATE ON status -FOR EACH ROW EXECUTE FUNCTION tf_aud_info(); +-- Analogiczne TRIGGERY dla pozostałych tabel: ---Posts -CREATE TRIGGER t_posts_aud_info -BEFORE INSERT OR UPDATE ON posts -FOR EACH ROW EXECUTE FUNCTION tf_aud_info(); - ---Images -CREATE TRIGGER t_images_aud_info -BEFORE INSERT OR UPDATE ON images -FOR EACH ROW EXECUTE FUNCTION tf_aud_info(); +CREATE OR REPLACE TRIGGER trg_users_aud_info + BEFORE INSERT OR UPDATE ON users + FOR EACH ROW +BEGIN + IF INSERTING THEN + :NEW.create_user_id := -1; + :NEW.create_date := SYSTIMESTAMP; + ELSIF UPDATING THEN + :NEW.mod_user_id := -1; + :NEW.mod_date := SYSTIMESTAMP; + :NEW.create_user_id := :OLD.create_user_id; + :NEW.create_date := :OLD.create_date; +END IF; +END; +/ ---Posts_Images -CREATE TRIGGER t_posts_images_aud_info -BEFORE INSERT OR UPDATE ON posts_images -FOR EACH ROW EXECUTE FUNCTION tf_aud_info(); +CREATE OR REPLACE TRIGGER trg_user_status_aud_info + BEFORE INSERT OR UPDATE ON user_status + FOR EACH ROW +BEGIN + IF INSERTING THEN + :NEW.create_user_id := -1; + :NEW.create_date := SYSTIMESTAMP; + ELSIF UPDATING THEN + :NEW.mod_user_id := -1; + :NEW.mod_date := SYSTIMESTAMP; + :NEW.create_user_id := :OLD.create_user_id; + :NEW.create_date := :OLD.create_date; +END IF; +END; +/ ---Tags -CREATE TRIGGER t_tags_aud_info -BEFORE INSERT OR UPDATE ON tags -FOR EACH ROW EXECUTE FUNCTION tf_aud_info(); +CREATE OR REPLACE TRIGGER trg_images_aud_info + BEFORE INSERT OR UPDATE ON images + FOR EACH ROW +BEGIN + IF INSERTING THEN + :NEW.create_user_id := -1; + :NEW.create_date := SYSTIMESTAMP; + ELSIF UPDATING THEN + :NEW.mod_user_id := -1; + :NEW.mod_date := SYSTIMESTAMP; + :NEW.create_user_id := :OLD.create_user_id; + :NEW.create_date := :OLD.create_date; +END IF; +END; +/ ---Posts_Tags -CREATE TRIGGER t_posts_tags_aud_info -BEFORE INSERT OR UPDATE ON posts_tags -FOR EACH ROW EXECUTE FUNCTION tf_aud_info(); +CREATE OR REPLACE TRIGGER trg_groups_aud_info + BEFORE INSERT OR UPDATE ON groups + FOR EACH ROW +BEGIN + IF INSERTING THEN + :NEW.create_user_id := -1; + :NEW.create_date := SYSTIMESTAMP; + ELSIF UPDATING THEN + :NEW.mod_user_id := -1; + :NEW.mod_date := SYSTIMESTAMP; + :NEW.create_user_id := :OLD.create_user_id; + :NEW.create_date := :OLD.create_date; +END IF; +END; +/ ---Comments -CREATE TRIGGER t_comments_aud_info -BEFORE INSERT OR UPDATE ON comments -FOR EACH ROW EXECUTE FUNCTION tf_aud_info(); +CREATE OR REPLACE TRIGGER trg_tags_aud_info + BEFORE INSERT OR UPDATE ON tags + FOR EACH ROW +BEGIN + IF INSERTING THEN + :NEW.create_user_id := -1; + :NEW.create_date := SYSTIMESTAMP; + ELSIF UPDATING THEN + :NEW.mod_user_id := -1; + :NEW.mod_date := SYSTIMESTAMP; + :NEW.create_user_id := :OLD.create_user_id; + :NEW.create_date := :OLD.create_date; +END IF; +END; +/ ---Groups -CREATE TRIGGER t_groups_aud_info -BEFORE INSERT OR UPDATE ON groups -FOR EACH ROW EXECUTE FUNCTION tf_aud_info(); +CREATE OR REPLACE TRIGGER trg_posts_aud_info + BEFORE INSERT OR UPDATE ON posts + FOR EACH ROW +BEGIN + IF INSERTING THEN + :NEW.create_user_id := -1; + :NEW.create_date := SYSTIMESTAMP; + ELSIF UPDATING THEN + :NEW.mod_user_id := -1; + :NEW.mod_date := SYSTIMESTAMP; + :NEW.create_user_id := :OLD.create_user_id; + :NEW.create_date := :OLD.create_date; +END IF; +END; +/ ---Group_users -CREATE TRIGGER t_groups_users_aud_info -BEFORE INSERT OR UPDATE ON group_users -FOR EACH ROW EXECUTE FUNCTION tf_aud_info(); +CREATE OR REPLACE TRIGGER trg_comments_aud_info + BEFORE INSERT OR UPDATE ON comments + FOR EACH ROW +BEGIN + IF INSERTING THEN + :NEW.create_user_id := -1; + :NEW.create_date := SYSTIMESTAMP; + ELSIF UPDATING THEN + :NEW.mod_user_id := -1; + :NEW.mod_date := SYSTIMESTAMP; + :NEW.create_user_id := :OLD.create_user_id; + :NEW.create_date := :OLD.create_date; +END IF; +END; +/ -commit; \ No newline at end of file +CREATE OR REPLACE TRIGGER trg_group_users_aud_info + BEFORE INSERT OR UPDATE ON group_users + FOR EACH ROW +BEGIN + IF INSERTING THEN + :NEW.create_user_id := -1; + :NEW.create_date := SYSTIMESTAMP; + ELSIF UPDATING THEN + :NEW.mod_user_id := -1; + :NEW.mod_date := SYSTIMESTAMP; + :NEW.create_user_id := :OLD.create_user_id; + :NEW.create_date := :OLD.create_date; +END IF; +END; +/ diff --git a/docker-compose.yaml b/docker-compose.yaml index ab63c0f..6b2dea1 100644 --- a/docker-compose.yaml +++ b/docker-compose.yaml @@ -5,18 +5,19 @@ services: - "1080:1080" - "1025:1025" oracle-db: - image: container-registry.oracle.com/database/enterprise:latest + image: container-registry.oracle.com/database/express:latest environment: - - ORACLE_SID=ORCLCDB - - ORACLE_PDB=ORCLPDB1 - - ORACLE_PWD=Oracle_123 + - ORACLE_SID=XE + - ORACLE_PDB=XEPDB1 + - ORACLE_PWD=oracle ports: - "1521:1521" volumes: - oracle-data:/opt/oracle/oradata - oracle-backup:/opt/oracle/backup + - ./db/scripts:/opt/oracle/scripts/startup healthcheck: - test: [ "CMD", "sqlplus", "-L", "sys/Oracle_123@//localhost:1521/ORCLCDB as sysdba", "@healthcheck.sql" ] + test: [ "CMD", "sqlplus", "-L", "sys/oracle//localhost:1521/XE as sysdba", "@healthcheck.sql" ] interval: 30s timeout: 10s retries: 5