first commit

This commit is contained in:
2024-04-08 17:17:57 +00:00
commit 3e3b9a8c61
9 changed files with 296 additions and 0 deletions

58
pkg/data/data.go Normal file
View File

@@ -0,0 +1,58 @@
package data
import (
"database/sql"
"errors"
"log"
"path/filepath"
_ "github.com/mattn/go-sqlite3"
)
type (
Repository struct {
path string
conn *sql.DB
}
)
func New(path string) *Repository {
dbPath := filepath.Join(path, "system.db")
conn, err := sql.Open("sqlite3", dbPath)
if err != nil {
log.Fatal(err)
}
r := &Repository{
path: path,
conn: conn,
}
if err := r.check(); err != nil {
log.Fatalf("[PANIC] %s", err)
}
return r
}
func (r *Repository) Close() error {
return r.conn.Close()
}
func (r *Repository) check() error {
rows, err := r.conn.Query("SELECT value FROM system WHERE key = 'db.version'")
if err != nil {
m := new(migratorVersion1)
m.conn = r.conn
return m.create()
}
defer rows.Close()
var version string
if !rows.Next() {
return errors.New("no version found in the system database")
}
if err := rows.Scan(&version); err != nil {
return err
}
log.Printf("[INFO] database loaded with version %s", version)
return nil
}