#!/usr/bin/env bash

# Copyright © 2012-2026 ScriptFanix
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# A copy of the GNU General Public License v3 is includded in the LICENSE file
# at the root of the project.

upgradedatabase_4_5() {
	echo "Upgrading database to version 5... (backup is $database.bak_v4)"
	cp "$database" "$database.bak_v4"
	# Drop and recreate the trigger so it now also watches releasecountry
	# (added in v4 but not yet included in the trigger's watched columns)
	echo 'DROP TRIGGER force_destination_update_on_tag_update;' >&3
	echo '
		CREATE TRIGGER IF NOT EXISTS force_destination_update_on_tag_update
		AFTER UPDATE OF
			genre,
			albumartist,
			year,
			album,
			disc,
			artist,
			track,
			title,
			composer,
			performer,
			releasecountry,
			rate,
			channels,
			bitrate,
			depth
		ON tags
		BEGIN
			-- Reset destination timestamp so the file gets
			-- re-encoded on next run
			UPDATE destination_files SET last_change=0
			WHERE source_file_id=old.source_file;
		END;
	' >&3

	Update atom version 5 <<<"1 = 1"
}
