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
20 changed files with 218 additions and 122 deletions

View File

@ -7,8 +7,8 @@ edition = "2018"
[dependencies]
diesel.workspace = true
diesel_derives = { version = "1.4.1", features = ["sqlite"] }
diesel_migrations = { version = "1.4.0", features = ["sqlite"] }
diesel_derives = { version = "2.1.0", features = ["sqlite", "r2d2"] }
diesel_migrations = { version = "2.1.0", features = ["sqlite"] }
tracing.workspace = true
lazy_static = "1.4.0"
serde.workspace = true
@ -16,8 +16,8 @@ serde_json.workspace = true
anyhow.workspace = true
parking_lot.workspace = true
r2d2 = "0.8.10"
libsqlite3-sys = { version = ">=0.8.0, <0.24.0", features = ["bundled"] }
r2d2 = ">= 0.8.2, < 0.9.0"
libsqlite3-sys = { version = ">=0.17.2, <0.28.0", features = ["bundled"] }
scheduled-thread-pool = "0.2.6"
error-chain = "=0.12.0"
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 anyhow::anyhow;
use diesel::{Connection, SqliteConnection};
use diesel::sql_query;
use serde::de::DeserializeOwned;
use serde::Serialize;
@ -25,8 +25,8 @@ impl StorePreferences {
let pool_config = PoolConfig::default();
let database = Database::new(root, DB_NAME, pool_config).unwrap();
let conn = database.get_connection().unwrap();
SqliteConnection::execute(&*conn, KV_SQL).unwrap();
let mut conn = database.get_connection().unwrap();
sql_query(KV_SQL).execute(&mut conn).unwrap();
tracing::trace!("Init StorePreferences with path: {}", root);
Ok(Self {
@ -86,13 +86,13 @@ impl StorePreferences {
}
pub fn remove(&self, key: &str) {
if let Some(conn) = self
if let Some(mut conn) = self
.database
.as_ref()
.and_then(|database| database.get_connection().ok())
{
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())
{
None => Err(anyhow!("StorePreferences is not initialized")),
Some(conn) => {
Some(mut conn) => {
diesel::replace_into(kv_table::table)
.values(KeyValue {
key: key.to_string(),
value,
})
.execute(&*conn)?;
.execute(&mut *conn)?;
Ok(())
},
}
}
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
.filter(kv_table::key.eq(key))
.first::<KeyValue>(&*conn)
.first::<KeyValue>(&mut *conn)
.ok()
}
}
#[derive(Clone, Debug, Default, Queryable, Identifiable, Insertable, AsChangeset)]
#[table_name = "kv_table"]
#[primary_key(key)]
#[diesel(table_name = kv_table)]
#[diesel(primary_key(key))]
pub struct KeyValue {
pub key: String,
pub value: Option<String>,

View File

@ -9,6 +9,7 @@ use std::{fmt::Debug, io, path::Path};
pub use diesel::*;
pub use diesel_derives::*;
use diesel_migrations::{EmbeddedMigrations, MigrationHarness};
use crate::sqlite_impl::PoolConfig;
pub use crate::sqlite_impl::{ConnectionPool, DBConnection, Database};
@ -29,7 +30,7 @@ pub mod prelude {
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 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 database = Database::new(storage_path, DB_NAME, pool_config).map_err(as_io_error)?;
let conn = database.get_connection().map_err(as_io_error)?;
embedded_migrations::run(&*conn).map_err(as_io_error)?;
let mut conn = database.get_connection().map_err(as_io_error)?;
(*conn)
.run_pending_migrations(MIGRATIONS)
.map_err(|e| io::Error::new(io::ErrorKind::Other, format!("{:?}", e)))?;
Ok(database)
}

View File

@ -1,26 +1,29 @@
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::*;
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
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 {
fn query<ST, T>(&self, query: &str) -> Result<T>
fn query<'query, ST, T>(&mut self, query: &str) -> Result<T>
where
SqlLiteral<ST>: LoadQuery<SqliteConnection, T>,
SqlLiteral<ST>: LoadQuery<'query, SqliteConnection, T>,
ST: SingleValue,
{
Ok(sql::<ST>(query).get_result(self)?)
}
fn exec(&self, query: impl AsRef<str>) -> Result<usize> {
Ok(SqliteConnection::execute(self, query.as_ref())?)
fn exec(&mut self, query: impl AsRef<str>) -> Result<usize> {
Ok(sql_query(query.as_ref()).execute(self)?)
}
}

View File

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

View File

@ -1,6 +1,6 @@
use std::{sync::Arc, time::Duration};
use diesel::{connection::Connection, SqliteConnection};
use diesel::{connection::Connection, r2d2::R2D2Connection, SqliteConnection};
use r2d2::{CustomizeConnection, ManageConnection, Pool};
use scheduled_thread_pool::ScheduledThreadPool;
@ -94,7 +94,7 @@ impl ManageConnection for ConnectionManager {
}
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 {

View File

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