-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #23 from CMIPT/zzq-refactorDatabase
Refactor the database script.
- Loading branch information
Showing
14 changed files
with
188 additions
and
363 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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." |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 not shown.
Oops, something went wrong.