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

オープン
#273 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 1 日以内に返信

まだ誰も着手していません。

評価

難易度
5/5
見積もり時間
1週間以上
初心者へのやさしさ
15/100
issue の種類
機能追加
明瞭さ
おおむね明確
活発さ
活発
技術スタック
sql, sqlite
領域
databases

調査の方向性

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.

索引モデルが issue の本文から書いたものです。

説明

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.

主要言語
HTML
スター
110
フォーク
25
平均マージ
18時間 28分
マージ済み PR(30日)
16

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

roc-lang/basic-webserver のほかの issue

roc-lang/basic-webserver の issue をすべて見る

似ている issue

Databases の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。