monarch-db 0.1.1

A simple SQLite DB Migration system
Documentation
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
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
//! # Monarch-DB
//!
//! Monarch-DB is a lightweight SQLite database migration tool designed to run whenever the first
//! connection in an app opens. It provides a simple, reliable way to manage SQLite database
//! schema evolution in Rust applications.
//!
//! ## Quick Start
//!
//! ```rust
//! use monarch_db::{StaticMonarchConfiguration, MonarchDB, ConnectionConfiguration};
//!
//! # fn main() -> Result<(), Box<dyn std::error::Error>> {
//! // Define your migrations at compile time
//! let config = StaticMonarchConfiguration {
//!     name: "my_app",
//!     enable_foreign_keys: true,
//!     migrations: [
//!         // Migration 1: Create users table
//!         r#"
//!         CREATE TABLE users (
//!             id INTEGER PRIMARY KEY AUTOINCREMENT,
//!             username TEXT NOT NULL UNIQUE,
//!             email TEXT NOT NULL,
//!             created_at DATETIME DEFAULT CURRENT_TIMESTAMP
//!         );
//!         "#,
//!         // Migration 2: Create posts table
//!         r#"
//!         CREATE TABLE posts (
//!             id INTEGER PRIMARY KEY AUTOINCREMENT,
//!             user_id INTEGER NOT NULL,
//!             title TEXT NOT NULL,
//!             content TEXT,
//!             created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
//!             FOREIGN KEY (user_id) REFERENCES users(id)
//!         );
//!         "#,
//!     ],
//! };
//!
//! // Convert to MonarchDB instance
//! let monarch_db: MonarchDB = config.into();
//!
//! // Create connection configuration
//! let connection_config = ConnectionConfiguration {
//!     database: None, // Use in-memory database for this example
//! };
//!
//! // Create database connection with migrations applied
//! let connection = monarch_db.create_connection(&connection_config)?;
//!
//! // Use your database normally
//! connection.execute(
//!     "INSERT INTO users (username, email) VALUES (?, ?)",
//!     ["alice2", "alice2@example.com"],
//! )?;
//! # Ok(())
//! # }
//! ```
//!
//! ### Directory-Based Configuration
//!
//! Use directory-based configuration when you want to manage migrations as separate files:
//!
//! ```rust,no_run
//! use monarch_db::{MonarchConfiguration, MonarchDB, ConnectionConfiguration};
//!
//! # fn main() -> Result<(), Box<dyn std::error::Error>> {
//! let config = MonarchConfiguration {
//!     name: "my_app".to_string(),
//!     enable_foreign_keys: true,
//!     migration_directory: "./migrations".into(),
//! };
//!
//! let monarch_db = MonarchDB::from_configuration(config)?;
//!
//! let connection_config = ConnectionConfiguration {
//!     database: Some("./my_app.db".into()),
//! };
//!
//! let connection = monarch_db.create_connection(&connection_config)?;
//!
//! // Database is ready with all migrations applied
//! # Ok(())
//! # }
//! ```
//!
//! ## Configuration Types
//!
//! - [`StaticMonarchConfiguration`] - For compile-time embedded migrations
//! - [`MonarchConfiguration`] - For runtime directory-based migrations
//! - [`ConnectionConfiguration`] - For specifying database file paths
//!
//! ## Core Types
//!
//! - [`MonarchDB`] - Main migration manager that applies schema changes
//! - [`Migrations`] - Helper for applying migrations to database connections
//!

use std::{borrow::Cow, collections::BTreeMap, io};

use camino::Utf8PathBuf;
use rusqlite::Connection;

type Migration = Cow<'static, str>;

const VERSION_TABLE: &str = "monarch_db_schema_version";

/// Configuration for opening a new SQLite database connection.
///
/// This struct controls how a database connection is established, including
/// whether to use a file-based database or an in-memory database.
#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize))]
pub struct ConnectionConfiguration {
    /// Optional path to the database file.
    ///
    /// If `None`, an in-memory database will be used. If `Some`, the database
    /// will be persisted to the specified file path.
    #[cfg_attr(feature = "serde", serde(default))]
    pub database: Option<Utf8PathBuf>,
}

/// Configuration for MonarchDB that loads migrations from a directory at runtime.
///
/// This configuration is used when migrations are stored as separate files in a
/// directory and need to be loaded dynamically when the application starts.
#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize))]
pub struct MonarchConfiguration {
    /// The name of the database schema, used for tracking migration versions.
    pub name: String,
    /// Whether to enable foreign key constraints in SQLite.
    pub enable_foreign_keys: bool,
    /// Path to the directory containing migration files.
    pub migration_directory: Utf8PathBuf,
}

/// Configuration for MonarchDB with compile-time known migrations.
///
/// This configuration is used when all migrations are embedded in the binary
/// at compile time, typically using `include_str!` or similar macros.
/// This provides better performance and eliminates runtime file I/O.
#[derive(Debug, Clone)]
pub struct StaticMonarchConfiguration<const N: usize> {
    /// The name of the database schema, used for tracking migration versions.
    pub name: &'static str,
    /// Whether to enable foreign key constraints in SQLite.
    pub enable_foreign_keys: bool,
    /// Array of migration SQL strings, ordered from oldest to newest.
    pub migrations: [&'static str; N],
}

impl<const N: usize> From<StaticMonarchConfiguration<N>> for MonarchDB {
    fn from(configuration: StaticMonarchConfiguration<N>) -> Self {
        MonarchDB {
            name: configuration.name.into(),
            enable_foreign_keys: configuration.enable_foreign_keys,
            migrations: configuration
                .migrations
                .iter()
                .map(|q| Cow::Borrowed(*q))
                .collect(),
        }
    }
}

/// MonarchDB manages schema migrations and new connections for a database.
#[derive(Debug)]
pub struct MonarchDB {
    name: Cow<'static, str>,
    enable_foreign_keys: bool,
    migrations: Vec<Migration>,
}

impl MonarchDB {
    /// Creates a new in-memory SQLite database connection with migrations applied.
    ///
    /// This is useful for testing or for applications that need a temporary database.
    /// All migrations will be automatically applied to the in-memory database.
    ///
    /// # Returns
    ///
    /// Returns a `rusqlite::Result<Connection>` with migrations applied on success.
    pub fn open_in_memory(&self) -> rusqlite::Result<Connection> {
        let connection = Connection::open_in_memory()?;
        self.migrate(connection)
    }

    /// Creates a new MonarchDB instance from a configuration that loads migrations from disk.
    ///
    /// This reads all migration files from the specified directory and creates a MonarchDB
    /// instance that can be used to manage database connections and schema migrations.
    ///
    /// # Arguments
    ///
    /// * `configuration` - A MonarchConfiguration containing the migration directory path,
    ///   database name, and foreign key settings.
    ///
    /// # Returns
    ///
    /// Returns a `io::Result<Self>` containing the configured MonarchDB instance.
    ///
    /// # Errors
    ///
    /// This function will return an error if:
    /// - The migration directory cannot be read
    /// - Any migration file cannot be read
    /// - File system operations fail
    pub fn from_configuration(configuration: MonarchConfiguration) -> io::Result<Self> {
        let mut migrations = BTreeMap::new();
        for diritem in configuration.migration_directory.read_dir_utf8()? {
            let entry = diritem?;

            if entry.file_type()?.is_file() {
                let query = std::fs::read_to_string(entry.path())?;
                migrations.insert(entry.file_name().to_owned(), Cow::from(query));
            }
        }

        Ok(MonarchDB {
            name: configuration.name.into(),
            enable_foreign_keys: configuration.enable_foreign_keys,
            migrations: migrations.into_values().collect(),
        })
    }

    /// Returns the current schema version, which is the number of migrations available.
    ///
    /// This represents the latest version that the database schema can be migrated to.
    ///
    /// # Returns
    ///
    /// Returns the number of migrations as a `u32`.
    pub fn current_version(&self) -> u32 {
        self.migrations.len() as u32
    }

    fn get_migration(&self, version: u32) -> Option<&str> {
        self.migrations
            .get(version as usize)
            .map(|query| query.as_ref())
    }

    /// Creates a new SQLite database connection with migrations applied.
    ///
    /// If a database path is specified in the configuration, opens that file.
    /// Otherwise, creates an in-memory database. All migrations will be automatically
    /// applied to ensure the schema is up to date.
    ///
    /// # Arguments
    ///
    /// * `configuration` - A ConnectionConfiguration specifying the database path.
    ///   If `database` is None, an in-memory database will be created.
    ///
    /// # Returns
    ///
    /// Returns a `rusqlite::Result<Connection>` with migrations applied on success.
    pub fn create_connection(
        &self,
        configuration: &ConnectionConfiguration,
    ) -> rusqlite::Result<Connection> {
        let connection = if let Some(path) = configuration.database.as_deref() {
            Connection::open(path)?
        } else {
            Connection::open_in_memory()?
        };
        self.migrate(connection)
    }

    /// Applies all necessary migrations to an existing database connection.
    ///
    /// This method takes ownership of a connection and returns it after applying
    /// all migrations to bring the schema up to the current version. It will
    /// also configure foreign key constraints if enabled.
    ///
    /// # Arguments
    ///
    /// * `connection` - An existing SQLite connection to migrate.
    ///
    /// # Returns
    ///
    /// Returns the connection with migrations applied on success.
    pub fn migrate(&self, mut connection: Connection) -> rusqlite::Result<Connection> {
        let migrations = Migrations {
            connection: &mut connection,
            monarch: self,
        };
        migrations.prepare()?;
        Ok(connection)
    }

    /// Create a migration manager for the given connection.
    ///
    /// This method initializes a new `Migrations` instance, which can be used to
    /// apply migrations to the provided connection.
    pub fn migrations<'c>(&'c self, connection: &'c mut Connection) -> Migrations<'c> {
        Migrations {
            connection,
            monarch: self,
        }
    }
}

/// Helper struct for applying migrations to a database connection.
///
/// This struct manages the migration process, ensuring that the database
/// schema is brought up to the current version by applying any pending migrations.
pub struct Migrations<'c> {
    connection: &'c mut Connection,
    monarch: &'c MonarchDB,
}

impl<'c> Migrations<'c> {
    /// Prepares the database connection by configuring settings and applying migrations.
    ///
    /// This method performs the following operations:
    /// 1. Enables foreign key constraints if configured
    /// 2. Applies any pending migrations to bring the schema up to date
    ///
    /// # Returns
    ///
    /// Returns `Ok(())` on success, or a `rusqlite::Error` if any operation fails.
    #[tracing::instrument(level = "trace", skip_all, fields(monarch=%self.monarch.name))]
    pub fn prepare(self) -> rusqlite::Result<()> {
        if self.monarch.enable_foreign_keys {
            tracing::trace!("Set foreign keys");
            self.connection.pragma_update(None, "foreign_keys", true)?;
        }
        self.migrate()?;
        Ok(())
    }

    fn migrate(self) -> rusqlite::Result<()> {
        let tx = self.connection.transaction()?;
        let mut version = select_schema_version(&tx, &self.monarch.name)?;

        while version < self.monarch.current_version() {
            let query = self
                .monarch
                .get_migration(version)
                .expect("version <-> migration mismatch");
            tracing::trace!("Running migration to version {}", version + 1);
            tx.execute_batch(query)?;
            version += 1;
        }

        set_schema_version(&tx, &self.monarch.name, version)?;
        tx.commit()?;
        tracing::debug!("Migrations complete");
        Ok(())
    }
}

fn create_schema_version_table(connection: &Connection) -> rusqlite::Result<()> {
    let mut stmt = connection.prepare(include_str!("00.versions.sql"))?;
    stmt.execute([])?;
    Ok(())
}

fn insert_initial_schema_version(connection: &Connection, name: &str) -> rusqlite::Result<()> {
    let mut stmt = connection.prepare(&format!(
        "INSERT INTO {VERSION_TABLE} (monarch_schema, version) VALUES (:name, 0)"
    ))?;
    stmt.execute(&[(":name", name)])?;
    Ok(())
}

fn select_schema_version(connection: &Connection, name: &str) -> rusqlite::Result<u32> {
    let mut stmt = connection.prepare("SELECT name FROM sqlite_master WHERE name = :table")?;

    let has_version_tbl: Option<Result<String, _>> = stmt
        .query_map(&[(":table", VERSION_TABLE)], |row| row.get(0))?
        .next();

    match has_version_tbl {
        Some(Ok(_)) => {}
        Some(Err(error)) => {
            return Err(error);
        }
        None => {
            tracing::trace!("Create schema version table {VERSION_TABLE}");
            create_schema_version_table(connection)?;
            insert_initial_schema_version(connection, name)?;
            return Ok(0u32);
        }
    };

    let mut stmt = connection.prepare(&format!(
        "SELECT version FROM {VERSION_TABLE} WHERE monarch_schema = :name"
    ))?;
    let version: Option<u32> = stmt
        .query_map(&[(":name", name)], |row| row.get::<_, u32>(0))?
        .next()
        .transpose()?;
    if let Some(version) = version {
        tracing::trace!(%version, "Get schema version");
        Ok(version)
    } else {
        tracing::trace!("Insert new version for {name}");
        insert_initial_schema_version(connection, name)?;
        Ok(0)
    }
}

fn set_schema_version(connection: &Connection, name: &str, version: u32) -> rusqlite::Result<()> {
    tracing::trace!(%version, "Set schema version for {name}");
    let mut stmt = connection.prepare(&format!(
        "UPDATE {VERSION_TABLE} SET version = :version WHERE monarch_schema = :name"
    ))?;
    stmt.execute(rusqlite::named_params! { ":version": version, ":name": name})?;
    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_static_monarch_configuration_creation() {
        let config = StaticMonarchConfiguration {
            name: "test_db",
            enable_foreign_keys: true,
            migrations: [
                "CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT NOT NULL);",
                "ALTER TABLE users ADD COLUMN email TEXT;",
            ],
        };

        assert_eq!(config.name, "test_db");
        assert!(config.enable_foreign_keys);
        assert_eq!(config.migrations.len(), 2);
    }

    #[test]
    fn test_static_configuration_to_monarch_db() {
        let config = StaticMonarchConfiguration {
            name: "test_db",
            enable_foreign_keys: false,
            migrations: ["CREATE TABLE posts (id INTEGER PRIMARY KEY, title TEXT NOT NULL);"],
        };

        let monarch_db: MonarchDB = config.into();
        assert_eq!(monarch_db.current_version(), 1);
        assert_eq!(monarch_db.name, "test_db");
        assert!(!monarch_db.enable_foreign_keys);
    }

    #[test]
    fn test_open_in_memory_with_static_migrations() -> rusqlite::Result<()> {
        let config = StaticMonarchConfiguration {
            name: "test_memory_db",
            enable_foreign_keys: true,
            migrations: [
                "CREATE TABLE items (id INTEGER PRIMARY KEY, name TEXT NOT NULL);",
                "CREATE INDEX idx_items_name ON items(name);",
            ],
        };

        let monarch_db: MonarchDB = config.into();
        let connection = monarch_db.open_in_memory()?;

        // Verify the table was created
        let mut stmt = connection
            .prepare("SELECT name FROM sqlite_master WHERE type='table' AND name='items'")?;
        let table_exists: bool = stmt.query_map([], |_| Ok(true))?.next().is_some();
        assert!(table_exists);

        // Verify the index was created
        let mut stmt = connection.prepare(
            "SELECT name FROM sqlite_master WHERE type='index' AND name='idx_items_name'",
        )?;
        let index_exists: bool = stmt.query_map([], |_| Ok(true))?.next().is_some();
        assert!(index_exists);

        Ok(())
    }

    #[test]
    fn test_create_connection_with_static_migrations() -> rusqlite::Result<()> {
        let config = StaticMonarchConfiguration {
            name: "test_file_db",
            enable_foreign_keys: false,
            migrations: [
                "CREATE TABLE products (id INTEGER PRIMARY KEY, name TEXT NOT NULL, price REAL);",
            ],
        };

        let monarch_db: MonarchDB = config.into();
        let connection_config = ConnectionConfiguration { database: None };
        let connection = monarch_db.create_connection(&connection_config)?;

        // Verify the table was created
        let mut stmt = connection
            .prepare("SELECT name FROM sqlite_master WHERE type='table' AND name='products'")?;
        let table_exists: bool = stmt.query_map([], |_| Ok(true))?.next().is_some();
        assert!(table_exists);

        // Test inserting data
        connection.execute(
            "INSERT INTO products (name, price) VALUES (?, ?)",
            ["Test Product", "19.99"],
        )?;

        // Verify data was inserted
        let mut stmt = connection.prepare("SELECT COUNT(*) FROM products")?;
        let count: i64 = stmt.query_row([], |row| row.get(0))?;
        assert_eq!(count, 1);

        Ok(())
    }

    #[test]
    fn test_migration_versioning() -> rusqlite::Result<()> {
        let config = StaticMonarchConfiguration {
            name: "versioning_test",
            enable_foreign_keys: false,
            migrations: [
                "CREATE TABLE v1_table (id INTEGER PRIMARY KEY);",
                "CREATE TABLE v2_table (id INTEGER PRIMARY KEY);",
                "CREATE TABLE v3_table (id INTEGER PRIMARY KEY);",
            ],
        };

        let monarch_db: MonarchDB = config.into();
        assert_eq!(monarch_db.current_version(), 3);

        let connection = monarch_db.open_in_memory()?;

        // Verify all tables were created
        let table_names = ["v1_table", "v2_table", "v3_table"];
        for table_name in table_names {
            let mut stmt = connection.prepare(&format!(
                "SELECT name FROM sqlite_master WHERE type='table' AND name='{table_name}'"
            ))?;
            let table_exists: bool = stmt.query_map([], |_| Ok(true))?.next().is_some();
            assert!(table_exists, "Table {table_name} should exist");
        }

        Ok(())
    }
}