migrations cmd parameter

This commit is contained in:
yequari 2026-01-03 13:14:24 -07:00
parent ebabd6c14d
commit efeda7662d
2 changed files with 15 additions and 16 deletions

View File

@ -11,6 +11,7 @@ import (
"net/http"
"net/url"
"os"
"path/filepath"
"strconv"
"strings"
"time"
@ -57,14 +58,16 @@ type application struct {
}
type appInstaller struct {
app *application
srv *http.Server
installModel models.InstallModelInterface
app *application
srv *http.Server
installModel models.InstallModelInterface
migrationsPath string
}
func main() {
addr := flag.String("addr", ":3000", "HTTP network address")
dsn := flag.String("dsn", "guestbook.db", "data source name")
migrations := flag.String("migrations", "migrations", "folder containing sql migrations")
debug := flag.Bool("debug", false, "enable debug mode")
env := flag.String("env", ".env", ".env file path")
flag.Parse()
@ -113,8 +116,9 @@ func main() {
}
installer := &appInstaller{
app: app,
installModel: &models.InstallModel{DB: db},
app: app,
installModel: &models.InstallModel{DB: db},
migrationsPath: filepath.Clean(*migrations),
}
installer.srv = &http.Server{
Addr: *addr,
@ -301,7 +305,7 @@ func walkTzDir(path string, zones []string) []string {
func runInstaller(i *appInstaller) error {
i.app.logger.Info("Performing migrations")
err := i.installModel.SetupDatabase()
err := i.installModel.SetupDatabase(i.migrationsPath)
if err != nil {
return err
}

View File

@ -4,7 +4,7 @@ import (
"database/sql"
"errors"
"fmt"
"os"
"path/filepath"
"time"
"github.com/golang-migrate/migrate/v4"
@ -14,26 +14,21 @@ import (
)
type InstallModel struct {
DB *sql.DB
DBPath string
DB *sql.DB
}
type InstallModelInterface interface {
SetupDatabase() error
SetupDatabase(migrations string) error
SetInstalledFlag() error
GetInstalledFlag() (bool, error)
}
func (m *InstallModel) SetupDatabase() error {
func (m *InstallModel) SetupDatabase(migrations string) error {
driver, err := sqlite3.WithInstance(m.DB, &sqlite3.Config{})
if err != nil {
return err
}
wd, err := os.Getwd()
if err != nil {
return err
}
mFolder := fmt.Sprintf("file://%s/migrations", wd)
mFolder := fmt.Sprintf("file://%s", filepath.Clean(migrations))
fmt.Printf("migrations %s\n", mFolder)
mi, err := migrate.NewWithDatabaseInstance(mFolder, "sqlite", driver)
if err != nil {