mirror of
https://github.com/alexbelgium/hassio-addons.git
synced 2026-04-11 18:09:55 +02:00
Update 01-structure.sh
This commit is contained in:
@@ -64,19 +64,31 @@ if [[ "${CURRENT_BIRDSONGS_FOLDER%/}" != "${$BIRDSONGS_FOLDER%/}" ]]; then
|
|||||||
fi
|
fi
|
||||||
# Adapt the database
|
# Adapt the database
|
||||||
if [ -f /config/birdnet.db ]; then
|
if [ -f /config/birdnet.db ]; then
|
||||||
# SQL query to update the paths
|
# Prepare
|
||||||
bashio::log.warning "Modifying database paths from $CURRENT_BIRDSONGS_FOLDER to $BIRDSONGS_FOLDER. A backup will be created before"
|
backup="$(date +%Y%m%d_%H%M%S)"
|
||||||
cp /config/birdnet.db /config/birdnet.db_$(date +%Y%m%d_%H%M%S)
|
bashio::log.warning "Modifying database paths from $CURRENT_BIRDSONGS_FOLDER to $BIRDSONGS_FOLDER. A backup named birdnet.db_$backup will be created before"
|
||||||
SQL_QUERY="UPDATE notes SET clip_name = REPLACE(clip_name, '^${CURRENT_BIRDSONGS_FOLDER}/', '${BIRDSONGS_FOLDER%/}/');"
|
|
||||||
|
# Create backup
|
||||||
|
cp /config/birdnet.db "birdnet.db_$backup"
|
||||||
|
if [ $? -ne 0 ]; then
|
||||||
|
bashio::log.error "Failed to create a backup of the database. Aborting path modification."
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
# Execute the query using sqlite3
|
# Execute the query using sqlite3
|
||||||
|
SQL_QUERY="UPDATE notes SET clip_name = '${BIRDSONGS_FOLDER%/}/' || substr(clip_name, length('${CURRENT_BIRDSONGS_FOLDER%/}/') + 1) WHERE clip_name LIKE '${CURRENT_BIRDSONGS_FOLDER%/}/%';"
|
||||||
sqlite3 /config/birdnet.db "$SQL_QUERY"
|
sqlite3 /config/birdnet.db "$SQL_QUERY"
|
||||||
|
|
||||||
# Check if the operation was successful
|
|
||||||
if [ $? -eq 0 ]; then
|
if [ $? -eq 0 ]; then
|
||||||
echo "Paths have been successfully updated."
|
echo "Paths have been successfully updated."
|
||||||
else
|
else
|
||||||
bashio::log.warning "An error occurred while updating the paths."
|
bashio::log.warning "An error occurred while updating the paths. The database backup will be restored."
|
||||||
|
BACKUP_FILE="/config/birdnet.db_$(date +%Y%m%d_%H%M%S)" # Make sure this matches the earlier backup filename
|
||||||
|
if [ -f "$BACKUP_FILE" ]; then
|
||||||
|
mv "$BACKUP_FILE" /config/birdnet.db
|
||||||
|
bashio::log.info "The database backup has been restored."
|
||||||
|
else
|
||||||
|
bashio::log.error "Backup file not found! Manual intervention required."
|
||||||
|
fi
|
||||||
fi
|
fi
|
||||||
fi
|
fi
|
||||||
fi
|
fi
|
||||||
|
|||||||
Reference in New Issue
Block a user