From 4449460f935269ee0f469e17deceb272b88ecda6 Mon Sep 17 00:00:00 2001 From: Yohan Boujon Date: Mon, 16 Dec 2024 15:07:03 +0100 Subject: [PATCH] Updated Database and created init scripts. --- .gitignore | 3 ++- db/README.md | 23 +++++++++++++++++++++++ db/db.sql | 20 ++++++++++---------- db/init.sh | 28 ++++++++++++++++++++++++++++ db/remove.sh | 21 +++++++++++++++++++++ 5 files changed, 84 insertions(+), 11 deletions(-) create mode 100644 db/README.md create mode 100755 db/init.sh create mode 100755 db/remove.sh diff --git a/.gitignore b/.gitignore index 9f97022..8180423 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ -target/ \ No newline at end of file +target/ +**/.env \ No newline at end of file diff --git a/db/README.md b/db/README.md new file mode 100644 index 0000000..84448f2 --- /dev/null +++ b/db/README.md @@ -0,0 +1,23 @@ +# Database Initialization + +First create a user in the mariadb: + +```bash +sudo mariab -u root -p +``` +```sql +CREATE USER ''@'localhost' IDENTIFIED BY ''; +GRANT ALL PRIVILEGES ON *.* TO ''@'localhost' WITH GRANT OPTION; +FLUSH PRIVILEGES; +``` + +Then modify the `.env` file: + +```bash +SERVICE_ARCHITECTURE_USER=user +SERVICE_ARCHITECTURE_PASSWORD=password +``` + +**And finally run the `init.sh` to create the database.** + +> If you want to update the database, it is recommended to run `remove.sh` before hand. \ No newline at end of file diff --git a/db/db.sql b/db/db.sql index 8a150d5..6bb5831 100644 --- a/db/db.sql +++ b/db/db.sql @@ -1,22 +1,22 @@ CREATE TABLE `users` ( - `id` integer UNIQUE PRIMARY KEY NOT NULL, + `id` integer UNIQUE PRIMARY KEY AUTO_INCREMENT, `id_role` integer NOT NULL, `username` text UNIQUE NOT NULL, `password` text NOT NULL ); CREATE TABLE `roles` ( - `id` integer UNIQUE PRIMARY KEY NOT NULL, + `id` integer UNIQUE PRIMARY KEY AUTO_INCREMENT, `name` text UNIQUE NOT NULL ); CREATE TABLE `status` ( - `id` integer UNIQUE PRIMARY KEY NOT NULL, + `id` integer UNIQUE PRIMARY KEY AUTO_INCREMENT, `name` text UNIQUE NOT NULL ); CREATE TABLE `requests` ( - `id` integer UNIQUE PRIMARY KEY NOT NULL, + `id` integer UNIQUE PRIMARY KEY AUTO_INCREMENT, `id_status` integer NOT NULL, `id_user` integer NOT NULL, `created_at` date DEFAULT (now()), @@ -24,18 +24,18 @@ CREATE TABLE `requests` ( ); CREATE TABLE `feedback` ( - `id` integer UNIQUE PRIMARY KEY NOT NULL, + `id` integer UNIQUE PRIMARY KEY AUTO_INCREMENT, `id_user` integer NOT NULL, `id_request` integer NOT NULL, `message` text NOT NULL ); -ALTER TABLE `roles` ADD FOREIGN KEY (`id`) REFERENCES `users` (`id_role`); +ALTER TABLE `users` ADD FOREIGN KEY (`id_role`) REFERENCES `roles` (`id`); -ALTER TABLE `users` ADD FOREIGN KEY (`id`) REFERENCES `requests` (`id_user`); +ALTER TABLE `requests` ADD FOREIGN KEY (`id_user`) REFERENCES `users` (`id`); -ALTER TABLE `status` ADD FOREIGN KEY (`id`) REFERENCES `requests` (`id_status`); +ALTER TABLE `requests` ADD FOREIGN KEY (`id_status`) REFERENCES `status` (`id`); -ALTER TABLE `users` ADD FOREIGN KEY (`id`) REFERENCES `feedback` (`id_user`); +ALTER TABLE `feedback` ADD FOREIGN KEY (`id_user`) REFERENCES `users` (`id`); -ALTER TABLE `requests` ADD FOREIGN KEY (`id`) REFERENCES `feedback` (`id_request`); +ALTER TABLE `feedback` ADD FOREIGN KEY (`id_request`) REFERENCES `requests` (`id`); diff --git a/db/init.sh b/db/init.sh new file mode 100755 index 0000000..11bc9ec --- /dev/null +++ b/db/init.sh @@ -0,0 +1,28 @@ +#!/bin/bash + +# Checks the .env file +if [ -f .env ]; then + source .env + user="$SERVICE_ARCHITECTURE_USER" + pwd="$SERVICE_ARCHITECTURE_PASSWORD" +else + echo ".env file not found. Please create one with USER and PASSWORD variables." + exit 1 +fi + + +echo "Creating database 'service-architecture'..." +if ! mariadb -u "$user" -p"$pwd" -e "CREATE DATABASE IF NOT EXISTS \`service-architecture\`;" +then + echo "Error: Failed to create database, check your .env" + exit 1 +fi + +echo "Uploading database 'service-architecture'..." +if ! mariadb -u "$user" -p"$pwd" "service-architecture" < db.sql +then + echo "Error: Failed to execute db.sql, contact the maintainers." + exit 1 +fi + +echo "Database setup successful!" diff --git a/db/remove.sh b/db/remove.sh new file mode 100755 index 0000000..a0e739f --- /dev/null +++ b/db/remove.sh @@ -0,0 +1,21 @@ +#!/bin/bash + +# Checks the .env file +if [ -f .env ]; then + source .env + user="$SERVICE_ARCHITECTURE_USER" + pwd="$SERVICE_ARCHITECTURE_PASSWORD" +else + echo ".env file not found. Please create one with USER and PASSWORD variables." + exit 1 +fi + + +echo "Removing database 'service-architecture'..." +if ! mariadb -u "$user" -p"$pwd" -e "DROP DATABASE \`service-architecture\`;" +then + echo "Error: Failed to create database, check your .env" + exit 1 +else + echo "Database removed successfully!" +fi