diesel-libsql 0.1.4

Diesel ORM backend for libSQL (Turso) — local, remote, replicas, async, OpenTelemetry
Documentation
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
//! Native async connection for diesel-libsql.
//!
//! Provides [`AsyncLibSqlConnection`] — a native [`diesel_async::AsyncConnection`]
//! implementation that talks directly to libsql's async API without any
//! `spawn_blocking` bridge.

use diesel::connection::{
    DynInstrumentation, Instrumentation, InstrumentationEvent, StrQueryHelper,
};
use diesel::query_builder::{AsQuery, QueryFragment, QueryId};
use diesel::result::*;
use diesel::ConnectionResult;
use diesel::QueryResult;
use futures_util::stream;
use futures_util::stream::BoxStream;
use futures_util::StreamExt;

use crate::backend::LibSql;
use crate::connection::{build_query, parse_remote_url, LibSqlConnection};
use crate::row::LibSqlRow;

use diesel_async::AnsiTransactionManager;

/// A native async connection to a libSQL database.
///
/// Unlike the previous `SyncConnectionWrapper`-based approach, this implementation
/// calls libsql's async API directly — no `spawn_blocking`, no sync bridge.
///
/// # Quick start
///
/// ```rust,no_run
/// use diesel_async::AsyncConnection;
/// use diesel_async::RunQueryDsl;
/// use diesel_libsql::AsyncLibSqlConnection;
///
/// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
/// let mut conn = AsyncLibSqlConnection::establish(":memory:").await?;
///
/// diesel::sql_query("CREATE TABLE demo (id INTEGER PRIMARY KEY, val TEXT)")
///     .execute(&mut conn)
///     .await?;
/// # Ok(())
/// # }
/// ```
#[allow(missing_debug_implementations)]
pub struct AsyncLibSqlConnection {
    database: libsql::Database,
    connection: libsql::Connection,
    transaction_state: AnsiTransactionManager,
    metadata_lookup: (),
    instrumentation: DynInstrumentation,
    /// Whether this connection is backed by an embedded replica.
    is_replica: bool,
}

// Safety: AsyncLibSqlConnection is only used from a single task at a time
// (enforced by &mut self on all trait methods). The libsql connection is not
// shared across threads.
#[allow(unsafe_code)]
unsafe impl Send for AsyncLibSqlConnection {}

impl AsyncLibSqlConnection {
    /// Create an `AsyncLibSqlConnection` from pre-built libsql parts.
    ///
    /// Used internally by [`ReplicaBuilder::establish_async`].
    pub(crate) fn from_parts(database: libsql::Database, connection: libsql::Connection) -> Self {
        Self {
            database,
            connection,
            transaction_state: AnsiTransactionManager::default(),
            metadata_lookup: (),
            instrumentation: DynInstrumentation::none(),
            is_replica: true,
        }
    }
}

/// Extension methods for [`AsyncLibSqlConnection`].
///
/// These expose libSQL-specific functionality (replicas, ALTER COLUMN, sync)
/// in an async context.
pub trait AsyncLibSqlConnectionExt {
    /// Establish an embedded replica connection asynchronously.
    ///
    /// The replica keeps a local SQLite file at `local_path` that syncs
    /// from `remote_url` using the provided `auth_token`. Reads are local;
    /// writes go to the remote primary.
    fn establish_replica(
        local_path: &str,
        remote_url: &str,
        auth_token: &str,
    ) -> impl std::future::Future<Output = ConnectionResult<AsyncLibSqlConnection>> + Send;

    /// Sync the embedded replica with the remote primary.
    ///
    /// No-op if this connection is not a replica.
    fn sync(&mut self) -> impl std::future::Future<Output = QueryResult<()>> + Send;

    /// Execute a libSQL-specific `ALTER TABLE ... ALTER COLUMN ... TO ...` statement.
    fn alter_column(
        &mut self,
        table: &str,
        column: &str,
        new_definition: &str,
    ) -> impl std::future::Future<Output = QueryResult<()>> + Send;

    /// Run a transaction with `BEGIN IMMEDIATE` asynchronously.
    ///
    /// Acquires a reserved lock immediately, preventing other writers.
    fn immediate_transaction<T, E, F>(
        &mut self,
        f: F,
    ) -> impl std::future::Future<Output = Result<T, E>> + Send
    where
        F: for<'a> FnOnce(
                &'a mut AsyncLibSqlConnection,
            ) -> futures_util::future::BoxFuture<'a, Result<T, E>>
            + Send,
        T: Send,
        E: From<diesel::result::Error> + Send;

    /// Run a transaction with `BEGIN EXCLUSIVE` asynchronously.
    ///
    /// Acquires an exclusive lock immediately, preventing all other connections
    /// from reading or writing.
    fn exclusive_transaction<T, E, F>(
        &mut self,
        f: F,
    ) -> impl std::future::Future<Output = Result<T, E>> + Send
    where
        F: for<'a> FnOnce(
                &'a mut AsyncLibSqlConnection,
            ) -> futures_util::future::BoxFuture<'a, Result<T, E>>
            + Send,
        T: Send,
        E: From<diesel::result::Error> + Send;

    /// Returns the row ID of the last successful `INSERT`.
    ///
    /// Returns `0` if no `INSERT` has been performed on this connection.
    fn last_insert_rowid(&self) -> i64;
}

impl AsyncLibSqlConnectionExt for AsyncLibSqlConnection {
    async fn establish_replica(
        local_path: &str,
        remote_url: &str,
        auth_token: &str,
    ) -> ConnectionResult<AsyncLibSqlConnection> {
        let database = libsql::Builder::new_remote_replica(
            local_path,
            remote_url.to_string(),
            auth_token.to_string(),
        )
        .build()
        .await
        .map_err(|e| diesel::ConnectionError::BadConnection(e.to_string()))?;

        let connection = database
            .connect()
            .map_err(|e| diesel::ConnectionError::BadConnection(e.to_string()))?;

        Ok(AsyncLibSqlConnection {
            database,
            connection,
            transaction_state: AnsiTransactionManager::default(),
            metadata_lookup: (),
            instrumentation: DynInstrumentation::none(),
            is_replica: true,
        })
    }

    async fn sync(&mut self) -> QueryResult<()> {
        if !self.is_replica {
            return Ok(());
        }
        self.database.sync().await.map_err(|e| {
            Error::DatabaseError(DatabaseErrorKind::Unknown, Box::new(e.to_string()))
        })?;
        Ok(())
    }

    async fn alter_column(
        &mut self,
        table: &str,
        column: &str,
        new_definition: &str,
    ) -> QueryResult<()> {
        let sql = format!(
            "ALTER TABLE {} ALTER COLUMN {} TO {}",
            table, column, new_definition
        );
        <Self as diesel_async::SimpleAsyncConnection>::batch_execute(self, &sql).await
    }

    async fn immediate_transaction<T, E, F>(&mut self, f: F) -> Result<T, E>
    where
        F: for<'a> FnOnce(
                &'a mut AsyncLibSqlConnection,
            ) -> futures_util::future::BoxFuture<'a, Result<T, E>>
            + Send,
        T: Send,
        E: From<diesel::result::Error> + Send,
    {
        <Self as diesel_async::SimpleAsyncConnection>::batch_execute(self, "BEGIN IMMEDIATE")
            .await?;
        match f(self).await {
            Ok(value) => {
                <Self as diesel_async::SimpleAsyncConnection>::batch_execute(self, "COMMIT")
                    .await?;
                Ok(value)
            }
            Err(e) => {
                let _ =
                    <Self as diesel_async::SimpleAsyncConnection>::batch_execute(self, "ROLLBACK")
                        .await;
                Err(e)
            }
        }
    }

    async fn exclusive_transaction<T, E, F>(&mut self, f: F) -> Result<T, E>
    where
        F: for<'a> FnOnce(
                &'a mut AsyncLibSqlConnection,
            ) -> futures_util::future::BoxFuture<'a, Result<T, E>>
            + Send,
        T: Send,
        E: From<diesel::result::Error> + Send,
    {
        <Self as diesel_async::SimpleAsyncConnection>::batch_execute(self, "BEGIN EXCLUSIVE")
            .await?;
        match f(self).await {
            Ok(value) => {
                <Self as diesel_async::SimpleAsyncConnection>::batch_execute(self, "COMMIT")
                    .await?;
                Ok(value)
            }
            Err(e) => {
                let _ =
                    <Self as diesel_async::SimpleAsyncConnection>::batch_execute(self, "ROLLBACK")
                        .await;
                Err(e)
            }
        }
    }

    fn last_insert_rowid(&self) -> i64 {
        self.connection.last_insert_rowid()
    }
}

impl diesel_async::SimpleAsyncConnection for AsyncLibSqlConnection {
    async fn batch_execute(&mut self, query: &str) -> QueryResult<()> {
        self.instrumentation
            .on_connection_event(InstrumentationEvent::start_query(&StrQueryHelper::new(
                query,
            )));

        let result = self
            .connection
            .execute_batch(query)
            .await
            .map(|_| ())
            .map_err(|e| Error::DatabaseError(DatabaseErrorKind::Unknown, Box::new(e.to_string())));

        self.instrumentation
            .on_connection_event(InstrumentationEvent::finish_query(
                &StrQueryHelper::new(query),
                result.as_ref().err(),
            ));

        result
    }
}

impl diesel_async::AsyncConnectionCore for AsyncLibSqlConnection {
    type ExecuteFuture<'conn, 'query> = futures_util::future::BoxFuture<'conn, QueryResult<usize>>;
    type LoadFuture<'conn, 'query> =
        futures_util::future::BoxFuture<'conn, QueryResult<Self::Stream<'conn, 'query>>>;
    type Stream<'conn, 'query> = BoxStream<'static, QueryResult<LibSqlRow>>;
    type Row<'conn, 'query> = LibSqlRow;
    type Backend = LibSql;

    fn load<'conn, 'query, T>(&'conn mut self, source: T) -> Self::LoadFuture<'conn, 'query>
    where
        T: AsQuery + 'query,
        T::Query: QueryFragment<LibSql> + QueryId + 'query,
    {
        let query = source.as_query();
        let (sql, params) = match build_query(&query, &mut self.metadata_lookup) {
            Ok(v) => v,
            Err(e) => return Box::pin(std::future::ready(Err(e))),
        };

        Box::pin(async move {
            self.instrumentation
                .on_connection_event(InstrumentationEvent::start_query(&StrQueryHelper::new(
                    &sql,
                )));

            let result = async {
                let stmt = self.connection.prepare(&sql).await.map_err(|e| {
                    Error::DatabaseError(DatabaseErrorKind::Unknown, Box::new(e.to_string()))
                })?;

                let rows_result = stmt.query(params).await.map_err(|e| {
                    Error::DatabaseError(DatabaseErrorKind::Unknown, Box::new(e.to_string()))
                })?;

                LibSqlConnection::collect_rows(rows_result).await
            }
            .await;

            self.instrumentation
                .on_connection_event(InstrumentationEvent::finish_query(
                    &StrQueryHelper::new(&sql),
                    result.as_ref().err(),
                ));

            let rows = result?;
            let s: BoxStream<'static, QueryResult<LibSqlRow>> =
                stream::iter(rows.into_iter().map(Ok)).boxed();
            Ok(s)
        })
    }

    fn execute_returning_count<'conn, 'query, T>(
        &'conn mut self,
        source: T,
    ) -> Self::ExecuteFuture<'conn, 'query>
    where
        T: QueryFragment<LibSql> + QueryId + 'query,
    {
        let (sql, params) = match build_query(&source, &mut self.metadata_lookup) {
            Ok(v) => v,
            Err(e) => return Box::pin(std::future::ready(Err(e))),
        };

        Box::pin(async move {
            self.instrumentation
                .on_connection_event(InstrumentationEvent::start_query(&StrQueryHelper::new(
                    &sql,
                )));

            let result = match self.connection.execute(&sql, params.clone()).await {
                Ok(affected) => Ok(affected as usize),
                Err(libsql::Error::ExecuteReturnedRows) => {
                    // libsql's execute() rejects SELECT statements. Fall back to
                    // query() and return the row count. This happens when diesel's
                    // migration harness runs SELECT via execute_returning_count().
                    match self.connection.query(&sql, params).await {
                        Ok(mut rows) => {
                            let mut count = 0usize;
                            loop {
                                match rows.next().await {
                                    Ok(Some(_)) => count += 1,
                                    Ok(None) => break Ok(count),
                                    Err(e) => break Err(Error::DatabaseError(
                                        DatabaseErrorKind::Unknown,
                                        Box::new(e.to_string()),
                                    )),
                                }
                            }
                        }
                        Err(e) => Err(Error::DatabaseError(
                            DatabaseErrorKind::Unknown,
                            Box::new(e.to_string()),
                        )),
                    }
                }
                Err(e) => Err(Error::DatabaseError(
                    DatabaseErrorKind::Unknown,
                    Box::new(e.to_string()),
                )),
            };

            self.instrumentation
                .on_connection_event(InstrumentationEvent::finish_query(
                    &StrQueryHelper::new(&sql),
                    result.as_ref().err(),
                ));

            result
        })
    }
}

impl diesel_async::AsyncConnection for AsyncLibSqlConnection {
    type TransactionManager = AnsiTransactionManager;

    async fn establish(database_url: &str) -> ConnectionResult<Self> {
        let mut instrumentation = diesel::connection::get_default_instrumentation();
        instrumentation.on_connection_event(InstrumentationEvent::start_establish_connection(
            database_url,
        ));

        let is_remote = database_url.starts_with("libsql://")
            || database_url.starts_with("https://")
            || database_url.starts_with("http://");

        let result = async {
            let database = if is_remote {
                let (url, auth_token) = parse_remote_url(database_url)?;
                libsql::Builder::new_remote(url, auth_token)
                    .build()
                    .await
                    .map_err(|e| diesel::ConnectionError::BadConnection(e.to_string()))?
            } else {
                libsql::Builder::new_local(database_url)
                    .build()
                    .await
                    .map_err(|e| diesel::ConnectionError::BadConnection(e.to_string()))?
            };

            let connection = database
                .connect()
                .map_err(|e| diesel::ConnectionError::BadConnection(e.to_string()))?;

            Ok(AsyncLibSqlConnection {
                database,
                connection,
                transaction_state: AnsiTransactionManager::default(),
                metadata_lookup: (),
                instrumentation: DynInstrumentation::none(),
                is_replica: false,
            })
        }
        .await;

        instrumentation.on_connection_event(InstrumentationEvent::finish_establish_connection(
            database_url,
            result.as_ref().err(),
        ));

        let mut conn = result?;
        conn.instrumentation = instrumentation.into();
        Ok(conn)
    }

    fn transaction_state(
        &mut self,
    ) -> &mut <Self::TransactionManager as diesel_async::TransactionManager<Self>>::TransactionStateData
    {
        &mut self.transaction_state
    }

    fn instrumentation(&mut self) -> &mut dyn Instrumentation {
        &mut *self.instrumentation
    }

    fn set_instrumentation(&mut self, instrumentation: impl Instrumentation) {
        self.instrumentation = instrumentation.into();
    }

    fn set_prepared_statement_cache_size(&mut self, _size: diesel::connection::CacheSize) {
        // No-op: we don't use a prepared statement cache currently
    }
}

#[cfg(any(feature = "deadpool", feature = "bb8"))]
impl diesel_async::pooled_connection::PoolableConnection for AsyncLibSqlConnection {}