|
| 1 | +use std::path::PathBuf; |
| 2 | + |
| 3 | +use eyre::Result; |
| 4 | +use rustix::fs::{ |
| 5 | + FlockOperation, |
| 6 | + flock, |
| 7 | +}; |
| 8 | +use tokio::fs; |
| 9 | +use tracing::debug; |
| 10 | + |
| 11 | +const KIRO_MIGRATION_KEY: &str = "migration.kiro.completed"; |
| 12 | + |
| 13 | +pub async fn migrate_if_needed() -> Result<bool> { |
| 14 | + let status = detect_migration().await?; |
| 15 | + |
| 16 | + match status { |
| 17 | + MigrationStatus::Completed => { |
| 18 | + debug!("Migration already completed"); |
| 19 | + return Ok(false); |
| 20 | + }, |
| 21 | + MigrationStatus::NotNeeded => { |
| 22 | + debug!("No migration needed"); |
| 23 | + mark_migration_completed()?; |
| 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 | + // Mark migration as completed in database |
| 53 | + debug!("Marking migration as completed"); |
| 54 | + mark_migration_completed()?; |
| 55 | + |
| 56 | + debug!("Migration completed successfully"); |
| 57 | + Ok(true) |
| 58 | +} |
| 59 | + |
| 60 | +#[derive(Debug)] |
| 61 | +enum MigrationStatus { |
| 62 | + NotNeeded, |
| 63 | + Needed, |
| 64 | + Completed, |
| 65 | +} |
| 66 | + |
| 67 | +async fn detect_migration() -> Result<MigrationStatus> { |
| 68 | + let old_dir = fig_util::directories::old_fig_data_dir()?; |
| 69 | + let new_dir = fig_util::directories::fig_data_dir()?; |
| 70 | + |
| 71 | + // If new directory doesn't exist yet, check if old directory exists |
| 72 | + if !new_dir.exists() { |
| 73 | + if old_dir.exists() && old_dir.is_dir() { |
| 74 | + return Ok(MigrationStatus::Needed); |
| 75 | + } else { |
| 76 | + return Ok(MigrationStatus::NotNeeded); |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + // New directory exists, check database flag (safe now since new_dir exists) |
| 81 | + let migration_completed = is_migration_completed()?; |
| 82 | + |
| 83 | + if migration_completed { |
| 84 | + Ok(MigrationStatus::Completed) |
| 85 | + } else if old_dir.exists() && old_dir.is_dir() { |
| 86 | + Ok(MigrationStatus::Needed) |
| 87 | + } else { |
| 88 | + Ok(MigrationStatus::NotNeeded) |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +async fn copy_essential_files(src: &std::path::Path, dst: &std::path::Path) -> Result<()> { |
| 93 | + // Copy data-local-dir files (database and history) |
| 94 | + let data_files = ["data.sqlite3", "history"]; |
| 95 | + for file_name in data_files { |
| 96 | + let src_path = src.join(file_name); |
| 97 | + let dst_path = dst.join(file_name); |
| 98 | + |
| 99 | + if src_path.exists() { |
| 100 | + debug!("Copying {} to {}", src_path.display(), dst_path.display()); |
| 101 | + fs::copy(&src_path, &dst_path).await?; |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + // Copy settings file to its specific location |
| 106 | + let old_settings = src.join("settings.json"); |
| 107 | + if old_settings.exists() { |
| 108 | + let new_settings = fig_util::directories::settings_path()?; |
| 109 | + if !new_settings.exists() { |
| 110 | + if let Some(parent) = new_settings.parent() { |
| 111 | + fs::create_dir_all(parent).await?; |
| 112 | + } |
| 113 | + debug!("Copying settings to {}", new_settings.display()); |
| 114 | + fs::copy(&old_settings, &new_settings).await?; |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + Ok(()) |
| 119 | +} |
| 120 | + |
| 121 | +fn mark_migration_completed() -> Result<()> { |
| 122 | + let db = fig_settings::sqlite::database()?; |
| 123 | + db.set_state_value(KIRO_MIGRATION_KEY, true)?; |
| 124 | + Ok(()) |
| 125 | +} |
| 126 | + |
| 127 | +struct MigrationLock { |
| 128 | + _file: std::fs::File, |
| 129 | + path: PathBuf, |
| 130 | +} |
| 131 | + |
| 132 | +impl Drop for MigrationLock { |
| 133 | + fn drop(&mut self) { |
| 134 | + let _ = std::fs::remove_file(&self.path); |
| 135 | + } |
| 136 | +} |
| 137 | + |
| 138 | +fn acquire_migration_lock() -> Result<Option<MigrationLock>> { |
| 139 | + let lock_path = migration_lock_path()?; |
| 140 | + |
| 141 | + if let Some(parent) = lock_path.parent() { |
| 142 | + std::fs::create_dir_all(parent)?; |
| 143 | + } |
| 144 | + |
| 145 | + // Check if lock file exists and is stale |
| 146 | + if lock_path.exists() { |
| 147 | + match std::fs::metadata(&lock_path) |
| 148 | + .and_then(|m| m.modified()) |
| 149 | + .and_then(|t| t.elapsed().map_err(std::io::Error::other)) |
| 150 | + { |
| 151 | + Ok(elapsed) if elapsed.as_secs() > 10 => { |
| 152 | + let _ = std::fs::remove_file(&lock_path); |
| 153 | + }, |
| 154 | + _ => { |
| 155 | + // Continue with lock attempt even if metadata operations fail |
| 156 | + debug!("Failed to get lock file metadata, continuing with lock attempt"); |
| 157 | + }, |
| 158 | + } |
| 159 | + } |
| 160 | + |
| 161 | + // Try to acquire the lock |
| 162 | + let file = std::fs::OpenOptions::new() |
| 163 | + .create(true) |
| 164 | + .write(true) |
| 165 | + .truncate(false) |
| 166 | + .open(&lock_path)?; |
| 167 | + |
| 168 | + match flock(&file, FlockOperation::NonBlockingLockExclusive) { |
| 169 | + Ok(()) => Ok(Some(MigrationLock { |
| 170 | + _file: file, |
| 171 | + path: lock_path, |
| 172 | + })), |
| 173 | + Err(_) => Ok(None), |
| 174 | + } |
| 175 | +} |
| 176 | + |
| 177 | +fn migration_lock_path() -> Result<PathBuf> { |
| 178 | + Ok(fig_util::directories::fig_data_dir()?.join("migration.lock")) |
| 179 | +} |
| 180 | + |
| 181 | +fn is_migration_completed() -> Result<bool> { |
| 182 | + Ok(fig_settings::state::get_bool_or(KIRO_MIGRATION_KEY, false)) |
| 183 | +} |
0 commit comments