fix(db_migrator): added dependency and embedded migration scripts

This commit is contained in:
Kristian Borgwarth 2026-03-22 19:42:05 +01:00
parent e18577b53d
commit c160bff2f8
6 changed files with 40 additions and 16 deletions

View file

@ -3,6 +3,7 @@ package main
import ( import (
"github.com/KristianJBorgwarth/dendrite.daemon/core/http" "github.com/KristianJBorgwarth/dendrite.daemon/core/http"
"github.com/KristianJBorgwarth/dendrite.daemon/core/logging" "github.com/KristianJBorgwarth/dendrite.daemon/core/logging"
_ "modernc.org/sqlite"
) )
func main() { func main() {

View file

@ -2,6 +2,8 @@ package handlers
import ( import (
"encoding/json" "encoding/json"
"log/slog"
"github.com/KristianJBorgwarth/dendrite.daemon/config" "github.com/KristianJBorgwarth/dendrite.daemon/config"
"github.com/KristianJBorgwarth/dendrite.daemon/persistence" "github.com/KristianJBorgwarth/dendrite.daemon/persistence"
) )
@ -25,6 +27,7 @@ type initializeCommand struct {
func Initialize(raw json.RawMessage) (*config.Config, error) { func Initialize(raw json.RawMessage) (*config.Config, error) {
var params initializeCommand var params initializeCommand
slog.Info("initializing with params", "params", string(raw))
if err := json.Unmarshal(raw, &params); err != nil { if err := json.Unmarshal(raw, &params); err != nil {
return nil, err return nil, err
} }

View file

@ -27,6 +27,10 @@ func (s *Server) Start() error {
mux := http.NewServeMux() mux := http.NewServeMux()
mux.HandleFunc("/rpc", s.RPCHandler) mux.HandleFunc("/rpc", s.RPCHandler)
mux.HandleFunc("/health", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
w.Write([]byte("ok"))
})
return http.ListenAndServe(s.addr, mux) return http.ListenAndServe(s.addr, mux)
} }

BIN
dendrite

Binary file not shown.

View file

@ -1,39 +1,46 @@
// Package persistence handles database schema migrations
package persistence package persistence
import ( import (
"database/sql" "database/sql"
"embed"
"log/slog" "log/slog"
"os"
"path/filepath" "path/filepath"
"sort" "sort"
) )
func InitializeIndex(vaulPath string) error { //go:embed migrations/*.sql
dbPath := filepath.Join(vaulPath, "index.db") var migrationsFS embed.FS
func InitializeIndex(vaultPath string) error {
dbPath := filepath.Join(vaultPath, "index.db")
db, err := sql.Open("sqlite", dbPath) db, err := sql.Open("sqlite", dbPath)
if err != nil { if err != nil {
slog.Error("failed to open database", "error", err)
return err return err
} }
defer db.Close() defer db.Close()
migrationsDir := filepath.Join("persistence", "migrations") if err := applyMigrations(db); err != nil {
if err := applyMigrations(db, migrationsDir); err != nil {
slog.Error("failed to apply migrations", "error", err) slog.Error("failed to apply migrations", "error", err)
return err return err
} }
return nil return nil
} }
func applyMigrations(db *sql.DB, dir string) error { func applyMigrations(db *sql.DB) error {
_, err := db.Exec(`CREATE TABLE IF NOT EXISTS schema_migrations (version TEXT PRIMARY KEY);`) _, err := db.Exec(`
CREATE TABLE IF NOT EXISTS schema_migrations (
version TEXT PRIMARY KEY
);
`)
if err != nil { if err != nil {
return err return err
} }
entries, err := os.ReadDir(dir) entries, err := migrationsFS.ReadDir("migrations")
if err != nil { if err != nil {
return err return err
} }
@ -43,7 +50,7 @@ func applyMigrations(db *sql.DB, dir string) error {
}) })
for _, e := range entries { for _, e := range entries {
if e.IsDir(){ if e.IsDir() {
continue continue
} }
@ -58,9 +65,7 @@ func applyMigrations(db *sql.DB, dir string) error {
continue continue
} }
path := filepath.Join(dir, name) sqlBytes, err := migrationsFS.ReadFile("migrations/" + name)
sqlBytes, err := os.ReadFile(path)
if err != nil { if err != nil {
return err return err
} }
@ -69,9 +74,14 @@ func applyMigrations(db *sql.DB, dir string) error {
return err return err
} }
if _, err := db.Exec(`INSERT INTO schema_migrations (version) VALUES (?)`, name); err != nil { if _, err := db.Exec(
`INSERT INTO schema_migrations (version) VALUES (?)`,
name,
); err != nil {
return err return err
} }
slog.Info("applied migration", "file", name)
} }
return nil return nil
@ -79,7 +89,11 @@ func applyMigrations(db *sql.DB, dir string) error {
func isMigrationApplied(db *sql.DB, version string) (bool, error) { func isMigrationApplied(db *sql.DB, version string) (bool, error) {
var v string var v string
err := db.QueryRow(`SELECT version FROM schema_migrations WHERE version= ?`, version).Scan(&v)
err := db.QueryRow(
`SELECT version FROM schema_migrations WHERE version = ?`,
version,
).Scan(&v)
if err == sql.ErrNoRows { if err == sql.ErrNoRows {
return false, nil return false, nil

2
persistence/doc.go Normal file
View file

@ -0,0 +1,2 @@
// Package persistence provides a simple interface for saving and loading data to and from a file.
package persistence