db script fixes, maybe it works this time?
This commit is contained in:
@@ -197,7 +197,7 @@ replace_string_recursive "/opt/stacks" "?onlyofficeJWT?" $onlyofficeJWT
|
|||||||
|
|
||||||
|
|
||||||
# install mailcow to /opt/stacks/mailcow so it shows up in dockge
|
# install mailcow to /opt/stacks/mailcow so it shows up in dockge
|
||||||
apt install -y git openssl curl gawk coreutils grep jq
|
apt install -y git openssl curl gawk coreutils grep jq sqlite3
|
||||||
umask 0022
|
umask 0022
|
||||||
#cd /opt/stacks
|
#cd /opt/stacks
|
||||||
#git clone https://github.com/mailcow/mailcow-dockerized mailcow
|
#git clone https://github.com/mailcow/mailcow-dockerized mailcow
|
||||||
|
|||||||
46
prepdb.sh
46
prepdb.sh
@@ -10,12 +10,13 @@ replace_in_sqlite_db() {
|
|||||||
return 1
|
return 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
echo "Starting replacement of '$OLD_STRING' with '$NEW_STRING' in '$DB_PATH'..."
|
# Check if jq is installed
|
||||||
|
if ! command -v jq &> /dev/null; then
|
||||||
|
echo "Error: 'jq' is not installed. Please install it first (e.g., 'sudo apt install jq')."
|
||||||
|
return 1
|
||||||
|
fi
|
||||||
|
|
||||||
# Enable the json1 extension
|
echo "Starting replacement of '$OLD_STRING' with '$NEW_STRING' in all string-based columns in '$DB_PATH'..."
|
||||||
sqlite3 "$DB_PATH" "SELECT load_extension('json1');" || {
|
|
||||||
echo "Warning: json1 extension not available. JSON fields will not be processed."
|
|
||||||
}
|
|
||||||
|
|
||||||
# Escape single quotes in the strings for SQL
|
# Escape single quotes in the strings for SQL
|
||||||
local OLD_STRING_ESC=$(echo "$OLD_STRING" | sed "s/'/''/g")
|
local OLD_STRING_ESC=$(echo "$OLD_STRING" | sed "s/'/''/g")
|
||||||
@@ -38,7 +39,7 @@ replace_in_sqlite_db() {
|
|||||||
|
|
||||||
# Check if the column is a string-based type (TEXT, VARCHAR, CHAR, CLOB, etc.)
|
# 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
|
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)"
|
echo " Processing column: $COLUMN (type: $COLUMN_TYPE)"
|
||||||
|
|
||||||
# Check if the column contains JSON data
|
# Check if the column contains JSON data
|
||||||
local IS_JSON=$(sqlite3 "$DB_PATH" "
|
local IS_JSON=$(sqlite3 "$DB_PATH" "
|
||||||
@@ -47,14 +48,39 @@ replace_in_sqlite_db() {
|
|||||||
")
|
")
|
||||||
|
|
||||||
if [ "$IS_JSON" -gt 0 ]; then
|
if [ "$IS_JSON" -gt 0 ]; then
|
||||||
echo " Detected JSON data in column $COLUMN. Using json_replace()."
|
echo " Detected JSON data in column $COLUMN. Processing with jq..."
|
||||||
|
|
||||||
# Use json_replace() to handle JSON strings
|
# Create a temporary table to store updated JSON
|
||||||
|
sqlite3 "$DB_PATH" "
|
||||||
|
CREATE TABLE IF NOT EXISTS temp_$TABLE (rowid INTEGER PRIMARY KEY, new_$COLUMN TEXT);
|
||||||
|
"
|
||||||
|
|
||||||
|
# Export JSON data, modify with jq, and store in temp table
|
||||||
|
sqlite3 "$DB_PATH" "SELECT rowid, $COLUMN FROM $TABLE WHERE json_valid($COLUMN) = 1;" | while read -r ROWID JSON_DATA; do
|
||||||
|
# Skip the header line (rowid|column)
|
||||||
|
if [ "$ROWID" != "rowid" ]; then
|
||||||
|
# Replace the string in the JSON using jq
|
||||||
|
local MODIFIED_JSON
|
||||||
|
MODIFIED_JSON=$(echo "$JSON_DATA" | jq --arg old_str "$OLD_STRING" --arg new_str "$NEW_STRING" '
|
||||||
|
walk(if type == "string" then gsub($old_str; $new_str) else . end)
|
||||||
|
')
|
||||||
|
|
||||||
|
# Insert the modified JSON into the temp table
|
||||||
|
sqlite3 "$DB_PATH" "
|
||||||
|
INSERT INTO temp_$TABLE (rowid, new_$COLUMN) VALUES ($ROWID, '$MODIFIED_JSON');
|
||||||
|
"
|
||||||
|
fi
|
||||||
|
done
|
||||||
|
|
||||||
|
# Update the original table with the modified JSON
|
||||||
sqlite3 "$DB_PATH" "
|
sqlite3 "$DB_PATH" "
|
||||||
UPDATE $TABLE
|
UPDATE $TABLE
|
||||||
SET $COLUMN = json_replace($COLUMN, '$OLD_STRING_ESC', '$NEW_STRING_ESC')
|
SET $COLUMN = (SELECT new_$COLUMN FROM temp_$TABLE WHERE temp_$TABLE.rowid = $TABLE.rowid)
|
||||||
WHERE json_valid($COLUMN) = 1;
|
WHERE rowid IN (SELECT rowid FROM temp_$TABLE);
|
||||||
"
|
"
|
||||||
|
|
||||||
|
# Drop the temporary table
|
||||||
|
sqlite3 "$DB_PATH" "DROP TABLE temp_$TABLE;"
|
||||||
else
|
else
|
||||||
echo " Column $COLUMN is not JSON. Using replace()."
|
echo " Column $COLUMN is not JSON. Using replace()."
|
||||||
|
|
||||||
|
|||||||
BIN
stacks/npm/data/database-post.sqlite
Normal file
BIN
stacks/npm/data/database-post.sqlite
Normal file
Binary file not shown.
Reference in New Issue
Block a user