MCPcopy Create free account
hub / github.com/erans/pgsqlite / test_existing_schema_detection

Function test_existing_schema_detection

tests/migration_test.rs:82–139  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

80
81#[test]
82fn test_existing_schema_detection() {
83 let temp_dir = TempDir::new().unwrap();
84 let db_path = temp_dir.path().join("test.db");
85
86 // Create a database with the old schema (pre-migration)
87 let conn = Connection::open(&db_path).unwrap();
88 conn.execute(
89 "CREATE TABLE __pgsqlite_schema (
90 table_name TEXT NOT NULL,
91 column_name TEXT NOT NULL,
92 pg_type TEXT NOT NULL,
93 sqlite_type TEXT NOT NULL,
94 PRIMARY KEY (table_name, column_name)
95 )",
96 []
97 ).unwrap();
98 drop(conn);
99
100 // Check should fail on pre-migration database
101 let conn = Connection::open(&db_path).unwrap();
102 let runner = MigrationRunner::new(conn);
103 let check_result = runner.check_schema_version();
104 assert!(check_result.is_err());
105 assert!(check_result.unwrap_err().to_string().contains("Database schema is outdated"));
106
107 // Run migrations
108 let conn = runner.into_connection();
109 let mut runner = MigrationRunner::new(conn);
110 let applied = runner.run_pending_migrations().unwrap();
111
112 // Should recognize existing schema as version 1 and only apply versions 2-27
113 assert_eq!(applied.len(), 26);
114 assert_eq!(applied[0], 2);
115 assert_eq!(applied[1], 3);
116 assert_eq!(applied[2], 4);
117 assert_eq!(applied[3], 5);
118 assert_eq!(applied[4], 6);
119 assert_eq!(applied[5], 7);
120 assert_eq!(applied[6], 8);
121 assert_eq!(applied[7], 9);
122 assert_eq!(applied[8], 10);
123 assert_eq!(applied[9], 11);
124 assert_eq!(applied[10], 12);
125 assert_eq!(applied[25], 27);
126
127 // Verify final version
128 let conn = runner.into_connection();
129 let version: String = conn.query_row(
130 "SELECT value FROM __pgsqlite_metadata WHERE key = 'schema_version'",
131 [],
132 |row| row.get(0)
133 ).unwrap();
134 assert_eq!(version, "27");
135
136 // Now check should pass
137 let runner2 = MigrationRunner::new(conn);
138 assert!(runner2.check_schema_version().is_ok());
139}

Callers

nothing calls this directly

Calls 5

check_schema_versionMethod · 0.80
into_connectionMethod · 0.80
executeMethod · 0.45
getMethod · 0.45

Tested by

no test coverage detected