mirror of
https://github.com/Lemonochrme/service-architecture.git
synced 2025-06-08 05:30:50 +02:00
Updated Database and created init scripts.
This commit is contained in:
parent
b772b23bf9
commit
4449460f93
5 changed files with 84 additions and 11 deletions
3
.gitignore
vendored
3
.gitignore
vendored
|
@ -1 +1,2 @@
|
|||
target/
|
||||
target/
|
||||
**/.env
|
23
db/README.md
Normal file
23
db/README.md
Normal file
|
@ -0,0 +1,23 @@
|
|||
# Database Initialization
|
||||
|
||||
First create a user in the mariadb:
|
||||
|
||||
```bash
|
||||
sudo mariab -u root -p
|
||||
```
|
||||
```sql
|
||||
CREATE USER '<user>'@'localhost' IDENTIFIED BY '<password>';
|
||||
GRANT ALL PRIVILEGES ON *.* TO '<user>'@'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.
|
20
db/db.sql
20
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`);
|
||||
|
|
28
db/init.sh
Executable file
28
db/init.sh
Executable file
|
@ -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!"
|
21
db/remove.sh
Executable file
21
db/remove.sh
Executable file
|
@ -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
|
Loading…
Add table
Reference in a new issue