2026-07-19 16:33:58 +00:00
|
|
|
package db
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"fmt"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// ── Plugin persistence (Phase C Step 9) ─────────────────────────────────────
|
|
|
|
|
//
|
|
|
|
|
// Moved verbatim from the store package's SQLiteStore (D3). The SQL is
|
|
|
|
|
// unchanged.
|
|
|
|
|
|
|
|
|
|
func (d *DB) InstallPlugin(ctx context.Context, name, version, manifestJSON string) (int64, error) {
|
2026-08-07 21:20:48 +02:00
|
|
|
// RETURNING instead of LastInsertId: last_insert_rowid is connection-
|
|
|
|
|
// scoped and NOT updated when the upsert takes the DO UPDATE branch, so
|
|
|
|
|
// on the shared writer connection a reinstall would return the rowid of
|
|
|
|
|
// some unrelated prior INSERT.
|
|
|
|
|
var id int64
|
|
|
|
|
err := d.writer.QueryRowContext(ctx,
|
2026-07-19 16:33:58 +00:00
|
|
|
`INSERT INTO plugins (name, version, enabled, manifest_json) VALUES (?, ?, 0, ?)
|
2026-08-07 21:20:48 +02:00
|
|
|
ON CONFLICT(name) DO UPDATE SET version = excluded.version, manifest_json = excluded.manifest_json
|
|
|
|
|
RETURNING id`,
|
2026-07-19 16:33:58 +00:00
|
|
|
name, version, manifestJSON,
|
2026-08-07 21:20:48 +02:00
|
|
|
).Scan(&id)
|
2026-07-19 16:33:58 +00:00
|
|
|
if err != nil {
|
|
|
|
|
return 0, fmt.Errorf("InstallPlugin: %w", err)
|
|
|
|
|
}
|
|
|
|
|
return id, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (d *DB) EnablePlugin(ctx context.Context, id int64) error {
|
2026-07-31 15:41:57 +02:00
|
|
|
_, err := d.writer.ExecContext(ctx, `UPDATE plugins SET enabled = 1 WHERE id = ?`, id)
|
2026-07-19 16:33:58 +00:00
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (d *DB) DisablePlugin(ctx context.Context, id int64) error {
|
2026-07-31 15:41:57 +02:00
|
|
|
_, err := d.writer.ExecContext(ctx, `UPDATE plugins SET enabled = 0 WHERE id = ?`, id)
|
2026-07-19 16:33:58 +00:00
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (d *DB) UninstallPlugin(ctx context.Context, id int64) error {
|
2026-07-31 15:41:57 +02:00
|
|
|
_, err := d.writer.ExecContext(ctx, `DELETE FROM plugins WHERE id = ?`, id)
|
2026-07-19 16:33:58 +00:00
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (d *DB) GetPlugin(ctx context.Context, id int64) (*PluginRow, error) {
|
2026-07-31 15:41:57 +02:00
|
|
|
row := d.reader.QueryRowContext(ctx,
|
2026-07-19 16:33:58 +00:00
|
|
|
`SELECT id, name, version, enabled, manifest_json, installed_at FROM plugins WHERE id = ?`,
|
|
|
|
|
id,
|
|
|
|
|
)
|
|
|
|
|
return scanPluginRow(row)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (d *DB) GetPluginByName(ctx context.Context, name string) (*PluginRow, error) {
|
2026-07-31 15:41:57 +02:00
|
|
|
row := d.reader.QueryRowContext(ctx,
|
2026-07-19 16:33:58 +00:00
|
|
|
`SELECT id, name, version, enabled, manifest_json, installed_at FROM plugins WHERE name = ?`,
|
|
|
|
|
name,
|
|
|
|
|
)
|
|
|
|
|
return scanPluginRow(row)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (d *DB) ListPlugins(ctx context.Context) ([]PluginRow, error) {
|
2026-07-31 15:41:57 +02:00
|
|
|
rows, err := d.reader.QueryContext(ctx,
|
2026-07-19 16:33:58 +00:00
|
|
|
`SELECT id, name, version, enabled, manifest_json, installed_at FROM plugins ORDER BY name`,
|
|
|
|
|
)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, fmt.Errorf("ListPlugins: %w", err)
|
|
|
|
|
}
|
|
|
|
|
defer func() { _ = rows.Close() }()
|
|
|
|
|
var out []PluginRow
|
|
|
|
|
for rows.Next() {
|
|
|
|
|
var p PluginRow
|
|
|
|
|
var enabledInt int64
|
|
|
|
|
var installedAt string
|
|
|
|
|
if err := rows.Scan(&p.ID, &p.Name, &p.Version, &enabledInt, &p.ManifestJSON, &installedAt); err != nil {
|
|
|
|
|
return nil, fmt.Errorf("ListPlugins scan: %w", err)
|
|
|
|
|
}
|
|
|
|
|
p.Enabled = enabledInt != 0
|
|
|
|
|
p.InstalledAt = parseSQLiteTime(installedAt)
|
|
|
|
|
out = append(out, p)
|
|
|
|
|
}
|
|
|
|
|
return out, rows.Err()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (d *DB) PluginKVGet(ctx context.Context, pluginID int64, key string) ([]byte, error) {
|
2026-07-31 15:41:57 +02:00
|
|
|
row := d.reader.QueryRowContext(ctx,
|
2026-07-19 16:33:58 +00:00
|
|
|
`SELECT value FROM plugin_kv WHERE plugin_id = ? AND key = ?`,
|
|
|
|
|
pluginID, key,
|
|
|
|
|
)
|
|
|
|
|
var v []byte
|
|
|
|
|
if err := row.Scan(&v); err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
return v, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (d *DB) PluginKVSet(ctx context.Context, pluginID int64, key string, value []byte) error {
|
2026-07-31 15:41:57 +02:00
|
|
|
_, err := d.writer.ExecContext(ctx,
|
2026-07-19 16:33:58 +00:00
|
|
|
`INSERT INTO plugin_kv (plugin_id, key, value) VALUES (?, ?, ?)
|
|
|
|
|
ON CONFLICT(plugin_id, key) DO UPDATE SET value = excluded.value`,
|
|
|
|
|
pluginID, key, value,
|
|
|
|
|
)
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (d *DB) PluginKVDelete(ctx context.Context, pluginID int64, key string) error {
|
2026-07-31 15:41:57 +02:00
|
|
|
_, err := d.writer.ExecContext(ctx,
|
2026-07-19 16:33:58 +00:00
|
|
|
`DELETE FROM plugin_kv WHERE plugin_id = ? AND key = ?`,
|
|
|
|
|
pluginID, key,
|
|
|
|
|
)
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (d *DB) PluginKVScan(ctx context.Context, pluginID int64, prefix string, limit int) (map[string][]byte, error) {
|
2026-08-19 16:32:07 +02:00
|
|
|
// A BINARY prefix comparison, not LIKE: LIKE treats '_'/'%' in prefix as
|
|
|
|
|
// wildcards and is ASCII-case-insensitive by default, which disagrees
|
|
|
|
|
// with the exact `key = ?` match used by PluginKVGet/Set/Delete on this
|
|
|
|
|
// same table. `key >= ?` keeps the (plugin_id, key) primary-key index
|
|
|
|
|
// usable for the seek; substr(key, 1, length(?)) = ? compares under the
|
|
|
|
|
// column's default BINARY collation, so no wildcards and no case-folding.
|
2026-07-31 15:41:57 +02:00
|
|
|
rows, err := d.reader.QueryContext(ctx,
|
2026-08-19 16:32:07 +02:00
|
|
|
`SELECT key, value FROM plugin_kv
|
|
|
|
|
WHERE plugin_id = ? AND key >= ? AND substr(key, 1, length(?)) = ?
|
|
|
|
|
ORDER BY key LIMIT ?`,
|
|
|
|
|
pluginID, prefix, prefix, prefix, limit,
|
2026-07-19 16:33:58 +00:00
|
|
|
)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, fmt.Errorf("PluginKVScan: %w", err)
|
|
|
|
|
}
|
|
|
|
|
defer func() { _ = rows.Close() }()
|
|
|
|
|
out := make(map[string][]byte)
|
|
|
|
|
for rows.Next() {
|
|
|
|
|
var k string
|
|
|
|
|
var v []byte
|
|
|
|
|
if err := rows.Scan(&k, &v); err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
out[k] = v
|
|
|
|
|
}
|
|
|
|
|
return out, rows.Err()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// rowScanner is satisfied by *sql.Row and *sql.Rows.
|
|
|
|
|
type rowScanner interface {
|
|
|
|
|
Scan(dest ...any) error
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func scanPluginRow(row rowScanner) (*PluginRow, error) {
|
|
|
|
|
var p PluginRow
|
|
|
|
|
var enabledInt int64
|
|
|
|
|
var installedAt string
|
|
|
|
|
if err := row.Scan(&p.ID, &p.Name, &p.Version, &enabledInt, &p.ManifestJSON, &installedAt); err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
p.Enabled = enabledInt != 0
|
|
|
|
|
p.InstalledAt = parseSQLiteTime(installedAt)
|
|
|
|
|
return &p, nil
|
|
|
|
|
}
|