From 395156e1d2be90801ee96e9ee26e7c30a4eb4385 Mon Sep 17 00:00:00 2001 From: Anirudh Oppiliappan Date: Thu, 14 Aug 2025 21:04:08 +0300 Subject: [PATCH] appview/db: make registration.secrets nullable and Add/DeleteKnot helpers Signed-off-by: Anirudh Oppiliappan --- appview/db/db.go | 25 +++++++++++++++++++++++++ appview/db/registration.go | 30 ++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+) diff --git a/appview/db/db.go b/appview/db/db.go index 45cafa30..ee37c9c2 100644 --- a/appview/db/db.go +++ b/appview/db/db.go @@ -612,6 +612,31 @@ func Make(dbPath string) (*DB, error) { return nil }) + // drop all knot secrets, add unique constraint to knots + // + // knots will henceforth use service auth for signed requests + runMigration(conn, "no-more-secrets", func(tx *sql.Tx) error { + _, err := tx.Exec(` + create table registrations_new ( + id integer primary key autoincrement, + domain text not null, + did text not null, + created text not null default (strftime('%Y-%m-%dT%H:%M:%SZ', 'now')), + registered text, + read_only integer not null default 0, + unique(domain, did) + ); + + insert into registrations_new (id, domain, did, created, registered, read_only) + select id, domain, did, created, registered, 1 from registrations + where registered is not null; + + drop table registrations; + alter table registrations_new rename to registrations; + `) + return err + }) + // recreate and add rkey + created columns with default constraint runMigration(conn, "rework-collaborators-table", func(tx *sql.Tx) error { // create new table diff --git a/appview/db/registration.go b/appview/db/registration.go index a1d1774a..b0b57bab 100644 --- a/appview/db/registration.go +++ b/appview/db/registration.go @@ -6,9 +6,12 @@ import ( "encoding/hex" "fmt" "log" + "strings" "time" ) +// Registration represents a knot registration. Knot would've been a better +// name but we're stuck with this for historical reasons. type Registration struct { Id int64 Domain string @@ -187,3 +190,30 @@ func Register(e Execer, domain string) error { return err } + +func AddKnot(e Execer, domain, did string) error { + _, err := e.Exec(` + insert into registrations (domain, did) + values (?, ?) + `, domain, did) + return err +} + +func DeleteKnot(e Execer, filters ...filter) error { + var conditions []string + var args []any + for _, filter := range filters { + conditions = append(conditions, filter.Condition()) + args = append(args, filter.Arg()...) + } + + whereClause := "" + if conditions != nil { + whereClause = " where " + strings.Join(conditions, " and ") + } + + query := fmt.Sprintf(`delete from registrations %s`, whereClause) + + _, err := e.Exec(query, args...) + return err +} -- knotmirror 0.0.1