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
import (
"embed"
"log"
"os"
"path/filepath"
@ -15,6 +16,9 @@ type Db struct {
*sqlx.DB
}
//go:embed migrations
var scripts embed.FS
const userInsert = "INSERT INTO users (username, password) VALUES (?, ?)"
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.
// This will eventually be used on an embedded directory
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 {
return err
}
@ -75,7 +80,8 @@ func (d *Db) Migrate(dirname string) error {
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)
if err != nil {
return err