fix(CI): simplify ci-pending-sql.sh and make it safer (#20209)

* apps: ci: ci-pending-sql.sh: simplify

* ensure proper file header is used
This commit is contained in:
Mike Delago
2024-10-14 17:42:04 -07:00
committed by GitHub
parent f87cd61a53
commit 3ef1a4a22a

View File

@@ -1,77 +1,57 @@
#!/usr/bin/env bash #!/usr/bin/env bash
set -euo pipefail
CURRENT_PATH="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" CURRENT_PATH="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
source "$CURRENT_PATH/../bash_shared/includes.sh" source "$CURRENT_PATH/../bash_shared/includes.sh"
UPDATES_PATH="$AC_PATH_ROOT/data/sql/updates/" UPDATES_PATH="$AC_PATH_ROOT/data/sql/updates"
COMMIT_HASH=
function import() { # get_next_index "data/sql/updates/db_world/2024_10_14_22.sql"
db=$1 # => 23
folder="db_"$db # get_next_index ""
pendingPath="$AC_PATH_ROOT/data/sql/updates/pending_$folder" # => 00
updPath="$UPDATES_PATH/$folder" function get_next_index() {
archivedPath="$AC_PATH_ROOT/data/sql/archive/$folder/6.x" if [[ -n "$1" ]]; then
PREV_COUNT="$(basename "$1" | cut -f4 -d_ | cut -f1 -d\.)"
latestUpd=$(ls -1 $updPath/ | tail -n 1) printf '%02d' "$((PREV_COUNT + 1))"
else
if [ -z $latestUpd ]; then echo "00"
latestUpd=$(ls -1 $archivedPath/ | tail -n 1)
echo "> Last update file for db $db is missing! Using archived file" $latestUpd
fi fi
}
dateToday=$(date +%Y_%m_%d) # lists all SQL files in the appropriate data/sql/updates/db_$1, and then moves them to a standard format, ordered by date and how many imports have happened that day. The name should be in this format:
counter=0 #
# /path/to/data/sql/updates/db_NAME/YYYY_MM_DD_INDEX.sql
#
# Where INDEX is a number with a minimum with a minimum width (0-padded) of 2
#
# for example, "data/sql/updates/db_world/2024_10_01_03.sql" translates to "the third update in the world database from October 01, 2024"
dateLast=$latestUpd TODAY="$(date +%Y_%m_%d)"
tmp=${dateLast#*_*_*_} function import() {
oldCnt=${tmp%.sql} PENDING_PATH="$AC_PATH_ROOT/data/sql/updates/pending_db_$1"
oldDate=${dateLast%_$tmp} UPDATES_DIR="$UPDATES_PATH/db_$1"
if [ "$oldDate" = "$dateToday" ]; then # Get latest SQL file applied to this database
((counter=10#$oldCnt+1)) # 10 # is needed to explictly add to a base 10 number LATEST_UPDATE="$(find "$UPDATES_DIR" -iname "*.sql" | sort -h | tail -n 1)"
fi;
for entry in "$pendingPath"/*.sql for entry in "$PENDING_PATH"/*.sql
do do
if [[ -e $entry ]]; then if [[ -f "$entry" ]]; then
oldVer=$oldDate"_"$oldCnt INDEX="$(get_next_index "$LATEST_UPDATE")"
OUTPUT_FILE="${UPDATES_DIR}/${TODAY}_${INDEX}.sql"
cnt=$(printf -v counter "%02d" $counter ; echo $counter) # ensure a note is added as a header comment
echo "-- DB update $(basename "$LATEST_UPDATE" .sql) -> $(basename "$OUTPUT_FILE" .sql)" > "$OUTPUT_FILE"
newVer=$dateToday"_"$cnt # fill in the SQL contents under that
cat "$entry" >> "$OUTPUT_FILE"
newFile="$updPath/"$dateToday"_"$cnt".sql" # remove the unneeded file
rm -f "$entry"
oldFile=$(basename "$entry") # set the newest file to the file we just moved
prefix=${oldFile%_*.sql} LATEST_UPDATE="$OUTPUT_FILE"
suffix=${oldFile#rev_}
rev=${suffix%.sql}
isRev=0
if [[ $prefix = "rev" && $rev =~ ^-?[0-9]+$ ]]; then
isRev=1
fi
echo "-- DB update $oldVer -> $newVer" > "$newFile";
cat $entry >> "$newFile";
currentHash="$(git log --diff-filter=A "$entry" | grep "^commit " | sed -e 's/commit //')"
if [[ "$COMMIT_HASH" != *"$currentHash"* ]]
then
COMMIT_HASH="$COMMIT_HASH $currentHash"
fi
rm $entry;
oldDate=$dateToday
oldCnt=$cnt
((counter+=1))
fi fi
done done