autumn-plugin-turso 0.3.0

TursoDB plugin for Autumn: an embedded, SQLite-compatible database handle with config, migrations, health and metrics
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
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
//! The `Turso` database handle.

use std::collections::HashMap;
use std::sync::Arc;
use std::sync::atomic::{AtomicBool, AtomicU64, Ordering};

use tokio::sync::RwLock;
use turso::{Connection, IntoParams, Row};

use crate::config::TursoConfig;
use crate::error::{Result, TursoError};
use crate::migrate::{self, MIGRATIONS_TABLE, Migration};

/// A shared handle to one Turso database.
///
/// Clones share the database. Get it in a handler as an extractor
/// (`db: Turso`), or from state with `state.extension::<Turso>()`.
#[derive(Clone)]
pub struct Turso {
    inner: Arc<Inner>,
}

struct Inner {
    backend: RwLock<Option<Backend>>,
    closed: AtomicBool,
    path: String,
    read_only: bool,
    synced: bool,
    busy_timeout: std::time::Duration,
    counters: Counters,
}

#[derive(Clone)]
enum Backend {
    Local(turso::Database),
    #[cfg(feature = "sync")]
    Synced(turso::sync::Database),
}

#[derive(Default)]
pub struct Counters {
    pub statements: AtomicU64,
    pub errors: AtomicU64,
    pub connections: AtomicU64,
    pub migrations: AtomicU64,
    pub sync_pushes: AtomicU64,
    pub sync_pulls: AtomicU64,
    pub sync_errors: AtomicU64,
}

/// Counter values for one handle.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
#[non_exhaustive]
pub struct TursoStats {
    /// Statements run through the handle helpers.
    pub statements: u64,
    /// Failed handle calls.
    pub errors: u64,
    /// Connections opened.
    pub connections: u64,
    /// Migrations applied.
    pub migrations_applied: u64,
    /// Successful sync pushes.
    pub sync_pushes: u64,
    /// Successful sync pulls.
    pub sync_pulls: u64,
    /// Failed sync calls.
    pub sync_errors: u64,
}

impl std::fmt::Debug for Turso {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("Turso")
            .field("path", &self.inner.path)
            .field("read_only", &self.inner.read_only)
            .field("synced", &self.inner.synced)
            .field("closed", &self.is_closed())
            .finish_non_exhaustive()
    }
}

impl Turso {
    /// Open the database that `config` names.
    ///
    /// This makes missing parent directories of a file path. With sync, it
    /// also pulls when `pull_on_start` is set.
    ///
    /// # Errors
    ///
    /// Returns [`TursoError::Config`] for a bad config, [`TursoError::Open`]
    /// when the file cannot open, and [`TursoError::Sync`] when the remote
    /// fails.
    pub async fn open(config: &TursoConfig) -> Result<Self> {
        config.validate()?;
        let path = config.path.trim().to_owned();
        if !config.read_only {
            make_parent_dir(&path)?;
        }
        let synced = config.active_sync().is_some();
        let backend = open_backend(config, &path).await?;
        let handle = Self {
            inner: Arc::new(Inner {
                backend: RwLock::new(Some(backend)),
                closed: AtomicBool::new(false),
                path,
                read_only: config.read_only,
                synced,
                busy_timeout: std::time::Duration::from_millis(config.busy_timeout_ms),
                counters: Counters::default(),
            }),
        };
        #[cfg(feature = "sync")]
        if config.active_sync().is_some_and(|sync| sync.pull_on_start) {
            handle.pull().await?;
        }
        Ok(handle)
    }

    /// Open a new connection. Use one connection per task.
    ///
    /// # Errors
    ///
    /// Returns [`TursoError::Closed`] after [`close`](Self::close), or the
    /// engine error.
    pub async fn connect(&self) -> Result<Connection> {
        let result = self.connect_inner().await;
        self.count(result)
    }

    /// Run one statement and return the number of changed rows.
    ///
    /// # Errors
    ///
    /// Returns the engine error, or [`TursoError::Closed`].
    pub async fn execute(&self, sql: &str, params: impl IntoParams + Send) -> Result<u64> {
        let result = async { Ok(self.statement_conn().await?.execute(sql, params).await?) }.await;
        self.count(result)
    }

    /// Run one or more statements with no parameters.
    ///
    /// # Errors
    ///
    /// Returns the engine error, or [`TursoError::Closed`].
    pub async fn execute_batch(&self, sql: &str) -> Result<()> {
        let result = async { Ok(self.statement_conn().await?.execute_batch(sql).await?) }.await;
        self.count(result)
    }

    /// Run a query and collect all rows.
    ///
    /// For large results, use [`connect`](Self::connect) and read rows one
    /// by one.
    ///
    /// # Errors
    ///
    /// Returns the engine error, or [`TursoError::Closed`].
    pub async fn query(&self, sql: &str, params: impl IntoParams + Send) -> Result<Vec<Row>> {
        let result = async {
            let mut rows = self.statement_conn().await?.query(sql, params).await?;
            let mut out = Vec::new();
            while let Some(row) = rows.next().await? {
                out.push(row);
            }
            Ok(out)
        }
        .await;
        self.count(result)
    }

    /// Run `SELECT 1`. The metrics do not count it, because health probes
    /// call it often.
    ///
    /// # Errors
    ///
    /// Returns the engine error, or [`TursoError::Closed`].
    pub async fn ping(&self) -> Result<()> {
        let conn = self.connect_inner().await?;
        let mut rows = conn.query("SELECT 1", ()).await?;
        while rows.next().await?.is_some() {}
        Ok(())
    }

    /// Apply the pending `migrations` in order. Return how many ran.
    ///
    /// # Errors
    ///
    /// Returns [`TursoError::DuplicateMigration`],
    /// [`TursoError::MigrationChecksum`], [`TursoError::ReadOnlyPending`],
    /// [`TursoError::Migration`], [`TursoError::Closed`] or an engine error.
    /// The plugin rolls back a failed migration.
    pub async fn migrate(&self, migrations: &[Migration]) -> Result<usize> {
        let result = self.migrate_inner(migrations).await;
        self.count(result)
    }

    /// The ids of the `migrations` that are not applied.
    ///
    /// # Errors
    ///
    /// Returns [`TursoError::DuplicateMigration`],
    /// [`TursoError::MigrationChecksum`] or an engine error.
    pub async fn pending(&self, migrations: &[Migration]) -> Result<Vec<String>> {
        let result = self.pending_inner(migrations).await;
        self.count(result)
    }

    async fn pending_inner(&self, migrations: &[Migration]) -> Result<Vec<String>> {
        migrate::check_list(migrations)?;
        let conn = self.connect_inner().await?;
        let applied = applied_checksums(&conn).await?;
        Ok(migrate::pending(migrations, &applied)?
            .into_iter()
            .map(|migration| migration.id().to_owned())
            .collect())
    }

    /// Close the handle and all its clones. Later calls return
    /// [`TursoError::Closed`]. A second close does nothing.
    ///
    /// Close does not stop work that started before it. A helper call in
    /// progress completes. A connection from [`connect`](Self::connect)
    /// stays open until you drop it.
    pub async fn close(&self) {
        self.inner.closed.store(true, Ordering::SeqCst);
        self.inner.backend.write().await.take();
    }

    /// `true` after [`close`](Self::close).
    #[must_use]
    pub fn is_closed(&self) -> bool {
        self.inner.closed.load(Ordering::SeqCst)
    }

    /// The database path.
    #[must_use]
    pub fn path(&self) -> &str {
        &self.inner.path
    }

    /// `true` when the database has no write access.
    #[must_use]
    pub fn is_read_only(&self) -> bool {
        self.inner.read_only
    }

    /// `true` when the database syncs with Turso Cloud.
    #[must_use]
    pub fn is_synced(&self) -> bool {
        self.inner.synced
    }

    /// The counter values.
    #[must_use]
    pub fn stats(&self) -> TursoStats {
        let c = &self.inner.counters;
        TursoStats {
            statements: c.statements.load(Ordering::Relaxed),
            errors: c.errors.load(Ordering::Relaxed),
            connections: c.connections.load(Ordering::Relaxed),
            migrations_applied: c.migrations.load(Ordering::Relaxed),
            sync_pushes: c.sync_pushes.load(Ordering::Relaxed),
            sync_pulls: c.sync_pulls.load(Ordering::Relaxed),
            sync_errors: c.sync_errors.load(Ordering::Relaxed),
        }
    }

    /// Push local changes to Turso Cloud.
    ///
    /// # Errors
    ///
    /// Returns [`TursoError::SyncNotConfigured`] for a local database,
    /// [`TursoError::Closed`], or [`TursoError::Sync`].
    #[cfg(feature = "sync")]
    pub async fn push(&self) -> Result<()> {
        let db = self.synced_backend().await?;
        let result = db
            .push()
            .await
            .map_err(|err| TursoError::Sync(err.to_string()));
        self.count_sync(&result, &self.inner.counters.sync_pushes);
        result
    }

    /// Pull remote changes from Turso Cloud. Return `true` when a change
    /// was applied.
    ///
    /// # Errors
    ///
    /// Returns [`TursoError::SyncNotConfigured`] for a local database,
    /// [`TursoError::Closed`], or [`TursoError::Sync`].
    #[cfg(feature = "sync")]
    pub async fn pull(&self) -> Result<bool> {
        let db = self.synced_backend().await?;
        let result = db
            .pull()
            .await
            .map_err(|err| TursoError::Sync(err.to_string()));
        self.count_sync(&result, &self.inner.counters.sync_pulls);
        result
    }

    #[cfg(feature = "sync")]
    async fn synced_backend(&self) -> Result<turso::sync::Database> {
        let backend = self.backend().await?;
        match backend {
            Backend::Synced(db) => Ok(db),
            Backend::Local(_) => Err(TursoError::SyncNotConfigured),
        }
    }

    #[cfg(feature = "sync")]
    fn count_sync<T>(&self, result: &Result<T>, ok_counter: &AtomicU64) {
        let counter = if result.is_ok() {
            ok_counter
        } else {
            &self.inner.counters.sync_errors
        };
        counter.fetch_add(1, Ordering::Relaxed);
    }

    async fn backend(&self) -> Result<Backend> {
        if self.is_closed() {
            return Err(TursoError::Closed);
        }
        self.inner
            .backend
            .read()
            .await
            .clone()
            .ok_or(TursoError::Closed)
    }

    async fn connect_inner(&self) -> Result<Connection> {
        let backend = self.backend().await?;
        let conn = match backend {
            Backend::Local(db) => db.connect()?,
            #[cfg(feature = "sync")]
            Backend::Synced(db) => db.connect().await?,
        };
        conn.busy_timeout(self.inner.busy_timeout)?;
        self.inner
            .counters
            .connections
            .fetch_add(1, Ordering::Relaxed);
        Ok(conn)
    }

    /// A connection for one helper statement. Counts the statement.
    async fn statement_conn(&self) -> Result<Connection> {
        let conn = self.connect_inner().await?;
        self.inner
            .counters
            .statements
            .fetch_add(1, Ordering::Relaxed);
        Ok(conn)
    }

    async fn migrate_inner(&self, migrations: &[Migration]) -> Result<usize> {
        migrate::check_list(migrations)?;
        let conn = self.connect_inner().await?;
        let applied = applied_checksums(&conn).await?;
        let pending = migrate::pending(migrations, &applied)?;
        if pending.is_empty() {
            return Ok(0);
        }
        if self.inner.read_only {
            return Err(TursoError::ReadOnlyPending(
                pending.iter().map(|m| m.id().to_owned()).collect(),
            ));
        }
        conn.execute(migrate::create_table_sql(), ()).await?;
        let mut applied = 0;
        for migration in &pending {
            if apply_one(&conn, migration).await? {
                applied += 1;
                self.inner
                    .counters
                    .migrations
                    .fetch_add(1, Ordering::Relaxed);
                tracing::info!(id = migration.id(), "turso migration applied");
            }
        }
        Ok(applied)
    }

    fn count<T>(&self, result: Result<T>) -> Result<T> {
        if result.is_err() {
            self.inner.counters.errors.fetch_add(1, Ordering::Relaxed);
        }
        result
    }
}

async fn open_backend(config: &TursoConfig, path: &str) -> Result<Backend> {
    #[cfg(feature = "sync")]
    if let Some(sync) = config.active_sync() {
        let mut builder = turso::sync::Builder::new_remote(path)
            .with_remote_url(sync.url.clone().unwrap_or_default())
            .with_client_name(concat!("autumn-plugin-turso/", env!("CARGO_PKG_VERSION")));
        if let Some(token) = sync.token() {
            builder = builder.with_auth_token(token.to_owned());
        }
        let db = builder
            .build()
            .await
            .map_err(|err| TursoError::Sync(err.to_string()))?;
        return Ok(Backend::Synced(db));
    }
    let db = turso::Builder::new_local(path)
        .read_only(config.read_only)
        .build()
        .await
        .map_err(|err| TursoError::Open {
            path: path.to_owned(),
            source: Box::new(err),
        })?;
    Ok(Backend::Local(db))
}

fn make_parent_dir(path: &str) -> Result<()> {
    if path == ":memory:" {
        return Ok(());
    }
    match std::path::Path::new(path).parent() {
        Some(parent) if !parent.as_os_str().is_empty() => {
            std::fs::create_dir_all(parent).map_err(|err| TursoError::Open {
                path: path.to_owned(),
                source: Box::new(err),
            })
        }
        _ => Ok(()),
    }
}

async fn applied_checksums(conn: &Connection) -> Result<HashMap<String, String>> {
    let exists = conn
        .query(
            "SELECT 1 FROM sqlite_schema WHERE type = 'table' AND name = ?1",
            [MIGRATIONS_TABLE],
        )
        .await?
        .next()
        .await?
        .is_some();
    let mut applied = HashMap::new();
    if !exists {
        return Ok(applied);
    }
    let mut rows = conn
        .query(format!("SELECT id, checksum FROM {MIGRATIONS_TABLE}"), ())
        .await?;
    while let Some(row) = rows.next().await? {
        applied.insert(row.get::<String>(0)?, row.get::<String>(1)?);
    }
    Ok(applied)
}

/// Apply one migration in one transaction. Return `false` when another
/// process applied it first.
async fn apply_one(conn: &Connection, migration: &Migration) -> Result<bool> {
    let fail = |message: String| TursoError::Migration {
        id: migration.id().to_owned(),
        message,
    };
    conn.execute("BEGIN IMMEDIATE", ())
        .await
        .map_err(|err| fail(err.to_string()))?;
    let result = apply_in_transaction(conn, migration).await;
    if result.is_err()
        && !conn.is_autocommit().unwrap_or(true)
        && let Err(rollback) = conn.execute("ROLLBACK", ()).await
    {
        tracing::warn!(id = migration.id(), %rollback, "turso migration rollback failed");
    }
    result
}

async fn apply_in_transaction(conn: &Connection, migration: &Migration) -> Result<bool> {
    let fail = |message: String| TursoError::Migration {
        id: migration.id().to_owned(),
        message,
    };
    // Read again under the write lock. Another process can be first.
    let mut rows = conn
        .query(
            format!("SELECT checksum FROM {MIGRATIONS_TABLE} WHERE id = ?1"),
            [migration.id()],
        )
        .await?;
    if let Some(row) = rows.next().await? {
        drop(rows);
        let applied = row.get::<String>(0)?;
        conn.execute("ROLLBACK", ()).await?;
        if applied == migration.checksum() {
            return Ok(false);
        }
        return Err(TursoError::MigrationChecksum {
            id: migration.id().to_owned(),
            applied,
            current: migration.checksum(),
        });
    }
    drop(rows);
    conn.execute_batch(migration.sql())
        .await
        .map_err(|err| fail(err.to_string()))?;
    if conn.is_autocommit()? {
        return Err(fail(
            "the migration ended the transaction; do not use BEGIN, COMMIT or ROLLBACK in a migration"
                .into(),
        ));
    }
    conn.execute(
        format!(
            "INSERT INTO {MIGRATIONS_TABLE} (id, checksum, applied_at) \
             VALUES (?1, ?2, CAST(strftime('%s', 'now') AS INTEGER))"
        ),
        (migration.id().to_owned(), migration.checksum()),
    )
    .await
    .map_err(|err| fail(err.to_string()))?;
    conn.execute("COMMIT", ())
        .await
        .map_err(|err| fail(err.to_string()))?;
    Ok(true)
}

impl axum::extract::FromRequestParts<autumn_web::AppState> for Turso {
    type Rejection = autumn_web::AutumnError;

    fn from_request_parts(
        _parts: &mut axum::http::request::Parts,
        state: &autumn_web::AppState,
    ) -> impl Future<Output = Result<Self, Self::Rejection>> + Send {
        std::future::ready(
            state
                .extension::<Self>()
                .map(|handle| (*handle).clone())
                .ok_or_else(|| {
                    autumn_web::AutumnError::internal_server_error(TursoError::NotInstalled)
                }),
        )
    }
}