diff --git a/src/config.yaml b/src/config.yaml index c3c177c..a624e0e 100644 --- a/src/config.yaml +++ b/src/config.yaml @@ -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/" diff --git a/src/config/config.go b/src/config/config.go index 18e1195..7fc7b29 100644 --- a/src/config/config.go +++ b/src/config/config.go @@ -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 ( diff --git a/src/expenses/app.go b/src/expenses/app.go index e522475..b36d217 100644 --- a/src/expenses/app.go +++ b/src/expenses/app.go @@ -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) } diff --git a/src/expenses/repo/pg_migrations.go.go b/src/expenses/repo/pg_migrations.go.go index 5ca97fc..c7e2cca 100644 --- a/src/expenses/repo/pg_migrations.go.go +++ b/src/expenses/repo/pg_migrations.go.go @@ -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 } diff --git a/src/pages/expensesHelper.go b/src/pages/expensesHelper.go index a779e2a..acf48f7 100644 --- a/src/pages/expensesHelper.go +++ b/src/pages/expensesHelper.go @@ -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{ diff --git a/src/pgImage/.gitignore b/src/pgImage/.gitignore index 8b6a8f9..cad00c8 100644 --- a/src/pgImage/.gitignore +++ b/src/pgImage/.gitignore @@ -1,3 +1,4 @@ schema.sql .env pgsql-data-dev/ +pgSocket/ diff --git a/src/pgImage/Makefile b/src/pgImage/Makefile index f8660b6..c61110f 100644 --- a/src/pgImage/Makefile +++ b/src/pgImage/Makefile @@ -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: