From cec7111adaa1b73ea4632fdf89dfdb4950964a4c Mon Sep 17 00:00:00 2001 From: oppiliappan Date: Tue, 09 Dec 2025 09:41:41 +0000 Subject: [PATCH] knotserver: follow common module nomenclature scheme `init` is a bad name for a file, a method inside the file could be called `init` perhaps. this is already the scheme followed by spindle and appview. Signed-off-by: oppiliappan --- knotserver/db/db.go | 64 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ knotserver/db/init.go | 64 ---------------------------------------------------------------- 2 file(s) changed, 64 insertion(s)(+), 64 deletion(s)(-) diff --git a/knotserver/db/db.go b/knotserver/db/db.go new file mode 100644 --- /dev/null +++ b/knotserver/db/db.go @@ -0,0 +1,64 @@ +package db + +import ( + "database/sql" + "strings" + + _ "github.com/mattn/go-sqlite3" +) + +type DB struct { + db *sql.DB +} + +func Setup(dbPath string) (*DB, error) { + // https://github.com/mattn/go-sqlite3#connection-string + opts := []string{ + "_foreign_keys=1", + "_journal_mode=WAL", + "_synchronous=NORMAL", + "_auto_vacuum=incremental", + } + + db, err := sql.Open("sqlite3", dbPath+"?"+strings.Join(opts, "&")) + if err != nil { + return nil, err + } + + // NOTE: If any other migration is added here, you MUST + // copy the pattern in appview: use a single sql.Conn + // for every migration. + + _, err = db.Exec(` + create table if not exists known_dids ( + did text primary key + ); + + create table if not exists public_keys ( + id integer primary key autoincrement, + did text not null, + key text not null, + created text not null default (strftime('%Y-%m-%dT%H:%M:%SZ', 'now')), + unique(did, key), + foreign key (did) references known_dids(did) on delete cascade + ); + + create table if not exists _jetstream ( + id integer primary key autoincrement, + last_time_us integer not null + ); + + create table if not exists events ( + rkey text not null, + nsid text not null, + event text not null, -- json + created integer not null default (strftime('%s', 'now')), + primary key (rkey, nsid) + ); + `) + if err != nil { + return nil, err + } + + return &DB{db: db}, nil +} diff --git a/knotserver/db/init.go b/knotserver/db/init.go deleted file mode 100644 --- a/knotserver/db/init.go +++ /dev/null @@ -1,64 +0,0 @@ -package db - -import ( - "database/sql" - "strings" - - _ "github.com/mattn/go-sqlite3" -) - -type DB struct { - db *sql.DB -} - -func Setup(dbPath string) (*DB, error) { - // https://github.com/mattn/go-sqlite3#connection-string - opts := []string{ - "_foreign_keys=1", - "_journal_mode=WAL", - "_synchronous=NORMAL", - "_auto_vacuum=incremental", - } - - db, err := sql.Open("sqlite3", dbPath+"?"+strings.Join(opts, "&")) - if err != nil { - return nil, err - } - - // NOTE: If any other migration is added here, you MUST - // copy the pattern in appview: use a single sql.Conn - // for every migration. - - _, err = db.Exec(` - create table if not exists known_dids ( - did text primary key - ); - - create table if not exists public_keys ( - id integer primary key autoincrement, - did text not null, - key text not null, - created text not null default (strftime('%Y-%m-%dT%H:%M:%SZ', 'now')), - unique(did, key), - foreign key (did) references known_dids(did) on delete cascade - ); - - create table if not exists _jetstream ( - id integer primary key autoincrement, - last_time_us integer not null - ); - - create table if not exists events ( - rkey text not null, - nsid text not null, - event text not null, -- json - created integer not null default (strftime('%s', 'now')), - primary key (rkey, nsid) - ); - `) - if err != nil { - return nil, err - } - - return &DB{db: db}, nil -} -- tangled.sh