Skip to content

Commit

Permalink
Merge pull request #23 from CMIPT/zzq-refactorDatabase
Browse files Browse the repository at this point in the history
Refactor the database script.
  • Loading branch information
Kaiser-Yang authored Aug 16, 2024
2 parents cf4baf8 + 781ea00 commit 0d51a7b
Show file tree
Hide file tree
Showing 14 changed files with 188 additions and 363 deletions.
18 changes: 18 additions & 0 deletions database/constraint/all_column_constraint.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
-- The constarint of the primary key and unique key is added to the table.
ALTER TABLE ONLY public.t_repository
ADD CONSTRAINT pk_repository PRIMARY KEY (pk_repository_id);

-- The constraint of t_user is added to the table.
ALTER TABLE ONLY public.t_user
ADD CONSTRAINT pk_user_table PRIMARY KEY (pk_user_id);

ALTER TABLE ONLY public.t_user
ADD CONSTRAINT users_email_key UNIQUE (email);

ALTER TABLE ONLY public.t_user
ADD CONSTRAINT users_username_key UNIQUE (username);

-- The constraint of t_user_repository is not necessary,
-- as the primary key is already unique.
ALTER TABLE ONLY public.t_user_repository
ADD CONSTRAINT pk_user_repository PRIMARY KEY (pk_user_repository_id);
55 changes: 55 additions & 0 deletions database/database_deploy.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#!/bin/bash

log_error () {
echo -e "\e[31m[ERROR]: $1\e[0m"
exit 1
}

log_info () {
echo "[INFO]: $1"
}

# Set database connection information
DB_USER="test_deploy"
DB_NAME="database_test13"
DB_HOST="localhost"
DB_PORT="5432"
DB_PASSWORD="root"
# Set the root directory where the SQL files are located
ROOT_DIR="."
SUB_DIR_SEQUENCE=(sequence table constraint function trigger)

TEMP_SQL_FILE="temp_sql_script.sql"

# Check if the database exists; if not, create it
if ! PGPASSWORD="$DB_PASSWORD" psql -U $DB_USER -h $DB_HOST -p $DB_PORT -lqt | cut -d \| -f 1 | grep -qw $DB_NAME; then
log_info "Database $DB_NAME does not exist. Creating database..."
if PGPASSWORD="$DB_PASSWORD" createdb -U $DB_USER -h $DB_HOST -p $DB_PORT $DB_NAME; then
log_info "Database $DB_NAME created."
else
log_error "Error: Failed to create database $DB_NAME."
fi
else
log_info "Database $DB_NAME exists. Proceeding with script execution."
fi

rm -f $TEMP_SQL_FILE || log_error "Error: Failed to remove temporary SQL file."
touch $TEMP_SQL_FILE || log_error "Error: Failed to create temporary SQL file."

# Ensure function creation scripts are executed before trigger scripts
# Iterate through all .sql files in each subdirectory
for dir in "${SUB_DIR_SEQUENCE[@]}"; do
for sql_file in "$ROOT_DIR/$dir"/*.sql; do
echo "\i $sql_file" >> $TEMP_SQL_FILE || log_error "Error: Failed to write to temporary SQL file."
done
done

# Execute the temporary SQL file in psql
if PGPASSWORD="$DB_PASSWORD" psql -U $DB_USER -d $DB_NAME -h $DB_HOST -p $DB_PORT -f $TEMP_SQL_FILE; then
log_info "SQL script executed successfully."
else
log_error "Error: Failed to execute SQL script."
fi

# Remove the temporary SQL file after execution
rm $TEMP_SQL_FILE || log_error "Error: Failed to remove temporary SQL file."
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<mxfile host="app.diagrams.net" agent="Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36 Edg/127.0.0.0" version="24.7.6">
<mxfile host="app.diagrams.net" agent="Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36" version="24.7.6">
<diagram id="R2lEEEUBdFMjLlhIrx00" name="Page-1">
<mxGraphModel dx="2836" dy="1089" grid="1" gridSize="10" guides="1" tooltips="1" connect="1" arrows="1" fold="1" page="1" pageScale="1" pageWidth="850" pageHeight="1100" math="0" shadow="0" extFonts="Permanent Marker^https://fonts.googleapis.com/css?family=Permanent+Marker">
<mxGraphModel dx="3248" dy="942" grid="1" gridSize="10" guides="1" tooltips="1" connect="1" arrows="1" fold="1" page="1" pageScale="1" pageWidth="850" pageHeight="1100" math="0" shadow="0" extFonts="Permanent Marker^https://fonts.googleapis.com/css?family=Permanent+Marker">
<root>
<mxCell id="0" />
<mxCell id="1" parent="0" />
Expand Down Expand Up @@ -63,7 +63,7 @@
<mxRectangle width="30" height="30" as="alternateBounds" />
</mxGeometry>
</mxCell>
<mxCell id="6WugjG2xhQJgWSEuV__S-114" value="repository_description text DEFAULT &#39;&#39;::text NOT NULL" style="shape=partialRectangle;overflow=hidden;connectable=0;fillColor=none;align=left;strokeColor=inherit;top=0;left=0;bottom=0;right=0;spacingLeft=6;" parent="6WugjG2xhQJgWSEuV__S-112" vertex="1">
<mxCell id="6WugjG2xhQJgWSEuV__S-114" value="repository_description character varying(255) NOT NULL" style="shape=partialRectangle;overflow=hidden;connectable=0;fillColor=none;align=left;strokeColor=inherit;top=0;left=0;bottom=0;right=0;spacingLeft=6;" parent="6WugjG2xhQJgWSEuV__S-112" vertex="1">
<mxGeometry x="30" width="520" height="30" as="geometry">
<mxRectangle width="520" height="30" as="alternateBounds" />
</mxGeometry>
Expand Down
Binary file added database/diagram/gcs_back_end.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 8 additions & 0 deletions database/function/update_gmt_updated_column.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
CREATE FUNCTION public.update_gmt_updated_column()
RETURNS trigger
LANGUAGE plpgsql AS $$
BEGIN
NEW.gmt_updated = CURRENT_TIMESTAMP;
RETURN NEW;
END;
$$;
Binary file removed database/gcs_back_end.png
Binary file not shown.
Loading

0 comments on commit 0d51a7b

Please sign in to comment.