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 (
"github.com/KristianJBorgwarth/dendrite.daemon/core/http"
"github.com/KristianJBorgwarth/dendrite.daemon/core/logging"
_ "modernc.org/sqlite"
)
func main() {

View file

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

View file

@ -27,6 +27,10 @@ func (s *Server) Start() error {
mux := http.NewServeMux()
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)
}

BIN
dendrite

Binary file not shown.

View file

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