Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add bash tools #4

Merged
merged 1 commit into from
Mar 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions Tools/db-check.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#!/bin/bash

if [ $# -ne 3 ]; then
echo "::error file=$0,line=$LINENO::Usage: $0 <host> <user> <database>"
exit 1
fi

MYSQL_HOST="$1"
MYSQL_USER="$2"
MYSQL_DB="$3"

DB_ENV="PRD"

if [[ "$MYSQL_HOST" == "localhost" && "$MYSQL_USER" == "test" ]]; then
DB_ENV="CI [Docker]"
fi

CONNECT=$(
mysql -h "$MYSQL_HOST" --protocol tcp "--user=$MYSQL_USER" --batch --skip-column-names -e \
"SHOW DATABASES LIKE '$DBNAME';" | grep "$DBNAME" >/dev/null
echo "$?"
)

if [ "$CONNECT" -eq 1 ]; then
MESSAGE=""
echo "::error file=$0,line=$LINENO::The database does not exist or cannot be accessed using these credentials."
exit 1
fi

EXISTS=$(mysql -h "$MYSQL_HOST" --protocol tcp "--user=$MYSQL_USER" "--database=$MYSQL_DB" -sse \
"SELECT COUNT(*) FROM information_schema.tables WHERE table_schema='$MYSQL_DB' AND table_name='schema_version';")

echo "### Database check summary (\`$DB_ENV\` environment) :eyes:" >>"$GITHUB_STEP_SUMMARY"
echo "" >>"$GITHUB_STEP_SUMMARY"

if [ "$EXISTS" -eq 1 ]; then
MESSAGE="The \`schema version\` table **does** exist."
echo "::notice file=$0,line=$LINENO::$MESSAGE"
echo ":thumbsup: $MESSAGE" >>"$GITHUB_STEP_SUMMARY"
echo "db_check_not_found=false" >>"$GITHUB_ENV"
echo "not_found=false" >>"$GITHUB_OUTPUT"
else
MESSAGE="The \`schema version\` table **does not** exist."
echo "::warning file=$0,line=$LINENO::$MESSAGE"
echo ":thumbsdown: $MESSAGE" >>"$GITHUB_STEP_SUMMARY"
echo "db_check_not_found=true" >>"$GITHUB_ENV"
echo "not_found=true" >>"$GITHUB_OUTPUT"
fi
142 changes: 142 additions & 0 deletions Tools/db-migration.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
#!/bin/bash

if [[ $# -ne 4 && $# -ne 5 ]]; then
echo "::error file=$0,line=$LINENO::Usage: $0 <path> <host> <user> <database>"
echo "::error file=$0,line=$LINENO::Or: $0 <path> <host> <user> <database> --dry-run"
exit 1
fi

if [ ! -d "$1" ]; then
echo "::warning file=$0,line=$LINENO::Directory $1 does not exist."
exit 0
fi

cd "$1" || exit

MYSQL_HOST="$2"
MYSQL_USER="$3"
MYSQL_DB="$4"
DRY_RUN="$5"

CONNECT=$(
mysql -h "$MYSQL_HOST" --protocol tcp "--user=$MYSQL_USER" --batch --skip-column-names -e \
"SHOW DATABASES LIKE '$DBNAME';" | grep "$DBNAME" >/dev/null
echo "$?"
)

if [ "$CONNECT" -eq 1 ]; then
MESSAGE="The database does not exist or is not accessible using this credentials."
echo "::error file=$0,line=$LINENO::$MESSAGE"
exit 1
fi

DB_ENV="PRD"

if [ "$DRY_RUN" == "--dry-run" ]; then
DB_ENV="PRD [dry run]"
fi
if [[ "$MYSQL_HOST" == "localhost" && "$MYSQL_USER" == "test" ]]; then
DB_ENV="CI [Docker]"
fi

echo "### Database migration summary (\`$DB_ENV\` environment) :rocket:" >>"$GITHUB_STEP_SUMMARY"
if [ "$DRY_RUN" == "--dry-run" ]; then
{
echo ""
echo ":warning: This is a dry run. No SQL will be executed."
echo ""
} >>"$GITHUB_STEP_SUMMARY"
fi
echo "" >>"$GITHUB_STEP_SUMMARY"

FILES=""
INDEX=0

SCHEMA_TABLE=$(mysql -h "$MYSQL_HOST" --protocol tcp "--user=$MYSQL_USER" "--database=$MYSQL_DB" -sse \
"SELECT COUNT(*) FROM information_schema.tables WHERE table_schema='$MYSQL_DB' AND table_name='schema_version';")

if [ "$SCHEMA_TABLE" -eq 1 ]; then
if [ "$DRY_RUN" == "--dry-run" ]; then
echo "The schema version table does exist."
else
echo "::notice file=$0,line=$LINENO::The schema version table does exist."
fi
echo "- :thumbsup: The \`schema version\` table already exists." >>"$GITHUB_STEP_SUMMARY"
else
if [ "$DRY_RUN" == "--dry-run" ]; then
echo "The schema version table does not exist."
echo "Dry run, not executing SQL"
FILES="$FILES- schema-version.sql\n"
echo "file[$INDEX]=schema-version.sql" >>"$GITHUB_OUTPUT"
INDEX=$((INDEX + 1))
else
echo "::warning file=$0,line=$LINENO::The schema version table does not exist."
mysql -h "$MYSQL_HOST" --protocol tcp "--user=$MYSQL_USER" "--database=$MYSQL_DB" <../Tools/schema-version.sql
fi
echo "- :white_check_mark: The \`schema version\` table was created." >>"$GITHUB_STEP_SUMMARY"
fi

WORKING_SQL_FILE="step.temp;"
echo "START TRANSACTION;" >$WORKING_SQL_FILE

for FILE in *.sql; do
echo "Checking file $FILE"
sha256=$(sha256sum "$FILE" | cut -d " " -f 1)
EXISTS=0

if [[ "$DRY_RUN" != "--dry-run" || $SCHEMA_TABLE -eq 1 ]]; then
EXISTS=$(
mysql -h "$MYSQL_HOST" --protocol tcp "--user=$MYSQL_USER" "--database=$MYSQL_DB" -sse \
"SELECT COUNT(*) FROM schema_version WHERE \
Filename='$FILE' AND Checksum='$sha256';"
)
fi

if [ "$EXISTS" -eq 1 ]; then
if [ "$DRY_RUN" == "--dry-run" ]; then
echo "File $FILE already processed"
else
echo "::notice file=$0,line=$LINENO::File $FILE already processed"
fi
echo "- :thumbsup: The file \`$FILE\` was already processed." >>"$GITHUB_STEP_SUMMARY"
else
if [ "$DRY_RUN" == "--dry-run" ]; then
echo "Running file $FILE"
else
echo "::warning file=$0,line=$LINENO::Running file $FILE"
fi
cat "$FILE" >>$WORKING_SQL_FILE
echo "INSERT INTO schema_version (Filename, Checksum) VALUES ('$FILE', '$sha256');" >>$WORKING_SQL_FILE
echo "- :white_check_mark: The file \`$FILE\` was processed." >>"$GITHUB_STEP_SUMMARY"
FILES="$FILES- $FILE\n"
echo "file[$INDEX]=$FILE" >>"$GITHUB_OUTPUT"
INDEX=$((INDEX + 1))
fi
done

echo "COMMIT;" >>$WORKING_SQL_FILE

if [ "$DRY_RUN" == "--dry-run" ]; then
echo "::warning file=$0,line=$LINENO::Dry run, not executing SQL"
else
err=$(mysql -h "$MYSQL_HOST" --protocol tcp "--user=$MYSQL_USER" "--database=$MYSQL_DB" <$WORKING_SQL_FILE 2>&1)
if [ "$err" != "" ]; then
echo "::error file=$0,line=$LINENO::$err"
echo "db_migration_error=true" >>"$GITHUB_ENV"
echo "error=$err" >>"$GITHUB_OUTPUT"
exit 1
fi
fi

rm $WORKING_SQL_FILE

if [ "$FILES" != "" ]; then
echo "The following database files were processed:"
echo -e "$FILES"

{
echo "files<<EOF"
echo -e "$FILES"
echo "EOF"
} >>"$GITHUB_OUTPUT"
fi
14 changes: 14 additions & 0 deletions Tools/schema-version.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
DROP TABLE IF EXISTS `schema_version`;

Check failure on line 1 in Tools/schema-version.sql

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

Tools/schema-version.sql#L1

syntax error at or near "`"

CREATE TABLE
`schema_version` (

Check failure on line 4 in Tools/schema-version.sql

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

Tools/schema-version.sql#L4

syntax error at or near "`"
`Sequence` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`Filename` VARCHAR(255) NOT NULL,
`Checksum` CHAR(64) NOT NULL,
`Date` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`Sequence`),
UNIQUE (`Filename`),
UNIQUE (`Checksum`)
) ENGINE = InnoDB;

ALTER TABLE `schema_version` CONVERT TO CHARACTER SET utf8;

Check failure on line 14 in Tools/schema-version.sql

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

Tools/schema-version.sql#L14

syntax error at or near "`"
Loading