-- 将 spectrum_cache 旧数据迁移到 observation_cache，并将单文件转成单元素 artifacts 数组
-- 仅当 spectrum_cache 存在且有数据时执行；迁移后旧表保留（由后续清理迁移删除）
INSERT OR IGNORE INTO observation_cache (source, product, source_id, ra, dec, artifacts_json, meta_json, created_at)
SELECT
    source,
    'spectrum' AS product,
    source_id,
    ra,
    dec,
    json_object(
        'path', file_path,
        'format', file_format,
        'size', 0,
        'band', NULL,
        'original_name', NULL
    ) AS artifacts_json,
    meta_json,
    created_at
FROM spectrum_cache
WHERE EXISTS (
    SELECT 1 FROM sqlite_master WHERE type = 'table' AND name = 'spectrum_cache'
);
