fix invoice number

This commit is contained in:
2026-05-17 22:41:47 +03:30
parent 7133b47c95
commit 0c6dbada15
6 changed files with 37 additions and 9 deletions
+3 -3
View File
@@ -2347,9 +2347,9 @@
},
"invoice_number": {
"name": "invoice_number",
"type": "int",
"unsigned": false,
"autoincrement": false,
"type": "serial",
"unsigned": true,
"autoincrement": true,
"primary": false,
"nullable": false,
"mappedType": "integer"
+14 -1
View File
@@ -3,7 +3,20 @@ import { Migration } from '@mikro-orm/migrations';
export class Migration20260516121012 extends Migration {
override async up(): Promise<void> {
this.addSql(`alter table "invoice_items" add column "sub_total" int generated always as (COALESCE(unit_price, 0) * quantity) STORED not null, add column "total" int generated always as ((COALESCE(unit_price, 0) * quantity) - COALESCE(discount, 0)) STORED not null;`);
this.addSql(`
DO $$ BEGIN
ALTER TABLE "invoice_items"
ADD COLUMN "sub_total" int GENERATED ALWAYS AS (COALESCE(unit_price, 0) * quantity) STORED NOT NULL,
ADD COLUMN "total" int GENERATED ALWAYS AS ((COALESCE(unit_price, 0) * quantity) - COALESCE(discount, 0)) STORED NOT NULL;
EXCEPTION
WHEN duplicate_column THEN NULL;
END $$;
`);
}
override async down(): Promise<void> {
this.addSql(`alter table "invoice_items" drop column if exists "sub_total";`);
this.addSql(`alter table "invoice_items" drop column if exists "total";`);
}
}
@@ -0,0 +1,15 @@
import { Migration } from '@mikro-orm/migrations';
export class Migration20260517190050 extends Migration {
override async up(): Promise<void> {
this.addSql(`CREATE SEQUENCE IF NOT EXISTS invoice_number_seq;`);
this.addSql(`SELECT setval('invoice_number_seq', COALESCE((SELECT MAX(invoice_number) FROM invoices), 0) + 1, false);`);
this.addSql(`ALTER TABLE "invoices" ALTER COLUMN "invoice_number" SET DEFAULT nextval('invoice_number_seq');`);
}
override async down(): Promise<void> {
this.addSql(`ALTER TABLE "invoices" ALTER COLUMN "invoice_number" DROP DEFAULT;`);
}
}