Sqlite: lease one pooled connection for pragmas plus a transaction (safe table rebuilds)

Aperta
#273 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
5/5
Tempo stimato
Più di una settimana
Idoneità per principianti
15/100
Tipo di issue
Funzionalità
Chiarezza
Abbastanza chiara
Stato di attività
Attiva
Stack tecnologico
sql, sqlite
Ambito
databases

Direzione di ricerca

This is a design request for a new platform API, not a located edit: the proposed Sqlite.with_connection! would hand one pooled connection to a block exposing execute!, query_many! and begin!, then reset open-time settings such as PRAGMA foreign_keys = ON on return. Start by reading how the basic-webserver Sqlite pool leases connections and where the foreign-keys pragma is applied at open time, then how Sqlite.begin! currently acquires its connection. Done looks like a lease that keeps the pragma and the transaction on one connection, plus a test that rebuilds a table with foreign keys off.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

Summary

Please give a way to lease one pooled connection for a short block. Inside the block, the code can send connection-level statements and then a transaction, all on that same connection. A possible form:

Sqlite.with_connection! : Sqlite.Db, (Sqlite.Connection => Try(a, e)) => Try(a, [Sqlite(Sqlite.QueryError), ..e])
# Sqlite.Connection has execute!, query_many! and begin! (a Sqlite.Transaction on this connection)

When the block ends, the platform could reset what it set at open time (for example PRAGMA foreign_keys = ON) before it returns the connection to the pool.

Why it is needed

Some SQLite schema migrations must rebuild a table: create a new table, copy the rows, drop the old table, rename the new one. The SQLite docs say to do this with foreign keys off:

PRAGMA foreign_keys = OFF;   -- a no-op inside a transaction, so it must come before BEGIN
BEGIN;
  -- create, copy, drop, rename
  PRAGMA foreign_key_check;
COMMIT;
PRAGMA foreign_keys = ON;

PRAGMA foreign_keys is per connection. On the basic-webserver pool, the pragma and the later Sqlite.begin! can lease different connections. The platform also turns foreign keys on for every pooled connection. So in the transaction, foreign keys are always on. A migration runner must then choose one of two options:

Option Result
Rebuild with foreign keys on DROP TABLE runs the ON DELETE CASCADE actions of the child tables and deletes their rows. This loses data
Refuse the rebuild Safe. The migration must run from another tool before the server starts

A migration runner takes the safe option today. With a connection lease, it could run every migration inside the server's init!.

Current workaround

Apply SQLite migrations with a separate one-connection program (for example on basic-cli) before the server starts. Other migrations and read-only schema checks work from the server today.

Priority

Not a blocker. The workaround is one more step in deployment. This is a wanted feature: it lets a basic-webserver app apply all of its own migrations at start, safely. The same lease could also help other per-connection settings that a library cannot apply reliably on a pool today.

Lingua principale
HTML
Stelle
110
Fork
25
Merge medio
18h 28m
PR unite (30g)
16

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di roc-lang/basic-webserver

Tutte le issue di roc-lang/basic-webserver

Issue simili

Altre issue su Databases

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.