1
- use std:: { path :: Path , time:: Duration } ;
1
+ use std:: time:: Duration ;
2
2
3
3
use anyhow:: { anyhow, Context , Result } ;
4
4
use debug_stub_derive:: DebugStub ;
5
5
use futures:: StreamExt ;
6
6
use log:: LevelFilter ;
7
7
use serde:: Deserialize ;
8
8
use sqlx:: {
9
- any:: AnyPoolOptions ,
9
+ any:: { AnyConnectOptions , AnyPoolOptions } ,
10
10
migrate:: MigrateDatabase ,
11
- mysql:: MySqlConnectOptions ,
12
- sqlite:: { SqliteAutoVacuum , SqliteConnectOptions } ,
13
11
Any , ConnectOptions , Pool ,
14
12
} ;
15
13
@@ -79,16 +77,20 @@ pub async fn new_sqlite_pool(
79
77
if !sqlx:: Sqlite :: database_exists ( & config. sqlite_url ( ) ) . await ? {
80
78
sqlx:: Sqlite :: create_database ( & config. sqlite_url ( ) ) . await ?;
81
79
}
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
+
88
90
let pr = AnyPoolOptions :: new ( )
89
91
. max_connections ( config. max_connections )
90
92
//.min_connections(3)
91
- . connect_with ( options. into ( ) )
93
+ . connect_with ( options)
92
94
. await
93
95
. context ( format ! (
94
96
"cannot initialize sql connection. url:{:?}" ,
@@ -119,17 +121,22 @@ async fn setup_sqlite(p: &Pool<Any>, init_schema: Option<&String>) -> Result<()>
119
121
}
120
122
121
123
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 ) ) ;
133
140
134
141
// TODO set from config
135
142
AnyPoolOptions :: new ( )
@@ -139,7 +146,7 @@ pub async fn new_mysql_pool(config: &RDBConfig) -> Result<Pool<Any>> {
139
146
. test_before_acquire ( true )
140
147
. max_connections ( config. max_connections )
141
148
. min_connections ( config. max_connections / 10 + 1 )
142
- . connect_with ( options. into ( ) )
149
+ . connect_with ( options)
143
150
// .connect(&config.mysql_url())
144
151
. await
145
152
. context ( format ! (
0 commit comments