Moving the migrations directory into database for embedding

This commit is contained in:
Imbus 2024-02-28 03:30:05 +01:00
parent 77e3324f79
commit 34c0712825
7 changed files with 8 additions and 2 deletions

View file

@ -1,6 +1,7 @@
package database package database
import ( import (
"embed"
"log" "log"
"os" "os"
"path/filepath" "path/filepath"
@ -15,6 +16,9 @@ type Db struct {
*sqlx.DB *sqlx.DB
} }
//go:embed migrations
var scripts embed.FS
const userInsert = "INSERT INTO users (username, password) VALUES (?, ?)" const userInsert = "INSERT INTO users (username, password) VALUES (?, ?)"
const projectInsert = "INSERT INTO projects (name, description, user_id) SELECT ?, ?, id FROM users WHERE username = ?" const projectInsert = "INSERT INTO projects (name, description, user_id) SELECT ?, ?, id FROM users WHERE username = ?"
@ -62,7 +66,8 @@ func (d *Db) AddProject(name string, description string, username string) error
// Reads a directory of migration files and applies them to the database. // Reads a directory of migration files and applies them to the database.
// This will eventually be used on an embedded directory // This will eventually be used on an embedded directory
func (d *Db) Migrate(dirname string) error { func (d *Db) Migrate(dirname string) error {
files, err := os.ReadDir(dirname) // Read the embedded scripts directory
files, err := scripts.ReadDir("migrations")
if err != nil { if err != nil {
return err return err
} }
@ -75,7 +80,8 @@ func (d *Db) Migrate(dirname string) error {
continue continue
} }
sqlFile := filepath.Join(dirname, file.Name()) // This is perhaps not the most elegant way to do this
sqlFile := filepath.Join("migrations", file.Name())
sqlBytes, err := os.ReadFile(sqlFile) sqlBytes, err := os.ReadFile(sqlFile)
if err != nil { if err != nil {
return err return err