Skip to content

Commit e3d256d

Browse files
committed
1 parent cd2136e commit e3d256d

File tree

3 files changed

+35
-25
lines changed

3 files changed

+35
-25
lines changed

Cargo.toml

+3-2
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ redis = { version = "0.24", features = [
2828
] }
2929
serde = "1.0"
3030
serde_json = "1.0"
31-
sqlx = { version = "0.6", features = [
32-
"runtime-tokio-native-tls",
31+
sqlx = { version = "0.7", features = [
32+
"runtime-tokio-rustls",
3333
"any",
3434
"sqlite",
3535
"mysql",
@@ -47,6 +47,7 @@ tokio = { version = "1", features = [
4747
"parking_lot",
4848
] }
4949
tracing = "0.1"
50+
url = "2"
5051
unicode-normalization = "0.1"
5152

5253
[dev-dependencies]

src/infra.rs

+2
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ pub mod test {
4242
});
4343

4444
pub async fn setup_test_sqlite<T: Into<String>>(dir: T) -> &'static Pool<Any> {
45+
sqlx::any::install_default_drivers();
4546
SQLITE_INIT
4647
.get_or_init(|| async {
4748
_setup_sqlite_internal(dir)
@@ -62,6 +63,7 @@ pub mod test {
6263
}
6364

6465
pub async fn setup_test_mysql<T: Into<String>>(dir: T) -> &'static Pool<Any> {
66+
sqlx::any::install_default_drivers();
6567
MYSQL_INIT
6668
.get_or_init(|| async {
6769
let pool = crate::infra::rdb::new_rdb_pool(&MYSQL_CONFIG, None)

src/infra/rdb.rs

+30-23
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
1-
use std::{path::Path, time::Duration};
1+
use std::time::Duration;
22

33
use anyhow::{anyhow, Context, Result};
44
use debug_stub_derive::DebugStub;
55
use futures::StreamExt;
66
use log::LevelFilter;
77
use serde::Deserialize;
88
use sqlx::{
9-
any::AnyPoolOptions,
9+
any::{AnyConnectOptions, AnyPoolOptions},
1010
migrate::MigrateDatabase,
11-
mysql::MySqlConnectOptions,
12-
sqlite::{SqliteAutoVacuum, SqliteConnectOptions},
1311
Any, ConnectOptions, Pool,
1412
};
1513

@@ -79,16 +77,20 @@ pub async fn new_sqlite_pool(
7977
if !sqlx::Sqlite::database_exists(&config.sqlite_url()).await? {
8078
sqlx::Sqlite::create_database(&config.sqlite_url()).await?;
8179
}
82-
// TODO config
83-
let mut options = SqliteConnectOptions::new()
84-
.auto_vacuum(SqliteAutoVacuum::Incremental)
85-
.filename(Path::new(&config.dbname));
86-
options.log_statements(LevelFilter::Debug);
87-
options.log_slow_statements(LevelFilter::Warn, Duration::from_secs(1));
80+
// from sqlx 0.7, auto_vacuum is not supported
81+
// ref. https://github.com/launchbadge/sqlx/issues/2773
82+
//
83+
// let mut options = SqliteConnectOptions::new()
84+
// .auto_vacuum(SqliteAutoVacuum::Incremental)
85+
// .filename(Path::new(&config.dbname));
86+
let options = AnyConnectOptions::from_url(&url::Url::parse(&config.sqlite_url())?)?
87+
.log_statements(LevelFilter::Debug)
88+
.log_slow_statements(LevelFilter::Warn, Duration::from_secs(1));
89+
8890
let pr = AnyPoolOptions::new()
8991
.max_connections(config.max_connections)
9092
//.min_connections(3)
91-
.connect_with(options.into())
93+
.connect_with(options)
9294
.await
9395
.context(format!(
9496
"cannot initialize sql connection. url:{:?}",
@@ -119,17 +121,22 @@ async fn setup_sqlite(p: &Pool<Any>, init_schema: Option<&String>) -> Result<()>
119121
}
120122

121123
pub async fn new_mysql_pool(config: &RDBConfig) -> Result<Pool<Any>> {
122-
let port = config.port.parse::<u16>()?;
123-
let mut options = MySqlConnectOptions::new()
124-
.host(&config.host)
125-
.port(port)
126-
.username(&config.user)
127-
.password(&config.password)
128-
.database(&config.dbname)
129-
.charset("utf8mb4")
130-
.statement_cache_capacity(2048); // TODO setting
131-
options.log_statements(LevelFilter::Debug);
132-
options.log_slow_statements(LevelFilter::Warn, Duration::from_secs(1));
124+
// let port = config.port.parse::<u16>()?;
125+
// from sqlx 0.7, mysql connection options not used (statement_cache_capacity)
126+
// ref. https://github.com/launchbadge/sqlx/issues/2773
127+
//
128+
// let mut options = MySqlConnectOptions::new()
129+
// .host(&config.host)
130+
// .port(port)
131+
// .username(&config.user)
132+
// .password(&config.password)
133+
// .database(&config.dbname)
134+
// .charset("utf8mb4")
135+
// .statement_cache_capacity(2048); // TODO setting
136+
137+
let options = AnyConnectOptions::from_url(&url::Url::parse(&config.mysql_url())?)?
138+
.log_statements(LevelFilter::Debug)
139+
.log_slow_statements(LevelFilter::Warn, Duration::from_secs(1));
133140

134141
// TODO set from config
135142
AnyPoolOptions::new()
@@ -139,7 +146,7 @@ pub async fn new_mysql_pool(config: &RDBConfig) -> Result<Pool<Any>> {
139146
.test_before_acquire(true)
140147
.max_connections(config.max_connections)
141148
.min_connections(config.max_connections / 10 + 1)
142-
.connect_with(options.into())
149+
.connect_with(options)
143150
// .connect(&config.mysql_url())
144151
.await
145152
.context(format!(

0 commit comments

Comments
 (0)