|
1 | 1 | use crate::{error, Migration, MigrationData, Result}; |
2 | | -use snafu::OptionExt; |
| 2 | +use regex::Regex; |
| 3 | +use snafu::{OptionExt, ResultExt}; |
3 | 4 |
|
4 | 5 | /// We use this migration when we add settings and want to make sure they're removed before we go |
5 | 6 | /// back to old versions that don't understand them. |
@@ -152,6 +153,206 @@ mod test_add_prefixes_migration { |
152 | 153 | } |
153 | 154 | } |
154 | 155 |
|
| 156 | +// =^..^= =^..^= =^..^= =^..^= =^..^= =^..^= =^..^= =^..^= =^..^= |
| 157 | +pub struct PrefixSuffix { |
| 158 | + pub prefix: &'static str, |
| 159 | + pub suffix: &'static str, |
| 160 | +} |
| 161 | +pub struct AddPrefixSuffixMigration(pub Vec<PrefixSuffix>); |
| 162 | + |
| 163 | +impl Migration for AddPrefixSuffixMigration { |
| 164 | + /// New versions must either have a default for the settings or generate them; we don't need to |
| 165 | + /// do anything. |
| 166 | + fn forward(&mut self, input: MigrationData) -> Result<MigrationData> { |
| 167 | + println!( |
| 168 | + "AddPrefixSuffixMigration({:?}) has no work to do on upgrade.", |
| 169 | + self.0 |
| 170 | + .iter() |
| 171 | + .map(|ps| format!("{}*{}", ps.prefix, ps.suffix)) |
| 172 | + .collect::<Vec<_>>() |
| 173 | + ); |
| 174 | + Ok(input) |
| 175 | + } |
| 176 | + |
| 177 | + /// Older versions don't know about the settings; we remove them so that old versions don't see |
| 178 | + /// them and fail deserialization. |
| 179 | + fn backward(&mut self, mut input: MigrationData) -> Result<MigrationData> { |
| 180 | + let mut compiled_patterns = Vec::new(); |
| 181 | + for pattern in &self.0 { |
| 182 | + let regex_pattern = format!( |
| 183 | + r"^{}\.(.+)\.{}$", |
| 184 | + regex::escape(pattern.prefix), |
| 185 | + regex::escape(pattern.suffix) |
| 186 | + ); |
| 187 | + let regex = |
| 188 | + Regex::new(®ex_pattern).context(error::InvalidPrefixSuffixPatternSnafu { |
| 189 | + prefix: pattern.prefix, |
| 190 | + suffix: pattern.suffix, |
| 191 | + })?; |
| 192 | + compiled_patterns.push(regex); |
| 193 | + } |
| 194 | + |
| 195 | + let settings = input |
| 196 | + .data |
| 197 | + .keys() |
| 198 | + .filter(|k| compiled_patterns.iter().any(|regex| regex.is_match(k))) |
| 199 | + .cloned() |
| 200 | + .collect::<Vec<_>>(); |
| 201 | + for setting in settings { |
| 202 | + if let Some(data) = input.data.remove(&setting) { |
| 203 | + println!("Removed {setting}, which was set to '{data}'"); |
| 204 | + } |
| 205 | + } |
| 206 | + Ok(input) |
| 207 | + } |
| 208 | +} |
| 209 | + |
| 210 | +#[cfg(test)] |
| 211 | +mod test_add_prefix_suffix_migration { |
| 212 | + use super::{AddPrefixSuffixMigration, PrefixSuffix}; |
| 213 | + use crate::{Migration, MigrationData}; |
| 214 | + use maplit::hashmap; |
| 215 | + use std::collections::HashMap; |
| 216 | + |
| 217 | + #[test] |
| 218 | + fn single_entry() { |
| 219 | + let data = MigrationData { |
| 220 | + data: hashmap! { |
| 221 | + "keep.stuff.like.this".into() => 0.into(), |
| 222 | + "remove.stuff.like.this".into() => 0.into(), |
| 223 | + "keep.this.too".into() => 0.into(), |
| 224 | + "remove.also.like.this".into() => 0.into(), |
| 225 | + }, |
| 226 | + metadata: HashMap::new(), |
| 227 | + }; |
| 228 | + let result = AddPrefixSuffixMigration(vec![PrefixSuffix { |
| 229 | + prefix: "remove", |
| 230 | + suffix: "this", |
| 231 | + }]) |
| 232 | + .backward(data) |
| 233 | + .unwrap(); |
| 234 | + assert_eq!( |
| 235 | + result.data, |
| 236 | + hashmap! { |
| 237 | + "keep.stuff.like.this".into() => 0.into(), |
| 238 | + "keep.this.too".into() => 0.into(), |
| 239 | + } |
| 240 | + ); |
| 241 | + } |
| 242 | + |
| 243 | + #[test] |
| 244 | + fn compound_suffix() { |
| 245 | + let data = MigrationData { |
| 246 | + data: hashmap! { |
| 247 | + "keep.stuff.like.this".into() => 0.into(), |
| 248 | + "remove.stuff.like.this".into() => 0.into(), |
| 249 | + "keep.this.too".into() => 0.into(), |
| 250 | + "remove.not.this".into() => 0.into(), |
| 251 | + }, |
| 252 | + metadata: HashMap::new(), |
| 253 | + }; |
| 254 | + let result = AddPrefixSuffixMigration(vec![PrefixSuffix { |
| 255 | + prefix: "remove", |
| 256 | + suffix: "like.this", |
| 257 | + }]) |
| 258 | + .backward(data) |
| 259 | + .unwrap(); |
| 260 | + assert_eq!( |
| 261 | + result.data, |
| 262 | + hashmap! { |
| 263 | + "keep.stuff.like.this".into() => 0.into(), |
| 264 | + "keep.this.too".into() => 0.into(), |
| 265 | + "remove.not.this".into() => 0.into(), |
| 266 | + } |
| 267 | + ); |
| 268 | + } |
| 269 | + |
| 270 | + #[test] |
| 271 | + fn multiple_entries() { |
| 272 | + let data = MigrationData { |
| 273 | + data: hashmap! { |
| 274 | + "keep.stuff.like.this".into() => 0.into(), |
| 275 | + "remove.stuff.like.this".into() => 0.into(), |
| 276 | + "keep.this.too".into() => 0.into(), |
| 277 | + "remove.also.this".into() => 0.into(), |
| 278 | + "delete.something.here".into() => 0.into(), |
| 279 | + }, |
| 280 | + metadata: HashMap::new(), |
| 281 | + }; |
| 282 | + let result = AddPrefixSuffixMigration(vec![ |
| 283 | + PrefixSuffix { |
| 284 | + prefix: "remove", |
| 285 | + suffix: "this", |
| 286 | + }, |
| 287 | + PrefixSuffix { |
| 288 | + prefix: "delete", |
| 289 | + suffix: "here", |
| 290 | + }, |
| 291 | + ]) |
| 292 | + .backward(data) |
| 293 | + .unwrap(); |
| 294 | + assert_eq!( |
| 295 | + result.data, |
| 296 | + hashmap! { |
| 297 | + "keep.stuff.like.this".into() => 0.into(), |
| 298 | + "keep.this.too".into() => 0.into(), |
| 299 | + } |
| 300 | + ); |
| 301 | + } |
| 302 | + |
| 303 | + #[test] |
| 304 | + fn no_match() { |
| 305 | + let data = MigrationData { |
| 306 | + data: hashmap! { |
| 307 | + "keep.stuff.like.this".into() => 0.into(), |
| 308 | + "keep.this.too".into() => 0.into(), |
| 309 | + "other.setting.here".into() => 0.into(), |
| 310 | + }, |
| 311 | + metadata: HashMap::new(), |
| 312 | + }; |
| 313 | + let result = AddPrefixSuffixMigration(vec![PrefixSuffix { |
| 314 | + prefix: "remove.", |
| 315 | + suffix: ".this", |
| 316 | + }]) |
| 317 | + .backward(data) |
| 318 | + .unwrap(); |
| 319 | + assert_eq!( |
| 320 | + result.data, |
| 321 | + hashmap! { |
| 322 | + "keep.stuff.like.this".into() => 0.into(), |
| 323 | + "keep.this.too".into() => 0.into(), |
| 324 | + "other.setting.here".into() => 0.into(), |
| 325 | + } |
| 326 | + ); |
| 327 | + } |
| 328 | + |
| 329 | + #[test] |
| 330 | + fn tight_matching() { |
| 331 | + let data = MigrationData { |
| 332 | + data: hashmap! { |
| 333 | + "settings.host-containers.admin.command".into() => 0.into(), |
| 334 | + "settings.host-containers.command".into() => 0.into(), // No middle segment |
| 335 | + "settings.host-containersadmincommand".into() => 0.into(), // No dots |
| 336 | + "keep.this".into() => 0.into(), |
| 337 | + }, |
| 338 | + metadata: HashMap::new(), |
| 339 | + }; |
| 340 | + let result = AddPrefixSuffixMigration(vec![PrefixSuffix { |
| 341 | + prefix: "settings.host-containers", |
| 342 | + suffix: "command", |
| 343 | + }]) |
| 344 | + .backward(data) |
| 345 | + .unwrap(); |
| 346 | + assert_eq!( |
| 347 | + result.data, |
| 348 | + hashmap! { |
| 349 | + "settings.host-containers.command".into() => 0.into(), |
| 350 | + "settings.host-containersadmincommand".into() => 0.into(), |
| 351 | + "keep.this".into() => 0.into(), |
| 352 | + } |
| 353 | + ); |
| 354 | + } |
| 355 | +} |
155 | 356 | // =^..^= =^..^= =^..^= =^..^= =^..^= =^..^= =^..^= =^..^= =^..^= |
156 | 357 |
|
157 | 358 | /// We use this migration when we remove settings from the model, so the new version doesn't see |
|
0 commit comments