Still migrating to postgres, have to check that everything works and fix stuff
This commit is contained in:
+16
-21
@@ -1,15 +1,15 @@
|
||||
package expenses
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"context"
|
||||
"expenses/config"
|
||||
"log"
|
||||
|
||||
"github.com/jackc/pgx/v5"
|
||||
lua "github.com/yuin/gopher-lua"
|
||||
"log"
|
||||
)
|
||||
|
||||
type ExpensesApp struct {
|
||||
DB *sql.DB
|
||||
DB *pgx.Conn
|
||||
Lua *lua.LState
|
||||
}
|
||||
|
||||
@@ -17,7 +17,7 @@ var (
|
||||
App *ExpensesApp
|
||||
)
|
||||
|
||||
func NewExpenseApp(db *sql.DB, luaVM *lua.LState) *ExpensesApp {
|
||||
func NewExpenseApp(db *pgx.Conn, luaVM *lua.LState) *ExpensesApp {
|
||||
return &ExpensesApp{
|
||||
DB: db,
|
||||
Lua: luaVM,
|
||||
@@ -25,19 +25,12 @@ func NewExpenseApp(db *sql.DB, luaVM *lua.LState) *ExpensesApp {
|
||||
}
|
||||
|
||||
func (a *ExpensesApp) Close() {
|
||||
a.DB.Close()
|
||||
ctx := context.Background()
|
||||
|
||||
a.DB.Close(ctx)
|
||||
a.Lua.Close()
|
||||
}
|
||||
|
||||
func openDB(sys string, path string, extra string) (*sql.DB, error) {
|
||||
db, err := sql.Open(sys, "file:"+path+extra)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return db, nil
|
||||
}
|
||||
|
||||
func StartLuaVM() (*lua.LState, error) {
|
||||
L := lua.NewState()
|
||||
App.registerLuaBinds(L)
|
||||
@@ -46,14 +39,16 @@ func StartLuaVM() (*lua.LState, error) {
|
||||
|
||||
func StartApp() error {
|
||||
config.SetConfig("./config.yaml")
|
||||
cfg := config.GetInstance()
|
||||
// cfg := config.GetInstance()
|
||||
|
||||
migDB, err := openDB(cfg.DBSys, cfg.DBPath, "")
|
||||
ctx := context.Background()
|
||||
pgStr := "port=5431 host=127.0.0.1 user=expuser dbname=expdb password=exppass"
|
||||
conn, err := pgx.Connect(ctx, pgStr)
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to open migration DB: %v", err)
|
||||
}
|
||||
|
||||
App = NewExpenseApp(migDB, nil)
|
||||
App = NewExpenseApp(conn, nil)
|
||||
|
||||
luaVM, err := StartLuaVM()
|
||||
if err != nil {
|
||||
@@ -61,17 +56,17 @@ func StartApp() error {
|
||||
}
|
||||
App.Lua = luaVM
|
||||
|
||||
err = RunMigrations(migDB, luaVM)
|
||||
err = RunMigrations(conn, luaVM)
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to apply migrations: %v", err)
|
||||
}
|
||||
App.Close()
|
||||
|
||||
db, err := openDB(cfg.DBSys, cfg.DBPath, "?_foreign_keys=on")
|
||||
conn, err = pgx.Connect(ctx, pgStr)
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to open DB: %v", err)
|
||||
}
|
||||
App = NewExpenseApp(db, luaVM)
|
||||
App = NewExpenseApp(conn, luaVM)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user