()
| 4 | |
| 5 | #[test] |
| 6 | fn test_fresh_database_migration() { |
| 7 | let temp_dir = TempDir::new().unwrap(); |
| 8 | let db_path = temp_dir.path().join("test.db"); |
| 9 | |
| 10 | // Create a fresh database |
| 11 | let conn = Connection::open(&db_path).unwrap(); |
| 12 | let runner = MigrationRunner::new(conn); |
| 13 | |
| 14 | // Check should fail on fresh database |
| 15 | let check_result = runner.check_schema_version(); |
| 16 | assert!(check_result.is_err()); |
| 17 | assert!(check_result.unwrap_err().to_string().contains("Database schema is outdated")); |
| 18 | |
| 19 | // Now run migrations |
| 20 | let conn = runner.into_connection(); |
| 21 | let mut runner = MigrationRunner::new(conn); |
| 22 | let applied = runner.run_pending_migrations().unwrap(); |
| 23 | |
| 24 | // Should apply all migrations |
| 25 | assert_eq!(applied.len(), MIGRATIONS.len()); |
| 26 | assert_eq!(applied, vec![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]); |
| 27 | |
| 28 | // Verify schema version |
| 29 | let conn = runner.into_connection(); |
| 30 | let version: String = conn.query_row( |
| 31 | "SELECT value FROM __pgsqlite_metadata WHERE key = 'schema_version'", |
| 32 | [], |
| 33 | |row| row.get(0) |
| 34 | ).unwrap(); |
| 35 | assert_eq!(version, "27"); |
| 36 | |
| 37 | // Now check should pass |
| 38 | let runner2 = MigrationRunner::new(conn); |
| 39 | assert!(runner2.check_schema_version().is_ok()); |
| 40 | |
| 41 | // Verify all tables exist |
| 42 | let conn = runner2.into_connection(); |
| 43 | let tables: Vec<String> = conn.prepare( |
| 44 | "SELECT name FROM sqlite_master WHERE type='table' AND name LIKE '__pgsqlite_%' ORDER BY name" |
| 45 | ).unwrap() |
| 46 | .query_map([], |row| row.get(0)).unwrap() |
| 47 | .collect::<Result<Vec<_>, _>>().unwrap(); |
| 48 | |
| 49 | assert!(tables.contains(&"__pgsqlite_enum_types".to_string())); |
| 50 | assert!(tables.contains(&"__pgsqlite_enum_usage".to_string())); |
| 51 | assert!(tables.contains(&"__pgsqlite_enum_values".to_string())); |
| 52 | assert!(tables.contains(&"__pgsqlite_metadata".to_string())); |
| 53 | assert!(tables.contains(&"__pgsqlite_migration_locks".to_string())); |
| 54 | assert!(tables.contains(&"__pgsqlite_migrations".to_string())); |
| 55 | assert!(tables.contains(&"__pgsqlite_schema".to_string())); |
| 56 | assert!(tables.contains(&"__pgsqlite_array_types".to_string())); |
| 57 | assert!(tables.contains(&"__pgsqlite_fts_metadata".to_string())); |
| 58 | assert!(tables.contains(&"__pgsqlite_type_map".to_string())); |
| 59 | assert!(tables.contains(&"__pgsqlite_comments".to_string())); |
| 60 | } |
| 61 | |
| 62 | #[test] |
| 63 | fn test_idempotent_migrations() { |
nothing calls this directly
no test coverage detected