Skip to content

Commit b0f9fe8

Browse files
committed
sqldb: move from AnyPool to specific pools
Signed-off-by: Yuki Kishimoto <[email protected]>
1 parent dd4cd8b commit b0f9fe8

File tree

2 files changed

+34
-33
lines changed

2 files changed

+34
-33
lines changed

database/nostr-sqldb/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ mysql = ["sqlx/mysql", "sqlx/tls-rustls-ring-webpki"]
2727
[dependencies]
2828
nostr = { workspace = true, features = ["std"] }
2929
nostr-database = { workspace = true, features = ["flatbuf"] }
30-
sqlx = { version = "0.8", features = ["any", "migrate", "runtime-tokio"] }
30+
sqlx = { version = "0.8", features = ["migrate", "runtime-tokio"] }
3131
tokio = { workspace = true, features = ["sync"] }
3232

3333
[dev-dependencies]

database/nostr-sqldb/src/db.rs

Lines changed: 33 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,12 @@ use std::path::{Path, PathBuf};
1010
use std::sync::Arc;
1111

1212
#[cfg(feature = "sqlite")]
13-
use sqlx::sqlite::Sqlite;
14-
use sqlx::{AnyPool, Any, Transaction, QueryBuilder};
15-
use sqlx::migrate::MigrateDatabase;
13+
use sqlx::sqlite::{SqliteConnectOptions, SqlitePool};
14+
#[cfg(feature = "postgres")]
15+
use sqlx::postgres::{PgConnectOptions, PgPool};
16+
#[cfg(feature = "mysql")]
17+
use sqlx::mysql::{MySql, MySqlPool};
18+
use sqlx::{Transaction, QueryBuilder};
1619
use nostr_database::prelude::*;
1720
use tokio::sync::Mutex;
1821

@@ -66,75 +69,73 @@ impl NostrSqlBackend {
6669
}
6770
}
6871

69-
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
70-
enum PoolKind {
72+
#[derive(Debug, Clone)]
73+
enum Db {
7174
#[cfg(feature = "sqlite")]
72-
Sqlite,
75+
Sqlite(SqlitePool),
7376
#[cfg(feature = "postgres")]
74-
Postgres,
77+
Postgres(PgPool),
7578
#[cfg(feature = "mysql")]
76-
MySql,
79+
MySql(MySqlPool),
7780
}
7881

7982
/// Nostr SQL database
8083
#[derive(Clone)]
8184
pub struct NostrSql {
82-
pool: AnyPool,
83-
kind: PoolKind,
85+
pool: Db,
8486
fbb: Arc<Mutex<FlatBufferBuilder<'static>>>,
8587
}
8688

8789
impl fmt::Debug for NostrSql {
8890
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
8991
f.debug_struct("NostrSql")
9092
.field("pool", &self.pool)
91-
.field("kind", &self.kind)
9293
.finish()
9394
}
9495
}
9596

9697
impl NostrSql {
9798
/// Connect to a SQL database
9899
pub async fn new(backend: NostrSqlBackend) -> Result<Self, Error> {
99-
// Install drivers
100-
sqlx::any::install_default_drivers();
101-
102-
let (pool, kind) = match backend {
100+
let pool = match backend {
103101
#[cfg(feature = "sqlite")]
104102
NostrSqlBackend::Sqlite {path} => {
105-
let uri: Cow<str> = match path {
106-
Some(path) => Cow::Owned(format!("sqlite://{}", path.display())),
107-
None => Cow::Borrowed("sqlite:memory:"),
103+
let mut opts: SqliteConnectOptions = SqliteConnectOptions::new().create_if_missing(true);
104+
105+
match path {
106+
Some(path) => opts = opts.filename(path),
107+
None => opts = opts.in_memory(true),
108108
};
109-
110-
if !Sqlite::database_exists(&uri).await? {
111-
Sqlite::create_database(&uri).await?;
112-
}
113-
114-
let pool: AnyPool = AnyPool::connect(&uri).await?;
109+
110+
111+
let pool: SqlitePool = SqlitePool::connect_with(opts).await?;
115112

116113
sqlx::migrate!("migrations/sqlite").run(&pool).await?;
117114

118-
(pool, PoolKind::Sqlite)
115+
Db::Sqlite(pool)
119116
}
120117
#[cfg(feature = "postgres")]
121118
NostrSqlBackend::Postgres {host, port, username, password, database } => {
122-
let uri: String = match (username, password) {
123-
(Some(username), Some(password)) => format!("postgres://{username}:{password}@{host}:{port}/{database}"),
124-
_ => format!("postgres://{host}:{port}/{database}")
125-
};
119+
let mut opts: PgConnectOptions = PgConnectOptions::new_without_pgpass().host(&host).port(port).database(&database);
120+
121+
if let Some(username) = username {
122+
opts = opts.username(&username);
123+
}
124+
125+
if let Some(password) = password {
126+
opts = opts.password(&password);
127+
}
126128

127-
let pool: AnyPool = AnyPool::connect(&uri).await?;
129+
let pool: PgPool = PgPool::connect_with(opts).await?;
128130

129131
sqlx::migrate!("migrations/postgres").run(&pool).await?;
130132

131-
(pool, PoolKind::Postgres)
133+
Db::Postgres(pool)
132134
}
133135
};
134136

135137
Ok(Self {
136138
pool,
137-
kind,
138139
fbb: Arc::new(Mutex::new(FlatBufferBuilder::new())),
139140
})
140141
}

0 commit comments

Comments
 (0)