forked from silvermine/tauri-plugin-sqlite
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.rs
More file actions
444 lines (404 loc) · 15.7 KB
/
database.rs
File metadata and controls
444 lines (404 loc) · 15.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
//! SQLite database with connection pooling and optional write access
use crate::Result;
use crate::config::SqliteDatabaseConfig;
use crate::error::Error;
use crate::registry::{get_or_open_database, is_memory_database, uncache_database};
use crate::write_guard::WriteGuard;
use sqlx::sqlite::{SqliteConnectOptions, SqlitePoolOptions};
use sqlx::{ConnectOptions, Pool, Sqlite};
use std::path::{Path, PathBuf};
use std::sync::Arc;
use std::sync::atomic::{AtomicBool, Ordering};
use tracing::{error, warn};
/// Analysis limit for PRAGMA optimize on close.
/// SQLite recommends 100-1000 for older versions; 3.46.0+ handles automatically.
/// See: https://www.sqlite.org/lang_analyze.html#recommended_usage_pattern
const OPTIMIZE_ANALYSIS_LIMIT: u32 = 400;
/// SQLite database with connection pooling for concurrent reads and optional exclusive writes.
///
/// Once the database is opened it can be used for read-only operations by calling `read_pool()`.
/// Write operations are available by calling `acquire_writer()` which lazily initializes WAL mode
/// on first use.
///
/// # Example
///
/// ```no_run
/// use sqlx_sqlite_conn_mgr::SqliteDatabase;
/// use std::sync::Arc;
///
/// # async fn example() -> Result<(), sqlx_sqlite_conn_mgr::Error> {
/// let db = SqliteDatabase::connect("test.db", None).await?;
///
/// // Use read_pool for SELECT queries (concurrent reads)
/// let rows = sqlx::query("SELECT * FROM users")
/// .fetch_all(db.read_pool()?)
/// .await?;
///
/// // Optionally acquire writer for INSERT/UPDATE/DELETE (exclusive)
/// let mut writer = db.acquire_writer().await?;
/// sqlx::query("INSERT INTO users (name) VALUES (?)")
/// .bind("Alice")
/// .execute(&mut *writer)
/// .await?;
///
/// db.close().await?;
/// # Ok(())
/// # }
/// ```
#[derive(Debug)]
pub struct SqliteDatabase {
/// Pool of read-only connections (defaults to max_connections=6) for concurrent reads
read_pool: Pool<Sqlite>,
/// Single read-write connection pool (max_connections=1) for serialized writes
write_conn: Pool<Sqlite>,
/// Tracks if WAL mode has been initialized (set on first write)
wal_initialized: AtomicBool,
/// Marks database as closed to prevent further operations
closed: AtomicBool,
/// Path to database file (used for cleanup and registry lookups)
path: PathBuf,
}
impl SqliteDatabase {
/// Get the database file path as a string
///
/// Used internally (crate-private) for ATTACH DATABASE statements
pub(crate) fn path_str(&self) -> String {
self.path.to_string_lossy().to_string()
}
/// Connect to a SQLite database
///
/// If the database is already connected, returns the existing connection.
/// Multiple calls with the same path will return the same database instance.
///
/// The database is created if it doesn't exist. WAL mode is enabled when
/// `acquire_writer()` is first called.
///
/// # Arguments
///
/// * `path` - Path to the SQLite database file (will be created if missing)
/// * `custom_config` - Optional custom configuration for connection pools.
/// Pass `None` to use defaults (6 max read connections, 30 second idle timeout).
/// Specify a custom configuration when the defaults don't meet your requirements.
///
/// # Examples
///
/// ```no_run
/// use sqlx_sqlite_conn_mgr::SqliteDatabase;
/// use std::sync::Arc;
///
/// # async fn example() -> Result<(), sqlx_sqlite_conn_mgr::Error> {
/// // Connect with default configuration (recommended for most use cases)
/// let db = SqliteDatabase::connect("test.db", None).await?;
/// # Ok(())
/// # }
/// ```
///
/// ```no_run
/// use sqlx_sqlite_conn_mgr::{SqliteDatabase, SqliteDatabaseConfig};
/// use std::sync::Arc;
///
/// # async fn example() -> Result<(), sqlx_sqlite_conn_mgr::Error> {
/// // Customize configuration when defaults don't meet your requirements
/// let custom_config = SqliteDatabaseConfig {
/// max_read_connections: 10,
/// idle_timeout_secs: 60,
/// };
/// let db = SqliteDatabase::connect("test.db", Some(custom_config)).await?;
/// # Ok(())
/// # }
/// ```
pub async fn connect(
path: impl AsRef<Path>,
custom_config: Option<SqliteDatabaseConfig>,
) -> Result<Arc<Self>> {
let config = custom_config.unwrap_or_default();
let path = path.as_ref();
// Validate path is not empty
if path.as_os_str().is_empty() {
return Err(crate::error::Error::Io(std::io::Error::new(
std::io::ErrorKind::InvalidInput,
"Database path cannot be empty",
)));
}
let path = path.to_path_buf();
get_or_open_database(&path, || async {
// Check if database file exists
let db_exists = path.exists();
// If database doesn't exist and not :memory:, create it with a temporary connection
// We don't keep this connection - WAL mode will be set later in acquire_writer()
//
// Why do we need to manually create the database file? We could just let the connection
// create it if it doesn't exist, using `create_if_missing(true)`, right? Not if we called
// connect and then our very first query was a read-only query, like `PRAGMA user_version;`,
// for example. That would fail because the read pool connections are read-only and cannot
// create the file
if !db_exists && !is_memory_database(&path) {
let create_options = SqliteConnectOptions::new()
.filename(&path)
.create_if_missing(true)
.read_only(false);
// Create database file with a temporary connection
let conn = create_options.connect().await?;
drop(conn); // Close immediately after creating the file
}
// Create read pool with read-only connections
let read_options = SqliteConnectOptions::new()
.filename(&path)
.read_only(true)
.optimize_on_close(true, OPTIMIZE_ANALYSIS_LIMIT);
let read_pool = SqlitePoolOptions::new()
.max_connections(config.max_read_connections)
.min_connections(0)
.idle_timeout(Some(std::time::Duration::from_secs(
config.idle_timeout_secs,
)))
.connect_with(read_options)
.await?;
// Create write pool with a single read-write connection
let write_options = SqliteConnectOptions::new()
.filename(&path)
.read_only(false)
.optimize_on_close(true, OPTIMIZE_ANALYSIS_LIMIT);
// Defense-in-depth: when any writer is returned to the pool, issue
// ROLLBACK to discard any transaction that a caller may have left open
// (e.g., a writer dropped after BEGIN without COMMIT/ROLLBACK). SQLite
// only auto-rollbacks on connection close, not on pool return, so
// without this the next acquire_writer() sees "cannot start a
// transaction within a transaction".
//
// Error handling: the expected benign case on a clean connection is
// "cannot rollback - no transaction is active" — recycle normally.
// Anything else means ROLLBACK itself failed or the connection is
// wedged; tell the pool not to recycle so a broken connection isn't
// handed to the next caller.
let write_conn = SqlitePoolOptions::new()
.max_connections(1)
.min_connections(0)
.idle_timeout(Some(std::time::Duration::from_secs(
config.idle_timeout_secs,
)))
.after_release(|conn, _meta| {
Box::pin(async move {
match sqlx::query("ROLLBACK").execute(&mut *conn).await {
Ok(_) => Ok(true),
Err(sqlx::Error::Database(e))
if e.message().contains("no transaction is active") =>
{
Ok(true)
}
Err(err) => {
warn!("after_release ROLLBACK failed, discarding connection: {err}");
Ok(false)
}
}
})
})
.connect_with(write_options)
.await?;
Ok(Self {
read_pool,
write_conn,
wal_initialized: AtomicBool::new(false),
closed: AtomicBool::new(false),
path: path.clone(),
})
})
.await
}
/// Get a reference to the connection pool for executing read queries
///
/// Use this for concurrent read operations. Multiple readers can access
/// the pool simultaneously.
///
/// # Example
///
/// ```no_run
/// use sqlx_sqlite_conn_mgr::SqliteDatabase;
/// use sqlx::query;
/// use std::sync::Arc;
///
/// # async fn example() -> Result<(), sqlx_sqlite_conn_mgr::Error> {
/// let db = SqliteDatabase::connect("test.db", None).await?;
/// let result = query("SELECT * FROM users")
/// .fetch_all(db.read_pool()?)
/// .await?;
/// # Ok(())
/// # }
/// ```
pub fn read_pool(&self) -> Result<&Pool<Sqlite>> {
if self.closed.load(Ordering::SeqCst) {
return Err(Error::DatabaseClosed);
}
Ok(&self.read_pool)
}
/// Acquire exclusive write access to the database
///
/// This method returns a `WriteGuard` that provides exclusive access to
/// the single write connection. Only one writer can exist at a time.
///
/// On the first call, this method will enable WAL mode on the database.
/// Subsequent calls reuse the same write connection.
///
/// # Example
///
/// ```no_run
/// use sqlx_sqlite_conn_mgr::SqliteDatabase;
/// use sqlx::query;
/// use std::sync::Arc;
///
/// # async fn example() -> Result<(), sqlx_sqlite_conn_mgr::Error> {
/// let db = SqliteDatabase::connect("test.db", None).await?;
/// let mut writer = db.acquire_writer().await?;
/// query("INSERT INTO users (name) VALUES (?)")
/// .bind("Alice")
/// .execute(&mut *writer)
/// .await?;
/// # Ok(())
/// # }
/// ```
pub async fn acquire_writer(&self) -> Result<WriteGuard> {
if self.closed.load(Ordering::SeqCst) {
return Err(Error::DatabaseClosed);
}
// Acquire connection from pool (max=1 ensures exclusive access)
let mut conn = self.write_conn.acquire().await?;
// Initialize WAL mode on first use (atomic check-and-set)
if self
.wal_initialized
.compare_exchange(false, true, Ordering::SeqCst, Ordering::SeqCst)
.is_ok()
{
sqlx::query("PRAGMA journal_mode = WAL")
.execute(&mut *conn)
.await?;
// https://www.sqlite.org/wal.html#performance_considerations
sqlx::query("PRAGMA synchronous = NORMAL")
.execute(&mut *conn)
.await?;
}
// Return WriteGuard wrapping the pool connection
Ok(WriteGuard::new(conn))
}
/// Run database migrations using the provided migrator
///
/// This method runs all pending migrations from the provided `Migrator`.
/// Migrations are executed using the write connection to ensure exclusive access.
/// WAL mode is enabled automatically before running migrations.
///
/// SQLx tracks applied migrations in a `_sqlx_migrations` table, so calling
/// this method multiple times is safe - already-applied migrations are skipped.
///
/// # Arguments
///
/// * `migrator` - A reference to a `Migrator` containing the migrations to run.
/// Typically created using `sqlx::migrate!()` macro.
///
/// # Example
///
/// ```no_run
/// use sqlx_sqlite_conn_mgr::SqliteDatabase;
///
/// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
/// // sqlx::migrate! is evaluated at compile time
/// static MIGRATOR: sqlx::migrate::Migrator = sqlx::migrate!("./migrations");
///
/// let db = SqliteDatabase::connect("test.db", None).await?;
/// db.run_migrations(&MIGRATOR).await?;
/// # Ok(())
/// # }
/// ```
pub async fn run_migrations(&self, migrator: &sqlx::migrate::Migrator) -> Result<()> {
// Ensure WAL mode is initialized via acquire_writer
// (WriteGuard dropped immediately, returning connection to pool)
{
let _writer = self.acquire_writer().await?;
}
// Migrator acquires its own connection from the write pool
migrator.run(&self.write_conn).await?;
Ok(())
}
/// Close the database and clean up resources
///
/// This closes all connections in the pool and removes the database from the cache.
/// After calling close, any operations on this database will return `Error::DatabaseClosed`.
///
/// Note: Takes `Arc<Self>` to consume ownership, preventing use-after-close at compile time.
/// The registry stores `Weak` references, so when this Arc is dropped, the database is freed.
///
/// # Example
///
/// ```no_run
/// use sqlx_sqlite_conn_mgr::SqliteDatabase;
///
/// # async fn example() -> Result<(), sqlx_sqlite_conn_mgr::Error> {
/// let db = SqliteDatabase::connect("test.db", None).await?;
/// // ... use database ...
/// db.close().await?;
/// # Ok(())
/// # }
/// ```
pub async fn close(self: Arc<Self>) -> Result<()> {
// Mark as closed
self.closed.store(true, Ordering::SeqCst);
// Remove from registry
if let Err(e) = uncache_database(&self.path).await {
error!("Failed to remove database from cache: {}", e);
}
// This will await all readers to be returned
self.read_pool.close().await;
// Checkpoint WAL before closing the write connection to flush changes and truncate WAL file
// Only attempt if WAL was initialized (write connection was used)
if self.wal_initialized.load(Ordering::SeqCst)
&& let Ok(mut conn) = self.write_conn.acquire().await
{
let _ = sqlx::query("PRAGMA wal_checkpoint(TRUNCATE)")
.execute(&mut *conn)
.await;
}
self.write_conn.close().await;
Ok(())
}
/// Close the database and delete all database files
///
/// This closes all connections and then deletes the database file,
/// WAL file, and SHM file from disk. Use with caution!
///
/// Note: Takes `Arc<Self>` to consume ownership, preventing use-after-close at compile time.
/// The registry stores `Weak` references, so when this Arc is dropped, the database is freed.
///
/// # Example
///
/// ```no_run
/// use sqlx_sqlite_conn_mgr::SqliteDatabase;
///
/// # async fn example() -> Result<(), sqlx_sqlite_conn_mgr::Error> {
/// let db = SqliteDatabase::connect("temp.db", None).await?;
/// // ... use database ...
/// db.remove().await?;
/// # Ok(())
/// # }
/// ```
pub async fn remove(self: Arc<Self>) -> Result<()> {
// Clone path before closing (since close consumes self)
let path = self.path.clone();
// Close all connections and clean up
self.close().await?;
// Remove main database file - propagate errors (file should exist)
std::fs::remove_file(&path).map_err(Error::Io)?;
// Remove WAL and SHM files - ignore "not found" but propagate other errors
// (these files may not exist if WAL was never initialized)
let wal_path = path.with_extension("db-wal");
if let Err(e) = std::fs::remove_file(&wal_path)
&& e.kind() != std::io::ErrorKind::NotFound
{
return Err(Error::Io(e));
}
let shm_path = path.with_extension("db-shm");
if let Err(e) = std::fs::remove_file(&shm_path)
&& e.kind() != std::io::ErrorKind::NotFound
{
return Err(Error::Io(e));
}
Ok(())
}
}