mln-rs-engine 0.0.23

Part of mln-rs, a MapLibre map renderer written in Rust. Depend on mln-rs instead.
Documentation
// Schema creation, migrations, and vacuuming for `OfflineDatabase`.

use std::time::Duration;

use rusqlite::{Connection, OpenFlags};

use super::{Begin, DbResult, OfflineDatabase, exec};
use crate::storage::offline_schema::OFFLINE_DATABASE_SCHEMA;

impl OfflineDatabase {
    pub(super) fn initialize(&mut self) -> DbResult<()> {
        assert!(self.db.is_none());

        let flags = if self.read_only {
            OpenFlags::SQLITE_OPEN_READ_ONLY | OpenFlags::SQLITE_OPEN_URI
        } else {
            OpenFlags::default()
        };
        let db = self.db.insert(Connection::open_with_flags(&self.path, flags)?);
        db.busy_timeout(Duration::from_millis(i32::MAX as u64))?;
        exec(db, "PRAGMA foreign_keys = ON")?;
        if self.read_only {
            return Ok(());
        }

        let user_version = self.pragma_i64("PRAGMA user_version")?;
        match user_version {
            0 | 1 => {
                // Newly created database, or old cache-only database; remove
                // old table if it exists.
                self.remove_old_cache_table()?;
                self.create_schema()?;
                Ok(())
            }
            2..=6 => {
                if user_version == 2 {
                    self.migrate_to_version3()?;
                }
                // 3: Removed migration, see below.
                if user_version <= 4 {
                    self.migrate_to_version5()?;
                }
                if user_version <= 5 {
                    self.migrate_to_version6()?;
                }
                // 6: Happy path; we're done
                Ok(())
            }
            _ => {
                // Downgrade: delete the database and try to reinitialize.
                self.remove_existing()?;
                self.initialize()
            }
        }
    }

    fn remove_old_cache_table(&mut self) -> DbResult<()> {
        self.check_flags()?;
        exec(self.db()?, "DROP TABLE IF EXISTS http_cache")?;
        if self.autopack {
            self.vacuum()?;
        }
        Ok(())
    }

    fn create_schema(&mut self) -> DbResult<()> {
        self.check_flags()?;

        self.vacuum()?;
        let db = self.db()?;
        exec(db, "PRAGMA journal_mode = DELETE")?;
        exec(db, "PRAGMA synchronous = FULL")?;
        self.with_transaction(Begin::Deferred, |this| {
            let db = this.db()?;
            exec(db, OFFLINE_DATABASE_SCHEMA)?;
            exec(db, "PRAGMA user_version = 6")?;
            Ok(())
        })
    }

    fn migrate_to_version3(&mut self) -> DbResult<()> {
        self.check_flags()?;
        self.vacuum()?;
        exec(self.db()?, "PRAGMA user_version = 3")?;
        Ok(())
    }

    // No released database has schema version 4, so nothing migrates to it.

    fn migrate_to_version5(&mut self) -> DbResult<()> {
        self.check_flags()?;
        let db = self.db()?;
        exec(db, "PRAGMA journal_mode = DELETE")?;
        exec(db, "PRAGMA synchronous = FULL")?;
        exec(db, "PRAGMA user_version = 5")?;
        Ok(())
    }

    fn migrate_to_version6(&mut self) -> DbResult<()> {
        self.check_flags()?;
        self.with_transaction(Begin::Deferred, |this| {
            let db = this.db()?;
            exec(db, "ALTER TABLE resources ADD COLUMN must_revalidate INTEGER NOT NULL DEFAULT 0")?;
            exec(db, "ALTER TABLE tiles ADD COLUMN must_revalidate INTEGER NOT NULL DEFAULT 0")?;
            exec(db, "PRAGMA user_version = 6")?;
            Ok(())
        })
    }

    pub(super) fn vacuum(&mut self) -> DbResult<()> {
        self.check_flags()?;
        if self.pragma_i64("PRAGMA auto_vacuum")? == 2 {
            exec(self.db()?, "PRAGMA incremental_vacuum")?;
        } else {
            // A database without pages, such as a new one in memory, takes
            // the setting with its first table, and has nothing to vacuum.
            let empty = self.pragma_i64("PRAGMA page_count")? == 0;
            let db = self.db()?;
            exec(db, "PRAGMA auto_vacuum = INCREMENTAL")?;
            if !empty {
                exec(db, "VACUUM")?;
            }
        }
        Ok(())
    }
}