diff --git a/database/migrations/.snapshot-negareh.json b/database/migrations/.snapshot-negareh.json index 41997d2..9ac3c42 100644 --- a/database/migrations/.snapshot-negareh.json +++ b/database/migrations/.snapshot-negareh.json @@ -732,67 +732,6 @@ "foreignKeys": {}, "nativeEnums": {} }, - { - "columns": { - "phone": { - "name": "phone", - "type": "varchar(255)", - "unsigned": false, - "autoincrement": false, - "primary": false, - "nullable": false, - "length": 255, - "mappedType": "string" - }, - "code": { - "name": "code", - "type": "varchar(255)", - "unsigned": false, - "autoincrement": false, - "primary": false, - "nullable": false, - "length": 255, - "mappedType": "string" - }, - "expires_at": { - "name": "expires_at", - "type": "timestamptz", - "unsigned": false, - "autoincrement": false, - "primary": false, - "nullable": false, - "length": 6, - "mappedType": "datetime" - } - }, - "name": "otp", - "schema": "public", - "indexes": [ - { - "keyName": "otp_phone_index", - "columnNames": [ - "phone" - ], - "composite": false, - "constraint": false, - "primary": false, - "unique": false - }, - { - "keyName": "otp_pkey", - "columnNames": [ - "phone" - ], - "composite": false, - "constraint": true, - "primary": true, - "unique": true - } - ], - "checks": [], - "foreignKeys": {}, - "nativeEnums": {} - }, { "columns": { "id": { diff --git a/database/migrations/Migration20260422123253.ts b/database/migrations/Migration20260422123253.ts new file mode 100644 index 0000000..3bb6006 --- /dev/null +++ b/database/migrations/Migration20260422123253.ts @@ -0,0 +1,24 @@ +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;`); + } + +}