Monorepo for Tangled tangled.org

appview/db: make registration.secrets nullable and Add/DeleteKnot helpers

Signed-off-by: Anirudh Oppiliappan <anirudh@tangled.sh>

anirudh.fi eb4ce023 5e108636

verified
Changed files
+52
appview
+22
appview/db/db.go
··· 600 600 return nil 601 601 }) 602 602 603 + // make registrations.secret nullable for unified registration flow 604 + runMigration(db, "make-registrations-secret-nullable", func(tx *sql.Tx) error { 605 + // sqlite doesn't support ALTER COLUMN, so we need to recreate the table 606 + _, err := tx.Exec(` 607 + create table registrations_new ( 608 + id integer primary key autoincrement, 609 + domain text not null unique, 610 + did text not null, 611 + secret text, 612 + created text not null default (strftime('%Y-%m-%dT%H:%M:%SZ', 'now')), 613 + registered text 614 + ); 615 + 616 + insert into registrations_new (id, domain, did, secret, created, registered) 617 + select id, domain, did, secret, created, registered from registrations; 618 + 619 + drop table registrations; 620 + alter table registrations_new rename to registrations; 621 + `) 622 + return err 623 + }) 624 + 603 625 // recreate and add rkey + created columns with default constraint 604 626 runMigration(db, "rework-collaborators-table", func(tx *sql.Tx) error { 605 627 // create new table
+30
appview/db/registration.go
··· 6 6 "encoding/hex" 7 7 "fmt" 8 8 "log" 9 + "strings" 9 10 "time" 10 11 ) 11 12 13 + // Registration represents a knot registration. Knot would've been a better 14 + // name but we're stuck with this for historical reasons. 12 15 type Registration struct { 13 16 Id int64 14 17 Domain string ··· 187 190 188 191 return err 189 192 } 193 + 194 + func AddKnot(e Execer, domain, did string) error { 195 + _, err := e.Exec(` 196 + insert into registrations (domain, did) 197 + values (?, ?) 198 + `, domain, did) 199 + return err 200 + } 201 + 202 + func DeleteKnot(e Execer, filters ...filter) error { 203 + var conditions []string 204 + var args []any 205 + for _, filter := range filters { 206 + conditions = append(conditions, filter.Condition()) 207 + args = append(args, filter.Arg()...) 208 + } 209 + 210 + whereClause := "" 211 + if conditions != nil { 212 + whereClause = " where " + strings.Join(conditions, " and ") 213 + } 214 + 215 + query := fmt.Sprintf(`delete from registrations %s`, whereClause) 216 + 217 + _, err := e.Exec(query, args...) 218 + return err 219 + }