Changes to the configuration. Will help with setting up DB with sockets

This commit is contained in:
2025-09-22 18:39:38 +01:00
parent 84601cfef0
commit 22d34a4b0b
7 changed files with 57 additions and 37 deletions
+4 -4
View File
@@ -4,7 +4,7 @@ postgres:
user: expdbuser
pass: thepass
db: thedb
host: 127.0.0.1
port: 5431
migrations_path: "file://expenses/repo/pgdb/migrations/"
mig_custom_scripts: "./expenses/repo/pgdb/migrations/custom/"
host: /home/esilva/Desktop/projetos/expenses/src/pgImage/pgSocket/
port: 5432
migrationsPath: "file://expenses/repo/pgdb/migrations/"
migCustomScripts: "./expenses/repo/pgdb/migrations/custom/"
+3 -3
View File
@@ -18,13 +18,13 @@ type PostgresConfig struct {
Pass string `yaml:"pass"`
}
// Configuration defines the base configuration of the expenses app
// Configuration defines the base configuration of the expenses app.
type Configuration struct {
AssetsDir string `yaml:"assets"`
Port string `yaml:"port"`
PgCfg *PostgresConfig `yaml:"postgres"`
MigrationsPath string `yaml:"migrations_path"`
MigCustomScript string `yaml:"mig_custom_scripts"`
MigrationsPath string `yaml:"migrationsPath"`
MigCustomScript string `yaml:"migCustomScripts"`
}
var (
+30 -15
View File
@@ -3,8 +3,8 @@ package expenses
import (
"context"
"fmt"
"log"
"strings"
"github.com/ESilva15/expenses/config"
"github.com/ESilva15/expenses/expenses/repo"
@@ -55,19 +55,15 @@ func startLuaVM() (*lua.LState, error) {
}
func getPgConnString(cfg *config.Configuration) string {
var s strings.Builder
s.WriteString("port=" + cfg.PgCfg.Port + " ")
s.WriteString("host=" + cfg.PgCfg.Host + " ")
s.WriteString("user=" + cfg.PgCfg.User + " ")
s.WriteString("dbname=" + cfg.PgCfg.DB + " ")
s.WriteString("password=" + cfg.PgCfg.Pass)
return s.String()
return fmt.Sprintf("postgres://%s:%s@/%s?host=%s&port=%s&sslmode=disable",
cfg.PgCfg.User, cfg.PgCfg.Pass, cfg.PgCfg.DB, cfg.PgCfg.Host, cfg.PgCfg.Port)
}
func runMigrations(luaVM *lua.LState) error {
migrator, err := repo.NewPgMigrator()
cfg := config.GetInstance()
pgStr := getPgConnString(cfg)
migrator, err := repo.NewPgMigrator(pgStr)
if err != nil {
return err
}
@@ -84,7 +80,10 @@ func runMigrations(luaVM *lua.LState) error {
// GoToMigration provides a way to force moving to the specified migration.
func (a *ExpApp) GoToMigration(id uint) error {
migrator, err := repo.NewPgMigrator()
cfg := config.GetInstance()
pgStr := getPgConnString(cfg)
migrator, err := repo.NewPgMigrator(pgStr)
if err != nil {
return err
}
@@ -92,12 +91,28 @@ func (a *ExpApp) GoToMigration(id uint) error {
return migrator.Goto(id)
}
func createPGXPoolConn(ctx context.Context) (*pgxpool.Pool, error) {
cfg := config.GetInstance()
pgStr := getPgConnString(cfg)
conn, err := pgxpool.New(ctx, pgStr)
if err != nil {
return nil, err
}
var result int
err = conn.QueryRow(ctx, "SELECT 1").Scan(&result)
if err != nil {
panic("connection test query failed: " + err.Error())
}
return conn, nil
}
// StartApp will configure and open the required connections to run this app.
func StartApp() error {
config.SetConfig("./config.yaml")
cfg := config.GetInstance()
ctx := context.Background()
pgStr := getPgConnString(cfg)
// Create the LuaVM
luaVM, err := startLuaVM()
@@ -112,7 +127,7 @@ func StartApp() error {
}
// Create our final app thing
conn, err := pgxpool.New(ctx, pgStr)
conn, err := createPGXPoolConn(ctx)
if err != nil {
log.Fatalf("Failed to open DB: %v", err)
}
+13 -9
View File
@@ -1,6 +1,7 @@
package repo
import (
"errors"
"log"
"os"
"path/filepath"
@@ -15,19 +16,20 @@ import (
lua "github.com/yuin/gopher-lua"
)
const LAST_GO_MIGRATION = 3
// LastGoMigration defines the last migration execute with go code. After
// that the lua API should be used.
const LastGoMigration = 3
// PgMigrator implements the Migrator interface for the PG connection.
type PgMigrator struct {
Mig *mig.Migrate
}
func NewPgMigrator() (Migrator, error) {
// NewPgMigrator creates and returns a new migrator.
func NewPgMigrator(connStr string) (PgMigrator, error) {
cfg := config.GetInstance()
str := "postgres://" + cfg.PgCfg.User + ":" + cfg.PgCfg.Pass + "@" +
cfg.PgCfg.Host + ":" + cfg.PgCfg.Port + "/" + cfg.PgCfg.DB + "?sslmode=disable"
m, err := mig.New(cfg.MigrationsPath, str)
m, err := mig.New(cfg.MigrationsPath, connStr)
if err != nil {
return PgMigrator{}, err
}
@@ -37,13 +39,15 @@ func NewPgMigrator() (Migrator, error) {
}, nil
}
// Close closes what is necessary to close on a PgMigrator.
func (p PgMigrator) Close() {
p.Mig.Close()
}
func (a PgMigrator) Goto(id uint) error {
err := a.Mig.Migrate(id)
if err != nil && err != mig.ErrNoChange {
// Goto forces the migrations to a given migrations of id: `id`.
func (p PgMigrator) Goto(id uint) error {
err := p.Mig.Migrate(id)
if err != nil && !errors.Is(err, mig.ErrNoChange) {
return err
}
+5 -6
View File
@@ -2,7 +2,6 @@ package pages
import (
"context"
"errors"
"fmt"
"log"
"time"
@@ -148,28 +147,28 @@ func expenseFromForm(ctx context.Context, c *gin.Context) (*mod.Expense, error)
func fetchExpensesData(c *gin.Context) (map[string]any, error) {
ctx, err := getLoggedInUserCTX(c)
if err != nil {
return nil, errors.New(fmt.Sprintf("could not get logged in user - ", err))
return nil, fmt.Errorf("could not get logged in user - %v", err)
}
eFilter := repo.NewExpFilter()
expenses, err := exp.App.GetAllExpenses(ctx, eFilter)
if err != nil {
return nil, errors.New(fmt.Sprintf("could not get expenses - ", err))
return nil, fmt.Errorf("could not get expenses - %v", err)
}
categories, err := exp.App.GetAllCategories(ctx)
if err != nil {
return nil, errors.New(fmt.Sprintf("could not get categories - ", err))
return nil, fmt.Errorf("could not get categories - %v", err)
}
stores, err := exp.App.GetAllStores(ctx)
if err != nil {
return nil, errors.New(fmt.Sprintf("could not get stores - ", err))
return nil, fmt.Errorf("could not get stores - %v", err)
}
types, err := exp.App.GetAllTypes(ctx)
if err != nil {
return nil, errors.New(fmt.Sprintf("could not get types - ", err))
return nil, fmt.Errorf("could not get types - %v", err)
}
return map[string]any{
+1
View File
@@ -1,3 +1,4 @@
schema.sql
.env
pgsql-data-dev/
pgSocket/
+1
View File
@@ -31,6 +31,7 @@ test: remove-dev-container
--name $(container)-dev \
--env-file .env \
-v ./pgsql-data-dev/:/var/lib/postgresql/data/ \
-v ./pgSocket:/run/postgresql \
-it $(registry)/$(repo)/expensesdb:v0.0.91-alpha
build_image: