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
use anyhow::{anyhow, Context};
use chrono::Utc;
use slog::{debug, error, info, Logger};
use std::{cmp::Ordering, collections::BTreeSet};

use mithril_common::logging::LoggerExtensions;
use mithril_common::StdResult;

use super::{
    ApplicationNodeType, DatabaseVersion, DbVersion, GetDatabaseVersionQuery,
    UpdateDatabaseVersionQuery,
};

use crate::sqlite::{ConnectionExtensions, SqliteConnection};

/// Struct to perform application version check in the database.
pub struct DatabaseVersionChecker<'conn> {
    /// Pathbuf to the SQLite3 file.
    connection: &'conn SqliteConnection,

    /// Application type which vesion is verified.
    application_type: ApplicationNodeType,

    /// logger
    logger: Logger,

    /// known migrations
    migrations: BTreeSet<SqlMigration>,
}

impl<'conn> DatabaseVersionChecker<'conn> {
    /// constructor
    pub fn new(
        logger: Logger,
        application_type: ApplicationNodeType,
        connection: &'conn SqliteConnection,
    ) -> Self {
        let migrations = BTreeSet::new();

        Self {
            connection,
            application_type,
            logger: logger.new_with_component_name::<Self>(),
            migrations,
        }
    }

    /// Register a migration.
    pub fn add_migration(&mut self, migration: SqlMigration) -> &mut Self {
        let _ = self.migrations.insert(migration);

        self
    }

    /// Apply migrations
    pub fn apply(&self) -> StdResult<()> {
        debug!(&self.logger, "Check database version",);
        self.create_table_if_not_exists(&self.application_type)
            .with_context(|| "Can not create table 'db_version' while applying migrations")?;
        let db_version = self
            .connection
            .fetch_first(GetDatabaseVersionQuery::get_application_version(
                &self.application_type,
            ))
            .with_context(|| "Can not get application version while applying migrations")?
            .unwrap(); // At least a record exists.

        // the current database version is equal to the maximum migration
        // version present in this software.
        // If no migration registered then version = 0.
        let migration_version = self.migrations.iter().map(|m| m.version).max().unwrap_or(0);

        match migration_version.cmp(&db_version.version) {
            Ordering::Greater => {
                debug!(
                    &self.logger,
                    "Database needs upgrade from version '{}' to version '{}', applying new migrations…",
                    db_version.version, migration_version
                );
                self.apply_migrations(&db_version, self.connection)?;
                info!(
                    &self.logger,
                    "Database upgraded to version '{migration_version}'"
                );
            }
            Ordering::Less => {
                error!(
                    &self.logger,
                    "Software version '{}' is older than database structure version '{}'.",
                    db_version.version,
                    migration_version,
                );

                Err(anyhow!("This software version is older than the database structure. Aborting launch to prevent possible data corruption."))?;
            }
            Ordering::Equal => {
                debug!(&self.logger, "database up to date");
            }
        };

        Ok(())
    }

    fn apply_migrations(
        &self,
        starting_version: &DatabaseVersion,
        connection: &SqliteConnection,
    ) -> StdResult<()> {
        for migration in &self
            .migrations
            .iter()
            .filter(|&m| m.version > starting_version.version)
            .collect::<Vec<&SqlMigration>>()
        {
            connection.execute(&migration.alterations)?;
            let db_version = DatabaseVersion {
                version: migration.version,
                application_type: self.application_type.clone(),
                updated_at: Utc::now(),
            };
            let _ = connection
                .fetch_first(UpdateDatabaseVersionQuery::one(db_version))
                .with_context(|| {
                    format!(
                        "Can not save database version when applying migration: '{}'",
                        migration.version
                    )
                })?;
        }

        Ok(())
    }

    /// Method to create the table at the beginning of the migration procedure.
    /// This code is temporary and should not last.
    pub fn create_table_if_not_exists(
        &self,
        application_type: &ApplicationNodeType,
    ) -> StdResult<()> {
        let connection = self.connection;
        let table_exists = connection.query_single_cell::<_, i64>(
            "select exists(select name from sqlite_master where type='table' and name='db_version') as table_exists",
            &[],
        )? == 1;

        if !table_exists {
            let sql = format!("
create table db_version (application_type text not null primary key, version integer not null, updated_at text not null);
insert into db_version (application_type, version, updated_at) values ('{application_type}', 0, '{}');
", Utc::now().to_rfc3339());
            connection.execute(sql)?;
        }

        Ok(())
    }
}

/// Represent a file containing SQL structure or data alterations.
#[derive(Debug, Clone)]
pub struct SqlMigration {
    /// The semver version this migration targets.
    pub version: DbVersion,

    /// SQL statements to alter the database.
    pub alterations: String,
}

impl SqlMigration {
    /// Create a new SQL migration instance.
    pub fn new<T: Into<String>>(version: DbVersion, alteration: T) -> Self {
        Self {
            version,
            alterations: alteration.into(),
        }
    }
}

impl PartialOrd for SqlMigration {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        Some(self.cmp(other))
    }
}

impl Ord for SqlMigration {
    fn cmp(&self, other: &Self) -> Ordering {
        self.version.cmp(&other.version)
    }
}

impl PartialEq for SqlMigration {
    fn eq(&self, other: &Self) -> bool {
        self.version.eq(&other.version)
    }
}

impl Eq for SqlMigration {}

#[cfg(test)]
mod tests {
    use anyhow::Context;
    use mithril_common::test_utils::TempDir;
    use mithril_common::StdResult;
    use sqlite::Connection;
    use std::path::PathBuf;

    use super::*;

    fn discard_logger() -> Logger {
        Logger::root(slog::Discard, slog::o!())
    }

    fn check_database_version(connection: &SqliteConnection, db_version: DbVersion) {
        let version = connection
            .fetch_first(GetDatabaseVersionQuery::get_application_version(
                &ApplicationNodeType::Aggregator,
            ))
            .unwrap()
            .unwrap();

        assert_eq!(db_version, version.version);
    }

    fn create_sqlite_file(test_name: &str) -> StdResult<(PathBuf, SqliteConnection)> {
        let dirpath = TempDir::create("mithril_test_database", test_name);
        let filepath = dirpath.join("db.sqlite3");

        let connection = Connection::open_thread_safe(&filepath)
            .with_context(|| "connection to sqlite file failure")?;

        Ok((filepath, connection))
    }

    fn get_table_whatever_column_count(connection: &SqliteConnection) -> i64 {
        let sql = "select count(*) as column_count from pragma_table_info('whatever');";
        let column_count = connection
            .prepare(sql)
            .unwrap()
            .iter()
            .next()
            .unwrap()
            .unwrap()
            .read::<i64, _>(0);

        column_count
    }

    #[test]
    fn test_upgrade_with_migration() {
        let (_filepath, connection) =
            create_sqlite_file("test_upgrade_with_migration.sqlite3").unwrap();
        let mut db_checker = DatabaseVersionChecker::new(
            discard_logger(),
            ApplicationNodeType::Aggregator,
            &connection,
        );

        db_checker.apply().unwrap();
        assert_eq!(0, get_table_whatever_column_count(&connection));

        db_checker.apply().unwrap();
        assert_eq!(0, get_table_whatever_column_count(&connection));

        let alterations = "create table whatever (thing_id integer); insert into whatever (thing_id) values (1), (2), (3), (4);";
        let migration = SqlMigration {
            version: 1,
            alterations: alterations.to_string(),
        };
        db_checker.add_migration(migration);
        db_checker.apply().unwrap();
        assert_eq!(1, get_table_whatever_column_count(&connection));
        check_database_version(&connection, 1);

        db_checker.apply().unwrap();
        assert_eq!(1, get_table_whatever_column_count(&connection));
        check_database_version(&connection, 1);

        let alterations = "alter table whatever add column thing_content text; update whatever set thing_content = 'some content'";
        let migration = SqlMigration {
            version: 2,
            alterations: alterations.to_string(),
        };
        db_checker.add_migration(migration);
        db_checker.apply().unwrap();
        assert_eq!(2, get_table_whatever_column_count(&connection));
        check_database_version(&connection, 2);

        // in the test below both migrations are declared in reversed order to
        // ensure they are played in the right order. The last one depends on
        // the 3rd.
        let alterations = "alter table whatever add column one_last_thing text; update whatever set one_last_thing = more_thing";
        let migration = SqlMigration {
            version: 4,
            alterations: alterations.to_string(),
        };
        db_checker.add_migration(migration);
        let alterations = "alter table whatever add column more_thing text; update whatever set more_thing = 'more thing'";
        let migration = SqlMigration {
            version: 3,
            alterations: alterations.to_string(),
        };
        db_checker.add_migration(migration);
        db_checker.apply().unwrap();
        assert_eq!(4, get_table_whatever_column_count(&connection));
        check_database_version(&connection, 4);
    }

    #[test]
    fn starting_with_migration() {
        let (_filepath, connection) = create_sqlite_file("starting_with_migration").unwrap();
        let mut db_checker = DatabaseVersionChecker::new(
            discard_logger(),
            ApplicationNodeType::Aggregator,
            &connection,
        );

        let alterations = "create table whatever (thing_id integer); insert into whatever (thing_id) values (1), (2), (3), (4);";
        let migration = SqlMigration {
            version: 1,
            alterations: alterations.to_string(),
        };
        db_checker.add_migration(migration);
        db_checker.apply().unwrap();
        assert_eq!(1, get_table_whatever_column_count(&connection));
        check_database_version(&connection, 1);
    }

    #[test]
    /// This test case ensure that when multiple migrations are played and one fails:
    /// * previous migrations are ok and the database version is updated
    /// * further migrations are not played.
    fn test_failing_migration() {
        let (_filepath, connection) = create_sqlite_file("test_failing_migration").unwrap();
        let mut db_checker = DatabaseVersionChecker::new(
            discard_logger(),
            ApplicationNodeType::Aggregator,
            &connection,
        );
        // Table whatever does not exist, this should fail with error.
        let alterations = "create table whatever (thing_id integer); insert into whatever (thing_id) values (1), (2), (3), (4);";
        let migration = SqlMigration {
            version: 1,
            alterations: alterations.to_string(),
        };
        db_checker.add_migration(migration);
        let alterations = "alter table wrong add column thing_content text; update whatever set thing_content = 'some content'";
        let migration = SqlMigration {
            version: 2,
            alterations: alterations.to_string(),
        };
        db_checker.add_migration(migration);
        let alterations = "alter table whatever add column thing_content text; update whatever set thing_content = 'some content'";
        let migration = SqlMigration {
            version: 3,
            alterations: alterations.to_string(),
        };
        db_checker.add_migration(migration);
        db_checker.apply().unwrap_err();
        check_database_version(&connection, 1);
    }

    #[test]
    fn test_fail_downgrading() {
        let (_filepath, connection) = create_sqlite_file("test_fail_downgrading").unwrap();
        let mut db_checker = DatabaseVersionChecker::new(
            discard_logger(),
            ApplicationNodeType::Aggregator,
            &connection,
        );
        let alterations = "create table whatever (thing_id integer); insert into whatever (thing_id) values (1), (2), (3), (4);";
        let migration = SqlMigration {
            version: 1,
            alterations: alterations.to_string(),
        };
        db_checker.add_migration(migration);
        db_checker.apply().unwrap();
        check_database_version(&connection, 1);

        // re instantiate a new checker with no migration registered (version 0).
        let db_checker = DatabaseVersionChecker::new(
            discard_logger(),
            ApplicationNodeType::Aggregator,
            &connection,
        );
        assert!(
            db_checker.apply().is_err(),
            "using an old version with an up to date database should fail"
        );
        check_database_version(&connection, 1);
    }
}