chore: Update Diesel to 2.1.x (#4197)

* chore: update diesel to 2.1.x

* chore: commit tauri cargo lock file

---------

Co-authored-by: nathan <nathan@appflowy.io>
This commit is contained in:
Jiraffe7 2023-12-23 23:27:15 +08:00 committed by GitHub
parent 851296fa0e
commit a4a21c7e94
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
20 changed files with 218 additions and 122 deletions

View File

@ -1560,37 +1560,49 @@ checksum = "850878694b7933ca4c9569d30a34b55031b9b139ee1fc7b94a527c4ef960d690"
[[package]] [[package]]
name = "diesel" name = "diesel"
version = "1.4.8" version = "2.1.4"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b28135ecf6b7d446b43e27e225622a038cc4e2930a1022f51cdb97ada19b8e4d" checksum = "62c6fcf842f17f8c78ecf7c81d75c5ce84436b41ee07e03f490fbb5f5a8731d8"
dependencies = [ dependencies = [
"byteorder",
"chrono", "chrono",
"diesel_derives", "diesel_derives",
"libsqlite3-sys", "libsqlite3-sys",
"r2d2",
"time",
] ]
[[package]] [[package]]
name = "diesel_derives" name = "diesel_derives"
version = "1.4.1" version = "2.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "45f5098f628d02a7a0f68ddba586fb61e80edec3bdc1be3b921f4ceec60858d3" checksum = "ef8337737574f55a468005a83499da720f20c65586241ffea339db9ecdfd2b44"
dependencies = [ dependencies = [
"diesel_table_macro_syntax",
"proc-macro2", "proc-macro2",
"quote", "quote",
"syn 1.0.109", "syn 2.0.32",
] ]
[[package]] [[package]]
name = "diesel_migrations" name = "diesel_migrations"
version = "1.4.0" version = "2.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bf3cde8413353dc7f5d72fa8ce0b99a560a359d2c5ef1e5817ca731cd9008f4c" checksum = "6036b3f0120c5961381b570ee20a02432d7e2d27ea60de9578799cf9156914ac"
dependencies = [ dependencies = [
"diesel",
"migrations_internals", "migrations_internals",
"migrations_macros", "migrations_macros",
] ]
[[package]]
name = "diesel_table_macro_syntax"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fc5557efc453706fed5e4fa85006fe9817c224c3f480a34c7e5959fd700921c5"
dependencies = [
"syn 2.0.32",
]
[[package]] [[package]]
name = "digest" name = "digest"
version = "0.10.7" version = "0.10.7"
@ -3746,23 +3758,23 @@ dependencies = [
[[package]] [[package]]
name = "migrations_internals" name = "migrations_internals"
version = "1.4.1" version = "2.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2b4fc84e4af020b837029e017966f86a1c2d5e83e64b589963d5047525995860" checksum = "0f23f71580015254b020e856feac3df5878c2c7a8812297edd6c0a485ac9dada"
dependencies = [ dependencies = [
"diesel", "serde",
"toml 0.7.5",
] ]
[[package]] [[package]]
name = "migrations_macros" name = "migrations_macros"
version = "1.4.2" version = "2.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9753f12909fd8d923f75ae5c3258cae1ed3c8ec052e1b38c93c21a6d157f789c" checksum = "cce3325ac70e67bbab5bd837a31cae01f1a6db64e0e744a33cb03a543469ef08"
dependencies = [ dependencies = [
"migrations_internals", "migrations_internals",
"proc-macro2", "proc-macro2",
"quote", "quote",
"syn 1.0.109",
] ]
[[package]] [[package]]

View File

@ -20,7 +20,7 @@ bytes = "1.5.0"
serde = "1.0.108" serde = "1.0.108"
serde_json = "1.0.108" serde_json = "1.0.108"
protobuf = { version = "2.28.0" } protobuf = { version = "2.28.0" }
diesel = { version = "1.4.8", features = ["sqlite", "chrono"] } diesel = { version = "2.1.0", features = ["sqlite", "chrono", "r2d2"] }
uuid = { version = "1.5.0", features = ["serde", "v4"] } uuid = { version = "1.5.0", features = ["serde", "v4"] }
serde_repr = "0.1" serde_repr = "0.1"
parking_lot = "0.12" parking_lot = "0.12"

View File

@ -1373,37 +1373,49 @@ checksum = "d95203a6a50906215a502507c0f879a0ce7ff205a6111e2db2a5ef8e4bb92e43"
[[package]] [[package]]
name = "diesel" name = "diesel"
version = "1.4.8" version = "2.1.4"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b28135ecf6b7d446b43e27e225622a038cc4e2930a1022f51cdb97ada19b8e4d" checksum = "62c6fcf842f17f8c78ecf7c81d75c5ce84436b41ee07e03f490fbb5f5a8731d8"
dependencies = [ dependencies = [
"byteorder",
"chrono", "chrono",
"diesel_derives", "diesel_derives",
"libsqlite3-sys", "libsqlite3-sys",
"r2d2",
"time",
] ]
[[package]] [[package]]
name = "diesel_derives" name = "diesel_derives"
version = "1.4.1" version = "2.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "45f5098f628d02a7a0f68ddba586fb61e80edec3bdc1be3b921f4ceec60858d3" checksum = "ef8337737574f55a468005a83499da720f20c65586241ffea339db9ecdfd2b44"
dependencies = [ dependencies = [
"diesel_table_macro_syntax",
"proc-macro2", "proc-macro2",
"quote", "quote",
"syn 1.0.109", "syn 2.0.31",
] ]
[[package]] [[package]]
name = "diesel_migrations" name = "diesel_migrations"
version = "1.4.0" version = "2.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bf3cde8413353dc7f5d72fa8ce0b99a560a359d2c5ef1e5817ca731cd9008f4c" checksum = "6036b3f0120c5961381b570ee20a02432d7e2d27ea60de9578799cf9156914ac"
dependencies = [ dependencies = [
"diesel",
"migrations_internals", "migrations_internals",
"migrations_macros", "migrations_macros",
] ]
[[package]]
name = "diesel_table_macro_syntax"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fc5557efc453706fed5e4fa85006fe9817c224c3f480a34c7e5959fd700921c5"
dependencies = [
"syn 2.0.31",
]
[[package]] [[package]]
name = "digest" name = "digest"
version = "0.10.7" version = "0.10.7"
@ -1730,7 +1742,7 @@ dependencies = [
"similar 1.3.0", "similar 1.3.0",
"syn 1.0.109", "syn 1.0.109",
"tera", "tera",
"toml", "toml 0.5.11",
"walkdir", "walkdir",
] ]
@ -3199,23 +3211,23 @@ dependencies = [
[[package]] [[package]]
name = "migrations_internals" name = "migrations_internals"
version = "1.4.1" version = "2.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2b4fc84e4af020b837029e017966f86a1c2d5e83e64b589963d5047525995860" checksum = "0f23f71580015254b020e856feac3df5878c2c7a8812297edd6c0a485ac9dada"
dependencies = [ dependencies = [
"diesel", "serde",
"toml 0.7.8",
] ]
[[package]] [[package]]
name = "migrations_macros" name = "migrations_macros"
version = "1.4.2" version = "2.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9753f12909fd8d923f75ae5c3258cae1ed3c8ec052e1b38c93c21a6d157f789c" checksum = "cce3325ac70e67bbab5bd837a31cae01f1a6db64e0e744a33cb03a543469ef08"
dependencies = [ dependencies = [
"migrations_internals", "migrations_internals",
"proc-macro2", "proc-macro2",
"quote", "quote",
"syn 1.0.109",
] ]
[[package]] [[package]]
@ -3897,7 +3909,7 @@ version = "0.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1d6ea3c4595b96363c13943497db34af4460fb474a95c43f4446ad341b8c9785" checksum = "1d6ea3c4595b96363c13943497db34af4460fb474a95c43f4446ad341b8c9785"
dependencies = [ dependencies = [
"toml", "toml 0.5.11",
] ]
[[package]] [[package]]
@ -4925,6 +4937,15 @@ dependencies = [
"syn 2.0.31", "syn 2.0.31",
] ]
[[package]]
name = "serde_spanned"
version = "0.6.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "eb3622f419d1296904700073ea6cc23ad690adbd66f13ea683df73298736f0c1"
dependencies = [
"serde",
]
[[package]] [[package]]
name = "serde_urlencoded" name = "serde_urlencoded"
version = "0.7.1" version = "0.7.1"
@ -5655,6 +5676,40 @@ dependencies = [
"serde", "serde",
] ]
[[package]]
name = "toml"
version = "0.7.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dd79e69d3b627db300ff956027cc6c3798cef26d22526befdfcd12feeb6d2257"
dependencies = [
"serde",
"serde_spanned",
"toml_datetime",
"toml_edit",
]
[[package]]
name = "toml_datetime"
version = "0.6.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3550f4e9685620ac18a50ed434eb3aec30db8ba93b0287467bca5826ea25baf1"
dependencies = [
"serde",
]
[[package]]
name = "toml_edit"
version = "0.19.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1b5bb770da30e5cbfde35a2d7b9b8a2c4b8ef89548a7a6aeab5c9a576e3e7421"
dependencies = [
"indexmap 2.1.0",
"serde",
"serde_spanned",
"toml_datetime",
"winnow",
]
[[package]] [[package]]
name = "tonic" name = "tonic"
version = "0.10.2" version = "0.10.2"
@ -6324,6 +6379,15 @@ version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ed94fce61571a4006852b7389a063ab983c02eb1bb37b47f8272ce92d06d9538" checksum = "ed94fce61571a4006852b7389a063ab983c02eb1bb37b47f8272ce92d06d9538"
[[package]]
name = "winnow"
version = "0.5.30"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9b5c3db89721d50d0e2a673f5043fc4722f76dcc352d7b1ab8b8288bed4ed2c5"
dependencies = [
"memchr",
]
[[package]] [[package]]
name = "winreg" name = "winreg"
version = "0.50.0" version = "0.50.0"

View File

@ -59,7 +59,7 @@ bytes = "1.5.0"
serde_json = "1.0.108" serde_json = "1.0.108"
serde = "1.0.108" serde = "1.0.108"
protobuf = { version = "2.28.0" } protobuf = { version = "2.28.0" }
diesel = { version = "1.4.8", features = ["sqlite", "chrono"] } diesel = { version = "2.1.0", features = ["sqlite", "chrono", "r2d2"] }
uuid = { version = "1.5.0", features = ["serde", "v4"] } uuid = { version = "1.5.0", features = ["serde", "v4"] }
serde_repr = "0.1" serde_repr = "0.1"
parking_lot = "0.12" parking_lot = "0.12"

View File

@ -36,7 +36,7 @@ impl RocksdbBackup for RocksdbBackupImpl {
self self
.get_pool(uid) .get_pool(uid)
.map(|pool| RocksdbBackupTableSql::create(row, &*pool.get()?))??; .map(|pool| RocksdbBackupTableSql::create(row, &mut *pool.get()?))??;
Ok(()) Ok(())
} }
@ -48,15 +48,15 @@ impl RocksdbBackup for RocksdbBackupImpl {
let pool = self.get_pool(uid)?; let pool = self.get_pool(uid)?;
let row = pool let row = pool
.get() .get()
.map(|conn| sql.first::<RocksdbBackupRow>(&*conn))??; .map(|mut conn| sql.first::<RocksdbBackupRow>(&mut *conn))??;
Ok(EncodedCollabV1::decode_from_bytes(&row.data)?) Ok(EncodedCollabV1::decode_from_bytes(&row.data)?)
} }
} }
#[derive(PartialEq, Clone, Debug, Queryable, Identifiable, Insertable, Associations)] #[derive(PartialEq, Clone, Debug, Queryable, Identifiable, Insertable)]
#[table_name = "rocksdb_backup"] #[diesel(table_name = rocksdb_backup)]
#[primary_key(object_id)] #[diesel(primary_key(object_id))]
struct RocksdbBackupRow { struct RocksdbBackupRow {
object_id: String, object_id: String,
timestamp: i64, timestamp: i64,
@ -65,7 +65,7 @@ struct RocksdbBackupRow {
struct RocksdbBackupTableSql; struct RocksdbBackupTableSql;
impl RocksdbBackupTableSql { impl RocksdbBackupTableSql {
fn create(row: RocksdbBackupRow, conn: &SqliteConnection) -> Result<(), FlowyError> { fn create(row: RocksdbBackupRow, conn: &mut SqliteConnection) -> Result<(), FlowyError> {
let _ = replace_into(dsl::rocksdb_backup) let _ = replace_into(dsl::rocksdb_backup)
.values(&row) .values(&row)
.execute(conn)?; .execute(conn)?;
@ -73,7 +73,7 @@ impl RocksdbBackupTableSql {
} }
#[allow(dead_code)] #[allow(dead_code)]
fn get_row(object_id: &str, conn: &SqliteConnection) -> Result<RocksdbBackupRow, FlowyError> { fn get_row(object_id: &str, conn: &mut SqliteConnection) -> Result<RocksdbBackupRow, FlowyError> {
let sql = dsl::rocksdb_backup let sql = dsl::rocksdb_backup
.filter(dsl::object_id.eq(object_id)) .filter(dsl::object_id.eq(object_id))
.into_boxed(); .into_boxed();

View File

@ -21,8 +21,8 @@ impl SnapshotPersistence for SnapshotDBImpl {
Some(user_session) => user_session Some(user_session) => user_session
.db_pool(uid) .db_pool(uid)
.and_then(|pool| Ok(pool.get()?)) .and_then(|pool| Ok(pool.get()?))
.and_then(|conn| { .and_then(|mut conn| {
CollabSnapshotTableSql::get_all_snapshots(object_id, &conn) CollabSnapshotTableSql::get_all_snapshots(object_id, &mut conn)
.map(|rows| rows.into_iter().map(|row| row.into()).collect()) .map(|rows| rows.into_iter().map(|row| row.into()).collect())
}) })
.unwrap_or_else(|_| vec![]), .unwrap_or_else(|_| vec![]),
@ -43,7 +43,7 @@ impl SnapshotPersistence for SnapshotDBImpl {
.upgrade() .upgrade()
.and_then(|user_session| user_session.db_pool(uid).ok()) .and_then(|user_session| user_session.db_pool(uid).ok())
{ {
let conn = pool let mut conn = pool
.get() .get()
.map_err(|e| PersistenceError::Internal(e.into()))?; .map_err(|e| PersistenceError::Internal(e.into()))?;
@ -58,7 +58,7 @@ impl SnapshotPersistence for SnapshotDBImpl {
timestamp: timestamp(), timestamp: timestamp(),
data: snapshot_data, data: snapshot_data,
}, },
&conn, &mut conn,
) )
.map_err(|e| PersistenceError::Internal(e.into())); .map_err(|e| PersistenceError::Internal(e.into()));
@ -72,8 +72,8 @@ impl SnapshotPersistence for SnapshotDBImpl {
} }
} }
#[derive(PartialEq, Clone, Debug, Queryable, Identifiable, Insertable, Associations)] #[derive(PartialEq, Clone, Debug, Queryable, Identifiable, Insertable)]
#[table_name = "collab_snapshot"] #[diesel(table_name = collab_snapshot)]
struct CollabSnapshotRow { struct CollabSnapshotRow {
id: String, id: String,
object_id: String, object_id: String,
@ -95,7 +95,7 @@ impl From<CollabSnapshotRow> for CollabSnapshot {
struct CollabSnapshotTableSql; struct CollabSnapshotTableSql;
impl CollabSnapshotTableSql { impl CollabSnapshotTableSql {
fn create(row: CollabSnapshotRow, conn: &SqliteConnection) -> Result<(), FlowyError> { fn create(row: CollabSnapshotRow, conn: &mut SqliteConnection) -> Result<(), FlowyError> {
// Batch insert: https://diesel.rs/guides/all-about-inserts.html // Batch insert: https://diesel.rs/guides/all-about-inserts.html
let values = ( let values = (
dsl::id.eq(row.id), dsl::id.eq(row.id),
@ -114,7 +114,7 @@ impl CollabSnapshotTableSql {
fn get_all_snapshots( fn get_all_snapshots(
object_id: &str, object_id: &str,
conn: &SqliteConnection, conn: &mut SqliteConnection,
) -> Result<Vec<CollabSnapshotRow>, FlowyError> { ) -> Result<Vec<CollabSnapshotRow>, FlowyError> {
let sql = dsl::collab_snapshot let sql = dsl::collab_snapshot
.filter(dsl::object_id.eq(object_id)) .filter(dsl::object_id.eq(object_id))
@ -128,7 +128,10 @@ impl CollabSnapshotTableSql {
} }
#[allow(dead_code)] #[allow(dead_code)]
fn get_latest_snapshot(object_id: &str, conn: &SqliteConnection) -> Option<CollabSnapshotRow> { fn get_latest_snapshot(
object_id: &str,
conn: &mut SqliteConnection,
) -> Option<CollabSnapshotRow> {
let sql = dsl::collab_snapshot let sql = dsl::collab_snapshot
.filter(dsl::object_id.eq(object_id)) .filter(dsl::object_id.eq(object_id))
.into_boxed(); .into_boxed();
@ -143,7 +146,7 @@ impl CollabSnapshotTableSql {
fn delete( fn delete(
object_id: &str, object_id: &str,
snapshot_ids: Option<Vec<String>>, snapshot_ids: Option<Vec<String>>,
conn: &SqliteConnection, conn: &mut SqliteConnection,
) -> Result<(), FlowyError> { ) -> Result<(), FlowyError> {
let mut sql = diesel::delete(dsl::collab_snapshot).into_boxed(); let mut sql = diesel::delete(dsl::collab_snapshot).into_boxed();
sql = sql.filter(dsl::object_id.eq(object_id)); sql = sql.filter(dsl::object_id.eq(object_id));

View File

@ -7,8 +7,8 @@ edition = "2018"
[dependencies] [dependencies]
diesel.workspace = true diesel.workspace = true
diesel_derives = { version = "1.4.1", features = ["sqlite"] } diesel_derives = { version = "2.1.0", features = ["sqlite", "r2d2"] }
diesel_migrations = { version = "1.4.0", features = ["sqlite"] } diesel_migrations = { version = "2.1.0", features = ["sqlite"] }
tracing.workspace = true tracing.workspace = true
lazy_static = "1.4.0" lazy_static = "1.4.0"
serde.workspace = true serde.workspace = true
@ -16,8 +16,8 @@ serde_json.workspace = true
anyhow.workspace = true anyhow.workspace = true
parking_lot.workspace = true parking_lot.workspace = true
r2d2 = "0.8.10" r2d2 = ">= 0.8.2, < 0.9.0"
libsqlite3-sys = { version = ">=0.8.0, <0.24.0", features = ["bundled"] } libsqlite3-sys = { version = ">=0.17.2, <0.28.0", features = ["bundled"] }
scheduled-thread-pool = "0.2.6" scheduled-thread-pool = "0.2.6"
error-chain = "=0.12.0" error-chain = "=0.12.0"
openssl = { version = "0.10.45", optional = true, features = ["vendored"] } openssl = { version = "0.10.45", optional = true, features = ["vendored"] }

View File

@ -2,7 +2,7 @@ use std::path::Path;
use ::diesel::{query_dsl::*, ExpressionMethods}; use ::diesel::{query_dsl::*, ExpressionMethods};
use anyhow::anyhow; use anyhow::anyhow;
use diesel::{Connection, SqliteConnection}; use diesel::sql_query;
use serde::de::DeserializeOwned; use serde::de::DeserializeOwned;
use serde::Serialize; use serde::Serialize;
@ -25,8 +25,8 @@ impl StorePreferences {
let pool_config = PoolConfig::default(); let pool_config = PoolConfig::default();
let database = Database::new(root, DB_NAME, pool_config).unwrap(); let database = Database::new(root, DB_NAME, pool_config).unwrap();
let conn = database.get_connection().unwrap(); let mut conn = database.get_connection().unwrap();
SqliteConnection::execute(&*conn, KV_SQL).unwrap(); sql_query(KV_SQL).execute(&mut conn).unwrap();
tracing::trace!("Init StorePreferences with path: {}", root); tracing::trace!("Init StorePreferences with path: {}", root);
Ok(Self { Ok(Self {
@ -86,13 +86,13 @@ impl StorePreferences {
} }
pub fn remove(&self, key: &str) { pub fn remove(&self, key: &str) {
if let Some(conn) = self if let Some(mut conn) = self
.database .database
.as_ref() .as_ref()
.and_then(|database| database.get_connection().ok()) .and_then(|database| database.get_connection().ok())
{ {
let sql = dsl::kv_table.filter(kv_table::key.eq(key)); let sql = dsl::kv_table.filter(kv_table::key.eq(key));
let _ = diesel::delete(sql).execute(&*conn); let _ = diesel::delete(sql).execute(&mut *conn);
} }
} }
@ -103,30 +103,30 @@ impl StorePreferences {
.and_then(|database| database.get_connection().ok()) .and_then(|database| database.get_connection().ok())
{ {
None => Err(anyhow!("StorePreferences is not initialized")), None => Err(anyhow!("StorePreferences is not initialized")),
Some(conn) => { Some(mut conn) => {
diesel::replace_into(kv_table::table) diesel::replace_into(kv_table::table)
.values(KeyValue { .values(KeyValue {
key: key.to_string(), key: key.to_string(),
value, value,
}) })
.execute(&*conn)?; .execute(&mut *conn)?;
Ok(()) Ok(())
}, },
} }
} }
fn get_key_value(&self, key: &str) -> Option<KeyValue> { fn get_key_value(&self, key: &str) -> Option<KeyValue> {
let conn = self.database.as_ref().unwrap().get_connection().ok()?; let mut conn = self.database.as_ref().unwrap().get_connection().ok()?;
dsl::kv_table dsl::kv_table
.filter(kv_table::key.eq(key)) .filter(kv_table::key.eq(key))
.first::<KeyValue>(&*conn) .first::<KeyValue>(&mut *conn)
.ok() .ok()
} }
} }
#[derive(Clone, Debug, Default, Queryable, Identifiable, Insertable, AsChangeset)] #[derive(Clone, Debug, Default, Queryable, Identifiable, Insertable, AsChangeset)]
#[table_name = "kv_table"] #[diesel(table_name = kv_table)]
#[primary_key(key)] #[diesel(primary_key(key))]
pub struct KeyValue { pub struct KeyValue {
pub key: String, pub key: String,
pub value: Option<String>, pub value: Option<String>,

View File

@ -9,6 +9,7 @@ use std::{fmt::Debug, io, path::Path};
pub use diesel::*; pub use diesel::*;
pub use diesel_derives::*; pub use diesel_derives::*;
use diesel_migrations::{EmbeddedMigrations, MigrationHarness};
use crate::sqlite_impl::PoolConfig; use crate::sqlite_impl::PoolConfig;
pub use crate::sqlite_impl::{ConnectionPool, DBConnection, Database}; pub use crate::sqlite_impl::{ConnectionPool, DBConnection, Database};
@ -29,7 +30,7 @@ pub mod prelude {
pub use crate::*; pub use crate::*;
} }
embed_migrations!("../flowy-sqlite/migrations/"); pub const MIGRATIONS: EmbeddedMigrations = embed_migrations!("../flowy-sqlite/migrations/");
pub const DB_NAME: &str = "flowy-database.db"; pub const DB_NAME: &str = "flowy-database.db";
pub fn init<P: AsRef<Path>>(storage_path: P) -> Result<Database, io::Error> { pub fn init<P: AsRef<Path>>(storage_path: P) -> Result<Database, io::Error> {
@ -39,8 +40,10 @@ pub fn init<P: AsRef<Path>>(storage_path: P) -> Result<Database, io::Error> {
} }
let pool_config = PoolConfig::default(); let pool_config = PoolConfig::default();
let database = Database::new(storage_path, DB_NAME, pool_config).map_err(as_io_error)?; let database = Database::new(storage_path, DB_NAME, pool_config).map_err(as_io_error)?;
let conn = database.get_connection().map_err(as_io_error)?; let mut conn = database.get_connection().map_err(as_io_error)?;
embedded_migrations::run(&*conn).map_err(as_io_error)?; (*conn)
.run_pending_migrations(MIGRATIONS)
.map_err(|e| io::Error::new(io::ErrorKind::Other, format!("{:?}", e)))?;
Ok(database) Ok(database)
} }

View File

@ -1,26 +1,29 @@
use diesel::{ use diesel::{
dsl::sql, expression::SqlLiteral, query_dsl::LoadQuery, Connection, RunQueryDsl, SqliteConnection, dsl::sql, expression::SqlLiteral, query_dsl::LoadQuery, sql_query, sql_types::SingleValue,
Connection, RunQueryDsl, SqliteConnection,
}; };
use crate::sqlite_impl::errors::*; use crate::sqlite_impl::errors::*;
pub trait ConnectionExtension: Connection { pub trait ConnectionExtension: Connection {
fn query<ST, T>(&self, query: &str) -> Result<T> fn query<'query, ST, T>(&mut self, query: &str) -> Result<T>
where where
SqlLiteral<ST>: LoadQuery<SqliteConnection, T>; SqlLiteral<ST>: LoadQuery<'query, SqliteConnection, T>,
ST: SingleValue;
fn exec(&self, query: impl AsRef<str>) -> Result<usize>; fn exec(&mut self, query: impl AsRef<str>) -> Result<usize>;
} }
impl ConnectionExtension for SqliteConnection { impl ConnectionExtension for SqliteConnection {
fn query<ST, T>(&self, query: &str) -> Result<T> fn query<'query, ST, T>(&mut self, query: &str) -> Result<T>
where where
SqlLiteral<ST>: LoadQuery<SqliteConnection, T>, SqlLiteral<ST>: LoadQuery<'query, SqliteConnection, T>,
ST: SingleValue,
{ {
Ok(sql::<ST>(query).get_result(self)?) Ok(sql::<ST>(query).get_result(self)?)
} }
fn exec(&self, query: impl AsRef<str>) -> Result<usize> { fn exec(&mut self, query: impl AsRef<str>) -> Result<usize> {
Ok(SqliteConnection::execute(self, query.as_ref())?) Ok(sql_query(query.as_ref()).execute(self)?)
} }
} }

View File

@ -11,7 +11,7 @@ error_chain! {
} }
foreign_links { foreign_links {
R2D2(::r2d2::Error); R2D2(::r2d2::Error);
Migrations(::diesel_migrations::RunMigrationsError); Migrations(::diesel_migrations::MigrationError);
Diesel(::diesel::result::Error); Diesel(::diesel::result::Error);
Connection(::diesel::ConnectionError); Connection(::diesel::ConnectionError);
Io(::std::io::Error); Io(::std::io::Error);

View File

@ -1,6 +1,6 @@
use std::{sync::Arc, time::Duration}; use std::{sync::Arc, time::Duration};
use diesel::{connection::Connection, SqliteConnection}; use diesel::{connection::Connection, r2d2::R2D2Connection, SqliteConnection};
use r2d2::{CustomizeConnection, ManageConnection, Pool}; use r2d2::{CustomizeConnection, ManageConnection, Pool};
use scheduled_thread_pool::ScheduledThreadPool; use scheduled_thread_pool::ScheduledThreadPool;
@ -94,7 +94,7 @@ impl ManageConnection for ConnectionManager {
} }
fn is_valid(&self, conn: &mut Self::Connection) -> Result<()> { fn is_valid(&self, conn: &mut Self::Connection) -> Result<()> {
Ok(conn.execute("SELECT 1").map(|_| ())?) Ok(conn.ping()?)
} }
fn has_broken(&self, _conn: &mut Self::Connection) -> bool { fn has_broken(&self, _conn: &mut Self::Connection) -> bool {

View File

@ -9,7 +9,7 @@ use std::{
use diesel::{ use diesel::{
expression::SqlLiteral, expression::SqlLiteral,
query_dsl::load_dsl::LoadQuery, query_dsl::load_dsl::LoadQuery,
sql_types::{Integer, Text}, sql_types::{Integer, SingleValue, Text},
SqliteConnection, SqliteConnection,
}; };
@ -17,7 +17,12 @@ use crate::sqlite_impl::conn_ext::ConnectionExtension;
use crate::sqlite_impl::errors::{Error, Result}; use crate::sqlite_impl::errors::{Error, Result};
pub trait PragmaExtension: ConnectionExtension { pub trait PragmaExtension: ConnectionExtension {
fn pragma<D: std::fmt::Display>(&self, key: &str, val: D, schema: Option<&str>) -> Result<()> { fn pragma<D: std::fmt::Display>(
&mut self,
key: &str,
val: D,
schema: Option<&str>,
) -> Result<()> {
let query = match schema { let query = match schema {
Some(schema) => format!("PRAGMA {}.{} = '{}'", schema, key, val), Some(schema) => format!("PRAGMA {}.{} = '{}'", schema, key, val),
None => format!("PRAGMA {} = '{}'", key, val), None => format!("PRAGMA {} = '{}'", key, val),
@ -27,14 +32,15 @@ pub trait PragmaExtension: ConnectionExtension {
Ok(()) Ok(())
} }
fn pragma_ret<ST, T, D: std::fmt::Display>( fn pragma_ret<'query, ST, T, D: std::fmt::Display>(
&self, &mut self,
key: &str, key: &str,
val: D, val: D,
schema: Option<&str>, schema: Option<&str>,
) -> Result<T> ) -> Result<T>
where where
SqlLiteral<ST>: LoadQuery<SqliteConnection, T>, SqlLiteral<ST>: LoadQuery<'query, SqliteConnection, T>,
ST: SingleValue,
{ {
let query = match schema { let query = match schema {
Some(schema) => format!("PRAGMA {}.{} = '{}'", schema, key, val), Some(schema) => format!("PRAGMA {}.{} = '{}'", schema, key, val),
@ -44,9 +50,10 @@ pub trait PragmaExtension: ConnectionExtension {
self.query::<ST, T>(&query) self.query::<ST, T>(&query)
} }
fn pragma_get<ST, T>(&self, key: &str, schema: Option<&str>) -> Result<T> fn pragma_get<'query, ST, T>(&mut self, key: &str, schema: Option<&str>) -> Result<T>
where where
SqlLiteral<ST>: LoadQuery<SqliteConnection, T>, SqlLiteral<ST>: LoadQuery<'query, SqliteConnection, T>,
ST: SingleValue,
{ {
let query = match schema { let query = match schema {
Some(schema) => format!("PRAGMA {}.{}", schema, key), Some(schema) => format!("PRAGMA {}.{}", schema, key),
@ -56,33 +63,37 @@ pub trait PragmaExtension: ConnectionExtension {
self.query::<ST, T>(&query) self.query::<ST, T>(&query)
} }
fn pragma_set_busy_timeout(&self, timeout_ms: i32) -> Result<i32> { fn pragma_set_busy_timeout(&mut self, timeout_ms: i32) -> Result<i32> {
self.pragma_ret::<Integer, i32, i32>("busy_timeout", timeout_ms, None) self.pragma_ret::<Integer, i32, i32>("busy_timeout", timeout_ms, None)
} }
fn pragma_get_busy_timeout(&self) -> Result<i32> { fn pragma_get_busy_timeout(&mut self) -> Result<i32> {
self.pragma_get::<Integer, i32>("busy_timeout", None) self.pragma_get::<Integer, i32>("busy_timeout", None)
} }
fn pragma_set_journal_mode(&self, mode: SQLiteJournalMode, schema: Option<&str>) -> Result<i32> { fn pragma_set_journal_mode(
&mut self,
mode: SQLiteJournalMode,
schema: Option<&str>,
) -> Result<i32> {
self.pragma_ret::<Integer, i32, SQLiteJournalMode>("journal_mode", mode, schema) self.pragma_ret::<Integer, i32, SQLiteJournalMode>("journal_mode", mode, schema)
} }
fn pragma_get_journal_mode(&self, schema: Option<&str>) -> Result<SQLiteJournalMode> { fn pragma_get_journal_mode(&mut self, schema: Option<&str>) -> Result<SQLiteJournalMode> {
self self
.pragma_get::<Text, String>("journal_mode", schema)? .pragma_get::<Text, String>("journal_mode", schema)?
.parse() .parse()
} }
fn pragma_set_synchronous( fn pragma_set_synchronous(
&self, &mut self,
synchronous: SQLiteSynchronous, synchronous: SQLiteSynchronous,
schema: Option<&str>, schema: Option<&str>,
) -> Result<()> { ) -> Result<()> {
self.pragma("synchronous", synchronous as u8, schema) self.pragma("synchronous", synchronous as u8, schema)
} }
fn pragma_get_synchronous(&self, schema: Option<&str>) -> Result<SQLiteSynchronous> { fn pragma_get_synchronous(&mut self, schema: Option<&str>) -> Result<SQLiteSynchronous> {
self self
.pragma_get::<Integer, i32>("synchronous", schema)? .pragma_get::<Integer, i32>("synchronous", schema)?
.try_into() .try_into()

View File

@ -32,7 +32,7 @@ serde_repr.workspace = true
protobuf.workspace = true protobuf.workspace = true
lazy_static = "1.4.0" lazy_static = "1.4.0"
diesel.workspace = true diesel.workspace = true
diesel_derives = { version = "1.4.1", features = ["sqlite"] } diesel_derives = { version = "2.1.0", features = ["sqlite", "r2d2"] }
once_cell = "1.17.1" once_cell = "1.17.1"
parking_lot.workspace = true parking_lot.workspace = true
strum = "0.25" strum = "0.25"

View File

@ -533,7 +533,7 @@ impl UserManager {
pub async fn get_user_profile_from_disk(&self, uid: i64) -> Result<UserProfile, FlowyError> { pub async fn get_user_profile_from_disk(&self, uid: i64) -> Result<UserProfile, FlowyError> {
let user: UserProfile = user_table::dsl::user_table let user: UserProfile = user_table::dsl::user_table
.filter(user_table::id.eq(&uid.to_string())) .filter(user_table::id.eq(&uid.to_string()))
.first::<UserTable>(&*(self.db_connection(uid)?)) .first::<UserTable>(&mut *(self.db_connection(uid)?))
.map_err(|err| { .map_err(|err| {
FlowyError::record_not_found().with_context(format!( FlowyError::record_not_found().with_context(format!(
"Can't find the user profile for user id: {}, error: {:?}", "Can't find the user profile for user id: {}, error: {:?}",
@ -639,15 +639,15 @@ impl UserManager {
} }
async fn save_user(&self, uid: i64, user: UserTable) -> Result<(), FlowyError> { async fn save_user(&self, uid: i64, user: UserTable) -> Result<(), FlowyError> {
let conn = self.db_connection(uid)?; let mut conn = self.db_connection(uid)?;
conn.immediate_transaction(|| { conn.immediate_transaction(|conn| {
// delete old user if exists // delete old user if exists
diesel::delete(user_table::dsl::user_table.filter(user_table::dsl::id.eq(&user.id))) diesel::delete(user_table::dsl::user_table.filter(user_table::dsl::id.eq(&user.id)))
.execute(&*conn)?; .execute(conn)?;
let _ = diesel::insert_into(user_table::table) let _ = diesel::insert_into(user_table::table)
.values(user) .values(user)
.execute(&*conn)?; .execute(conn)?;
Ok::<(), FlowyError>(()) Ok::<(), FlowyError>(())
})?; })?;
@ -858,11 +858,11 @@ fn upsert_user_profile_change(
"Update user profile with changeset: {:?}", "Update user profile with changeset: {:?}",
changeset changeset
); );
let conn = pool.get()?; let mut conn = pool.get()?;
diesel_update_table!(user_table, changeset, &*conn); diesel_update_table!(user_table, changeset, &mut *conn);
let user: UserProfile = user_table::dsl::user_table let user: UserProfile = 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>(&mut *conn)?
.into(); .into();
send_notification(&uid.to_string(), UserNotification::DidUpdateUserProfile) send_notification(&uid.to_string(), UserNotification::DidUpdateUserProfile)
.payload(UserProfilePB::from(user)) .payload(UserProfilePB::from(user))

View File

@ -49,8 +49,8 @@ impl UserLocalDataMigration {
authenticator: &Authenticator, authenticator: &Authenticator,
) -> FlowyResult<Vec<String>> { ) -> FlowyResult<Vec<String>> {
let mut applied_migrations = vec![]; let mut applied_migrations = vec![];
let conn = self.sqlite_pool.get()?; let mut conn = self.sqlite_pool.get()?;
let record = get_all_records(&conn)?; let record = get_all_records(&mut conn)?;
let mut duplicated_names = vec![]; let mut duplicated_names = vec![];
for migration in migrations { for migration in migrations {
if !record if !record
@ -61,7 +61,7 @@ impl UserLocalDataMigration {
if !duplicated_names.contains(&migration_name) { if !duplicated_names.contains(&migration_name) {
migration.run(&self.session, &self.collab_db, authenticator)?; migration.run(&self.session, &self.collab_db, authenticator)?;
applied_migrations.push(migration.name().to_string()); applied_migrations.push(migration.name().to_string());
save_record(&conn, &migration_name); save_record(&mut conn, &migration_name);
duplicated_names.push(migration_name); duplicated_names.push(migration_name);
} else { } else {
tracing::error!("Duplicated migration name: {}", migration_name); tracing::error!("Duplicated migration name: {}", migration_name);
@ -83,7 +83,7 @@ pub trait UserDataMigration {
) -> FlowyResult<()>; ) -> FlowyResult<()>;
} }
fn save_record(conn: &SqliteConnection, migration_name: &str) { fn save_record(conn: &mut SqliteConnection, migration_name: &str) {
let new_record = NewUserDataMigrationRecord { let new_record = NewUserDataMigrationRecord {
migration_name: migration_name.to_string(), migration_name: migration_name.to_string(),
}; };
@ -93,7 +93,7 @@ fn save_record(conn: &SqliteConnection, migration_name: &str) {
.expect("Error inserting new migration record"); .expect("Error inserting new migration record");
} }
fn get_all_records(conn: &SqliteConnection) -> FlowyResult<Vec<UserDataMigrationRecord>> { fn get_all_records(conn: &mut SqliteConnection) -> FlowyResult<Vec<UserDataMigrationRecord>> {
Ok( Ok(
user_data_migration_records::table user_data_migration_records::table
.load::<UserDataMigrationRecord>(conn) .load::<UserDataMigrationRecord>(conn)
@ -102,7 +102,7 @@ fn get_all_records(conn: &SqliteConnection) -> FlowyResult<Vec<UserDataMigration
} }
#[derive(Clone, Default, Queryable, Identifiable)] #[derive(Clone, Default, Queryable, Identifiable)]
#[table_name = "user_data_migration_records"] #[diesel(table_name = user_data_migration_records)]
pub struct UserDataMigrationRecord { pub struct UserDataMigrationRecord {
pub id: i32, pub id: i32,
pub migration_name: String, pub migration_name: String,
@ -110,7 +110,7 @@ pub struct UserDataMigrationRecord {
} }
#[derive(Insertable)] #[derive(Insertable)]
#[table_name = "user_data_migration_records"] #[diesel(table_name = user_data_migration_records)]
pub struct NewUserDataMigrationRecord { pub struct NewUserDataMigrationRecord {
pub migration_name: String, pub migration_name: String,
} }

View File

@ -148,10 +148,10 @@ pub fn open_user_db(
pub fn get_user_profile(pool: &Arc<ConnectionPool>, uid: i64) -> Result<UserProfile, FlowyError> { pub fn get_user_profile(pool: &Arc<ConnectionPool>, uid: i64) -> Result<UserProfile, FlowyError> {
let uid = uid.to_string(); let uid = uid.to_string();
let conn = pool.get()?; let mut conn = pool.get()?;
let user = dsl::user_table let user = dsl::user_table
.filter(user_table::id.eq(&uid)) .filter(user_table::id.eq(&uid))
.first::<UserTable>(&*conn)?; .first::<UserTable>(&mut *conn)?;
Ok(user.into()) Ok(user.into())
} }
@ -160,10 +160,10 @@ pub fn get_user_workspace(
pool: &Arc<ConnectionPool>, pool: &Arc<ConnectionPool>,
uid: i64, uid: i64,
) -> Result<Option<UserWorkspace>, FlowyError> { ) -> Result<Option<UserWorkspace>, FlowyError> {
let conn = pool.get()?; let mut conn = pool.get()?;
let row = user_workspace_table::dsl::user_workspace_table let row = user_workspace_table::dsl::user_workspace_table
.filter(user_workspace_table::uid.eq(uid)) .filter(user_workspace_table::uid.eq(uid))
.first::<UserWorkspaceTable>(&*conn)?; .first::<UserWorkspaceTable>(&mut *conn)?;
Ok(Some(UserWorkspace::from(row))) Ok(Some(UserWorkspace::from(row)))
} }

View File

@ -7,7 +7,7 @@ use flowy_user_deps::entities::*;
/// 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.
#[derive(Clone, Default, Queryable, Identifiable, Insertable)] #[derive(Clone, Default, Queryable, Identifiable, Insertable)]
#[table_name = "user_table"] #[diesel(table_name = user_table)]
pub struct UserTable { pub struct UserTable {
pub(crate) id: String, pub(crate) id: String,
pub(crate) name: String, pub(crate) name: String,
@ -68,7 +68,7 @@ impl From<UserTable> for UserProfile {
} }
#[derive(AsChangeset, Identifiable, Default, Debug)] #[derive(AsChangeset, Identifiable, Default, Debug)]
#[table_name = "user_table"] #[diesel(table_name = user_table)]
pub struct UserTableChangeset { pub struct UserTableChangeset {
pub id: String, pub id: String,
pub workspace: Option<String>, // deprecated pub workspace: Option<String>, // deprecated

View File

@ -91,19 +91,19 @@ impl UserManager {
} }
pub fn get_user_workspace(&self, uid: i64, workspace_id: &str) -> Option<UserWorkspace> { pub fn get_user_workspace(&self, uid: i64, workspace_id: &str) -> Option<UserWorkspace> {
let conn = self.db_connection(uid).ok()?; let mut conn = self.db_connection(uid).ok()?;
let row = user_workspace_table::dsl::user_workspace_table let row = user_workspace_table::dsl::user_workspace_table
.filter(user_workspace_table::id.eq(workspace_id)) .filter(user_workspace_table::id.eq(workspace_id))
.first::<UserWorkspaceTable>(&*conn) .first::<UserWorkspaceTable>(&mut *conn)
.ok()?; .ok()?;
Some(UserWorkspace::from(row)) Some(UserWorkspace::from(row))
} }
pub fn get_all_user_workspaces(&self, uid: i64) -> FlowyResult<Vec<UserWorkspace>> { pub fn get_all_user_workspaces(&self, uid: i64) -> FlowyResult<Vec<UserWorkspace>> {
let conn = self.db_connection(uid)?; let mut conn = self.db_connection(uid)?;
let rows = user_workspace_table::dsl::user_workspace_table let rows = user_workspace_table::dsl::user_workspace_table
.filter(user_workspace_table::uid.eq(uid)) .filter(user_workspace_table::uid.eq(uid))
.load::<UserWorkspaceTable>(&*conn)?; .load::<UserWorkspaceTable>(&mut *conn)?;
if let Ok(service) = self.cloud_services.get_user_service() { if let Ok(service) = self.cloud_services.get_user_service() {
if let Ok(pool) = self.db_pool(uid) { if let Ok(pool) = self.db_pool(uid) {
@ -150,8 +150,8 @@ pub fn save_user_workspaces(
.flat_map(|user_workspace| UserWorkspaceTable::try_from((uid, user_workspace)).ok()) .flat_map(|user_workspace| UserWorkspaceTable::try_from((uid, user_workspace)).ok())
.collect::<Vec<UserWorkspaceTable>>(); .collect::<Vec<UserWorkspaceTable>>();
let conn = pool.get()?; let mut conn = pool.get()?;
conn.immediate_transaction(|| { conn.immediate_transaction(|conn| {
for user_workspace in user_workspaces { for user_workspace in user_workspaces {
if let Err(err) = diesel::update( if let Err(err) = diesel::update(
user_workspace_table::dsl::user_workspace_table user_workspace_table::dsl::user_workspace_table
@ -162,12 +162,12 @@ pub fn save_user_workspaces(
user_workspace_table::created_at.eq(&user_workspace.created_at), user_workspace_table::created_at.eq(&user_workspace.created_at),
user_workspace_table::database_storage_id.eq(&user_workspace.database_storage_id), user_workspace_table::database_storage_id.eq(&user_workspace.database_storage_id),
)) ))
.execute(&*conn) .execute(conn)
.and_then(|rows| { .and_then(|rows| {
if rows == 0 { if rows == 0 {
let _ = diesel::insert_into(user_workspace_table::table) let _ = diesel::insert_into(user_workspace_table::table)
.values(user_workspace) .values(user_workspace)
.execute(&*conn)?; .execute(conn)?;
} }
Ok(()) Ok(())
}) { }) {

View File

@ -7,7 +7,7 @@ use flowy_sqlite::schema::user_workspace_table;
use flowy_user_deps::entities::UserWorkspace; use flowy_user_deps::entities::UserWorkspace;
#[derive(Clone, Default, Queryable, Identifiable, Insertable)] #[derive(Clone, Default, Queryable, Identifiable, Insertable)]
#[table_name = "user_workspace_table"] #[diesel(table_name = user_workspace_table)]
pub struct UserWorkspaceTable { pub struct UserWorkspaceTable {
pub id: String, pub id: String,
pub name: String, pub name: String,