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
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
use rusqlite::Connection;
use sql_support::ConnExt;

use crate::error::Result;

const VERSION: i64 = 2;

const CREATE_TABLE_PUSH_SQL: &str = include_str!("schema.sql");

pub const COMMON_COLS: &str = "
    uaid,
    channel_id,
    endpoint,
    scope,
    key,
    ctime,
    app_server_key,
    native_id
";

pub fn init(db: &Connection) -> Result<()> {
    let user_version = db.query_one::<i64>("PRAGMA user_version")?;
    if user_version == 0 {
        create(db)?;
    } else if user_version != VERSION {
        if user_version < VERSION {
            upgrade(db, user_version)?;
        } else {
            log::warn!(
                "Loaded future schema version {} (we only understand version {}). \
                 Optimistically ",
                user_version,
                VERSION
            )
        }
    }
    Ok(())
}

fn upgrade(db: &Connection, from: i64) -> Result<()> {
    log::debug!("Upgrading schema from {} to {}", from, VERSION);
    match from {
        VERSION => Ok(()),
        0 => create(db),
        1 => create(db),
        _ => panic!("sorry, no upgrades yet - delete your db!"),
    }
}

pub fn create(db: &Connection) -> Result<()> {
    let statements = format!(
        "{create}\n\nPRAGMA user_version = {version}",
        create = CREATE_TABLE_PUSH_SQL,
        version = VERSION
    );
    db.execute_batch(&statements)?;

    Ok(())
}