Rome wasn't built in a day my guys
This commit is contained in:
@@ -23,9 +23,9 @@ func migrate(cmd *cobra.Command, args []string) {
|
||||
log.Fatalf("%s is an invalid migration ID", args[1])
|
||||
}
|
||||
|
||||
err = expenses.App.Goto(uint(migID))
|
||||
err = expenses.App.GoToMigration(uint(migID))
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to apply migration: %v", err)
|
||||
log.Fatalf("Failed to move to migration `%d`: %+v", migID, err)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+4
-4
@@ -1,10 +1,10 @@
|
||||
assets: "./assets/"
|
||||
port: 8081
|
||||
postgres:
|
||||
user: expuser
|
||||
pass: exppass
|
||||
db: expdb
|
||||
user: expdbuser
|
||||
pass: thepass
|
||||
db: thedb
|
||||
host: 127.0.0.1
|
||||
port: 5431
|
||||
migrations_path: "file://expenses/db/migrations/"
|
||||
migrations_path: "file://expenses/repo/pgdb/migrations/"
|
||||
mig_custom_scripts: "./expenses/db/migrations/custom/"
|
||||
|
||||
+30
-15
@@ -7,7 +7,6 @@ import (
|
||||
"log"
|
||||
"strings"
|
||||
|
||||
"github.com/jackc/pgx/v5"
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
lua "github.com/yuin/gopher-lua"
|
||||
)
|
||||
@@ -60,32 +59,48 @@ func getPgConnString(cfg *config.Configuration) string {
|
||||
return s.String()
|
||||
}
|
||||
|
||||
func runMigrations(luaVM *lua.LState) error {
|
||||
migrator, err := repo.NewPgMigrator()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = migrator.RunMigrations(luaVM)
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to apply migrations: %v", err)
|
||||
}
|
||||
|
||||
migrator.Close()
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (a *ExpensesApp) GoToMigration(id uint) error {
|
||||
migrator, err := repo.NewPgMigrator()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return migrator.Goto(id)
|
||||
}
|
||||
|
||||
func StartApp() error {
|
||||
config.SetConfig("./config.yaml")
|
||||
cfg := config.GetInstance()
|
||||
|
||||
ctx := context.Background()
|
||||
pgStr := getPgConnString(cfg)
|
||||
conn, err := pgxpool.New(ctx, pgStr)
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to open migration DB: %v", err)
|
||||
}
|
||||
|
||||
App = NewExpenseApp(conn, nil)
|
||||
|
||||
// Create the LuaVM
|
||||
luaVM, err := StartLuaVM()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
App.Lua = luaVM
|
||||
|
||||
err = RunMigrations(conn, luaVM)
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to apply migrations: %v", err)
|
||||
}
|
||||
App.Close()
|
||||
// Run the migrations
|
||||
err = runMigrations(luaVM)
|
||||
|
||||
conn, err = pgx.Connect(ctx, pgStr)
|
||||
// Create our final app thing
|
||||
conn, err := pgxpool.New(ctx, pgStr)
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to open DB: %v", err)
|
||||
}
|
||||
|
||||
@@ -3,92 +3,40 @@ package expenses
|
||||
import (
|
||||
"context"
|
||||
mod "expenses/expenses/models"
|
||||
"github.com/jackc/pgx/v5"
|
||||
)
|
||||
|
||||
func (a *ExpensesApp) GetAllCategories() ([]mod.Category, error) {
|
||||
ctx := context.Background()
|
||||
|
||||
categories, err := a.CategoryRepo.GetAll(ctx)
|
||||
if err != nil {
|
||||
return []mod.Category{}, err
|
||||
}
|
||||
|
||||
return categories, nil
|
||||
return a.CategoryRepo.GetAll(ctx)
|
||||
}
|
||||
|
||||
func (a *ExpensesApp) GetCategory(id int32) (mod.Category, error) {
|
||||
ctx := context.Background()
|
||||
tx, err := a.DB.BeginTx(ctx, pgx.TxOptions{})
|
||||
if err != nil {
|
||||
return mod.Category{}, err
|
||||
}
|
||||
defer tx.Rollback(ctx)
|
||||
|
||||
category, err := mod.GetCategory(a.DB, tx, id)
|
||||
if err != nil {
|
||||
return mod.Category{}, err
|
||||
}
|
||||
|
||||
return category, tx.Commit(ctx)
|
||||
return a.CategoryRepo.Get(ctx, id)
|
||||
}
|
||||
|
||||
func (a *ExpensesApp) CreateCategory(name string) error {
|
||||
ctx := context.Background()
|
||||
tx, err := a.DB.BeginTx(ctx, pgx.TxOptions{})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer tx.Rollback(ctx)
|
||||
|
||||
newCat := mod.Category{
|
||||
CategoryName: name,
|
||||
}
|
||||
|
||||
err = newCat.Insert(a.DB, tx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return tx.Commit(ctx)
|
||||
return a.CategoryRepo.Insert(ctx, newCat)
|
||||
}
|
||||
|
||||
func (a *ExpensesApp) UpdateCategory(id int32, name string) error {
|
||||
ctx := context.Background()
|
||||
tx, err := a.DB.BeginTx(ctx, pgx.TxOptions{})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer tx.Rollback(ctx)
|
||||
|
||||
cat := mod.Category{
|
||||
CategoryID: id,
|
||||
CategoryName: name,
|
||||
}
|
||||
err = cat.Update(a.DB, tx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return tx.Commit(ctx)
|
||||
return a.CategoryRepo.Update(ctx, cat)
|
||||
}
|
||||
|
||||
func (a *ExpensesApp) DeleteCategory(id int32) error {
|
||||
ctx := context.Background()
|
||||
tx, err := a.DB.BeginTx(ctx, pgx.TxOptions{})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer tx.Rollback(ctx)
|
||||
|
||||
cat := mod.Category{
|
||||
CategoryID: id,
|
||||
}
|
||||
|
||||
err = cat.Delete(a.DB, tx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return tx.Commit(ctx)
|
||||
return a.CategoryRepo.Delete(ctx, id)
|
||||
}
|
||||
|
||||
@@ -3,10 +3,8 @@ package expenses
|
||||
import (
|
||||
"context"
|
||||
mod "expenses/expenses/models"
|
||||
"sort"
|
||||
|
||||
"github.com/jackc/pgx/v5"
|
||||
dec "github.com/shopspring/decimal"
|
||||
"sort"
|
||||
)
|
||||
|
||||
type UserTab struct {
|
||||
@@ -147,24 +145,15 @@ func CalculateDebts(e *mod.Expense) (mod.Debts, error) {
|
||||
func (a *ExpensesApp) settleDebt(payment mod.Payment,
|
||||
credit mod.Payment, eId int32) error {
|
||||
ctx := context.Background()
|
||||
tx, err := a.DB.BeginTx(ctx, pgx.TxOptions{})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer tx.Rollback(ctx)
|
||||
|
||||
credit.PayedAmount = credit.PayedAmount.Sub(payment.PayedAmount)
|
||||
|
||||
err = payment.Insert(a.DB, tx, eId)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = credit.Update(a.DB, tx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
// TODO
|
||||
// This needs to run as a transaction
|
||||
err := a.ExpRepo.InsertPayment(ctx, eId, payment)
|
||||
err = a.ExpRepo.UpdatePayment(ctx, credit)
|
||||
|
||||
return tx.Commit(ctx)
|
||||
return err
|
||||
}
|
||||
|
||||
func (a *ExpensesApp) ProcessDebt(expID int32, debt mod.Debt) error {
|
||||
|
||||
@@ -3,8 +3,6 @@ package expenses
|
||||
import (
|
||||
"context"
|
||||
mod "expenses/expenses/models"
|
||||
|
||||
"github.com/jackc/pgx/v5"
|
||||
dec "github.com/shopspring/decimal"
|
||||
)
|
||||
|
||||
@@ -51,69 +49,24 @@ func ParseFormPayments(userIDs []string, paymentsIDs []string,
|
||||
func (a *ExpensesApp) GetExpensePaymentByUserID(eId int32, uId int32,
|
||||
) (mod.Payment, error) {
|
||||
ctx := context.Background()
|
||||
tx, err := a.DB.BeginTx(ctx, pgx.TxOptions{})
|
||||
if err != nil {
|
||||
return mod.Payment{}, err
|
||||
}
|
||||
defer tx.Rollback(ctx)
|
||||
|
||||
payment, err := mod.GetExpensePaymentByUserID(a.DB, tx, eId, uId)
|
||||
|
||||
return payment, tx.Commit(ctx)
|
||||
return a.ExpRepo.GetExpensePaymentByUserID(ctx, eId, uId)
|
||||
}
|
||||
|
||||
// insertPayment allows us to insert a payment manually
|
||||
// for now its private, need to figure out if it needs to be public
|
||||
func (a *ExpensesApp) insertPayment(payment mod.Payment, eIdx int32) error {
|
||||
ctx := context.Background()
|
||||
tx, err := a.DB.BeginTx(ctx, pgx.TxOptions{})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer tx.Rollback(ctx)
|
||||
|
||||
err = payment.Insert(a.DB, tx, eIdx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return tx.Commit(ctx)
|
||||
return a.ExpRepo.InsertPayment(ctx, eIdx, payment)
|
||||
}
|
||||
|
||||
func (a *ExpensesApp) DeletePayment(id int32) error {
|
||||
ctx := context.Background()
|
||||
tx, err := a.DB.BeginTx(ctx, pgx.TxOptions{})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer tx.Rollback(ctx)
|
||||
|
||||
payment := mod.Payment{
|
||||
ExpPaymID: id,
|
||||
}
|
||||
|
||||
err = payment.Delete(a.DB, tx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return tx.Commit(ctx)
|
||||
return a.ExpRepo.DeletePayment(ctx, id)
|
||||
}
|
||||
|
||||
func (a *ExpensesApp) UpdatePayment(payment mod.Payment) error {
|
||||
ctx := context.Background()
|
||||
tx, err := a.DB.BeginTx(ctx, pgx.TxOptions{})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer tx.Rollback(ctx)
|
||||
|
||||
err = payment.Update(a.DB, tx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return tx.Commit(ctx)
|
||||
return a.ExpRepo.UpdatePayment(ctx, payment)
|
||||
}
|
||||
|
||||
func (a *ExpensesApp) AddPayment(expID int32, userID int32, sum dec.Decimal) error {
|
||||
|
||||
@@ -3,7 +3,6 @@ package expenses
|
||||
import (
|
||||
"context"
|
||||
mod "expenses/expenses/models"
|
||||
"github.com/jackc/pgx/v5"
|
||||
dec "github.com/shopspring/decimal"
|
||||
)
|
||||
|
||||
@@ -70,38 +69,12 @@ func (a *ExpensesApp) NormalizeShares(e *mod.Expense) error {
|
||||
|
||||
// insertShare allows us to insert a share manually
|
||||
// for now its private, need to figure out if it needs to be public
|
||||
func (a *ExpensesApp) insertShare(share *mod.Share, eIdx int32) error {
|
||||
func (a *ExpensesApp) insertShare(share mod.Share, eIdx int32) error {
|
||||
ctx := context.Background()
|
||||
tx, err := a.DB.BeginTx(ctx, pgx.TxOptions{})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer tx.Rollback(ctx)
|
||||
|
||||
err = share.Insert(a.DB, tx, eIdx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return tx.Commit(ctx)
|
||||
return a.ExpRepo.InsertShare(ctx, eIdx, share)
|
||||
}
|
||||
|
||||
func (a *ExpensesApp) DeleteShare(id int32) error {
|
||||
ctx := context.Background()
|
||||
tx, err := a.DB.BeginTx(ctx, pgx.TxOptions{})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer tx.Rollback(ctx)
|
||||
|
||||
share := mod.Share{
|
||||
ExpShareID: id,
|
||||
}
|
||||
|
||||
err = share.Delete(a.DB, tx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return tx.Commit(ctx)
|
||||
return a.ExpRepo.DeleteShare(ctx, id)
|
||||
}
|
||||
|
||||
@@ -14,7 +14,7 @@ func (a *ExpensesApp) luaInsertShare(L *lua.LState) int {
|
||||
return returnWithError(L, err.Error())
|
||||
}
|
||||
|
||||
err = a.insertShare(share, int32(expenseID))
|
||||
err = a.insertShare(*share, int32(expenseID))
|
||||
if err != nil {
|
||||
return returnWithError(L, err.Error())
|
||||
}
|
||||
|
||||
+14
-52
@@ -3,11 +3,9 @@ package expenses
|
||||
import (
|
||||
"context"
|
||||
mod "expenses/expenses/models"
|
||||
"github.com/shopspring/decimal"
|
||||
"log"
|
||||
"time"
|
||||
|
||||
"github.com/jackc/pgx/v5"
|
||||
"github.com/shopspring/decimal"
|
||||
)
|
||||
|
||||
func (a *ExpensesApp) GetAllExpenses() ([]mod.Expense, error) {
|
||||
@@ -36,15 +34,7 @@ func (a *ExpensesApp) GetExpensesRanged(startDate string, endDate string) ([]mod
|
||||
|
||||
ctx := context.Background()
|
||||
|
||||
tx, err := a.DB.Begin(ctx)
|
||||
if err != nil {
|
||||
return []mod.Expense{}, err
|
||||
}
|
||||
defer tx.Rollback(ctx)
|
||||
|
||||
expenses, err := mod.GetExpensesRange(a.DB, tx, startDateTime, endDateTime)
|
||||
|
||||
return expenses, tx.Commit(ctx)
|
||||
return a.ExpRepo.GetExpensesRange(ctx, startDateTime, endDateTime)
|
||||
}
|
||||
|
||||
func (a *ExpensesApp) GetExpense(id int32) (mod.Expense, error) {
|
||||
@@ -61,35 +51,19 @@ func (a *ExpensesApp) GetExpense(id int32) (mod.Expense, error) {
|
||||
func (a *ExpensesApp) LoadExpenseShares(e *mod.Expense) error {
|
||||
ctx := context.Background()
|
||||
|
||||
tx, err := a.DB.Begin(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer tx.Rollback(ctx)
|
||||
var err error
|
||||
e.Shares, err = a.ExpRepo.GetShares(ctx, e.ExpID)
|
||||
|
||||
err = e.GetShares(a.DB, tx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return tx.Commit(ctx)
|
||||
return err
|
||||
}
|
||||
|
||||
func (a *ExpensesApp) LoadExpensePayments(e *mod.Expense) error {
|
||||
ctx := context.Background()
|
||||
|
||||
tx, err := a.DB.Begin(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer tx.Rollback(ctx)
|
||||
var err error
|
||||
e.Payments, err = a.ExpRepo.GetPayments(ctx, e.ExpID)
|
||||
|
||||
err = e.GetPayments(a.DB, tx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return tx.Commit(ctx)
|
||||
return err
|
||||
}
|
||||
|
||||
func (a *ExpensesApp) LoadExpenseDebts(e *mod.Expense) error {
|
||||
@@ -164,31 +138,19 @@ func (a *ExpensesApp) analyzeExpense(e *mod.Expense) {
|
||||
}
|
||||
|
||||
func (a *ExpensesApp) NewExpense(exp mod.Expense) error {
|
||||
ctx := context.Background()
|
||||
|
||||
a.analyzeExpense(&exp)
|
||||
|
||||
err := a.Storage.Insert()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return tx.Commit(ctx)
|
||||
return a.ExpRepo.Insert(ctx, exp)
|
||||
}
|
||||
|
||||
func (a *ExpensesApp) UpdateExpense(exp mod.Expense) error {
|
||||
ctx := context.Background()
|
||||
|
||||
tx, err := a.DB.Begin(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer tx.Rollback(ctx)
|
||||
|
||||
// TODO
|
||||
// Move these functions to the models
|
||||
a.analyzeExpense(&exp)
|
||||
|
||||
err = exp.Update(a.DB, tx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return tx.Commit(ctx)
|
||||
return a.ExpRepo.Update(ctx, exp)
|
||||
}
|
||||
|
||||
@@ -22,7 +22,8 @@ func (a *ExpensesApp) prepareExpense(exp *mod.Expense) error {
|
||||
}
|
||||
|
||||
func (a *ExpensesApp) luaGetAllExpenses(L *lua.LState) int {
|
||||
expenses, err := a.Storage.GetAllExpenses()
|
||||
ctx := context.Background()
|
||||
expenses, err := a.ExpRepo.GetAll(ctx)
|
||||
|
||||
tbl := L.NewTable()
|
||||
for _, e := range expenses {
|
||||
@@ -52,13 +53,8 @@ func (a *ExpensesApp) luaGetExpense(L *lua.LState) int {
|
||||
// TODO
|
||||
// This also doesnt make sense
|
||||
ctx := context.Background()
|
||||
tx, err := a.DB.Begin(ctx)
|
||||
if err != nil {
|
||||
return returnWithError(L, err.Error())
|
||||
}
|
||||
defer tx.Rollback(ctx)
|
||||
|
||||
expense, err := mod.GetExpense(a.DB, tx, int32(expId))
|
||||
expense, err := a.ExpRepo.Get(ctx, int32(expId))
|
||||
if err != nil {
|
||||
return returnWithError(L, err.Error())
|
||||
}
|
||||
|
||||
@@ -11,4 +11,6 @@ type CategoryRepository interface {
|
||||
Update(ctx context.Context, cat mod.Category) error
|
||||
Insert(ctx context.Context, cat mod.Category) error
|
||||
Delete(ctx context.Context, id int32) error
|
||||
|
||||
Close()
|
||||
}
|
||||
|
||||
@@ -21,7 +21,7 @@ type ExpenseRepository interface {
|
||||
UpdateShare(ctx context.Context, sh mod.Share) error
|
||||
InsertShare(ctx context.Context, eId int32, sh mod.Share) error
|
||||
InsertShares(ctx context.Context, eId int32, sh mod.Shares) error
|
||||
DeleteShare(ctx context.Context, sh mod.Share) error
|
||||
DeleteShare(ctx context.Context, id int32) error
|
||||
|
||||
// Payment methods
|
||||
GetPayments(ctx context.Context, eId int32) (mod.Payments, error)
|
||||
@@ -30,5 +30,7 @@ type ExpenseRepository interface {
|
||||
UpdatePayment(ctx context.Context, pm mod.Payment) error
|
||||
InsertPayment(ctx context.Context, eId int32, pm mod.Payment) error
|
||||
InsertPayments(ctx context.Context, eId int32, pm mod.Payments) error
|
||||
DeletePayment(ctx context.Context, pm mod.Payment) error
|
||||
DeletePayment(ctx context.Context, id int32) error
|
||||
|
||||
Close()
|
||||
}
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
package repo
|
||||
|
||||
import lua "github.com/yuin/gopher-lua"
|
||||
|
||||
type Migrator interface {
|
||||
Goto(id uint) error
|
||||
RunMigrations(lua *lua.LState) error
|
||||
Close()
|
||||
}
|
||||
@@ -19,6 +19,10 @@ func NewPgCatRepo(db *pgxpool.Pool) CategoryRepository {
|
||||
}
|
||||
}
|
||||
|
||||
func (p PgCatRepo) Close() {
|
||||
p.DB.Close()
|
||||
}
|
||||
|
||||
func (p PgCatRepo) Get(ctx context.Context, id int32) (mod.Category, error) {
|
||||
queries := pgsqlc.New(p.DB)
|
||||
category, err := queries.GetCategory(ctx, id)
|
||||
|
||||
@@ -84,9 +84,9 @@ func (p PgExpRepo) UpdatePayment(
|
||||
}
|
||||
|
||||
func (p *PgExpRepo) deletePayment(
|
||||
ctx context.Context, q *pgsqlc.Queries, pm mod.Payment) error {
|
||||
ctx context.Context, q *pgsqlc.Queries, id int32) error {
|
||||
|
||||
res, err := q.DeletePayment(ctx, pm.ExpPaymID)
|
||||
res, err := q.DeletePayment(ctx, id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -99,8 +99,8 @@ func (p *PgExpRepo) deletePayment(
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p PgExpRepo) DeletePayment(ctx context.Context, pm mod.Payment) error {
|
||||
return p.deletePayment(ctx, pgsqlc.New(p.DB), pm)
|
||||
func (p PgExpRepo) DeletePayment(ctx context.Context, id int32) error {
|
||||
return p.deletePayment(ctx, pgsqlc.New(p.DB), id)
|
||||
}
|
||||
|
||||
func (p *PgExpRepo) getPayments(
|
||||
|
||||
@@ -22,6 +22,10 @@ func NewPgExpRepo(db *pgxpool.Pool) ExpenseRepository {
|
||||
}
|
||||
}
|
||||
|
||||
func (p PgExpRepo) Close() {
|
||||
p.DB.Close()
|
||||
}
|
||||
|
||||
func (r *PgExpRepo) withTx(ctx context.Context, fn func(q *pgsqlc.Queries) error) error {
|
||||
tx, err := r.DB.BeginTx(ctx, pgx.TxOptions{})
|
||||
if err != nil {
|
||||
@@ -120,6 +124,9 @@ func (p PgExpRepo) Update(
|
||||
}
|
||||
} else {
|
||||
err := p.updateShare(ctx, q, share)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -107,9 +107,9 @@ func (p PgExpRepo) UpdateShare(ctx context.Context, sh mod.Share) error {
|
||||
}
|
||||
|
||||
func (p *PgExpRepo) deleteShare(
|
||||
ctx context.Context, q *pgsqlc.Queries, sh mod.Share) error {
|
||||
ctx context.Context, q *pgsqlc.Queries, id int32) error {
|
||||
|
||||
res, err := q.DeleteShare(ctx, sh.ExpShareID)
|
||||
res, err := q.DeleteShare(ctx, id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -122,8 +122,8 @@ func (p *PgExpRepo) deleteShare(
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p PgExpRepo) DeleteShare(ctx context.Context, sh mod.Share) error {
|
||||
return p.deleteShare(ctx, pgsqlc.New(p.DB), sh)
|
||||
func (p PgExpRepo) DeleteShare(ctx context.Context, id int32) error {
|
||||
return p.deleteShare(ctx, pgsqlc.New(p.DB), id)
|
||||
}
|
||||
|
||||
func (p *PgExpRepo) getShares(
|
||||
|
||||
@@ -1,43 +1,47 @@
|
||||
package expenses
|
||||
package repo
|
||||
|
||||
import (
|
||||
"expenses/config"
|
||||
mig "github.com/golang-migrate/migrate/v4"
|
||||
_ "github.com/golang-migrate/migrate/v4/database/postgres"
|
||||
_ "github.com/golang-migrate/migrate/v4/source/file"
|
||||
"github.com/jackc/pgx/v5"
|
||||
lua "github.com/yuin/gopher-lua"
|
||||
"log"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
mig "github.com/golang-migrate/migrate/v4"
|
||||
_ "github.com/golang-migrate/migrate/v4/database/postgres"
|
||||
_ "github.com/golang-migrate/migrate/v4/source/file"
|
||||
lua "github.com/yuin/gopher-lua"
|
||||
)
|
||||
|
||||
const LAST_GO_MIGRATION = 3
|
||||
|
||||
func prepareMigrator(db *pgx.Conn) (*mig.Migrate, error) {
|
||||
cfg := config.GetInstance()
|
||||
|
||||
m, err := mig.New(cfg.MigrationsPath,
|
||||
"postgres://"+cfg.PgCfg.User+":"+cfg.PgCfg.Pass+"@"+cfg.PgCfg.Host+":"+
|
||||
cfg.PgCfg.Port+"/"+cfg.PgCfg.DB+"?sslmode=disable",
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return m, err
|
||||
type PgMigrator struct {
|
||||
Mig *mig.Migrate
|
||||
}
|
||||
|
||||
func (a *ExpensesApp) Goto(id uint) error {
|
||||
m, err := prepareMigrator(a.DB)
|
||||
func NewPgMigrator() (Migrator, error) {
|
||||
cfg := config.GetInstance()
|
||||
|
||||
str := "postgres://" + cfg.PgCfg.User + ":" + cfg.PgCfg.Pass + "@" +
|
||||
cfg.PgCfg.Host + ":" + cfg.PgCfg.Port + "/" + cfg.PgCfg.DB + "?sslmod=disable"
|
||||
|
||||
m, err := mig.New(cfg.MigrationsPath, str)
|
||||
if err != nil {
|
||||
return err
|
||||
return PgMigrator{}, err
|
||||
}
|
||||
|
||||
err = m.Migrate(id)
|
||||
// err = m.Force(int(id))
|
||||
return PgMigrator{
|
||||
Mig: m,
|
||||
}, nil
|
||||
}
|
||||
|
||||
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 {
|
||||
return err
|
||||
}
|
||||
@@ -89,20 +93,8 @@ func runCustomScript(ver uint, lua *lua.LState) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func runCustomMigrationLogic(ver uint) {
|
||||
switch ver {
|
||||
case 4:
|
||||
// Get all expenses, normalize the shares, update them
|
||||
}
|
||||
}
|
||||
|
||||
func RunMigrations(db *pgx.Conn, lua *lua.LState) error {
|
||||
m, err := prepareMigrator(db)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = m.Up()
|
||||
func (p PgMigrator) RunMigrations(lua *lua.LState) error {
|
||||
err := p.Mig.Up()
|
||||
if err != nil {
|
||||
if err == mig.ErrNoChange {
|
||||
return nil
|
||||
@@ -111,27 +103,22 @@ func RunMigrations(db *pgx.Conn, lua *lua.LState) error {
|
||||
if dirty, ok := err.(mig.ErrDirty); ok {
|
||||
log.Printf("migration %d is dirty:", err)
|
||||
|
||||
forceErr := m.Force(int(dirty.Version - 1))
|
||||
forceErr := p.Mig.Force(int(dirty.Version - 1))
|
||||
if forceErr != nil {
|
||||
log.Fatalln("force reset failed: %w", forceErr)
|
||||
}
|
||||
|
||||
migErr := m.Migrate(uint(dirty.Version))
|
||||
migErr := p.Mig.Migrate(uint(dirty.Version))
|
||||
log.Fatalln(migErr)
|
||||
}
|
||||
}
|
||||
|
||||
ver, _, _ := m.Version()
|
||||
ver, _, _ := p.Mig.Version()
|
||||
log.Printf("Successfuly jumped to migration %d", ver)
|
||||
|
||||
// One day we will have custom stuff being ran in Lua instead of here
|
||||
if ver > LAST_GO_MIGRATION {
|
||||
err = runCustomScript(ver, lua)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
runCustomMigrationLogic(ver)
|
||||
err = runCustomScript(ver, lua)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
@@ -19,6 +19,10 @@ func NewPgStoreRepo(db *pgxpool.Pool) StoreRepository {
|
||||
}
|
||||
}
|
||||
|
||||
func (p PgStoreRepo) Close() {
|
||||
p.DB.Close()
|
||||
}
|
||||
|
||||
func (p PgStoreRepo) Get(ctx context.Context, id int32) (mod.Store, error) {
|
||||
queries := pgsqlc.New(p.DB)
|
||||
store, err := queries.GetStore(ctx, id)
|
||||
|
||||
@@ -19,6 +19,10 @@ func NewPgTypeRepo(db *pgxpool.Pool) TypeRepository {
|
||||
}
|
||||
}
|
||||
|
||||
func (p PgTypeRepo) Close() {
|
||||
p.DB.Close()
|
||||
}
|
||||
|
||||
func (p PgTypeRepo) Get(ctx context.Context, id int32) (mod.Type, error) {
|
||||
queries := pgsqlc.New(p.DB)
|
||||
typ, err := queries.GetType(ctx, id)
|
||||
|
||||
@@ -17,6 +17,10 @@ func NewPgUserRepo(db *pgxpool.Pool) UserRepository {
|
||||
}
|
||||
}
|
||||
|
||||
func (p PgUserRepo) Close() {
|
||||
p.DB.Close()
|
||||
}
|
||||
|
||||
func (p PgUserRepo) GetAll(ctx context.Context) (mod.Users, error) {
|
||||
queries := pgsqlc.New(p.DB)
|
||||
userList, err := queries.GetUsers(ctx)
|
||||
|
||||
@@ -11,4 +11,6 @@ type StoreRepository interface {
|
||||
Update(ctx context.Context, cat mod.Store) error
|
||||
Insert(ctx context.Context, cat mod.Store) error
|
||||
Delete(ctx context.Context, id int32) error
|
||||
|
||||
Close()
|
||||
}
|
||||
|
||||
@@ -11,4 +11,6 @@ type TypeRepository interface {
|
||||
Update(ctx context.Context, typ mod.Type) error
|
||||
Insert(ctx context.Context, typ mod.Type) error
|
||||
Delete(ctx context.Context, id int32) error
|
||||
|
||||
Close()
|
||||
}
|
||||
|
||||
@@ -7,4 +7,6 @@ import (
|
||||
|
||||
type UserRepository interface {
|
||||
GetAll(ctx context.Context) (mod.Users, error)
|
||||
|
||||
Close()
|
||||
}
|
||||
|
||||
+5
-60
@@ -2,96 +2,41 @@ package expenses
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/jackc/pgx/v5"
|
||||
mod "expenses/expenses/models"
|
||||
)
|
||||
|
||||
func (a *ExpensesApp) GetAllStores() ([]mod.Store, error) {
|
||||
ctx := context.Background()
|
||||
tx, err := a.DB.BeginTx(ctx, pgx.TxOptions{})
|
||||
if err != nil {
|
||||
return []mod.Store{}, err
|
||||
}
|
||||
defer tx.Rollback(ctx)
|
||||
|
||||
stores, err := mod.GetAllStores(a.DB, tx)
|
||||
if err != nil {
|
||||
return []mod.Store{}, err
|
||||
}
|
||||
|
||||
return stores, tx.Commit(ctx)
|
||||
return a.StoreRepo.GetAll(ctx)
|
||||
}
|
||||
|
||||
func (a *ExpensesApp) GetStore(id int32) (mod.Store, error) {
|
||||
ctx := context.Background()
|
||||
tx, err := a.DB.BeginTx(ctx, pgx.TxOptions{})
|
||||
if err != nil {
|
||||
return mod.Store{}, err
|
||||
}
|
||||
defer tx.Rollback(ctx)
|
||||
|
||||
store, err := mod.GetStore(a.DB, tx, id)
|
||||
if err != nil {
|
||||
return mod.Store{}, err
|
||||
}
|
||||
|
||||
return store, tx.Commit(ctx)
|
||||
return a.StoreRepo.Get(ctx, id)
|
||||
}
|
||||
|
||||
func (a *ExpensesApp) NewStore(name string) error {
|
||||
ctx := context.Background()
|
||||
tx, err := a.DB.BeginTx(ctx, pgx.TxOptions{})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer tx.Rollback(ctx)
|
||||
|
||||
newStore := mod.Store{
|
||||
StoreName: name,
|
||||
}
|
||||
|
||||
err = newStore.Insert(a.DB, tx)
|
||||
|
||||
return tx.Commit(ctx)
|
||||
return a.StoreRepo.Insert(ctx, newStore)
|
||||
}
|
||||
|
||||
func (a *ExpensesApp) UpdateStore(id int32, name string) error {
|
||||
ctx := context.Background()
|
||||
tx, err := a.DB.BeginTx(ctx, pgx.TxOptions{})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer tx.Rollback(ctx)
|
||||
|
||||
store := mod.Store{
|
||||
StoreID: id,
|
||||
StoreName: name,
|
||||
}
|
||||
|
||||
err = store.Update(a.DB, tx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return tx.Commit(ctx)
|
||||
return a.StoreRepo.Update(ctx, store)
|
||||
}
|
||||
|
||||
func (a *ExpensesApp) DeleteStore(id int32) error {
|
||||
ctx := context.Background()
|
||||
tx, err := a.DB.BeginTx(ctx, pgx.TxOptions{})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer tx.Rollback(ctx)
|
||||
|
||||
store := mod.Store{
|
||||
StoreID: id,
|
||||
}
|
||||
|
||||
err = store.Delete(a.DB, tx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return tx.Commit(ctx)
|
||||
return a.StoreRepo.Delete(ctx, id)
|
||||
}
|
||||
|
||||
+7
-65
@@ -3,98 +3,40 @@ package expenses
|
||||
import (
|
||||
"context"
|
||||
mod "expenses/expenses/models"
|
||||
"github.com/jackc/pgx/v5"
|
||||
)
|
||||
|
||||
func (a *ExpensesApp) GetAllTypes() ([]mod.Type, error) {
|
||||
func (a *ExpensesApp) GetAllTypes() (mod.Types, error) {
|
||||
ctx := context.Background()
|
||||
tx, err := a.DB.BeginTx(ctx, pgx.TxOptions{})
|
||||
if err != nil {
|
||||
return []mod.Type{}, err
|
||||
}
|
||||
defer tx.Rollback(ctx)
|
||||
|
||||
types, err := mod.GetAllTypes(a.DB, tx)
|
||||
if err != nil {
|
||||
return []mod.Type{}, err
|
||||
}
|
||||
|
||||
return types, tx.Commit(ctx)
|
||||
return a.TypeRepo.GetAll(ctx)
|
||||
}
|
||||
|
||||
func (a *ExpensesApp) GetType(id int32) (mod.Type, error) {
|
||||
ctx := context.Background()
|
||||
tx, err := a.DB.BeginTx(ctx, pgx.TxOptions{})
|
||||
if err != nil {
|
||||
return mod.Type{}, err
|
||||
}
|
||||
defer tx.Rollback(ctx)
|
||||
|
||||
typ, err := mod.GetType(a.DB, tx, id)
|
||||
if err != nil {
|
||||
return mod.Type{}, err
|
||||
}
|
||||
|
||||
return typ, tx.Commit(ctx)
|
||||
return a.TypeRepo.Get(ctx, id)
|
||||
}
|
||||
|
||||
func (a *ExpensesApp) NewType(name string) error {
|
||||
ctx := context.Background()
|
||||
tx, err := a.DB.BeginTx(ctx, pgx.TxOptions{})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer tx.Rollback(ctx)
|
||||
|
||||
newTyp := mod.Type{
|
||||
TypeName: name,
|
||||
}
|
||||
|
||||
err = newTyp.Insert(a.DB, tx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return tx.Commit(ctx)
|
||||
return a.TypeRepo.Insert(ctx, newTyp)
|
||||
}
|
||||
|
||||
func (a *ExpensesApp) DeleteType(id int32) error {
|
||||
ctx := context.Background()
|
||||
tx, err := a.DB.BeginTx(ctx, pgx.TxOptions{})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer tx.Rollback(ctx)
|
||||
|
||||
typ := mod.Type{
|
||||
TypeID: id,
|
||||
}
|
||||
|
||||
err = typ.Delete(a.DB, tx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return tx.Commit(ctx)
|
||||
return a.TypeRepo.Delete(ctx, id)
|
||||
}
|
||||
|
||||
func (a *ExpensesApp) UpdateType(id int32, name string) error {
|
||||
ctx := context.Background()
|
||||
tx, err := a.DB.BeginTx(ctx, pgx.TxOptions{})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer tx.Rollback(ctx)
|
||||
|
||||
newTyp := mod.Type{
|
||||
updatedType := mod.Type{
|
||||
TypeID: id,
|
||||
TypeName: name,
|
||||
}
|
||||
|
||||
err = newTyp.Update(a.DB, tx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return tx.Commit(ctx)
|
||||
return a.TypeRepo.Update(ctx, updatedType)
|
||||
}
|
||||
|
||||
+1
-14
@@ -3,22 +3,9 @@ package expenses
|
||||
import (
|
||||
"context"
|
||||
mod "expenses/expenses/models"
|
||||
|
||||
"github.com/jackc/pgx/v5"
|
||||
)
|
||||
|
||||
func (a *ExpensesApp) GetAllUsers() ([]mod.User, error) {
|
||||
ctx := context.Background()
|
||||
tx, err := a.DB.BeginTx(ctx, pgx.TxOptions{})
|
||||
if err != nil {
|
||||
return []mod.User{}, err
|
||||
}
|
||||
defer tx.Rollback(ctx)
|
||||
|
||||
users, err := mod.GetAllUsers(a.DB, tx)
|
||||
if err != nil {
|
||||
return []mod.User{}, err
|
||||
}
|
||||
|
||||
return users, tx.Commit(ctx)
|
||||
return a.UserRepo.GetAll(ctx)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user