Skip to content

Commit

Permalink
fix "default templates are re-added upon restart"
Browse files Browse the repository at this point in the history
Mentioned in #161
  • Loading branch information
marcopeocchi committed Jul 8, 2024
1 parent 3edebbd commit c0c2fcb
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 7 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,4 @@ ui/
frontend/.pnp.cjs
frontend/.pnp.loader.mjs
frontend/.yarn/install-state.gz
.db.lock
10 changes: 10 additions & 0 deletions server/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package config

import (
"os"
"path/filepath"
"sync"

"gopkg.in/yaml.v3"
Expand All @@ -20,6 +21,7 @@ type Config struct {
Password string `yaml:"password"`
QueueSize int `yaml:"queue_size"`
SessionFilePath string `yaml:"session_file_path"`
path string
}

var (
Expand All @@ -43,9 +45,17 @@ func (c *Config) LoadFile(filename string) error {
return err
}

c.path = filename

if err := yaml.NewDecoder(fd).Decode(c); err != nil {
return err
}

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 }
31 changes: 25 additions & 6 deletions server/dbutil/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,41 @@ package dbutil
import (
"context"
"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
func AutoMigrate(ctx context.Context, db *sql.DB) error {
func Migrate(ctx context.Context, db *sql.DB) error {
conn, err := db.Conn(ctx)
if err != nil {
return err
}

defer conn.Close()
defer func() {
conn.Close()
createLockFile()
}()

_, err = db.ExecContext(
if _, err := db.ExecContext(
ctx,
`CREATE TABLE IF NOT EXISTS templates (
id CHAR(36) PRIMARY KEY,
name VARCHAR(255) NOT NULL,
content TEXT NOT NULL
)`,
)
if err != nil {
); err != nil {
return err
}

if lockFileExists() {
return nil
}

db.ExecContext(
ctx,
`INSERT INTO templates (id, name, content) VALUES
Expand All @@ -35,5 +47,12 @@ func AutoMigrate(ctx context.Context, db *sql.DB) error {
"1", "audio only", "-x",
)

return err
return nil
}

func createLockFile() { os.Create(lockFilePath) }

func lockFileExists() bool {
_, err := os.Stat(lockFilePath)
return os.IsExist(err)
}
2 changes: 1 addition & 1 deletion server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func RunBlocking(cfg *RunConfig) {
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()))
}

Expand Down

0 comments on commit c0c2fcb

Please sign in to comment.