fix(migration): implement SQL file migration runner
- Add logic to read and execute .sql migration files - Replace schema placeholders (public. -> current schema) - Track applied migrations in schema_migrations table - Support both public and dev schema environments - Fixes B-4: 27/40 migrations failed on M5Max128 Ref: docs_v1.0/M4_workspace/2026-10-04_B-4_Migration_Fixed.md
This commit is contained in:
+86
-78
@@ -1638,87 +1638,95 @@ impl PostgresDb {
|
||||
// ── Seed admin user if not exists ──
|
||||
sqlx::query("INSERT INTO users (username, password_hash, role) SELECT 'admin', '$argon2id$v=19$m=65536,t=3,p=4$YWRtaW4$placeholder', 'admin' WHERE NOT EXISTS (SELECT 1 FROM users WHERE username = 'admin')").execute(pool).await.ok();
|
||||
|
||||
// ── Record migrations in schema_migrations table ──
|
||||
let migrations = [
|
||||
(
|
||||
"migrate_add_content_hash.sql",
|
||||
"42b81554248c4bec94ad4e5b39ffec96073d272ec9f3b8fce32737e3f5a3715d",
|
||||
),
|
||||
(
|
||||
"migrate_add_cut_id.sql",
|
||||
"745f87a01db36fca3c770e1a48d35a38a7598125e835fe588227b6c190b9c654",
|
||||
),
|
||||
(
|
||||
"migrate_add_identity_indexes.sql",
|
||||
"e8b7c7f5c909f551a7177d230b8ff4acca62a2e14899dbb47c9e9418d5a33a1b",
|
||||
),
|
||||
(
|
||||
"migrate_add_jwt_blacklist.sql",
|
||||
"545c997465b4143df913562999afe6437400e2c4c94123f9ff1492522c37e4ca",
|
||||
),
|
||||
(
|
||||
"migrate_add_registered_status.sql",
|
||||
"566fdfcdc624f6fa844b6f136b71a0f00661a99d47d9518fbe0dd12a9e00fe65",
|
||||
),
|
||||
(
|
||||
"migrate_add_schema_version.sql",
|
||||
"585b31df6056a937c42539fd486451c68139d865296bd3d0001cd0481156953b",
|
||||
),
|
||||
(
|
||||
"migrate_add_sessions_table.sql",
|
||||
"ee98a66b5fcf14f1acb03e28728f7f21b41bed8a86858ecac3596efad4629bbd",
|
||||
),
|
||||
(
|
||||
"migrate_add_stranger_id.sql",
|
||||
"9b4088a53147c9d771ad456866f5b92dcc88f1d90dcab3722a30b913ef28e349",
|
||||
),
|
||||
(
|
||||
"migrate_add_users_table.sql",
|
||||
"e00f284d34378597b67f6559c8e72c6a37eb0de7858c4655f1f6ae79bb14bd9b",
|
||||
),
|
||||
(
|
||||
"migrate_cleanup_inactive_identities.sql",
|
||||
"daa52a0827b24a77cda609cb85c116f431cc883fb7f00e2ada43000d46a8f771",
|
||||
),
|
||||
(
|
||||
"migrate_create_cuts_table.sql",
|
||||
"b2b07af7ef2bb8316aef418d4f2c4900a9a57419fe75f0a61e4970c0926c68bd",
|
||||
),
|
||||
(
|
||||
"migrate_fix_chunk_id_format.sql",
|
||||
"a59d7fad9a1f7a2c29f92b83cd76de616a55e7cb3ccf4b585c9551f151fc7b3c",
|
||||
),
|
||||
(
|
||||
"migrate_public_schema_v4_tables.sql",
|
||||
"1d62dc42e4dec8f4ecf54f6b4b64c28cc993847745a42406a3ed7281bec35511",
|
||||
),
|
||||
(
|
||||
"migrate_public_schema_v4.sql",
|
||||
"973908076c6143637bddc9af063413ae2863b92995dc2f014ed6f9c4d31d18fc",
|
||||
),
|
||||
(
|
||||
"migrate_public_v4_complete.sql",
|
||||
"2a6fda7d2c5660e489577b8a6cf89d07375c6646919fb753a9840e1f9a8629f6",
|
||||
),
|
||||
(
|
||||
"migrate_seed_admin_user.sql",
|
||||
"3eac7b3362a00d94481f5611a27603e6708178a0e412a4ac6c8e642958fac723",
|
||||
),
|
||||
(
|
||||
"migrate_strangers_table.sql",
|
||||
"1900ae28ebf5532c05165690f53f22ae852ce2d0ba53e5002b01daf5d36d97bf",
|
||||
),
|
||||
];
|
||||
// ── Execute SQL migration files ──
|
||||
let migrations_dir = std::path::Path::new("migrations");
|
||||
if migrations_dir.exists() {
|
||||
tracing::info!("Executing SQL migration files from migrations/");
|
||||
|
||||
for (filename, checksum) in &migrations {
|
||||
sqlx::query(
|
||||
"INSERT INTO schema_migrations (filename, checksum) VALUES ($1, $2) ON CONFLICT (filename) DO NOTHING",
|
||||
let mut entries: Vec<_> = match std::fs::read_dir(migrations_dir) {
|
||||
Ok(entries) => entries
|
||||
.filter_map(|e| e.ok())
|
||||
.filter(|e| e.path().extension().map_or(false, |ext| ext == "sql"))
|
||||
.collect(),
|
||||
Err(e) => {
|
||||
tracing::error!("Failed to read migrations directory: {}", e);
|
||||
return Err(anyhow::anyhow!("Failed to read migrations directory: {}", e));
|
||||
}
|
||||
};
|
||||
|
||||
entries.sort_by_key(|e| e.file_name());
|
||||
|
||||
tracing::info!("Found {} migration files", entries.len());
|
||||
|
||||
let applied: Vec<String> = sqlx::query_scalar(
|
||||
"SELECT filename FROM schema_migrations ORDER BY id"
|
||||
)
|
||||
.bind(filename)
|
||||
.bind(checksum)
|
||||
.execute(pool)
|
||||
.fetch_all(pool)
|
||||
.await
|
||||
.ok();
|
||||
.unwrap_or_default();
|
||||
|
||||
let mut applied_count = 0;
|
||||
let mut skipped_count = 0;
|
||||
|
||||
for entry in entries {
|
||||
let filename = entry.file_name().to_string_lossy().to_string();
|
||||
|
||||
if applied.contains(&filename) {
|
||||
tracing::debug!("Skipping already applied: {}", filename);
|
||||
skipped_count += 1;
|
||||
continue;
|
||||
}
|
||||
|
||||
let file_path = entry.path();
|
||||
let file_start = std::time::Instant::now();
|
||||
|
||||
tracing::info!("Applying migration: {}", filename);
|
||||
|
||||
let sql_content = match std::fs::read_to_string(&file_path) {
|
||||
Ok(content) => content,
|
||||
Err(e) => {
|
||||
tracing::error!("Failed to read migration file {}: {}", filename, e);
|
||||
return Err(anyhow::anyhow!("Failed to read migration file {}: {}", filename, e));
|
||||
}
|
||||
};
|
||||
|
||||
let sql_with_schema = if schema != "public" {
|
||||
sql_content
|
||||
.replace("public.", &format!("{}.", schema))
|
||||
.replace("public::", &format!("{}::", schema))
|
||||
} else {
|
||||
sql_content.clone()
|
||||
};
|
||||
|
||||
let checksum = format!("{:x}", md5::compute(&sql_content));
|
||||
|
||||
match sqlx::query(&sql_with_schema).execute(pool).await {
|
||||
Ok(_) => {
|
||||
let duration_ms = file_start.elapsed().as_millis() as i32;
|
||||
|
||||
sqlx::query(
|
||||
"INSERT INTO schema_migrations (filename, checksum, duration_ms)
|
||||
VALUES ($1, $2, $3)"
|
||||
)
|
||||
.bind(&filename)
|
||||
.bind(&checksum)
|
||||
.bind(duration_ms)
|
||||
.execute(pool)
|
||||
.await?;
|
||||
|
||||
tracing::info!("✓ Applied {} ({}ms)", filename, duration_ms);
|
||||
applied_count += 1;
|
||||
}
|
||||
Err(e) => {
|
||||
tracing::error!("✗ Failed to apply {}: {}", filename, e);
|
||||
return Err(anyhow::anyhow!("Migration failed: {} - {}", filename, e));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
tracing::info!("SQL migrations: {} applied, {} skipped", applied_count, skipped_count);
|
||||
} else {
|
||||
tracing::warn!("No migrations/ directory found, skipping SQL file migrations");
|
||||
}
|
||||
|
||||
tracing::info!("Database migrations completed.");
|
||||
|
||||
Reference in New Issue
Block a user