up
deploy to danak / build_and_deploy (push) Has been cancelled

This commit is contained in:
2026-06-26 12:24:21 +03:30
parent 841406ad84
commit 6d16a7dde5
8 changed files with 55 additions and 22 deletions
@@ -1,13 +0,0 @@
import { Migration } from '@mikro-orm/migrations';
export class Migration20260626120000 extends Migration {
override async up(): Promise<void> {
this.addSql(`alter table "requests" drop column if exists "status";`);
}
override async down(): Promise<void> {
this.addSql(`alter table "requests" add column "status" text check ("status" in ('pending', 'invoiced')) not null default 'pending';`);
}
}
@@ -1,8 +1,13 @@
import { Migration } from '@mikro-orm/migrations';
export class Migration20260526120100 extends Migration {
export class Migration20260626140000 extends Migration {
override async up(): Promise<void> {
this.addSql(`
alter table "invoices"
add column "confirm_status" text check ("confirm_status" in ('pending', 'confirmed')) not null default 'pending';
`);
this.addSql(`
CREATE OR REPLACE FUNCTION recalc_invoice_totals()
RETURNS TRIGGER AS $$
@@ -14,6 +19,7 @@ export class Migration20260526120100 extends Migration {
v_total int;
v_paid int;
v_enable_tax boolean;
v_confirm_status text;
BEGIN
v_invoice_id := COALESCE(NEW.invoice_id, OLD.invoice_id);
@@ -48,6 +54,18 @@ export class Migration20260526120100 extends Migration {
AND status = 'paid'
AND deleted_at IS NULL;
SELECT CASE
WHEN EXISTS (
SELECT 1
FROM invoice_items
WHERE invoice_id = v_invoice_id
AND deleted_at IS NULL
AND confirmed_at IS NOT NULL
) THEN 'confirmed'
ELSE 'pending'
END
INTO v_confirm_status;
UPDATE invoices
SET
sub_total = v_subtotal,
@@ -55,7 +73,8 @@ export class Migration20260526120100 extends Migration {
tax_amount = v_tax,
total = v_total,
paid_amount = v_paid,
balance = v_total - v_paid
balance = v_total - v_paid,
confirm_status = v_confirm_status
WHERE id = v_invoice_id;
RETURN NULL;
@@ -119,6 +138,7 @@ export class Migration20260526120100 extends Migration {
this.addSql(`DROP TRIGGER IF EXISTS trg_payments_recalc ON payments;`);
this.addSql(`DROP FUNCTION IF EXISTS recalc_invoice_totals();`);
this.addSql(`DROP FUNCTION IF EXISTS recalc_invoice_payments();`);
this.addSql(`alter table "invoices" drop column if exists "confirm_status";`);
}
}