Skip to content

Commit

Permalink
Improve migration script
Browse files Browse the repository at this point in the history
* Do not print/do anything if there’s no .metadata.json file
* Do the migration even if there’s already a .old file, by creating a unique backup file name
* Tell the user where the backup is stored
  • Loading branch information
matteodelabre committed May 9, 2021
1 parent c5e17c7 commit 5efcaa1
Showing 1 changed file with 21 additions and 30 deletions.
51 changes: 21 additions & 30 deletions package/plato/package
Original file line number Diff line number Diff line change
Expand Up @@ -62,55 +62,46 @@ package() {
configure() {
local mediadir=/home/root/plato-media

if [ ! -d $mediadir ]; then
if [[ ! -d $mediadir ]]; then
# This directory should not be tracked by opkg
# so that its contents are kept after removing
mkdir $mediadir
mkdir "$mediadir"
echo ""
echo "Place your media files for plato in '$mediadir'"
echo ""
else
# Fix breaking change introduced in version 0.9.14
# https://github.com/baskerville/plato/releases/tag/0.9.14
# Convert old library metadata format for version 0.9.14
# (see <https://github.com/baskerville/plato/releases/tag/0.9.14>)
local metadata_file="$mediadir"/.metadata.json
local key_len
[[ -f $metadata_file ]] && key_len="$(jq --raw-output 'keys[]' "$metadata_file" | wc -L)" || key_len=-1

local metadata_file=$mediadir/.metadata.json
local metadata_file_old=$mediadir/.metadata-old.json

local max_key_len
[ -e $metadata_file ] && max_key_len=$(jq --raw-output 'keys[]' < $metadata_file | wc -L) || max_key_len=-1
if [ "$max_key_len" -gt 16 ] && [ ! -e $metadata_file_old ]; then
if [[ "$key_len" -gt 16 ]]; then
local metadata_file_bak
metadata_file_bak="$(mktemp "$metadata_file".bak.XXXXXXXX)"

echo ""
echo "In Plato 0.9.14 the metadata format for libraries changed."
echo "Found your outdated metadata file. Will update it automatically..."
echo "Found existing metadata file in '$metadata_file'. Will update it automatically..."

# Routine is based on baskerville's migrate-hex_keys.py (from the release page)
# Rewritten in bash to only require jq rather than python.
mv $metadata_file $metadata_file_old
mv "$metadata_file" "$metadata_file_bak"
local change_cmds=""
local del_list=""
for key in $(jq --raw-output 'keys[]' < $metadata_file_old); do
hex_key=$(printf %016X "$key")
#echo "Key: $key --> $hex_key"

[ -z "$del_list" ] || del_list="${del_list}, "
[ -z "$change_cmds" ] || change_cmds="${change_cmds} | "
del_list="${del_list}.[\"${key}\"]"
change_cmds="${change_cmds}.[\"$hex_key\"] = .[\"$key\"]"
done
while read -r key; do
hex_key="$(printf %016X "$key")"
[[ -z "$del_list" ]] || del_list="$del_list, "
del_list="$del_list.[\"$key\"]"
[[ -z "$change_cmds" ]] || change_cmds="$change_cmds | "
change_cmds="$change_cmds.[\"$hex_key\"] = .[\"$key\"]"
done <<< "$(jq --raw-output 'keys[]' "$metadata_file_bak")"

# Create updated json file
jq "$change_cmds | del($del_list)" < $metadata_file_old > $metadata_file
echo "Update complete. The old file was renamed to '.metadata-old.json'."
echo ""
jq "$change_cmds | del($del_list)" "$metadata_file_bak" > "$metadata_file"

elif [ "$max_key_len" -gt 16 ] && [ -e $metadata_file_old ] || [ ! -e $metadata_file ]; then
echo ""
echo "In Plato 0.9.14 the metadata format for libraries changed."
echo "MANUAL INTERVENTION REQUIRED\!\!\!"
echo "Please follow the following guide to update your library metadata manually:"
echo "https://github.com/baskerville/plato/releases/tag/0.9.14"
echo "Please LAUNCH plato ONLY AFTER this was done\!"
echo "Update complete. Backup of previous version created in '$metadata_file_bak'."
echo ""
fi
fi
Expand Down

0 comments on commit 5efcaa1

Please sign in to comment.