feat(init): initialize run migrations

This commit is contained in:
Kristian Borgwarth 2026-03-22 16:10:35 +01:00
parent 1c8e19b83f
commit 7f21c39f41
5 changed files with 29 additions and 26 deletions

View file

@ -1,11 +1,6 @@
package main
import (
"database/sql"
"log"
"path/filepath"
"github.com/KristianJBorgwarth/dendrite.daemon/persistence"
"github.com/KristianJBorgwarth/dendrite.daemon/core"
_ "modernc.org/sqlite"
)
@ -15,20 +10,3 @@ func main() {
server.Run()
}
func runMigration() {
dbPath := filepath.Join("persistence", "index.db")
db, err := sql.Open("sqlite", dbPath)
if err != nil {
log.Fatal(err)
}
defer db.Close()
migrationsDir := filepath.Join("persistence", "migrations")
if err := persistence.ApplyMigrations(db, migrationsDir); err != nil {
log.Fatal(err)
}
log.Println("migrations applied successfully")
}

View file

@ -2,8 +2,8 @@ package handlers
import (
"encoding/json"
"github.com/KristianJBorgwarth/dendrite.daemon/config"
"github.com/KristianJBorgwarth/dendrite.daemon/persistence"
)
type initializeParams struct {
@ -24,6 +24,7 @@ type initializeParams struct {
func Initialize(raw json.RawMessage) (*config.Config, error) {
var params initializeParams
if err := json.Unmarshal(raw, &params); err != nil {
return nil, err
}
@ -41,7 +42,14 @@ func Initialize(raw json.RawMessage) (*config.Config, error) {
FilenameFormat: params.DailyNote.FilenameFormat,
},
}
cfg.SetDefaults()
err := persistence.InitializeIndex(cfg.VaultPath)
if err != nil {
return nil, err
}
return cfg, nil
}

View file

@ -115,13 +115,13 @@ func (s *Server) handleRequest(req types.Request) {
err error
)
// move to handler package?
switch req.Method {
case "initialize":
s.config, err = handlers.Initialize(req.Params)
result = map[string]any{"status": "ok"}
default:
err = fmt.Errorf("unknown method: %s", req.Method)
}
s.writeResponse(req.ID, result, err)

BIN
dendrite

Binary file not shown.

View file

@ -1,5 +1,5 @@
// Package persistence handles database schema migrations
package persistence
package persistence
import (
"database/sql"
@ -8,7 +8,24 @@ import (
"sort"
)
func ApplyMigrations(db *sql.DB, dir string) error {
func InitializeIndex(vaulPath string) error {
dbPath := filepath.Join(vaulPath, "index.db")
db, err := sql.Open("sqlite", dbPath)
if err != nil {
return err
}
defer db.Close()
migrationsDir := filepath.Join("persistence", "migrations")
if err := applyMigrations(db, migrationsDir); err != nil {
return err
}
return nil
}
func applyMigrations(db *sql.DB, dir string) error {
_, err := db.Exec(`CREATE TABLE IF NOT EXISTS schema_migrations (version TEXT PRIMARY KEY);`)
if err != nil {
return err