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
use anyhow::Result;
use rusqlite::{Connection, OptionalExtension};
use tracing::*;

pub trait Migration: Sync {
    fn run(&self, connection: &Connection) -> Result<()>;
}

impl Migration for &str {
    fn run(&self, connection: &Connection) -> Result<()> {
        connection.execute_batch(self)?;
        Ok(())
    }
}

pub static MIGRATIONS: &[&dyn Migration] = &[&include_str!("migrations/v1.sql")];

fn migration_version(connection: &Connection) -> Result<u64> {
    // check if there is a migrations table
    let result = connection
        .query_row(
            "SELECT name FROM sqlite_master WHERE type = 'table' AND name = ?",
            ("migrations",),
            |_row| Ok(()),
        )
        .optional()?;
    if result.is_none() {
        return Ok(0);
    }

    // get current migration version
    let result = connection.query_row("SELECT MAX(version) FROM migrations", (), |row| {
        row.get::<_, u64>(0)
    })?;
    Ok(result)
}

/// Migrates the database schema
pub fn migrate(connection: &Connection) -> Result<()> {
    connection.pragma_update(None, "optimize", "0x10002")?;

    // TODO: check for some migration table
    let version = migration_version(connection)? as usize;

    for migration in &MIGRATIONS[version..] {
        info!("Running migration");
        migration.run(connection)?;
        connection.pragma_update(None, "optimize", "")?;
    }

    //connection.pragma_update(None, "integrity_check", ())?;
    Ok(())
}

#[test]
fn migrate_once() {
    let connection = Connection::open_in_memory().unwrap();
    super::functions::register_all(&connection).unwrap();
    migrate(&connection).unwrap();
}

#[test]
fn migrate_twice() {
    let connection = Connection::open_in_memory().unwrap();
    super::functions::register_all(&connection).unwrap();
    migrate(&connection).unwrap();
    migrate(&connection).unwrap();
}