Class: ActiveRecord::ConnectionAdapters::BeagleTursoAdapter

Inherits:
SQLite3Adapter
  • Object
show all
Defined in:
lib/active_record/connection_adapters/beagle_turso_adapter.rb

Overview

ActiveRecord Beagle Turso Adapter

Subclasses the stock SQLite3Adapter and reroutes only its raw-execution seam (+perform_query+) onto a beagle-turso Connection. beagle-turso is a libsql/Turso driver: it speaks a SQLite-compatible dialect but has its own native handle rather than the sqlite3 gem's ::SQLite3::Database.

Everything ActiveRecord relies on above the raw connection -- SQL generation, quoting, schema introspection (PRAGMA table_xinfo, sqlite_master), type mapping, transactions -- is inherited unchanged from SQLite3Adapter. We swap out:

* +new_client+ / +connect+   : open a Beagle::Turso::Database instead of a
::SQLite3::Database, wrapped in a Client that answers the handful of
lifecycle/pragma messages the SQLite3Adapter sends a raw connection.
* +configure_connection+     : re-assert `PRAGMA foreign_keys = ON` through
the real exec path, because the stock DEFAULT_PRAGMA setter is a no-op on
our Client (SQLite defaults FKs OFF per-connection).
* +perform_query+            : the single method every query flows through
(see AbstractAdapter#raw_execute). Reads/RETURNING -> ActiveRecord::Result
with columns+rows; writes (incl. data-modifying CTEs) -> affected-row
count + last_insert_rowid.
* +last_inserted_id+         : fall back to last_insert_rowid when a plain
INSERT (no RETURNING) was used.

Defined Under Namespace

Classes: Client

Constant Summary collapse

ADAPTER_NAME =
"BeagleTurso"

Class Method Summary collapse

Class Method Details

.new_client(config) ⇒ Object

Open a beagle-turso Database (synced remote when remote_url is present, otherwise local/in-memory) and wrap it so the SQLite3Adapter can drive it as a raw connection.



66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/active_record/connection_adapters/beagle_turso_adapter.rb', line 66

def new_client(config)
  database =
    if config[:remote_url]
      Beagle::Turso::Database.open(
        local_path: config[:database].to_s,
        remote_url: config[:remote_url],
        auth_token: config[:auth_token],
        bootstrap_if_empty: config.fetch(:bootstrap_if_empty, true)
      )
    else
      Beagle::Turso::Database.open_local(config[:database].to_s)
    end
  Client.new(database)
end