#include "echoion/core/state/StateMigration.hpp" #include "echoion/core/state/StateSerializer.hpp" #include #include #include #include namespace Echoion::Core { Api::Result StateMigration::migrate(uint32_t fromVersion, const std::string& jsonData) { // Migration chain: fromVersion -> ... -> CURRENT_VERSION // For now, only V0 -> V1 is implemented // Future: V1 -> V2, V2 -> V3, etc. if (fromVersion == 0) { auto parsed = Void::Json::parse(jsonData); if (!parsed) { return std::unexpected(Api::Error{Api::ErrorCode::ParseError, "Failed to parse V0 state"}); } auto v0 = Void::Json::from_json(*parsed); if (!v0) { return std::unexpected(Api::Error{Api::ErrorCode::ParseError, "Failed to parse V0 state"}); } return fromV0(*v0); } // Add more migrations here as versions evolve // if (fromVersion == 1) { return fromV1(...); } return std::unexpected(Api::Error{ Api::ErrorCode::IncompatibleVersion, std::format("No migration path from version {}", fromVersion)}); } StateV1 StateMigration::fromV0(const StateV0& v0) { StateV1 v1; // Migrate queue for (const auto& trackId : v0.trackIds) { StateV1::QueueEntryState entry; entry.track.id = trackId; entry.track.title = "Unknown"; entry.track.artist = "Unknown"; v1.queue.push_back(std::move(entry)); } // Migrate playback state if (!v1.queue.empty()) { StateV1::PlaybackState playback; playback.currentTrack = v1.queue[0].track; playback.volume = v0.volume; v1.playback = std::move(playback); v1.hasPlayback = true; } v1.version = 1; v1.savedAt = Void::Time::utc_now_iso8601(); return v1; } bool StateMigration::canMigrate(uint32_t fromVersion) { return fromVersion >= StateSerializer::getMinimumSupportedVersion() && fromVersion < StateSerializer::getCurrentVersion(); } } // namespace Echoion::Core