Skip to content

Commit 3f01d07

Browse files
committed
feat(database): 禁用 SQLite 外键约束以提升兼容性
- 在建立 SQLite 数据库连接后执行 PRAGMA foreign_keys = OFF
1 parent 53f16a5 commit 3f01d07

2 files changed

Lines changed: 38 additions & 6 deletions

File tree

file_classification_core/src/utils/database.rs

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
//! 并封装了通用的数据库连接类型。
66
77
pub use diesel::{Connection, QueryResult};
8-
8+
use diesel::RunQueryDsl;
99
/// 通用数据库连接枚举
1010
///
1111
/// 使用 `diesel::MultiConnection` 宏定义的枚举类型,支持多种数据库连接类型。
@@ -31,10 +31,19 @@ pub enum AnyConnection {
3131
pub fn establish_connection(database_url: &str, database_type: &str) -> AnyConnection {
3232
// 根据数据库类型建立相应的连接
3333
match database_type {
34-
"sqlite" => AnyConnection::Sqlite(
35-
diesel::SqliteConnection::establish(database_url)
36-
.unwrap_or_else(|_| panic!("Error connecting to {}", database_url)),
37-
),
34+
"sqlite" => {
35+
let mut conn = AnyConnection::Sqlite(
36+
diesel::SqliteConnection::establish(database_url)
37+
.unwrap_or_else(|_| panic!("Error connecting to {}", database_url)),
38+
);
39+
40+
// 关闭外键约束检查
41+
diesel::sql_query("PRAGMA foreign_keys = OFF")
42+
.execute(&mut conn)
43+
.expect("Error executing PRAGMA foreign_keys = OFF");
44+
45+
conn
46+
},
3847
// 不支持的数据库类型直接 panic
3948
_ => panic!("Unsupported database type: {}", database_type),
4049
}

file_classification_webapi/src/bin/utils/database.rs

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,32 @@ pub use diesel::r2d2::{ConnectionManager, Pool, PooledConnection};
22
use file_classification_common::env_loader::load_env_file;
33
use file_classification_core::utils::database::AnyConnection;
44
use std::env;
5+
use diesel::RunQueryDsl;
56

67
// 定义连接池类型
78
pub type DbPool = Pool<ConnectionManager<AnyConnection>>;
89
// 定义池化连接类型
910
pub type DbPooledConnection = PooledConnection<ConnectionManager<AnyConnection>>;
1011

12+
/// 自定义连接创建器,用于在创建连接后执行 PRAGMA 命令
13+
#[derive(Debug)]
14+
struct ConnectionCreator;
15+
16+
impl diesel::r2d2::CustomizeConnection<AnyConnection, diesel::r2d2::Error> for ConnectionCreator {
17+
fn on_acquire(&self, conn: &mut AnyConnection) -> Result<(), diesel::r2d2::Error> {
18+
// 只有 SQLite 连接才这样关闭外键约束
19+
match conn {
20+
AnyConnection::Sqlite(_) => {
21+
// 关闭外键约束检查
22+
diesel::sql_query("PRAGMA foreign_keys = OFF")
23+
.execute(conn)
24+
.map_err(diesel::r2d2::Error::QueryError)?;
25+
}
26+
}
27+
Ok(())
28+
}
29+
}
30+
1131
pub fn establish_connection_pool() -> DbPool {
1232
// 加载环境变量文件
1333
if let Err(e) = load_env_file() {
@@ -23,5 +43,8 @@ pub fn establish_connection_pool() -> DbPool {
2343
_ => panic!("Unsupported database type: {}", database_type),
2444
};
2545

26-
Pool::builder().build(manager).expect("Failed to create pool.")
46+
Pool::builder()
47+
.connection_customizer(Box::new(ConnectionCreator))
48+
.build(manager)
49+
.expect("Failed to create pool.")
2750
}

0 commit comments

Comments
 (0)