import { Migration } from '@mikro-orm/migrations'; export class Migration20260422123253 extends Migration { override async up(): Promise { // 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 { // 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;`); } }