db: integrate golang-migrate for schema migrations
- internal/db/migrations/: five embedded SQL files containing
the migration SQL previously held as Go string literals.
Files follow the NNN_description.up.sql naming convention
required by golang-migrate's iofs source.
- internal/db/migrate.go: rewritten to use
github.com/golang-migrate/migrate/v4 with the
database/sqlite driver (modernc.org/sqlite, pure Go) and
source/iofs for compile-time embedded SQL.
- newMigrate() opens a dedicated *sql.DB so m.Close() does
not affect the caller's shared connection.
- Migrate() includes a compatibility shim: reads the legacy
schema_version table and calls m.Force(v) before m.Up()
so existing databases are not re-migrated.
- LatestSchemaVersion promoted from var to const.
- internal/db/db.go: added path field to DB struct; Open()
translates ':memory:' to a named shared-cache URI
(file:mcias_N?mode=memory&cache=shared) so the migration
runner can open a second connection to the same in-memory
database without sharing the handle that golang-migrate
will close on teardown.
- go.mod: added golang-migrate/migrate/v4 v4.19.1 (direct).
All callers unchanged. All tests pass; golangci-lint clean.
This commit is contained in:
24
internal/db/migrations/000005_pgcred_access.up.sql
Normal file
24
internal/db/migrations/000005_pgcred_access.up.sql
Normal file
@@ -0,0 +1,24 @@
|
||||
-- Track which accounts own each set of pg_credentials and which other
|
||||
-- accounts have been granted read access to them.
|
||||
--
|
||||
-- owner_id: the account that administers the credentials and may grant/revoke
|
||||
-- access. Defaults to the system account itself. This column is
|
||||
-- nullable so that rows created before migration 5 are not broken.
|
||||
ALTER TABLE pg_credentials ADD COLUMN owner_id INTEGER REFERENCES accounts(id);
|
||||
|
||||
-- pg_credential_access records an explicit "all-or-nothing" read grant from
|
||||
-- the credential owner to another account. Grantees may view connection
|
||||
-- metadata (host, port, database, username) but the password is never
|
||||
-- decrypted for them in the UI. Only the owner may update or delete the
|
||||
-- credential set.
|
||||
CREATE TABLE IF NOT EXISTS pg_credential_access (
|
||||
id INTEGER PRIMARY KEY,
|
||||
credential_id INTEGER NOT NULL REFERENCES pg_credentials(id) ON DELETE CASCADE,
|
||||
grantee_id INTEGER NOT NULL REFERENCES accounts(id) ON DELETE CASCADE,
|
||||
granted_by INTEGER REFERENCES accounts(id),
|
||||
granted_at TEXT NOT NULL DEFAULT (strftime('%Y-%m-%dT%H:%M:%SZ','now')),
|
||||
UNIQUE (credential_id, grantee_id)
|
||||
);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_pgcred_access_cred ON pg_credential_access (credential_id);
|
||||
CREATE INDEX IF NOT EXISTS idx_pgcred_access_grantee ON pg_credential_access (grantee_id);
|
||||
Reference in New Issue
Block a user