diff --git a/deploy.sh b/deploy.sh index 11e8f47..998059b 100644 --- a/deploy.sh +++ b/deploy.sh @@ -215,7 +215,9 @@ cd /opt/dockge docker compose up -d # call replacements for nginx db - +replace_in_sqlite_db "/opt/stacks/npm/data/database.sqlite" "?domain?" $domain +replace_in_sqlite_db "/opt/stacks/npm/data/database.sqlite" "?localip?" $localip +replace_in_sqlite_db "/opt/stacks/npm/data/database.sqlite" "?adminpass?" $adminpass echo "" diff --git a/prepdb.sh b/prepdb.sh index d649799..507447c 100644 --- a/prepdb.sh +++ b/prepdb.sh @@ -12,6 +12,10 @@ replace_in_sqlite_db() { echo "Starting replacement of '$OLD_STRING' with '$NEW_STRING' in '$DB_PATH'..." + # Escape single quotes in the strings for SQL + local OLD_STRING_ESC=$(echo "$OLD_STRING" | sed "s/'/''/g") + local NEW_STRING_ESC=$(echo "$NEW_STRING" | sed "s/'/''/g") + # Get a list of all tables in the database local TABLES TABLES=$(sqlite3 "$DB_PATH" ".tables") @@ -21,30 +25,27 @@ replace_in_sqlite_db() { echo "Processing table: $TABLE" # Get a list of all columns in the table - local COLUMNS - COLUMNS=$(sqlite3 "$DB_PATH" "PRAGMA table_info($TABLE);" | awk -F'|' '{print $2}' | grep -v '^$') + sqlite3 "$DB_PATH" "PRAGMA table_info($TABLE);" | awk -F'|' '{print $2","$3}' | while IFS=, read -r COLUMN COLUMN_TYPE; do + # Skip empty lines + if [ -z "$COLUMN" ]; then + continue + fi - # Loop through each column - for COLUMN in $COLUMNS; do - # Check if the column is a text type (simplistic check) - local COLUMN_TYPE - COLUMN_TYPE=$(sqlite3 "$DB_PATH" "PRAGMA table_info($TABLE);" | awk -F'|' -v col="$COLUMN" '$2 == col {print $3}') - - # Only proceed if the column type contains 'TEXT' (case-insensitive) - if [[ "$COLUMN_TYPE" =~ [Tt][Ee][Xx][Tt] ]]; then + # Check if the column is a string-based type (TEXT, VARCHAR, CHAR, CLOB, etc.) + if [[ "$COLUMN_TYPE" =~ [Tt][Ee][Xx][Tt]|[Vv][Aa][Rr][Cc][Hh][Aa][Rr]|[Cc][Hh][Aa][Rr]|[Cc][Ll][Oo][Bb]|[Ss][Tt][Rr][Ii][Nn][Gg] ]]; then echo " Updating column: $COLUMN (type: $COLUMN_TYPE)" - # Perform the replacement + # Perform the replacement (handles infixes) sqlite3 "$DB_PATH" " UPDATE $TABLE - SET $COLUMN = replace($COLUMN, '$OLD_STRING', '$NEW_STRING') - WHERE $COLUMN LIKE '%$OLD_STRING%'; + SET $COLUMN = replace($COLUMN, '$OLD_STRING_ESC', '$NEW_STRING_ESC') + WHERE $COLUMN LIKE '%$OLD_STRING_ESC%'; " fi done done - echo "Replacement completed for all text-type columns in '$DB_PATH'." + echo "Replacement completed for all string-based columns in '$DB_PATH'." } #set script directory