mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-08-11 14:08:55 +08:00
feat: improve admin build (#14485)
#### PR Dependency Tree * **PR #14485** 👈 This tree was auto-generated by [Charcoal](https://github.com/danerwilliams/charcoal) <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **Chores** * Admin static assets now served under /admin for self-hosted installs * CLI is directly executable from the command line * Build tooling supports a configurable self-hosted public path * Updated admin package script for adding UI components * Added a PostCSS dependency and plugin to the build toolchain for admin builds * **Style** * Switched queue module to a local queuedash stylesheet, added queuedash Tailwind layer, and scoped queuedash styles for the admin UI * **Bug Fixes** * Improved error propagation in the Electron renderer * Migration compatibility to repair a legacy checksum during native storage upgrades * **Tests** * Added tests covering the migration repair flow <!-- end of auto-generated comment: release notes by coderabbit.ai -->
This commit is contained in:
@@ -4,7 +4,7 @@ use affine_schema::get_migrator;
|
||||
use memory_indexer::InMemoryIndex;
|
||||
use sqlx::{
|
||||
Pool, Row,
|
||||
migrate::MigrateDatabase,
|
||||
migrate::{MigrateDatabase, Migration, Migrator},
|
||||
sqlite::{Sqlite, SqliteConnectOptions, SqlitePoolOptions},
|
||||
};
|
||||
use tokio::sync::RwLock;
|
||||
@@ -75,11 +75,74 @@ impl SqliteDocStorage {
|
||||
|
||||
async fn migrate(&self) -> Result<()> {
|
||||
let migrator = get_migrator();
|
||||
migrator.run(&self.pool).await?;
|
||||
if let Err(err) = migrator.run(&self.pool).await {
|
||||
// Compatibility: migration 3 (`add_idx_snapshots`) had a whitespace-only SQL
|
||||
// change (trailing space) between releases, which causes sqlx to reject
|
||||
// existing DBs with: `VersionMismatch(3)`. It's safe to fix by updating
|
||||
// the stored checksum.
|
||||
if matches!(err, sqlx::migrate::MigrateError::VersionMismatch(3))
|
||||
&& self.try_repair_migration_3_checksum(&migrator).await?
|
||||
{
|
||||
migrator.run(&self.pool).await?;
|
||||
} else {
|
||||
return Err(err.into());
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn try_repair_migration_3_checksum(&self, migrator: &Migrator) -> Result<bool> {
|
||||
let Some(migration) = migrator.iter().find(|m| m.version == 3) else {
|
||||
return Ok(false);
|
||||
};
|
||||
|
||||
// We're only prepared to repair the known `add_idx_snapshots` whitespace-only
|
||||
// mismatch.
|
||||
if migration.description.as_ref() != "add_idx_snapshots" {
|
||||
return Ok(false);
|
||||
}
|
||||
|
||||
let row = sqlx::query("SELECT description, checksum FROM _sqlx_migrations WHERE version = 3")
|
||||
.fetch_optional(&self.pool)
|
||||
.await?;
|
||||
|
||||
let Some(row) = row else {
|
||||
return Ok(false);
|
||||
};
|
||||
|
||||
let applied_description: String = row.try_get("description")?;
|
||||
if applied_description != migration.description.as_ref() {
|
||||
return Ok(false);
|
||||
}
|
||||
|
||||
let applied_checksum: Vec<u8> = row.try_get("checksum")?;
|
||||
let expected_checksum = migration.checksum.as_ref();
|
||||
|
||||
// sqlx computes the checksum as SHA-384 of the raw SQL bytes. The legacy
|
||||
// variant had an extra trailing space at the end of the SQL string (after
|
||||
// the final newline).
|
||||
let legacy_sql = format!("{} ", migration.sql);
|
||||
let legacy_migration = Migration::new(
|
||||
migration.version,
|
||||
migration.description.clone(),
|
||||
migration.migration_type,
|
||||
std::borrow::Cow::Owned(legacy_sql),
|
||||
migration.no_tx,
|
||||
);
|
||||
|
||||
if applied_checksum.as_slice() != legacy_migration.checksum.as_ref() {
|
||||
return Ok(false);
|
||||
}
|
||||
|
||||
sqlx::query("UPDATE _sqlx_migrations SET checksum = ? WHERE version = 3")
|
||||
.bind(expected_checksum)
|
||||
.execute(&self.pool)
|
||||
.await?;
|
||||
|
||||
Ok(true)
|
||||
}
|
||||
|
||||
pub async fn close(&self) {
|
||||
self.pool.close().await
|
||||
}
|
||||
@@ -100,6 +163,11 @@ impl SqliteDocStorage {
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use std::borrow::Cow;
|
||||
|
||||
use affine_schema::get_migrator;
|
||||
use sqlx::migrate::{Migration, Migrator};
|
||||
|
||||
use super::*;
|
||||
|
||||
async fn get_storage() -> SqliteDocStorage {
|
||||
@@ -135,4 +203,57 @@ mod tests {
|
||||
let storage = SqliteDocStorage::new(":memory:".to_string());
|
||||
assert!(!storage.validate().await.unwrap());
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn connect_repairs_whitespace_only_migration_checksum_mismatch() {
|
||||
// Simulate a DB migrated with an older `add_idx_snapshots` SQL that had a
|
||||
// trailing space.
|
||||
let storage = SqliteDocStorage::new(":memory:".to_string());
|
||||
|
||||
let new_migrator = get_migrator();
|
||||
let mut migrations = new_migrator.migrations.to_vec();
|
||||
assert!(migrations.len() >= 3);
|
||||
|
||||
let mig3 = migrations[2].clone();
|
||||
assert_eq!(mig3.version, 3);
|
||||
assert_eq!(mig3.description.as_ref(), "add_idx_snapshots");
|
||||
|
||||
let legacy_sql = format!("{} ", mig3.sql);
|
||||
migrations[2] = Migration::new(
|
||||
mig3.version,
|
||||
mig3.description.clone(),
|
||||
mig3.migration_type,
|
||||
Cow::Owned(legacy_sql),
|
||||
mig3.no_tx,
|
||||
);
|
||||
|
||||
// The legacy DB didn't have newer migrations.
|
||||
migrations.truncate(3);
|
||||
let legacy_migrator = Migrator {
|
||||
migrations: Cow::Owned(migrations),
|
||||
..Migrator::DEFAULT
|
||||
};
|
||||
|
||||
legacy_migrator.run(&storage.pool).await.unwrap();
|
||||
|
||||
// Now connecting with the current code should auto-repair the checksum and
|
||||
// succeed.
|
||||
storage.connect().await.unwrap();
|
||||
|
||||
let expected_checksum = get_migrator()
|
||||
.iter()
|
||||
.find(|m| m.version == 3)
|
||||
.unwrap()
|
||||
.checksum
|
||||
.as_ref()
|
||||
.to_vec();
|
||||
|
||||
let row = sqlx::query("SELECT checksum FROM _sqlx_migrations WHERE version = 3")
|
||||
.fetch_one(&storage.pool)
|
||||
.await
|
||||
.unwrap();
|
||||
let checksum: Vec<u8> = row.get("checksum");
|
||||
|
||||
assert_eq!(checksum, expected_checksum);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user