Added a lua migration helper script, hope it works correctly all the time!
This commit is contained in:
@@ -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::
|
||||
|
||||
Reference in New Issue
Block a user