chore: fix rust test

This commit is contained in:
Nathan 2025-04-21 13:07:50 +08:00
parent 7885cb80f4
commit 2cf96a2ce3
4 changed files with 53 additions and 18 deletions

View File

@ -159,7 +159,6 @@ impl DocumentManager {
format!("document {} already exists", doc_id), format!("document {} already exists", doc_id),
)) ))
} else { } else {
info!("create document {}", doc_id);
let encoded_collab = doc_state_from_document_data(doc_id, data).await?; let encoded_collab = doc_state_from_document_data(doc_id, data).await?;
self self
.persistence()? .persistence()?

View File

@ -4,7 +4,7 @@ use crate::sql::select_user_workspace;
use flowy_error::{FlowyError, FlowyResult}; use flowy_error::{FlowyError, FlowyResult};
use flowy_sqlite::schema::user_table; use flowy_sqlite::schema::user_table;
use flowy_sqlite::{prelude::*, DBConnection, ExpressionMethods, RunQueryDsl}; use flowy_sqlite::{prelude::*, DBConnection, ExpressionMethods, RunQueryDsl};
use tracing::trace; use tracing::{trace, warn};
/// The order of the fields in the struct must be the same as the order of the fields in the table. /// The order of the fields in the struct must be the same as the order of the fields in the table.
/// Check out the [schema.rs] for table schema. /// Check out the [schema.rs] for table schema.
@ -92,14 +92,7 @@ pub fn update_user_profile(
Ok(()) Ok(())
} }
pub fn select_user_profile( fn select_user_table_row(uid: i64, conn: &mut SqliteConnection) -> Result<UserTable, FlowyError> {
uid: i64,
workspace_id: &str,
conn: &mut SqliteConnection,
) -> Result<UserProfile, FlowyError> {
let workspace = select_user_workspace(workspace_id, conn)?;
let workspace_auth_type = AuthType::from(workspace.workspace_type);
let row = user_table::dsl::user_table let row = user_table::dsl::user_table
.filter(user_table::id.eq(&uid.to_string())) .filter(user_table::id.eq(&uid.to_string()))
.first::<UserTable>(conn) .first::<UserTable>(conn)
@ -109,6 +102,17 @@ pub fn select_user_profile(
uid, err uid, err
)) ))
})?; })?;
Ok(row)
}
pub fn select_user_profile(
uid: i64,
workspace_id: &str,
conn: &mut SqliteConnection,
) -> Result<UserProfile, FlowyError> {
let workspace = select_user_workspace(workspace_id, conn)?;
let workspace_auth_type = AuthType::from(workspace.workspace_type);
let row = select_user_table_row(uid, conn)?;
let user = UserProfile { let user = UserProfile {
uid: row.id.parse::<i64>().unwrap_or(0), uid: row.id.parse::<i64>().unwrap_or(0),
@ -124,6 +128,28 @@ pub fn select_user_profile(
Ok(user) Ok(user)
} }
pub fn select_workspace_auth_type(
uid: i64,
workspace_id: &str,
conn: &mut SqliteConnection,
) -> Result<AuthType, FlowyError> {
match select_user_workspace(workspace_id, conn) {
Ok(workspace) => Ok(AuthType::from(workspace.workspace_type)),
Err(err) => {
if err.is_record_not_found() {
let row = select_user_table_row(uid, conn)?;
warn!(
"user user auth type:{} as workspace auth type",
row.auth_type
);
Ok(AuthType::from(row.auth_type))
} else {
Err(err)
}
},
}
}
pub fn upsert_user(user: UserTable, mut conn: DBConnection) -> FlowyResult<()> { pub fn upsert_user(user: UserTable, mut conn: DBConnection) -> FlowyResult<()> {
conn.immediate_transaction(|conn| { conn.immediate_transaction(|conn| {
// delete old user if exists // delete old user if exists

View File

@ -36,7 +36,7 @@ use std::collections::{HashMap, HashSet};
use collab_document::blocks::TextDelta; use collab_document::blocks::TextDelta;
use collab_document::document::Document; use collab_document::document::Document;
use flowy_user_pub::sql::select_user_profile; use flowy_user_pub::sql::{select_user_profile, select_workspace_auth_type};
use semver::Version; use semver::Version;
use serde_json::json; use serde_json::json;
use std::ops::{Deref, DerefMut}; use std::ops::{Deref, DerefMut};
@ -101,15 +101,25 @@ pub(crate) fn prepare_import(
CollabKVDB::open(collab_db_path) CollabKVDB::open(collab_db_path)
.map_err(|err| anyhow!("[AppflowyData]: open import collab db failed: {:?}", err))?, .map_err(|err| anyhow!("[AppflowyData]: open import collab db failed: {:?}", err))?,
); );
let imported_user = select_user_profile(
let mut conn = imported_sqlite_db.get_connection()?;
let imported_workspace_auth_type = select_user_profile(
imported_session.user_id, imported_session.user_id,
&imported_session.user_workspace.id, &imported_session.user_workspace.id,
&mut *imported_sqlite_db.get_connection()?, &mut conn,
)?; )
.map(|v| v.workspace_auth_type)
.or_else(|_| {
select_workspace_auth_type(
imported_session.user_id,
&imported_session.user_workspace.id,
&mut conn,
)
})?;
run_collab_data_migration( run_collab_data_migration(
&imported_session, &imported_session,
&imported_user, &imported_workspace_auth_type,
imported_collab_db.clone(), imported_collab_db.clone(),
imported_sqlite_db.get_pool(), imported_sqlite_db.get_pool(),
other_store_preferences.clone(), other_store_preferences.clone(),

View File

@ -253,7 +253,7 @@ impl UserManager {
(Ok(collab_db), Ok(sqlite_pool)) => { (Ok(collab_db), Ok(sqlite_pool)) => {
run_collab_data_migration( run_collab_data_migration(
&session, &session,
&user, &user.auth_type,
collab_db, collab_db,
sqlite_pool, sqlite_pool,
self.store_preferences.clone(), self.store_preferences.clone(),
@ -869,7 +869,7 @@ fn mark_all_migrations_as_applied(sqlite_pool: &Arc<ConnectionPool>) {
pub(crate) fn run_collab_data_migration( pub(crate) fn run_collab_data_migration(
session: &Session, session: &Session,
user: &UserProfile, auth_type: &AuthType,
collab_db: Arc<CollabKVDB>, collab_db: Arc<CollabKVDB>,
sqlite_pool: Arc<ConnectionPool>, sqlite_pool: Arc<ConnectionPool>,
kv: Arc<KVStorePreferences>, kv: Arc<KVStorePreferences>,
@ -878,7 +878,7 @@ pub(crate) fn run_collab_data_migration(
let migrations = collab_migration_list(); let migrations = collab_migration_list();
match UserLocalDataMigration::new(session.clone(), collab_db, sqlite_pool, kv).run( match UserLocalDataMigration::new(session.clone(), collab_db, sqlite_pool, kv).run(
migrations, migrations,
&user.auth_type, auth_type,
app_version, app_version,
) { ) {
Ok(applied_migrations) => { Ok(applied_migrations) => {