fix "default templates are re-added upon restart"

Mentioned in #161
This commit is contained in:
2024-07-08 10:19:44 +02:00
parent 3edebbdb6c
commit c0c2fcb009
4 changed files with 37 additions and 7 deletions

1
.gitignore vendored
View File

@@ -19,3 +19,4 @@ ui/
frontend/.pnp.cjs frontend/.pnp.cjs
frontend/.pnp.loader.mjs frontend/.pnp.loader.mjs
frontend/.yarn/install-state.gz frontend/.yarn/install-state.gz
.db.lock

View File

@@ -2,6 +2,7 @@ package config
import ( import (
"os" "os"
"path/filepath"
"sync" "sync"
"gopkg.in/yaml.v3" "gopkg.in/yaml.v3"
@@ -20,6 +21,7 @@ type Config struct {
Password string `yaml:"password"` Password string `yaml:"password"`
QueueSize int `yaml:"queue_size"` QueueSize int `yaml:"queue_size"`
SessionFilePath string `yaml:"session_file_path"` SessionFilePath string `yaml:"session_file_path"`
path string
} }
var ( var (
@@ -43,9 +45,17 @@ func (c *Config) LoadFile(filename string) error {
return err return err
} }
c.path = filename
if err := yaml.NewDecoder(fd).Decode(c); err != nil { if err := yaml.NewDecoder(fd).Decode(c); err != nil {
return err return err
} }
return nil return nil
} }
// Path of the directory containing the config file
func (c *Config) Dir() string { return filepath.Dir(c.path) }
// Absolute path of the config file
func (c *Config) Path() string { return c.path }

View File

@@ -3,29 +3,41 @@ package dbutil
import ( import (
"context" "context"
"database/sql" "database/sql"
"os"
"path/filepath"
"github.com/marcopeocchi/yt-dlp-web-ui/server/config"
) )
var lockFilePath = filepath.Join(config.Instance().Dir(), ".db.lock")
// Run the table migration // Run the table migration
func AutoMigrate(ctx context.Context, db *sql.DB) error { func Migrate(ctx context.Context, db *sql.DB) error {
conn, err := db.Conn(ctx) conn, err := db.Conn(ctx)
if err != nil { if err != nil {
return err return err
} }
defer conn.Close() defer func() {
conn.Close()
createLockFile()
}()
_, err = db.ExecContext( if _, err := db.ExecContext(
ctx, ctx,
`CREATE TABLE IF NOT EXISTS templates ( `CREATE TABLE IF NOT EXISTS templates (
id CHAR(36) PRIMARY KEY, id CHAR(36) PRIMARY KEY,
name VARCHAR(255) NOT NULL, name VARCHAR(255) NOT NULL,
content TEXT NOT NULL content TEXT NOT NULL
)`, )`,
) ); err != nil {
if err != nil {
return err return err
} }
if lockFileExists() {
return nil
}
db.ExecContext( db.ExecContext(
ctx, ctx,
`INSERT INTO templates (id, name, content) VALUES `INSERT INTO templates (id, name, content) VALUES
@@ -35,5 +47,12 @@ func AutoMigrate(ctx context.Context, db *sql.DB) error {
"1", "audio only", "-x", "1", "audio only", "-x",
) )
return err return nil
}
func createLockFile() { os.Create(lockFilePath) }
func lockFileExists() bool {
_, err := os.Stat(lockFilePath)
return os.IsExist(err)
} }

View File

@@ -84,7 +84,7 @@ func RunBlocking(cfg *RunConfig) {
logger.Error("failed to open database", slog.String("err", err.Error())) logger.Error("failed to open database", slog.String("err", err.Error()))
} }
if err := dbutil.AutoMigrate(context.Background(), db); err != nil { if err := dbutil.Migrate(context.Background(), db); err != nil {
logger.Error("failed to init database", slog.String("err", err.Error())) logger.Error("failed to init database", slog.String("err", err.Error()))
} }