invoice number

This commit is contained in:
2026-04-23 11:03:16 +03:30
parent 076793c845
commit f3ad487760
2 changed files with 24 additions and 61 deletions
@@ -0,0 +1,24 @@
import { Migration } from '@mikro-orm/migrations';
export class Migration20260422123253 extends Migration {
override async up(): Promise<void> {
// Create sequence for invoice_number
this.addSql(`CREATE SEQUENCE IF NOT EXISTS invoice_number_seq START 1;`);
// Set the sequence to start from the max existing invoice_number + 1
this.addSql(`SELECT setval('invoice_number_seq', COALESCE((SELECT MAX(invoice_number) FROM invoices), 0) + 1, false);`);
// Set default value for invoice_number to use the sequence
this.addSql(`ALTER TABLE invoices ALTER COLUMN invoice_number SET DEFAULT nextval('invoice_number_seq');`);
}
override async down(): Promise<void> {
// Remove default from invoice_number
this.addSql(`ALTER TABLE invoices ALTER COLUMN invoice_number DROP DEFAULT;`);
// Drop the sequence
this.addSql(`DROP SEQUENCE IF EXISTS invoice_number_seq;`);
}
}