dbmigrator 0.4.4-alpha

Powerful SQL migration toolkit for Rust
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
use crate::changelog::Changelog;
use crate::drivers::AsyncClient;
use crate::recipe::{order_recipes, RecipeKind, RecipeScript};
use crate::RecipeError;
use std::cmp::Ordering;
use thiserror::Error;
#[cfg(feature = "tokio-postgres")]
use tokio_postgres::error::Error as PgError;

/// An Error occurred during a migration cycle
#[derive(Debug, Error)]
pub enum MigratorError {
    #[error(transparent)]
    RecipeError(RecipeError),

    #[error("no baseline migration available")]
    NoBaseline(),

    #[error("unknown baseline for version {0}")]
    UnknownBaseline(String),

    #[error("unknown target version {version} (try {})", .available.as_deref().unwrap_or("-"))]
    UnknownTarget {
        version: String,
        available: Option<String>,
    },

    #[error("no dbmigrator_log table available")]
    NoLogTable(),

    #[error("unknown migration in database `{log}`")]
    UnknownMigration { log: Changelog },

    #[error("missing migration in database `{script}`")]
    MissingMigration { script: RecipeScript },

    #[error("conflicted migration - db: `{log}`, script: `{script}`")]
    ConflictedMigration {
        log: Changelog,
        script: RecipeScript,
    },

    #[cfg(feature = "tokio-postgres")]
    #[error(transparent)]
    PgError(PgError),
}

impl From<RecipeError> for MigratorError {
    fn from(err: RecipeError) -> MigratorError {
        MigratorError::RecipeError(err)
    }
}

#[cfg(feature = "tokio-postgres")]
impl From<PgError> for MigratorError {
    fn from(err: PgError) -> MigratorError {
        MigratorError::PgError(err)
    }
}

#[derive(Clone, Debug, Default)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
pub struct Config {
    /// Allow create dbmigrator log table if not exists.
    pub auto_initialize: bool,

    /// Table name for dbmigrator_log table (should include DB schema).
    pub log_table_name: Option<String>,

    /// Baseline for initialization (if not defined use last available baseline).
    pub suggested_baseline_version: Option<String>,

    /// Limit migration to specified version (if not defined apply all).
    pub target_version: Option<String>,

    /// Optional description of the application that applies migrations.
    pub apply_by: Option<String>,

    /// Allow to apply revert and fixup migrations
    pub allow_fixes: bool,

    /// Allow to out of order migrations
    pub allow_out_of_order: bool,
}

impl Config {
    pub fn effective_log_table_name(&self) -> &str {
        self.log_table_name.as_deref().unwrap_or("dbmigrator_log")
    }
}

fn update_agg_log<'a>(
    agg_log: &mut Vec<Changelog>,
    version_comparator: fn(&str, &str) -> std::cmp::Ordering,
    log: &Changelog,
) {
    match (
        agg_log.binary_search_by(|a| (version_comparator)(&a.version(), log.version())),
        log.checksum().is_some(),
    ) {
        (Err(index), true) => {
            agg_log.insert(index, log.clone());
        }
        (Err(_), false) => (),
        (Ok(index), true) => {
            agg_log[index] = log.clone();
        }
        (Ok(index), false) => {
            agg_log.remove(index);
        }
    }
}

fn find_agg_log<'a>(
    agg_log: &'a Vec<Changelog>,
    version_comparator: fn(&str, &str) -> std::cmp::Ordering,
    version: &str,
) -> Option<&'a Changelog> {
    match agg_log.binary_search_by(|a| (version_comparator)(&a.version(), version)) {
        Ok(index) => Some(&agg_log[index]),
        Err(_) => None,
    }
}

/*
1. Sprawdzamy wersjÄ™ ostatniej migracji (`current_version`) w bazie.
2. Jeśli brak tabeli dziennika to:
    - gdy `auto_initialize` to tworzymy tabelÄ™ dziennika
    - gdy brak `auto_initialize` to zwracamy błąd
3. Weryfikujemy integralność skryptów migracji:
    - sprawdzamy czy skrypty fixup nie dotyczą istniejących skryptów migracji (niedozwolone),
    - sprawdzamy czy skrypty fixup z akcją `change_checksum` kierują do istniejących skryptów,
    - sprawdzamy czy nie ma zdublowanych skryptów upgradu (tylko jeden na wersję),
    - sprawdzamy czy nie ma zdublowanych skryptów baseline (tylko jeden na wersję),
4. Wczytujemy dziennik migracji z bazy.
6. Gdy `allow_pending_fixup = true` to:
    - poczynajÄ…c od pierwszej do ostatniej migracji z dziennika sprawdzamy czy istniejÄ… dla niej fixupy
    - jak fixup może być zastosowany (spełniony warunek maximum_version) to dodajemy do planu
    - jeśli zastosowaliśmy jakikolwiek fixup z sukcesem to ponawiamy sprawdzenie całej listy od nowa
7. Z dziennika ustalamy `baseline_version`.
8. Gdy brak `baseline_version` to:
    - do planu wstawiamy migrację baseline (`suggested_baseline_version` albo najstarszą z dostępnych)
    - jak brak w/w to błąd
    - ustalamy `baseline_version` na w/w migracjÄ™
9. Mając `baseline_version` i ewentualnie `target_version` filtrujemy listę skryptów migracji rodzaju `upgrade` do weryfikacji.
10. Następnie wryfikujemy listę dziennika, czy wszystkie wpisy są zgodne z listą skryptów migracji.
    - Gdy `allow_out_of_order` to dana migracja musi istnieć. Gdy nie `allow_out_of_order`, to to dana migracja musi być pierwsza na liście.
    - Zgodne wpisy usuwamy z listy skryptów migracji.
11. Wpisy, które pozostały trafiają do planu migracji.
12. Wykonujemy plan migracji.
 */
pub struct Migrator {
    config: Config,
    version_comparator: fn(&str, &str) -> std::cmp::Ordering,
    recipes: Vec<RecipeScript>,
    last_log_id: i32,
    next_log_id: i32,
    raw_logs: Vec<Changelog>,
    consolidated_logs: Vec<Changelog>,
    updated_logs: Vec<Changelog>,
    baseline_version: Option<String>,
    plans: Vec<MigrationPlan>,
}

impl Migrator {
    pub fn new(config: Config, version_comparator: fn(&str, &str) -> std::cmp::Ordering) -> Self {
        Migrator {
            config,
            version_comparator,
            recipes: Vec::new(),
            last_log_id: 0,
            next_log_id: 1,
            raw_logs: Vec::new(),
            consolidated_logs: Vec::new(),
            updated_logs: Vec::new(),
            baseline_version: None,
            plans: Vec::new(),
        }
    }

    fn finder(&self) -> impl Fn(&RecipeScript, &str, RecipeKind) -> std::cmp::Ordering + use<'_> {
        |item: &RecipeScript, version: &str, kind: RecipeKind| {
            (self.version_comparator)(item.version(), version).then_with(|| item.kind().cmp(&kind))
        }
    }

    pub fn config(&self) -> &Config {
        &self.config
    }

    pub fn recipes(&self) -> &Vec<RecipeScript> {
        &self.recipes
    }

    pub fn raw_logs(&self) -> &Vec<Changelog> {
        &self.raw_logs
    }

    pub fn consolidated_logs(&self) -> &Vec<Changelog> {
        &self.consolidated_logs
    }

    pub fn updated_logs(&self) -> &Vec<Changelog> {
        &self.updated_logs
    }

    pub fn plans(&self) -> &Vec<MigrationPlan> {
        &self.plans
    }

    pub fn set_recipes(&mut self, mut recipes: Vec<RecipeScript>) -> Result<(), MigratorError> {
        order_recipes(&mut recipes, self.version_comparator)?;
        self.recipes = recipes;
        Ok(())
    }

    /// Read changelog from the database and consolidate it to an ordered and effective list.
    pub async fn read_changelog(
        &mut self,
        client: &mut dyn AsyncClient,
    ) -> Result<(), MigratorError> {
        let last_log_id = client
            .last_log_id(self.config.effective_log_table_name())
            .await;
        match last_log_id {
            Ok(last_log_id) => {
                self.last_log_id = last_log_id;
            }
            Err(MigratorError::NoLogTable()) => {
                if !self.config.auto_initialize {
                    return Err(MigratorError::NoLogTable());
                }
                self.last_log_id = 0;
            }
            Err(e) => return Err(e),
        }
        self.next_log_id = self.last_log_id + 1;

        self.raw_logs = client
            .get_changelog(self.config.effective_log_table_name())
            .await?;
        self.consolidated_logs.clear();
        for log in self.raw_logs.iter() {
            update_agg_log(&mut self.consolidated_logs, self.version_comparator, log);
        }
        self.updated_logs = self.consolidated_logs.clone();

        self.plans.clear();

        Ok(())
    }

    fn recipes_for_version(&self, version: &str) -> &[RecipeScript] {
        match self
            .recipes
            .binary_search_by(|a| (self.version_comparator)(a.version(), version))
        {
            Ok(first) => {
                if let Some(last) = self.recipes[first..].iter().position(|a| {
                    (self.version_comparator)(a.version(), version) == Ordering::Greater
                }) {
                    &self.recipes[first..first + last + 1]
                } else {
                    &self.recipes[first..]
                }
            }
            Err(first) => &self.recipes[first..first],
        }
    }

    fn match_fix_recipe(
        &self,
        log_version: &str,
        log_checksum: &str,
        recipe: &RecipeScript,
        current_version: &str,
    ) -> bool {
        if let (Some(old_checksum), Some(maximum_version)) =
            (recipe.old_checksum(), recipe.maximum_version())
        {
            log_version == recipe.version()
                && log_checksum.starts_with(old_checksum)
                && matches!(
                    (self.version_comparator)(current_version, maximum_version),
                    std::cmp::Ordering::Less | std::cmp::Ordering::Equal
                )
        } else {
            false
        }
    }

    fn baseline_recipe(&self) -> Result<RecipeScript, MigratorError> {
        match self.config.suggested_baseline_version.as_ref() {
            Some(suggested_baseline_version) => {
                match self.recipes.binary_search_by(|a| {
                    (self.finder())(a, suggested_baseline_version, RecipeKind::Baseline)
                }) {
                    Ok(index) => Ok(self.recipes[index].clone()),
                    Err(_) => Err(MigratorError::UnknownBaseline(
                        suggested_baseline_version.to_string(),
                    )),
                }
            }
            None => match self.recipes.iter().rev().find(|&s| s.is_baseline()) {
                Some(recipe) => Ok(recipe.clone()),
                None => Err(MigratorError::NoBaseline()),
            },
        }
    }

    pub fn make_plan(&mut self) -> Result<(), MigratorError> {
        if self.config.allow_fixes {
            let mut current_version: Option<String> = None;
            let mut new_logs: Vec<Changelog> = Vec::new();
            for log in self.updated_logs.iter().rev() {
                if current_version.is_none() {
                    current_version = Some(log.version().to_string());
                }
                // TODO: Dlaczego muszę kopiować wektor poniżej?!
                let fixes = self.recipes_for_version(log.version()).to_vec();
                if let Some(fix) = fixes.iter().find(|fix| {
                    self.match_fix_recipe(
                        log.version(),
                        log.checksum().unwrap(),
                        fix,
                        &current_version.clone().unwrap(),
                    )
                }) {
                    let revert_log = Changelog::new(
                        self.next_log_id,
                        log.version().to_string(),
                        Some(fix.name().to_string()),
                        fix.kind().to_string(),
                        None,
                        self.config.apply_by.clone(),
                        None,
                        None,
                        None,
                    );
                    self.next_log_id += 1;

                    let apply_log =
                        if let Some((new_version, new_name, new_checksum)) = fix.new_target() {
                            let log = Some(Changelog::new(
                                self.next_log_id,
                                new_version.to_string(),
                                Some(new_name.to_string()),
                                fix.kind().to_string(),
                                Some(new_checksum.to_string()),
                                self.config.apply_by.clone(),
                                None,
                                None,
                                None,
                            ));
                            self.next_log_id += 1;
                            log
                        } else {
                            None
                        };
                    new_logs.push(revert_log.clone());
                    if let Some(apply_log) = apply_log.as_ref() {
                        new_logs.push(apply_log.clone());
                    }
                    self.plans.push(MigrationPlan {
                        recipe: fix.clone(),
                        log_id_to_revert: Some(log.log_id()),
                        revert_log: Some(revert_log.clone()),
                        apply_log: apply_log.clone(),
                    });
                    // We have to update current version of DB scheme. It is important for next fixups.
                    // For `Revert` we reset to None, for `Fixup` we set to new_version.
                    current_version = fix.new_version().map(|v| v.to_string());
                    break;
                }
            }
            for log in new_logs {
                update_agg_log(&mut self.updated_logs, self.version_comparator, &log);
            }
        }

        let mut last_version: String;
        if self.updated_logs.len() > 0 {
            self.baseline_version = Some(self.updated_logs[0].version().to_string());
            last_version = self.updated_logs.last().unwrap().version().to_string();
        } else {
            let baseline_recipe = self.baseline_recipe()?;
            self.baseline_version = Some(baseline_recipe.version().to_string());
            last_version = baseline_recipe.version().to_string();
            let apply_log = Changelog::new(
                self.next_log_id,
                baseline_recipe.version().to_string(),
                Some(baseline_recipe.name().to_string()),
                baseline_recipe.kind().to_string(),
                Some(baseline_recipe.checksum().to_string()),
                self.config.apply_by.clone(),
                None,
                None,
                None,
            );
            self.next_log_id += 1;
            update_agg_log(&mut self.updated_logs, self.version_comparator, &apply_log);
            self.plans.push(MigrationPlan {
                recipe: baseline_recipe,
                log_id_to_revert: None,
                revert_log: None,
                apply_log: Some(apply_log),
            });
        }
        for recipe in self
            .recipes
            .iter()
            .skip_while(|r| {
                matches!(
                    (self.version_comparator)(r.version(), &last_version),
                    Ordering::Less | Ordering::Equal
                )
            })
            .take_while(|r| match &self.config.target_version {
                Some(target_version) => matches!(
                    (self.version_comparator)(r.version(), target_version),
                    Ordering::Less | Ordering::Equal
                ),
                None => true,
            })
            .filter(|r| r.is_upgrade())
        {
            let apply_log = Changelog::new(
                self.next_log_id,
                recipe.version().to_string(),
                Some(recipe.name().to_string()),
                recipe.kind().to_string(),
                Some(recipe.checksum().to_string()),
                self.config.apply_by.clone(),
                None,
                None,
                None,
            );
            self.next_log_id += 1;
            update_agg_log(&mut self.updated_logs, self.version_comparator, &apply_log);
            self.plans.push(MigrationPlan {
                recipe: recipe.clone(),
                log_id_to_revert: None,
                revert_log: None,
                apply_log: Some(apply_log),
            });
        }
        Ok(())
    }

    pub fn check_updated_log(&self) -> Result<(), MigratorError> {
        // Check if target version is known.
        if let Some(target_version) = &self.config.target_version {
            if let Err(_) = self
                .recipes
                .binary_search_by(|a| (self.finder())(a, target_version, RecipeKind::Baseline))
            {
                if let Err(index) = self
                    .recipes
                    .binary_search_by(|a| (self.finder())(a, target_version, RecipeKind::Upgrade))
                {
                    return Err(MigratorError::UnknownTarget {
                        version: target_version.clone(),
                        available: if 1 <= index {
                            Some(self.recipes[index - 1].version().to_string())
                        } else {
                            None
                        },
                    });
                }
            }
        }

        // Check if all applied migrations in the database are known.
        for (index, log) in self.updated_logs.iter().enumerate() {
            if index > 0 {
                match self
                    .recipes
                    .binary_search_by(|a| (self.finder())(a, log.version(), RecipeKind::Upgrade))
                {
                    Ok(index) => {
                        if log.checksum().unwrap_or("") != self.recipes[index].checksum() {
                            return Err(MigratorError::ConflictedMigration {
                                log: log.clone(),
                                script: self.recipes[index].clone(),
                            });
                        }
                    }
                    Err(_) => return Err(MigratorError::UnknownMigration { log: log.clone() }),
                }
            }
        }

        // Check if all upgrade recipes are applied.
        if let Some(baseline_version) = &self.baseline_version {
            for script in self
                .recipes
                .iter()
                .skip_while(|r| {
                    matches!(
                        (self.version_comparator)(r.version(), baseline_version),
                        Ordering::Less | Ordering::Equal
                    )
                })
                .take_while(|r| match &self.config.target_version {
                    Some(target_version) => matches!(
                        (self.version_comparator)(r.version(), target_version),
                        Ordering::Less | Ordering::Equal
                    ),
                    None => true,
                })
                .filter(|r| r.is_upgrade())
            {
                match find_agg_log(
                    &self.updated_logs,
                    self.version_comparator,
                    script.version(),
                ) {
                    Some(log) => {
                        if log.checksum().unwrap_or("") != script.checksum() {
                            return Err(MigratorError::ConflictedMigration {
                                log: log.clone(),
                                script: script.clone(),
                            });
                        }
                    }
                    None => {
                        return Err(MigratorError::MissingMigration {
                            script: script.clone(),
                        })
                    }
                }
            }
        }
        Ok(())
    }

    pub async fn apply_plan(
        &self,
        client: &mut dyn AsyncClient,
        plan: &MigrationPlan,
    ) -> Result<(), MigratorError> {
        client
            .apply_plan(self.config.effective_log_table_name(), plan)
            .await?;
        Ok(())
    }
}

#[derive(Clone, Debug)]
pub struct MigrationPlan {
    recipe: RecipeScript,
    log_id_to_revert: Option<i32>,
    revert_log: Option<Changelog>,
    apply_log: Option<Changelog>,
}

impl MigrationPlan {
    pub fn script(&self) -> &RecipeScript {
        &self.recipe
    }

    pub fn sql(&self) -> &str {
        self.recipe.sql()
    }
    pub fn log_id_to_revert(&self) -> Option<i32> {
        self.log_id_to_revert
    }
    pub fn revert_log(&self) -> Option<&Changelog> {
        self.revert_log.as_ref()
    }
    pub fn apply_log(&self) -> Option<&Changelog> {
        self.apply_log.as_ref()
    }
}