better testing idk
This commit is contained in:
@@ -70,11 +70,20 @@ const budgetSelectCols = `
|
|||||||
amount, currency, notes, created_by, created_at, updated_at`
|
amount, currency, notes, created_by, created_at, updated_at`
|
||||||
|
|
||||||
func (r *BudgetRepo) Create(ctx context.Context, req model.CreateBudgetRequest) (*model.Budget, error) {
|
func (r *BudgetRepo) Create(ctx context.Context, req model.CreateBudgetRequest) (*model.Budget, error) {
|
||||||
|
// ON CONFLICT upsert: if the unique key (year, period, version, dept, gl) already
|
||||||
|
// exists, update the mutable fields instead of failing.
|
||||||
|
// Makes the endpoint idempotent — safe for repeated test runs and re-imports.
|
||||||
res, err := r.db.ExecContext(ctx, `
|
res, err := r.db.ExecContext(ctx, `
|
||||||
INSERT INTO budgets
|
INSERT INTO budgets
|
||||||
(fiscal_year, fiscal_period, version, department_id, gl_account_id,
|
(fiscal_year, fiscal_period, version, department_id, gl_account_id,
|
||||||
amount, currency, notes, created_by)
|
amount, currency, notes, created_by)
|
||||||
VALUES (?,?,?,?,?,?,?,?,?)`,
|
VALUES (?,?,?,?,?,?,?,?,?)
|
||||||
|
ON CONFLICT(fiscal_year, fiscal_period, version, department_id, gl_account_id)
|
||||||
|
DO UPDATE SET
|
||||||
|
amount = excluded.amount,
|
||||||
|
currency = excluded.currency,
|
||||||
|
notes = excluded.notes,
|
||||||
|
updated_at = strftime('%Y-%m-%dT%H:%M:%SZ','now')`,
|
||||||
req.FiscalYear, req.FiscalPeriod, req.Version,
|
req.FiscalYear, req.FiscalPeriod, req.Version,
|
||||||
req.DepartmentID, req.GLAccountID, req.Amount,
|
req.DepartmentID, req.GLAccountID, req.Amount,
|
||||||
req.Currency, req.Notes, req.CreatedBy,
|
req.Currency, req.Notes, req.CreatedBy,
|
||||||
@@ -83,13 +92,14 @@ func (r *BudgetRepo) Create(ctx context.Context, req model.CreateBudgetRequest)
|
|||||||
return nil, fmt.Errorf("create budget: %w", err)
|
return nil, fmt.Errorf("create budget: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// LastInsertId returns the existing row id on a conflict branch in SQLite.
|
||||||
id, err := res.LastInsertId()
|
id, err := res.LastInsertId()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("last insert id: %w", err)
|
return nil, fmt.Errorf("last insert id: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
row := r.db.QueryRowContext(ctx,
|
row := r.db.QueryRowContext(ctx,
|
||||||
`SELECT`+budgetSelectCols+`FROM budgets WHERE id = ?`, id)
|
`SELECT `+budgetSelectCols+` FROM budgets WHERE id = ?`, id)
|
||||||
b, err := scanBudget(row)
|
b, err := scanBudget(row)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("fetch created budget: %w", err)
|
return nil, fmt.Errorf("fetch created budget: %w", err)
|
||||||
|
|||||||
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user