Skip to content

Commit e906473

Browse files
committed
feat(database): 切换默认数据库为PostgreSQL并优化连接配置
- 修改默认数据库配置为PostgreSQL - 在MySQL和PostgreSQL连接中添加外键约束检查关闭逻辑 - 为WebAPI模块添加数据库连接初始化逻辑 - 支持MySQL和PostgreSQL的外键约束动态关闭功能
1 parent 184eb74 commit e906473

3 files changed

Lines changed: 40 additions & 6 deletions

File tree

.file_classification_env

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,14 @@
66
#DATABASE_URL=file_classification.db
77
#DATABASE_TYPE=sqlite
88

9-
DATABASE_URL=mysql://root:@localhost:3306/file_classification
10-
DATABASE_TYPE=mysql
9+
#DATABASE_URL=postgres:mypassword//postgres:@localhost:5432/file_classification
10+
#DATABASE_TYPE=postgres
11+
12+
#DATABASE_URL=mysql://root:@localhost:3306/file_classification
13+
#DATABASE_TYPE=mysql
14+
15+
DATABASE_URL=postgres://postgres:@localhost:5432/file_classification
16+
DATABASE_TYPE=postgres
1117

1218
# Web API 配置
1319
BIND_ADDRESS=127.0.0.1

file_classification_core/src/utils/database.rs

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,18 +55,32 @@ pub fn establish_connection(database_url: &str, database_type: &str) -> AnyConne
5555

5656
#[cfg(feature = "mysql")]
5757
"mysql" => {
58-
AnyConnection::Mysql(
58+
let mut conn = AnyConnection::Mysql(
5959
diesel::MysqlConnection::establish(database_url)
6060
.unwrap_or_else(|_| panic!("Error connecting to {}", database_url)),
61-
)
61+
);
62+
63+
// 关闭外键约束检查
64+
diesel::sql_query("SET FOREIGN_KEY_CHECKS = 0")
65+
.execute(&mut conn)
66+
.expect("Error executing SET FOREIGN_KEY_CHECKS = 0");
67+
68+
conn
6269
},
6370

6471
#[cfg(feature = "postgres")]
6572
"postgres" => {
66-
AnyConnection::Postgresql(
73+
let mut conn = AnyConnection::Postgresql(
6774
diesel::PgConnection::establish(database_url)
6875
.unwrap_or_else(|_| panic!("Error connecting to {}", database_url)),
69-
)
76+
);
77+
78+
// 关闭外键约束检查
79+
diesel::sql_query("SET session_replication_role = 'replica'")
80+
.execute(&mut conn)
81+
.expect("Error executing SET session_replication_role = 'replica'");
82+
83+
conn
7084
},
7185

7286
// 不支持的数据库类型直接 panic

file_classification_webapi/src/bin/utils/database.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,20 @@ impl diesel::r2d2::CustomizeConnection<AnyConnection, diesel::r2d2::Error> for C
2424
.execute(conn)
2525
.map_err(diesel::r2d2::Error::QueryError)?;
2626
}
27+
#[cfg(feature = "mysql")]
28+
// MySQL 连接关闭外键约束检查
29+
AnyConnection::Mysql(_) => {
30+
diesel::sql_query("SET FOREIGN_KEY_CHECKS = 0")
31+
.execute(conn)
32+
.map_err(diesel::r2d2::Error::QueryError)?;
33+
}
34+
#[cfg(feature = "postgres")]
35+
// PostgreSQL 连接关闭外键约束检查
36+
AnyConnection::Postgresql(_) => {
37+
diesel::sql_query("SET session_replication_role = 'replica'")
38+
.execute(conn)
39+
.map_err(diesel::r2d2::Error::QueryError)?;
40+
}
2741
_ => {}
2842
}
2943
Ok(())

0 commit comments

Comments
 (0)