Use new UpdateAssignmentWithDifference for ynab-import to prevent errors on duplicates
This commit is contained in:
parent
a62ab543b0
commit
97de326527
@ -148,3 +148,22 @@ func (q *Queries) UpdateAssignment(ctx context.Context, arg UpdateAssignmentPara
|
|||||||
_, err := q.db.ExecContext(ctx, updateAssignment, arg.CategoryID, arg.Date, arg.Amount)
|
_, err := q.db.ExecContext(ctx, updateAssignment, arg.CategoryID, arg.Date, arg.Amount)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const updateAssignmentWithDifference = `-- name: UpdateAssignmentWithDifference :exec
|
||||||
|
INSERT INTO assignments (category_id, date, amount)
|
||||||
|
VALUES($1, $2, $3)
|
||||||
|
ON CONFLICT (category_id, date)
|
||||||
|
DO
|
||||||
|
UPDATE SET amount = assignments.amount + $3
|
||||||
|
`
|
||||||
|
|
||||||
|
type UpdateAssignmentWithDifferenceParams struct {
|
||||||
|
CategoryID uuid.UUID
|
||||||
|
Date time.Time
|
||||||
|
Amount numeric.Numeric
|
||||||
|
}
|
||||||
|
|
||||||
|
func (q *Queries) UpdateAssignmentWithDifference(ctx context.Context, arg UpdateAssignmentWithDifferenceParams) error {
|
||||||
|
_, err := q.db.ExecContext(ctx, updateAssignmentWithDifference, arg.CategoryID, arg.Date, arg.Amount)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
@ -29,4 +29,11 @@ INSERT INTO assignments (category_id, date, amount)
|
|||||||
VALUES($1, $2, $3)
|
VALUES($1, $2, $3)
|
||||||
ON CONFLICT (category_id, date)
|
ON CONFLICT (category_id, date)
|
||||||
DO
|
DO
|
||||||
UPDATE SET amount = $3;
|
UPDATE SET amount = $3;
|
||||||
|
|
||||||
|
-- name: UpdateAssignmentWithDifference :exec
|
||||||
|
INSERT INTO assignments (category_id, date, amount)
|
||||||
|
VALUES($1, $2, $3)
|
||||||
|
ON CONFLICT (category_id, date)
|
||||||
|
DO
|
||||||
|
UPDATE SET amount = assignments.amount + $3;
|
@ -91,12 +91,12 @@ func (ynab *YNABImport) ImportAssignments(context context.Context, r io.Reader)
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
assignment := CreateAssignmentParams{
|
assignment := UpdateAssignmentWithDifferenceParams{
|
||||||
Date: date,
|
Date: date,
|
||||||
CategoryID: category.UUID,
|
CategoryID: category.UUID,
|
||||||
Amount: amount,
|
Amount: amount,
|
||||||
}
|
}
|
||||||
_, err = ynab.queries.CreateAssignment(context, assignment)
|
err = ynab.queries.UpdateAssignmentWithDifference(context, assignment)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("save assignment %v: %w", assignment, err)
|
return fmt.Errorf("save assignment %v: %w", assignment, err)
|
||||||
}
|
}
|
||||||
@ -226,7 +226,8 @@ func (ynab *YNABImport) GetTransaction(context context.Context, record []string)
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (ynab *YNABImport) ImportRegularTransaction(context context.Context, payeeName string,
|
func (ynab *YNABImport) ImportRegularTransaction(context context.Context, payeeName string,
|
||||||
transaction CreateTransactionParams) error {
|
transaction CreateTransactionParams,
|
||||||
|
) error {
|
||||||
payeeID, err := ynab.GetPayee(context, payeeName)
|
payeeID, err := ynab.GetPayee(context, payeeName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("get payee %s: %w", payeeName, err)
|
return fmt.Errorf("get payee %s: %w", payeeName, err)
|
||||||
@ -242,7 +243,8 @@ func (ynab *YNABImport) ImportRegularTransaction(context context.Context, payeeN
|
|||||||
|
|
||||||
func (ynab *YNABImport) ImportTransferTransaction(context context.Context, payeeName string,
|
func (ynab *YNABImport) ImportTransferTransaction(context context.Context, payeeName string,
|
||||||
transaction CreateTransactionParams, openTransfers *[]Transfer,
|
transaction CreateTransactionParams, openTransfers *[]Transfer,
|
||||||
account *Account, amount numeric.Numeric) error {
|
account *Account, amount numeric.Numeric,
|
||||||
|
) error {
|
||||||
transferToAccountName := payeeName[11:]
|
transferToAccountName := payeeName[11:]
|
||||||
transferToAccount, err := ynab.GetAccount(context, transferToAccountName)
|
transferToAccount, err := ynab.GetAccount(context, transferToAccountName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user