Added a lua migration helper script, hope it works correctly all the time!
This commit is contained in:
@@ -59,7 +59,6 @@ func StartApp() error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
// api := luapi.New(App)
|
||||
App.Lua = luaVM
|
||||
|
||||
err = RunMigrations(migDB, luaVM)
|
||||
|
||||
@@ -1,10 +1,13 @@
|
||||
-- This scripts needs to do 2 things:
|
||||
-- Update the expenses where the owner doesn't have shares or payments to have
|
||||
-- some of his
|
||||
-- Normalize the shares
|
||||
-- some of his because of how the normalization function works
|
||||
-- Normalize the shares and update the expense
|
||||
|
||||
local json = require("json")
|
||||
|
||||
local info = debug.getinfo(1, 'S')
|
||||
print("Running ", info.source)
|
||||
|
||||
local function printTable(t)
|
||||
for key, value in pairs(t) do
|
||||
if type(value) == "table" then
|
||||
@@ -35,30 +38,92 @@ local function isOwnerInPayments(exp)
|
||||
return false
|
||||
end
|
||||
|
||||
---@diagnostic disable-next-line: undefined-global
|
||||
local expenses, err = GetAllExpenses()
|
||||
if not expenses then
|
||||
print("Error:", err)
|
||||
local function insertOwnerShare(exp)
|
||||
local share = {
|
||||
User = exp.Owner,
|
||||
Share = 0,
|
||||
}
|
||||
|
||||
local ok, err = InsertShare(json.encode(share), exp.ExpID)
|
||||
return ok, err
|
||||
end
|
||||
|
||||
for i = 1, #expenses do
|
||||
-- print "------------------------------------"
|
||||
local eJson = expenses[i].expense
|
||||
local function insertOwnerPayment(exp)
|
||||
local payment = {
|
||||
User = exp.Owner,
|
||||
PayedAmount = 0,
|
||||
}
|
||||
|
||||
local ok, err = InsertPayment(json.encode(payment), exp.ExpID)
|
||||
return ok, err
|
||||
end
|
||||
|
||||
local function validateSharesAndPayments(exp)
|
||||
if not isOwnerInShares(exp) then
|
||||
local ok, err = insertOwnerShare(exp)
|
||||
if not ok then
|
||||
print("Error inserting owner share: ", err)
|
||||
return false
|
||||
end
|
||||
print("Inserted share in expense: ", exp.ExpID)
|
||||
end
|
||||
|
||||
if not isOwnerInPayments(exp) then
|
||||
local ok, err = insertOwnerPayment(exp)
|
||||
if not ok then
|
||||
print("Error inserting owner payment: ", err)
|
||||
return false
|
||||
end
|
||||
print("Inserted payment in expense: ", exp.ExpID)
|
||||
end
|
||||
|
||||
return true
|
||||
end
|
||||
|
||||
local function requiresUpdate(exp, normalized)
|
||||
return exp ~= normalized
|
||||
end
|
||||
|
||||
---@diagnostic disable-next-line: undefined-global
|
||||
local ok, data = GetAllExpenses()
|
||||
if not ok or data == nil then
|
||||
print("Error:", data)
|
||||
return
|
||||
end
|
||||
|
||||
for i = 1, #data do
|
||||
local eJson = data[i].expense
|
||||
local e = json.decode(eJson)
|
||||
|
||||
if not isOwnerInShares(e) then
|
||||
-- Insert a share in the owner user into shares as 0.0
|
||||
local res = validateSharesAndPayments(e)
|
||||
if not res then
|
||||
goto continue
|
||||
end
|
||||
|
||||
if not isOwnerInPayments(e) then
|
||||
-- Insert a share in the owner user into shares as 0.0
|
||||
-- if we inserted shares and payments we need to update the expense here
|
||||
local updatedEJson
|
||||
ok, updatedEJson = GetExpense(e.ExpID)
|
||||
if not ok then
|
||||
goto continue
|
||||
end
|
||||
|
||||
normalizedExpense = NormalizeShare(eJson)
|
||||
if eJson == normalizedExpense then
|
||||
print "This one needs to be updated"
|
||||
local normalizedExpense
|
||||
ok, normalizedExpense = NormalizeShare(updatedEJson)
|
||||
if not ok then
|
||||
print("Failed to normalize share: ", normalizedExpense)
|
||||
goto continue
|
||||
end
|
||||
|
||||
local newExp = json.decode(updatedEJson)
|
||||
local normalized = json.decode(normalizedExpense)
|
||||
if requiresUpdate(updatedEJson, normalizedExpense) then
|
||||
local err
|
||||
ok, err = UpdateExpense(normalizedExpense)
|
||||
if not ok then
|
||||
print("Failed to update expense ", normalized.ExpID, ": ", err)
|
||||
goto continue
|
||||
end
|
||||
print("Updated expense ", normalized.ExpID)
|
||||
end
|
||||
|
||||
::continue::
|
||||
|
||||
@@ -46,6 +46,23 @@ func ParseFormPayments(userIDs []string, paymentsIDs []string,
|
||||
return payments, nil
|
||||
}
|
||||
|
||||
// insertPayment allows us to insert a share manually
|
||||
// for now its private, need to figure out if it needs to be public
|
||||
func (a *ExpensesApp) insertPayment(payment mod.ExpensePayment, eIdx int64) error {
|
||||
tx, err := a.DB.Begin()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer tx.Rollback()
|
||||
|
||||
err = payment.Insert(tx, eIdx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return tx.Commit()
|
||||
}
|
||||
|
||||
func (a *ExpensesApp) DeletePayment(id int64) error {
|
||||
tx, err := a.DB.Begin()
|
||||
if err != nil {
|
||||
|
||||
@@ -0,0 +1,26 @@
|
||||
package expenses
|
||||
|
||||
import (
|
||||
mod "expenses/expenses/models"
|
||||
|
||||
lua "github.com/yuin/gopher-lua"
|
||||
)
|
||||
|
||||
func (a *ExpensesApp) luaInsertPayment(L *lua.LState) int {
|
||||
paymentJSON := L.CheckString(1)
|
||||
expenseID := L.CheckInt(2)
|
||||
|
||||
payment, err := mod.PaymentFromJSON([]byte(paymentJSON))
|
||||
if err != nil {
|
||||
return returnWithError(L, err.Error())
|
||||
}
|
||||
|
||||
err = a.insertPayment(*payment, int64(expenseID))
|
||||
if err != nil {
|
||||
return returnWithError(L, err.Error())
|
||||
}
|
||||
|
||||
L.Push(lua.LBool(true))
|
||||
L.Push(lua.LString(""))
|
||||
return 2
|
||||
}
|
||||
@@ -2,8 +2,6 @@ package expenses
|
||||
|
||||
import (
|
||||
mod "expenses/expenses/models"
|
||||
"log"
|
||||
|
||||
dec "github.com/shopspring/decimal"
|
||||
)
|
||||
|
||||
@@ -58,18 +56,33 @@ func (a *ExpensesApp) NormalizeShares(e *mod.Expense) error {
|
||||
}
|
||||
owed := e.Value.Mul(e.Shares[k].Share)
|
||||
excess = excess.Add(owed.Sub(owed.Truncate(2)))
|
||||
log.Println(excess)
|
||||
|
||||
e.Shares[k].Calculated = owed.Truncate(2)
|
||||
}
|
||||
|
||||
// It fails here for some reason
|
||||
log.Println(e)
|
||||
e.Shares[ownerShIdx].Calculated = e.Shares[ownerShIdx].Calculated.Add(excess)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// 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 int64) error {
|
||||
tx, err := a.DB.Begin()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer tx.Rollback()
|
||||
|
||||
err = share.Insert(tx, eIdx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return tx.Commit()
|
||||
}
|
||||
|
||||
func (a *ExpensesApp) DeleteShare(id int64) error {
|
||||
tx, err := a.DB.Begin()
|
||||
if err != nil {
|
||||
|
||||
@@ -1,16 +1,25 @@
|
||||
package expenses
|
||||
|
||||
import (
|
||||
mod "expenses/expenses/models"
|
||||
lua "github.com/yuin/gopher-lua"
|
||||
)
|
||||
|
||||
func (a *ExpensesApp) luaInsertShare(L *lua.LState) int {
|
||||
// shareJson := L.CheckString(1)
|
||||
shareJson := L.CheckString(1)
|
||||
expenseID := L.CheckInt(2)
|
||||
|
||||
// share, err := mod.ShareFromJSON([]byte(shareJson))
|
||||
// if err != nil {
|
||||
// return ReturnWithError(L, err.Error())
|
||||
// }
|
||||
share, err := mod.ShareFromJSON([]byte(shareJson))
|
||||
if err != nil {
|
||||
return returnWithError(L, err.Error())
|
||||
}
|
||||
|
||||
return 1
|
||||
err = a.insertShare(share, int64(expenseID))
|
||||
if err != nil {
|
||||
return returnWithError(L, err.Error())
|
||||
}
|
||||
|
||||
L.Push(lua.LBool(true))
|
||||
L.Push(lua.LString(""))
|
||||
return 2
|
||||
}
|
||||
|
||||
@@ -145,8 +145,6 @@ func ExpenseTotalPayed(exp *mod.Expense) decimal.Decimal {
|
||||
}
|
||||
|
||||
func ExpenseIsEvenlyShared(exp *mod.Expense) bool {
|
||||
log.Printf("is exp %d even?", exp.ExpID)
|
||||
|
||||
shares := mapShares(exp)
|
||||
payments := mapPayments(exp)
|
||||
|
||||
@@ -160,13 +158,10 @@ func ExpenseIsEvenlyShared(exp *mod.Expense) bool {
|
||||
}
|
||||
|
||||
if !val.Truncate(2).Equal(userShare.Truncate(2)) {
|
||||
log.Printf("No: %s != %s", val.Truncate(2), userShare.Truncate(2))
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
log.Printf("Yes")
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
|
||||
@@ -3,45 +3,99 @@ package expenses
|
||||
import (
|
||||
"encoding/json"
|
||||
mod "expenses/expenses/models"
|
||||
"log"
|
||||
|
||||
lua "github.com/yuin/gopher-lua"
|
||||
)
|
||||
|
||||
func (a *ExpensesApp) prepareExpense(exp *mod.Expense) error {
|
||||
err := a.LoadExpensePayments(exp)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = a.LoadExpenseShares(exp)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (a *ExpensesApp) luaGetAllExpenses(L *lua.LState) int {
|
||||
log.Println("HERE")
|
||||
tx, err := a.DB.Begin()
|
||||
if err != nil {
|
||||
return returnWithError(L, err.Error())
|
||||
}
|
||||
defer tx.Rollback()
|
||||
|
||||
log.Println("HERE 2")
|
||||
expenses, err := mod.GetAllExpenses(tx)
|
||||
|
||||
log.Println("HERE 2")
|
||||
tbl := L.NewTable()
|
||||
for _, e := range expenses {
|
||||
err := a.LoadExpensePayments(&e)
|
||||
if err != nil {
|
||||
return returnWithError(L, err.Error())
|
||||
}
|
||||
|
||||
err = a.LoadExpenseShares(&e)
|
||||
err = a.prepareExpense(&e)
|
||||
if err != nil {
|
||||
return returnWithError(L, err.Error())
|
||||
}
|
||||
|
||||
jsonData, err := json.Marshal(&e)
|
||||
log.Println(string(jsonData))
|
||||
if err != nil {
|
||||
return returnWithError(L, err.Error())
|
||||
}
|
||||
|
||||
et := L.NewTable()
|
||||
et.RawSetString("expense", lua.LString(jsonData))
|
||||
tbl.Append(et)
|
||||
}
|
||||
|
||||
L.Push(lua.LBool(true))
|
||||
L.Push(tbl)
|
||||
return 1
|
||||
return 2
|
||||
}
|
||||
|
||||
func (a *ExpensesApp) luaGetExpense(L *lua.LState) int {
|
||||
expId := L.CheckInt(1)
|
||||
|
||||
tx, err := a.DB.Begin()
|
||||
if err != nil {
|
||||
return returnWithError(L, err.Error())
|
||||
}
|
||||
defer tx.Rollback()
|
||||
|
||||
expense, err := mod.GetExpense(tx, int64(expId))
|
||||
if err != nil {
|
||||
return returnWithError(L, err.Error())
|
||||
}
|
||||
|
||||
err = a.prepareExpense(&expense)
|
||||
if err != nil {
|
||||
return returnWithError(L, err.Error())
|
||||
}
|
||||
|
||||
jsonData, err := json.Marshal(&expense)
|
||||
if err != nil {
|
||||
return returnWithError(L, err.Error())
|
||||
}
|
||||
|
||||
L.Push(lua.LBool(true))
|
||||
L.Push(lua.LString(jsonData))
|
||||
return 2
|
||||
}
|
||||
|
||||
func (a *ExpensesApp) luaUpdateExpense(L *lua.LState) int {
|
||||
expJson := L.CheckString(1)
|
||||
|
||||
exp, err := mod.ExpenseFromJSON([]byte(expJson))
|
||||
if err != nil {
|
||||
return returnWithError(L, err.Error())
|
||||
}
|
||||
|
||||
err = a.UpdateExpense(*exp)
|
||||
if err != nil {
|
||||
return returnWithError(L, err.Error())
|
||||
}
|
||||
|
||||
L.Push(lua.LBool(true))
|
||||
L.Push(lua.LString(""))
|
||||
return 2
|
||||
}
|
||||
|
||||
func (a *ExpensesApp) luaNormalizeShares(L *lua.LState) int {
|
||||
@@ -59,9 +113,7 @@ func (a *ExpensesApp) luaNormalizeShares(L *lua.LState) int {
|
||||
return returnWithError(L, err.Error())
|
||||
}
|
||||
|
||||
resultExpense := L.NewTable()
|
||||
resultExpense.RawSetString("expense", lua.LString(jsonData))
|
||||
|
||||
L.Push(resultExpense)
|
||||
return 1
|
||||
L.Push(lua.LBool(true))
|
||||
L.Push(lua.LString(jsonData))
|
||||
return 2
|
||||
}
|
||||
|
||||
+11
-2
@@ -9,13 +9,22 @@ import (
|
||||
|
||||
func (a *ExpensesApp) registerLuaBinds(L *lua.LState) {
|
||||
ljson.Preload(L)
|
||||
L.SetGlobal("GetAllExpenses", L.NewFunction(a.luaGetAllExpenses))
|
||||
// Add the decimal.Decimal operations
|
||||
L.SetGlobal("AddDecimal", L.NewFunction(luadec.AddDecimal))
|
||||
|
||||
// Add our API
|
||||
L.SetGlobal("GetAllExpenses", L.NewFunction(a.luaGetAllExpenses))
|
||||
L.SetGlobal("GetExpense", L.NewFunction(a.luaGetExpense))
|
||||
L.SetGlobal("UpdateExpense", L.NewFunction(a.luaUpdateExpense))
|
||||
L.SetGlobal("NormalizeShare", L.NewFunction(a.luaNormalizeShares))
|
||||
|
||||
L.SetGlobal("InsertShare", L.NewFunction(a.luaInsertShare))
|
||||
|
||||
L.SetGlobal("InsertPayment", L.NewFunction(a.luaInsertPayment))
|
||||
}
|
||||
|
||||
func returnWithError(L *lua.LState, strErr string) int {
|
||||
L.Push(lua.LNil)
|
||||
L.Push(lua.LBool(false))
|
||||
L.Push(lua.LString(strErr))
|
||||
return 2
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ package models
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
repo "expenses/expenses/db/repository"
|
||||
|
||||
"database/sql"
|
||||
@@ -17,6 +18,13 @@ type ExpensePayment struct {
|
||||
PayedAmount decimal.Decimal
|
||||
}
|
||||
|
||||
// PaymentFromJSON takes []byte and returns an *ExpensePayment
|
||||
func PaymentFromJSON(data []byte) (*ExpensePayment, error) {
|
||||
var payment ExpensePayment
|
||||
err := json.Unmarshal(data, &payment)
|
||||
return &payment, err
|
||||
}
|
||||
|
||||
func (e *Expense) GetPayments(tx *sql.Tx) error {
|
||||
ctx := context.Background()
|
||||
|
||||
|
||||
@@ -2,11 +2,12 @@ package models
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"encoding/json"
|
||||
"expenses/expenses/db/repository"
|
||||
repo "expenses/expenses/db/repository"
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"encoding/json"
|
||||
|
||||
_ "github.com/mattn/go-sqlite3"
|
||||
dec "github.com/shopspring/decimal"
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user