Files
Anya Tokugawa c1175d389f Implement Knowledge City platform, organizer and DAV integration
Capture accumulated application, UI, CLI, regression tests and design documentation changes, including task project mapping and default CalDAV list naming.

Keep deployment data, backups, local credentials and build artifacts out of Git. Move the Compose enrollment token to an ignored local environment file.
2026-09-27 14:44:43 +09:00

92 lines
2.1 KiB
Go

package db
import (
"database/sql"
"net/url"
"path/filepath"
_ "modernc.org/sqlite"
)
func Open(path string) (*sql.DB, error) {
absolutePath, err := filepath.Abs(path)
if err != nil {
return nil, err
}
dsn := (&url.URL{
Scheme: "file",
Path: absolutePath,
RawQuery: url.Values{
"_pragma": {
"busy_timeout(5000)",
"journal_mode(WAL)",
"synchronous(NORMAL)",
},
}.Encode(),
}).String()
return sql.Open("sqlite", dsn)
}
func Init(db *sql.DB) error {
stmts := []string{
// objects: has UUID, kind, title
`
CREATE TABLE IF NOT EXISTS objects (
uuid TEXT PRIMARY KEY,
kind TEXT NOT NULL,
title TEXT NOT NULL,
created_at INTEGER NOT NULL
)
`,
// revisions: immutable event metadata referencing a Document hash
`
CREATE TABLE IF NOT EXISTS revisions (
uuid TEXT PRIMARY KEY,
object_uuid TEXT NOT NULL,
body TEXT NOT NULL,
document_hash TEXT NOT NULL,
lang TEXT NOT NULL DEFAULT 'und',
content_type TEXT NOT NULL DEFAULT 'text/html; charset=utf-8',
filename TEXT NOT NULL DEFAULT '',
message TEXT NOT NULL,
created_at_ms INTEGER NOT NULL,
created_at INTEGER NOT NULL
)
`,
// paths: path -> object_uuid, created_at
`
CREATE TABLE IF NOT EXISTS paths (
path TEXT PRIMARY KEY,
object_uuid TEXT NOT NULL,
created_at INTEGER NOT NULL
)
`,
// tags: object_uuid --> tag
`
CREATE TABLE IF NOT EXISTS tags (
object_uuid TEXT NOT NULL,
tag TEXT NOT NULL
)
`,
`
CREATE TABLE IF NOT EXISTS assets (
address TEXT PRIMARY KEY,
object_uuid TEXT NOT NULL,
head_document_hash TEXT,
created_at_ms INTEGER NOT NULL,
updated_at_ms INTEGER NOT NULL
)
`,
}
for _, stmt := range stmts {
_, err := db.Exec(stmt)
if err != nil {
return err
}
}
return nil
}