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
#![doc(
    html_root_url = "https://cmsd2.github.io/rust-docs/schemamama_rusqlite/schemamama_rusqlite/"
)]

#[allow(unused_imports)]
use log::warn;
use rusqlite::{
    Connection as SqliteConnection, Error as SqliteError, Result as SqliteResult, Row as SqliteRow,
};
use schemamama::{Adapter, Migration, Version};
use std::cell::RefCell;
use std::collections::BTreeSet;
use std::rc::Rc;
use thiserror::Error;

#[derive(Error, Debug)]
pub enum SqliteMigrationError {
    #[error("unknown error")]
    UknownError,
    #[error("sqlite error")]
    RusqliteError(#[from] SqliteError),
    #[error("sql error")]
    SqlError(String),
}

pub type Result<T> = std::result::Result<T, SqliteMigrationError>;

/// A migration to be used within a PostgreSQL connection.
pub trait SqliteMigration: Migration {
    /// Called when this migration is to be executed. This function has an empty body by default,
    /// so its implementation is optional.
    #[allow(unused_variables)]
    fn up(&self, conn: &SqliteConnection) -> SqliteResult<()> {
        Ok(())
    }

    /// Called when this migration is to be reversed. This function has an empty body by default,
    /// so its implementation is optional.
    #[allow(unused_variables)]
    fn down(&self, conn: &SqliteConnection) -> SqliteResult<()> {
        Ok(())
    }
}

/// An adapter that allows its migrations to act upon PostgreSQL connection transactions.
pub struct SqliteAdapter {
    connection: Rc<RefCell<SqliteConnection>>,
}

impl SqliteAdapter {
    /// Create a new migrator tied to a SQLite connection.
    pub fn new(connection: Rc<RefCell<SqliteConnection>>) -> SqliteAdapter {
        SqliteAdapter {
            connection: connection,
        }
    }

    /// Create the tables Schemamama requires to keep track of schema state. If the tables already
    /// exist, this function has no operation.
    pub fn setup_schema(&self) {
        let conn = self.connection.borrow();

        let query = "CREATE TABLE IF NOT EXISTS schemamama (version BIGINT PRIMARY KEY);";
        if let Err(e) = conn.execute(query, []) {
            panic!("Schema setup failed: {:?}", e);
        }
    }

    // Panics if `setup_schema` hasn't previously been called or if the insertion query otherwise
    // fails.
    fn record_version(&self, conn: &SqliteConnection, version: Version) -> SqliteResult<()> {
        let query = "INSERT INTO schemamama (version) VALUES ($1);";
        let mut stmt = conn.prepare(query)?;

        match stmt.execute(&[&version]) {
            Err(e) => {
                warn!("Failed to delete version {:?}: {:?}", version, e);
                Err(e)
            }
            _ => Ok(()),
        }
    }

    // Panics if `setup_schema` hasn't previously been called or if the deletion query otherwise
    // fails.
    fn erase_version(&self, conn: &SqliteConnection, version: Version) -> SqliteResult<()> {
        let query = "DELETE FROM schemamama WHERE version = $1;";
        let mut stmt = conn.prepare(query).unwrap();

        match stmt.execute(&[&version]) {
            Err(e) => {
                warn!("Failed to delete version {:?}: {:?}", version, e);
                Err(e)
            }
            _ => Ok(()),
        }
    }

    fn execute_transaction<F>(&self, block: F) -> SqliteResult<()>
    where
        F: Fn(&SqliteConnection) -> SqliteResult<()>,
    {
        let mut conn = self.connection.borrow_mut();

        let tx = conn.transaction()?;

        block(&tx)?;

        tx.commit()
    }

    fn query_row<T, F>(&self, q: &str, block: F) -> SqliteResult<T>
    where
        F: FnOnce(&SqliteRow) -> SqliteResult<T>,
    {
        let conn = self.connection.borrow();

        let result = conn.query_row(q, [], block)?;

        Ok(result)
    }

    fn query_map<T, F>(&self, q: &str, block: F) -> SqliteResult<Vec<T>>
    where
        F: FnMut(&SqliteRow) -> SqliteResult<T>,
    {
        let conn = self.connection.borrow();

        let mut statement = conn.prepare(q)?;

        let result = statement.query_map([], block)?;

        result.collect()
    }
}

impl Adapter for SqliteAdapter {
    type MigrationType = dyn SqliteMigration;

    type Error = SqliteMigrationError;

    /// Panics if `setup_schema` hasn't previously been called or if the query otherwise fails.
    fn current_version(&self) -> Result<Option<Version>> {
        let query = "SELECT version FROM schemamama ORDER BY version DESC LIMIT 1;";

        match self.query_row(query, |row| row.get(0)) {
            Ok(version) => Ok(Some(version)),
            Err(SqliteError::QueryReturnedNoRows) => Ok(None),
            Err(e) => Err(e.into()),
        }
    }

    /// Panics if `setup_schema` hasn't previously been called or if the query otherwise fails.
    fn migrated_versions(&self) -> Result<BTreeSet<Version>> {
        let query = "SELECT version FROM schemamama;";

        let rows = self.query_map(query, |row_result| row_result.get::<usize, i64>(0))?;

        let mut versions = BTreeSet::new();

        for vresult in rows {
            versions.insert(vresult);
        }

        Ok(versions)
    }

    /// Panics if `setup_schema` hasn't previously been called or if the migration otherwise fails.
    fn apply_migration(&self, migration: &dyn SqliteMigration) -> Result<()> {
        self.execute_transaction(|transaction| {
            migration.up(&transaction)?;
            self.record_version(transaction, migration.version())?;
            Ok(())
        })?;

        Ok(())
    }

    /// Panics if `setup_schema` hasn't previously been called or if the migration otherwise fails.
    fn revert_migration(&self, migration: &dyn SqliteMigration) -> Result<()> {
        self.execute_transaction(|transaction| {
            migration.down(&transaction)?;
            self.erase_version(transaction, migration.version())?;
            Ok(())
        })?;

        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use super::{SqliteAdapter, SqliteMigration};

    use rusqlite::{Connection as SqliteConnection, Result as SqliteResult};
    use schemamama::{migration, Migrator};
    use std::cell::RefCell;
    use std::rc::Rc;

    struct CreateUsers;
    migration!(CreateUsers, 1, "create users table");

    impl SqliteMigration for CreateUsers {
        fn up(&self, conn: &SqliteConnection) -> SqliteResult<()> {
            conn.execute("CREATE TABLE users (id BIGINT PRIMARY KEY);", [])
                .map(|_| ())
        }

        fn down(&self, conn: &SqliteConnection) -> SqliteResult<()> {
            conn.execute("DROP TABLE users;", []).map(|_| ())
        }
    }

    #[test]
    pub fn test_register() {
        let conn = Rc::new(RefCell::new(SqliteConnection::open_in_memory().unwrap()));

        let adapter = SqliteAdapter::new(conn);

        adapter.setup_schema();

        let mut migrator = Migrator::new(adapter);

        migrator.register(Box::new(CreateUsers));

        migrator.up(Some(1)).unwrap();

        assert_eq!(migrator.current_version().unwrap(), Some(1));

        migrator.down(None).unwrap();

        assert_eq!(migrator.current_version().unwrap(), None);
    }
}