|
| 1 | +use std::path::PathBuf; |
| 2 | + |
| 3 | +use eyre::Result; |
| 4 | +use rustix::fs::{ |
| 5 | + FlockOperation, |
| 6 | + flock, |
| 7 | +}; |
| 8 | +use serde_json::Value; |
| 9 | +use tokio::fs; |
| 10 | +use tracing::debug; |
| 11 | + |
| 12 | +const KIRO_MIGRATION_KEY: &str = "migration.kiro.completed"; |
| 13 | + |
| 14 | +pub async fn migrate_if_needed() -> Result<bool> { |
| 15 | + let status = detect_migration().await?; |
| 16 | + |
| 17 | + match status { |
| 18 | + MigrationStatus::Completed => { |
| 19 | + debug!("Migration already completed"); |
| 20 | + return Ok(false); |
| 21 | + }, |
| 22 | + MigrationStatus::NotNeeded => { |
| 23 | + debug!("No migration needed"); |
| 24 | + return Ok(false); |
| 25 | + }, |
| 26 | + MigrationStatus::Needed => { |
| 27 | + debug!("Migrating database and settings"); |
| 28 | + }, |
| 29 | + } |
| 30 | + |
| 31 | + let _lock = match acquire_migration_lock()? { |
| 32 | + Some(lock) => lock, |
| 33 | + None => { |
| 34 | + debug!("Migration already in progress"); |
| 35 | + return Ok(false); |
| 36 | + }, |
| 37 | + }; |
| 38 | + |
| 39 | + let old_dir = fig_util::directories::old_fig_data_dir()?; |
| 40 | + let new_dir = fig_util::directories::fig_data_dir()?; |
| 41 | + |
| 42 | + debug!("Old directory: {}", old_dir.display()); |
| 43 | + debug!("New directory: {}", new_dir.display()); |
| 44 | + |
| 45 | + // Copy essential files from old directory to new directory |
| 46 | + if !new_dir.exists() { |
| 47 | + fs::create_dir_all(&new_dir).await?; |
| 48 | + } |
| 49 | + debug!("Copying essential files from old to new directory"); |
| 50 | + copy_essential_files(&old_dir, &new_dir).await?; |
| 51 | + |
| 52 | + // Migrate settings to new location |
| 53 | + debug!("Migrating settings"); |
| 54 | + migrate_settings().await?; |
| 55 | + |
| 56 | + // Mark migration as completed in database |
| 57 | + debug!("Marking migration as completed"); |
| 58 | + mark_migration_completed()?; |
| 59 | + |
| 60 | + debug!("Migration completed successfully"); |
| 61 | + Ok(true) |
| 62 | +} |
| 63 | + |
| 64 | +#[derive(Debug)] |
| 65 | +enum MigrationStatus { |
| 66 | + NotNeeded, |
| 67 | + Needed, |
| 68 | + Completed, |
| 69 | +} |
| 70 | + |
| 71 | +async fn detect_migration() -> Result<MigrationStatus> { |
| 72 | + let old_dir = fig_util::directories::old_fig_data_dir()?; |
| 73 | + let new_dir = fig_util::directories::fig_data_dir()?; |
| 74 | + |
| 75 | + // If new directory doesn't exist yet, check if old directory exists |
| 76 | + if !new_dir.exists() { |
| 77 | + if old_dir.exists() && old_dir.is_dir() { |
| 78 | + return Ok(MigrationStatus::Needed); |
| 79 | + } else { |
| 80 | + return Ok(MigrationStatus::NotNeeded); |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + // New directory exists, check database flag (safe now since new_dir exists) |
| 85 | + let migration_completed = is_migration_completed()?; |
| 86 | + |
| 87 | + if migration_completed { |
| 88 | + Ok(MigrationStatus::Completed) |
| 89 | + } else if old_dir.exists() && old_dir.is_dir() { |
| 90 | + Ok(MigrationStatus::Needed) |
| 91 | + } else { |
| 92 | + Ok(MigrationStatus::NotNeeded) |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +async fn migrate_settings() -> Result<()> { |
| 97 | + let old_settings = fig_util::directories::fig_data_dir()?.join("settings.json"); |
| 98 | + let new_settings = fig_util::directories::settings_path()?; |
| 99 | + |
| 100 | + if !old_settings.exists() || new_settings.exists() { |
| 101 | + return Ok(()); |
| 102 | + } |
| 103 | + |
| 104 | + let content = fs::read_to_string(&old_settings).await?; |
| 105 | + let settings: serde_json::Map<String, Value> = serde_json::from_str(&content)?; |
| 106 | + |
| 107 | + if let Some(parent) = new_settings.parent() { |
| 108 | + fs::create_dir_all(parent).await?; |
| 109 | + } |
| 110 | + |
| 111 | + let json = serde_json::to_string_pretty(&settings)?; |
| 112 | + fs::write(new_settings, json).await?; |
| 113 | + |
| 114 | + Ok(()) |
| 115 | +} |
| 116 | + |
| 117 | +async fn copy_essential_files(src: &std::path::Path, dst: &std::path::Path) -> Result<()> { |
| 118 | + // Only copy SQLite database and settings files |
| 119 | + let essential_files = ["data.sqlite3", "settings.json"]; |
| 120 | + |
| 121 | + for file_name in essential_files { |
| 122 | + let src_path = src.join(file_name); |
| 123 | + let dst_path = dst.join(file_name); |
| 124 | + |
| 125 | + if src_path.exists() { |
| 126 | + debug!("Copying {} to {}", src_path.display(), dst_path.display()); |
| 127 | + fs::copy(&src_path, &dst_path).await?; |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + Ok(()) |
| 132 | +} |
| 133 | + |
| 134 | +fn mark_migration_completed() -> Result<()> { |
| 135 | + let db = fig_settings::sqlite::database()?; |
| 136 | + db.set_state_value(KIRO_MIGRATION_KEY, true)?; |
| 137 | + Ok(()) |
| 138 | +} |
| 139 | + |
| 140 | +struct MigrationLock { |
| 141 | + _file: std::fs::File, |
| 142 | + path: PathBuf, |
| 143 | +} |
| 144 | + |
| 145 | +impl Drop for MigrationLock { |
| 146 | + fn drop(&mut self) { |
| 147 | + let _ = std::fs::remove_file(&self.path); |
| 148 | + } |
| 149 | +} |
| 150 | + |
| 151 | +fn acquire_migration_lock() -> Result<Option<MigrationLock>> { |
| 152 | + let lock_path = migration_lock_path()?; |
| 153 | + |
| 154 | + if let Some(parent) = lock_path.parent() { |
| 155 | + std::fs::create_dir_all(parent)?; |
| 156 | + } |
| 157 | + |
| 158 | + let file = std::fs::OpenOptions::new() |
| 159 | + .create(true) |
| 160 | + .write(true) |
| 161 | + .truncate(false) |
| 162 | + .open(&lock_path)?; |
| 163 | + |
| 164 | + match flock(&file, FlockOperation::NonBlockingLockExclusive) { |
| 165 | + Ok(()) => Ok(Some(MigrationLock { |
| 166 | + _file: file, |
| 167 | + path: lock_path, |
| 168 | + })), |
| 169 | + Err(_) => { |
| 170 | + if let Ok(metadata) = std::fs::metadata(&lock_path) { |
| 171 | + if let Ok(modified) = metadata.modified() { |
| 172 | + if let Ok(elapsed) = modified.elapsed() { |
| 173 | + if elapsed.as_secs() > 10 { |
| 174 | + std::fs::remove_file(&lock_path)?; |
| 175 | + let file = std::fs::OpenOptions::new() |
| 176 | + .create(true) |
| 177 | + .write(true) |
| 178 | + .truncate(false) |
| 179 | + .open(&lock_path)?; |
| 180 | + return match flock(&file, FlockOperation::NonBlockingLockExclusive) { |
| 181 | + Ok(()) => Ok(Some(MigrationLock { |
| 182 | + _file: file, |
| 183 | + path: lock_path, |
| 184 | + })), |
| 185 | + Err(_) => Ok(None), |
| 186 | + }; |
| 187 | + } |
| 188 | + } |
| 189 | + } |
| 190 | + } |
| 191 | + Ok(None) |
| 192 | + }, |
| 193 | + } |
| 194 | +} |
| 195 | + |
| 196 | +fn migration_lock_path() -> Result<PathBuf> { |
| 197 | + Ok(fig_util::directories::fig_data_dir()?.join("migration.lock")) |
| 198 | +} |
| 199 | + |
| 200 | +fn is_migration_completed() -> Result<bool> { |
| 201 | + Ok(fig_settings::state::get_bool_or(KIRO_MIGRATION_KEY, false)) |
| 202 | +} |
0 commit comments