030e59e
zhenyi
refactor(lib): migrate storage and security tier to central lib/config (auth, cache, db, email, jwt, payment, queue, rate_limit, storage)
030e59e
use crate::conn::DbError;
use sea_orm::{ConnectionTrait, DatabaseBackend, DatabaseConnection};
pub async fn sync_schema(db: &DatabaseConnection) -> Result<(), DbError> {
db.get_schema_registry("db::")
.sync(db)
.await
.map_err(DbError::SchemaSync)?;
if db.get_database_backend() == DatabaseBackend::Postgres {
db.execute_unprepared(include_str!("schema_extras.sql"))
.await
.map_err(DbError::SchemaSync)?;
}
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
use sea_orm::{Database, Statement};
async fn table_exists(db: &DatabaseConnection, name: &str) -> bool {
db.query_one_raw(Statement::from_sql_and_values(
DatabaseBackend::Sqlite,
"SELECT 1 FROM sqlite_master WHERE type = 'table' AND name = ?",
[name.into()],
))
.await
.unwrap()
.is_some()
}
#[tokio::test]
async fn registry_prefix_matches_module() {
let db = Database::connect("sqlite::memory:").await.unwrap();
db.get_schema_registry("db::channel::")
.sync(&db)
.await
.unwrap();
assert!(table_exists(&db, "channel").await);
assert!(table_exists(&db, "channel_overwrite_role").await);
db.get_schema_registry("db::nope::")
.sync(&db)
.await
.unwrap();
assert!(!table_exists(&db, "project").await);
}
#[tokio::test]
async fn entity_first_sync_converges_on_sqlite() {
use crate::{
channel::channels as channel_entity,
chat::{chat_reads as read_entity, chats as chat_entity},
issue::{issue_reactions as reaction_entity, issue_watch as watch_entity},
project::project as project_entity,
user::users as user_entity,
};
let db = Database::connect("sqlite::memory:").await.unwrap();
db.get_schema_builder()
.register(user_entity::Entity)
.register(channel_entity::Entity)
.register(project_entity::Entity)
.register(chat_entity::Entity)
.register(read_entity::Entity)
.register(watch_entity::Entity)
.register(reaction_entity::Entity)
.sync(&db)
.await
.unwrap();
db.get_schema_builder()
.register(user_entity::Entity)
.register(channel_entity::Entity)
.register(project_entity::Entity)
.register(chat_entity::Entity)
.register(read_entity::Entity)
.register(watch_entity::Entity)
.register(reaction_entity::Entity)
.sync(&db)
.await
.unwrap();
for table in [
"channel",
"project",
"chat_reads",
"issue_watch",
"issue_reaction",
"users",
] {
assert!(
table_exists(&db, table).await,
"table `{table}` should exist after sync"
);
}
}
}