invoice number

This commit is contained in:
2026-04-04 15:24:29 +03:30
parent b29bf380ce
commit b5d555e9ab
15 changed files with 583 additions and 529 deletions
+283 -215
View File
@@ -823,25 +823,29 @@
"create_invoice",
"update_invoice",
"delete_invoice",
"view_all_invoiced_orders",
"view_my_assigned_invoiced_requests",
"view_orders",
"view_assigned_orders",
"create_order",
"update_order",
"assign_designer",
"delete_order",
"create_user",
"update_user",
"delete_user",
"view_users",
"manage_admins",
"manage_roles",
"manage_users",
"manage_payments",
"manage_settings",
"manage_form_builder",
"create_print",
"view_print",
"manage_learnings",
"manage_criticisms",
"manage_announcements"
"manage_announcements",
"manage_tickets",
"new_request_notification",
"new_payment_notification",
"new_ticket_notification"
],
"mappedType": "enum"
},
@@ -1367,101 +1371,6 @@
},
"nativeEnums": {}
},
{
"columns": {
"id": {
"name": "id",
"type": "char(26)",
"unsigned": false,
"autoincrement": false,
"primary": false,
"nullable": false,
"length": 26,
"mappedType": "character"
},
"created_at": {
"name": "created_at",
"type": "timestamptz",
"unsigned": false,
"autoincrement": false,
"primary": false,
"nullable": false,
"length": 6,
"default": "now()",
"mappedType": "datetime"
},
"deleted_at": {
"name": "deleted_at",
"type": "timestamptz",
"unsigned": false,
"autoincrement": false,
"primary": false,
"nullable": true,
"length": 6,
"mappedType": "datetime"
},
"admin_id": {
"name": "admin_id",
"type": "char(26)",
"unsigned": false,
"autoincrement": false,
"primary": false,
"nullable": true,
"length": 26,
"mappedType": "character"
},
"events": {
"name": "events",
"type": "jsonb",
"unsigned": false,
"autoincrement": false,
"primary": false,
"nullable": true,
"mappedType": "json"
}
},
"name": "notification_preferences",
"schema": "public",
"indexes": [
{
"columnNames": [
"admin_id"
],
"composite": false,
"keyName": "notification_preferences_admin_id_unique",
"constraint": true,
"primary": false,
"unique": true
},
{
"keyName": "notification_preferences_pkey",
"columnNames": [
"id"
],
"composite": false,
"constraint": true,
"primary": true,
"unique": true
}
],
"checks": [],
"foreignKeys": {
"notification_preferences_admin_id_foreign": {
"constraintName": "notification_preferences_admin_id_foreign",
"columnNames": [
"admin_id"
],
"localTableName": "public.notification_preferences",
"referencedColumnNames": [
"id"
],
"referencedTableName": "public.admins",
"deleteRule": "cascade",
"updateRule": "cascade"
}
},
"nativeEnums": {}
},
{
"columns": {
"role_id": {
@@ -1723,74 +1632,6 @@
"length": 26,
"mappedType": "character"
},
"deleted_at": {
"name": "deleted_at",
"type": "timestamptz",
"unsigned": false,
"autoincrement": false,
"primary": false,
"nullable": true,
"length": 6,
"mappedType": "datetime"
},
"content": {
"name": "content",
"type": "text",
"unsigned": false,
"autoincrement": false,
"primary": false,
"nullable": false,
"mappedType": "text"
},
"user_id": {
"name": "user_id",
"type": "char(26)",
"unsigned": false,
"autoincrement": false,
"primary": false,
"nullable": true,
"length": 26,
"mappedType": "character"
},
"admin_id": {
"name": "admin_id",
"type": "char(26)",
"unsigned": false,
"autoincrement": false,
"primary": false,
"nullable": true,
"length": 26,
"mappedType": "character"
},
"attachments": {
"name": "attachments",
"type": "jsonb",
"unsigned": false,
"autoincrement": false,
"primary": false,
"nullable": true,
"mappedType": "json"
},
"ref_id": {
"name": "ref_id",
"type": "varchar(255)",
"unsigned": false,
"autoincrement": false,
"primary": false,
"nullable": false,
"length": 255,
"mappedType": "string"
},
"parent_id": {
"name": "parent_id",
"type": "char(26)",
"unsigned": false,
"autoincrement": false,
"primary": false,
"nullable": true,
"length": 26,
"mappedType": "character"
},
"created_at": {
"name": "created_at",
"type": "timestamptz",
@@ -1802,8 +1643,8 @@
"default": "now()",
"mappedType": "datetime"
},
"read_at": {
"name": "read_at",
"deleted_at": {
"name": "deleted_at",
"type": "timestamptz",
"unsigned": false,
"autoincrement": false,
@@ -1811,13 +1652,87 @@
"nullable": true,
"length": 6,
"mappedType": "datetime"
},
"parent_id": {
"name": "parent_id",
"type": "char(26)",
"unsigned": false,
"autoincrement": false,
"primary": false,
"nullable": true,
"length": 26,
"mappedType": "character"
},
"subject": {
"name": "subject",
"type": "varchar(255)",
"unsigned": false,
"autoincrement": false,
"primary": false,
"nullable": true,
"length": 255,
"mappedType": "string"
},
"content": {
"name": "content",
"type": "varchar(255)",
"unsigned": false,
"autoincrement": false,
"primary": false,
"nullable": true,
"length": 255,
"mappedType": "string"
},
"status": {
"name": "status",
"type": "text",
"unsigned": false,
"autoincrement": false,
"primary": false,
"nullable": false,
"enumItems": [
"open",
"closed",
"pending",
"in_progress"
],
"mappedType": "enum"
},
"attachments": {
"name": "attachments",
"type": "text[]",
"unsigned": false,
"autoincrement": false,
"primary": false,
"nullable": true,
"mappedType": "array"
},
"admin_id": {
"name": "admin_id",
"type": "char(26)",
"unsigned": false,
"autoincrement": false,
"primary": false,
"nullable": true,
"length": 26,
"mappedType": "character"
},
"user_id": {
"name": "user_id",
"type": "char(26)",
"unsigned": false,
"autoincrement": false,
"primary": false,
"nullable": true,
"length": 26,
"mappedType": "character"
}
},
"name": "ticket",
"name": "tickets",
"schema": "public",
"indexes": [
{
"keyName": "ticket_pkey",
"keyName": "tickets_pkey",
"columnNames": [
"id"
],
@@ -1829,25 +1744,25 @@
],
"checks": [],
"foreignKeys": {
"ticket_user_id_foreign": {
"constraintName": "ticket_user_id_foreign",
"tickets_parent_id_foreign": {
"constraintName": "tickets_parent_id_foreign",
"columnNames": [
"user_id"
"parent_id"
],
"localTableName": "public.ticket",
"localTableName": "public.tickets",
"referencedColumnNames": [
"id"
],
"referencedTableName": "public.users",
"referencedTableName": "public.tickets",
"deleteRule": "set null",
"updateRule": "cascade"
},
"ticket_admin_id_foreign": {
"constraintName": "ticket_admin_id_foreign",
"tickets_admin_id_foreign": {
"constraintName": "tickets_admin_id_foreign",
"columnNames": [
"admin_id"
],
"localTableName": "public.ticket",
"localTableName": "public.tickets",
"referencedColumnNames": [
"id"
],
@@ -1855,16 +1770,16 @@
"deleteRule": "set null",
"updateRule": "cascade"
},
"ticket_parent_id_foreign": {
"constraintName": "ticket_parent_id_foreign",
"tickets_user_id_foreign": {
"constraintName": "tickets_user_id_foreign",
"columnNames": [
"parent_id"
"user_id"
],
"localTableName": "public.ticket",
"localTableName": "public.tickets",
"referencedColumnNames": [
"id"
],
"referencedTableName": "public.ticket",
"referencedTableName": "public.users",
"deleteRule": "set null",
"updateRule": "cascade"
}
@@ -2174,11 +2089,13 @@
"nullable": false,
"enumItems": [
"newRequest",
"invoiced",
"newInvoice",
"newPayment",
"newOrder",
"designerAssign",
"paymentSuccess",
"newCriticism"
"newCriticism",
"newTicket",
"newOrderMessage",
"newRequestMessage"
],
"mappedType": "enum"
},
@@ -2411,9 +2328,9 @@
},
"invoice_number": {
"name": "invoice_number",
"type": "serial",
"unsigned": true,
"autoincrement": true,
"type": "int",
"unsigned": false,
"autoincrement": false,
"primary": false,
"nullable": false,
"mappedType": "integer"
@@ -2424,7 +2341,7 @@
"unsigned": false,
"autoincrement": false,
"primary": false,
"nullable": true,
"nullable": false,
"mappedType": "integer"
},
"sub_total": {
@@ -2433,7 +2350,7 @@
"unsigned": false,
"autoincrement": false,
"primary": false,
"nullable": true,
"nullable": false,
"mappedType": "integer"
},
"tax_amount": {
@@ -2442,7 +2359,7 @@
"unsigned": false,
"autoincrement": false,
"primary": false,
"nullable": true,
"nullable": false,
"mappedType": "integer"
},
"total": {
@@ -2451,7 +2368,7 @@
"unsigned": false,
"autoincrement": false,
"primary": false,
"nullable": true,
"nullable": false,
"mappedType": "integer"
},
"paid_amount": {
@@ -2471,7 +2388,6 @@
"autoincrement": false,
"primary": false,
"nullable": false,
"default": "0",
"mappedType": "integer"
},
"enable_tax": {
@@ -2526,6 +2442,16 @@
"name": "invoices",
"schema": "public",
"indexes": [
{
"columnNames": [
"invoice_number"
],
"composite": false,
"keyName": "invoices_invoice_number_unique",
"constraint": true,
"primary": false,
"unique": true
},
{
"keyName": "invoices_user_id_index",
"columnNames": [
@@ -2886,15 +2812,6 @@
"nullable": false,
"mappedType": "integer"
},
"sub_total": {
"name": "sub_total",
"type": "int",
"unsigned": false,
"autoincrement": false,
"primary": false,
"nullable": true,
"mappedType": "integer"
},
"discount": {
"name": "discount",
"type": "int",
@@ -2904,15 +2821,6 @@
"nullable": true,
"mappedType": "integer"
},
"total": {
"name": "total",
"type": "int",
"unsigned": false,
"autoincrement": false,
"primary": false,
"nullable": true,
"mappedType": "integer"
},
"description": {
"name": "description",
"type": "text",
@@ -3588,6 +3496,166 @@
},
"nativeEnums": {}
},
{
"columns": {
"id": {
"name": "id",
"type": "char(26)",
"unsigned": false,
"autoincrement": false,
"primary": false,
"nullable": false,
"length": 26,
"mappedType": "character"
},
"deleted_at": {
"name": "deleted_at",
"type": "timestamptz",
"unsigned": false,
"autoincrement": false,
"primary": false,
"nullable": true,
"length": 6,
"mappedType": "datetime"
},
"content": {
"name": "content",
"type": "text",
"unsigned": false,
"autoincrement": false,
"primary": false,
"nullable": false,
"mappedType": "text"
},
"user_id": {
"name": "user_id",
"type": "char(26)",
"unsigned": false,
"autoincrement": false,
"primary": false,
"nullable": true,
"length": 26,
"mappedType": "character"
},
"admin_id": {
"name": "admin_id",
"type": "char(26)",
"unsigned": false,
"autoincrement": false,
"primary": false,
"nullable": true,
"length": 26,
"mappedType": "character"
},
"attachments": {
"name": "attachments",
"type": "jsonb",
"unsigned": false,
"autoincrement": false,
"primary": false,
"nullable": true,
"mappedType": "json"
},
"ref_id": {
"name": "ref_id",
"type": "varchar(255)",
"unsigned": false,
"autoincrement": false,
"primary": false,
"nullable": false,
"length": 255,
"mappedType": "string"
},
"parent_id": {
"name": "parent_id",
"type": "char(26)",
"unsigned": false,
"autoincrement": false,
"primary": false,
"nullable": true,
"length": 26,
"mappedType": "character"
},
"created_at": {
"name": "created_at",
"type": "timestamptz",
"unsigned": false,
"autoincrement": false,
"primary": false,
"nullable": false,
"length": 6,
"default": "now()",
"mappedType": "datetime"
},
"read_at": {
"name": "read_at",
"type": "timestamptz",
"unsigned": false,
"autoincrement": false,
"primary": false,
"nullable": true,
"length": 6,
"mappedType": "datetime"
}
},
"name": "chat",
"schema": "public",
"indexes": [
{
"keyName": "chat_pkey",
"columnNames": [
"id"
],
"composite": false,
"constraint": true,
"primary": true,
"unique": true
}
],
"checks": [],
"foreignKeys": {
"chat_user_id_foreign": {
"constraintName": "chat_user_id_foreign",
"columnNames": [
"user_id"
],
"localTableName": "public.chat",
"referencedColumnNames": [
"id"
],
"referencedTableName": "public.users",
"deleteRule": "set null",
"updateRule": "cascade"
},
"chat_admin_id_foreign": {
"constraintName": "chat_admin_id_foreign",
"columnNames": [
"admin_id"
],
"localTableName": "public.chat",
"referencedColumnNames": [
"id"
],
"referencedTableName": "public.admins",
"deleteRule": "set null",
"updateRule": "cascade"
},
"chat_parent_id_foreign": {
"constraintName": "chat_parent_id_foreign",
"columnNames": [
"parent_id"
],
"localTableName": "public.chat",
"referencedColumnNames": [
"id"
],
"referencedTableName": "public.chat",
"deleteRule": "set null",
"updateRule": "cascade"
}
},
"nativeEnums": {}
},
{
"columns": {
"id": {
@@ -1,43 +0,0 @@
import { Migration } from '@mikro-orm/migrations';
export class Migration20260222084735 extends Migration {
override async up(): Promise<void> {
this.addSql(`create table "learning_categories" ("id" char(26) not null, "created_at" timestamptz not null default now(), "deleted_at" timestamptz null, "name" varchar(100) not null, constraint "learning_categories_pkey" primary key ("id"));`);
this.addSql(`alter table "learning_categories" add constraint "learning_categories_name_unique" unique ("name");`);
this.addSql(`create table "learnings" ("id" char(26) not null, "created_at" timestamptz not null default now(), "deleted_at" timestamptz null, "title" varchar(200) not null, "description" text not null, "cover_url" text not null, "video_url" text null, "video_duration" varchar(200) null, "category_id" char(26) not null, constraint "learnings_pkey" primary key ("id"));`);
this.addSql(`create table "prints" ("id" char(26) not null, "created_at" timestamptz not null default now(), "deleted_at" timestamptz null, "fileds" jsonb null, constraint "prints_pkey" primary key ("id"));`);
this.addSql(`create table "learning_progress" ("id" char(26) not null, "created_at" timestamptz not null default now(), "deleted_at" timestamptz null, "user_id" char(26) not null, "learning_id" char(26) not null, "watched_at" timestamptz not null default CURRENT_TIMESTAMP, constraint "learning_progress_pkey" primary key ("id"));`);
this.addSql(`alter table "learnings" add constraint "learnings_category_id_foreign" foreign key ("category_id") references "learning_categories" ("id") on update cascade;`);
this.addSql(`alter table "learning_progress" add constraint "learning_progress_user_id_foreign" foreign key ("user_id") references "users" ("id") on update cascade;`);
this.addSql(`alter table "learning_progress" add constraint "learning_progress_learning_id_foreign" foreign key ("learning_id") references "learnings" ("id") on update cascade;`);
this.addSql(`alter table "permissions" drop constraint if exists "permissions_name_check";`);
this.addSql(`alter table "permissions" add constraint "permissions_name_check" check("name" in ('manage_products', 'manage_categories', 'view_requests', 'delete_request', 'update_request', 'view_invoices', 'create_invoice', 'update_invoice', 'delete_invoice', 'view_all_invoiced_orders', 'view_my_assigned_invoiced_requests', 'view_orders', 'view_assigned_orders', 'create_order', 'update_order', 'assign_designer', 'delete_order', 'manage_admins', 'manage_roles', 'manage_users', 'manage_payments', 'manage_settings', 'manage_form_builder', 'create_print', 'view_print', 'manage_learnings'));`);
this.addSql(`alter table "orders" add column "print_id" char(26) null;`);
this.addSql(`alter table "orders" add constraint "orders_print_id_foreign" foreign key ("print_id") references "prints" ("id") on update cascade on delete set null;`);
this.addSql(`alter table "orders" add constraint "orders_print_id_unique" unique ("print_id");`);
this.addSql(`alter table "credit_transactions" rename column "order_id" to "invoice_id";`);
}
override async down(): Promise<void> {
this.addSql(`alter table "permissions" drop constraint if exists "permissions_name_check";`);
this.addSql(`alter table "permissions" add constraint "permissions_name_check" check("name" in ('manage_products', 'manage_categories', 'view_requests', 'delete_request', 'update_request', 'view_invoices', 'create_invoice', 'update_invoice', 'delete_invoice', 'view_all_invoiced_orders', 'view_my_assigned_invoiced_requests', 'view_orders', 'view_assigned_orders', 'create_order', 'update_order', 'assign_designer', 'delete_order', 'manage_admins', 'manage_roles', 'manage_users', 'manage_payments', 'manage_settings', 'manage_form_builder', 'manage_print'));`);
this.addSql(`alter table "orders" drop constraint "orders_print_id_unique";`);
this.addSql(`alter table "orders" add column "attributes" jsonb null, add column "printery" varchar(255) null, add column "lithography" varchar(255) null;`);
this.addSql(`alter table "credit_transactions" rename column "invoice_id" to "order_id";`);
}
}
@@ -1,27 +0,0 @@
import { Migration } from '@mikro-orm/migrations';
export class Migration20260222092921 extends Migration {
override async up(): Promise<void> {
this.addSql(`create table "announcements" ("id" char(26) not null, "created_at" timestamptz not null default now(), "deleted_at" timestamptz null, "title" varchar(100) not null, "content" text not null, "publish_at" timestamptz null, "is_important" boolean not null default false, "is_public" boolean not null default false, constraint "announcements_pkey" primary key ("id"));`);
this.addSql(`create index "announcements_is_public_created_at_index" on "announcements" ("is_public", "created_at");`);
this.addSql(`create table "user_announcements" ("id" char(26) not null, "created_at" timestamptz not null default now(), "deleted_at" timestamptz null, "user_id" char(26) not null, "announcement_id" char(26) not null, "is_read" boolean not null default false, constraint "user_announcements_pkey" primary key ("id"));`);
this.addSql(`alter table "user_announcements" add constraint "user_announcements_user_id_foreign" foreign key ("user_id") references "users" ("id") on update cascade on delete cascade;`);
this.addSql(`alter table "user_announcements" add constraint "user_announcements_announcement_id_foreign" foreign key ("announcement_id") references "announcements" ("id") on update cascade on delete cascade;`);
this.addSql(`alter table "permissions" drop constraint if exists "permissions_name_check";`);
this.addSql(`alter table "permissions" add constraint "permissions_name_check" check("name" in ('manage_products', 'manage_categories', 'view_requests', 'delete_request', 'update_request', 'view_invoices', 'create_invoice', 'update_invoice', 'delete_invoice', 'view_all_invoiced_orders', 'view_my_assigned_invoiced_requests', 'view_orders', 'view_assigned_orders', 'create_order', 'update_order', 'assign_designer', 'delete_order', 'manage_admins', 'manage_roles', 'manage_users', 'manage_payments', 'manage_settings', 'manage_form_builder', 'create_print', 'view_print', 'manage_learnings', 'manage_criticisms', 'manage_announcements'));`);
}
override async down(): Promise<void> {
this.addSql(`alter table "permissions" drop constraint if exists "permissions_name_check";`);
this.addSql(`alter table "permissions" add constraint "permissions_name_check" check("name" in ('manage_products', 'manage_categories', 'view_requests', 'delete_request', 'update_request', 'view_invoices', 'create_invoice', 'update_invoice', 'delete_invoice', 'view_all_invoiced_orders', 'view_my_assigned_invoiced_requests', 'view_orders', 'view_assigned_orders', 'create_order', 'update_order', 'assign_designer', 'delete_order', 'manage_admins', 'manage_roles', 'manage_users', 'manage_payments', 'manage_settings', 'manage_form_builder', 'create_print', 'view_print', 'manage_learnings', 'manage_criticisms'));`);
this.addSql(`drop table if exists "user_announcements";`);
this.addSql(`drop table if exists "announcements";`);
}
}
@@ -0,0 +1,17 @@
import { Migration } from '@mikro-orm/migrations';
export class Migration20260404082727 extends Migration {
override async up(): Promise<void> {
this.addSql(`alter table "credit_transactions" drop constraint if exists "credit_transactions_type_check";`);
this.addSql(`alter table "credit_transactions" add constraint "credit_transactions_type_check" check("type" in (''));`);
}
override async down(): Promise<void> {
this.addSql(`alter table "credit_transactions" drop constraint if exists "credit_transactions_type_check";`);
this.addSql(`alter table "credit_transactions" add constraint "credit_transactions_type_check" check("type" in (''));`);
}
}
@@ -0,0 +1,149 @@
import { Migration } from '@mikro-orm/migrations';
export class Migration20260404083020 extends Migration {
override async up(): Promise<void> {
//recalc_invoice_totals
this.addSql(`
CREATE OR REPLACE FUNCTION recalc_invoice_totals()
RETURNS TRIGGER AS $$
DECLARE
v_invoice_id char(26);
v_subtotal int;
v_discount int;
v_tax int;
v_total int;
v_paid int;
v_enable_tax boolean;
BEGIN
v_invoice_id := COALESCE(NEW.invoice_id, OLD.invoice_id);
SELECT
COALESCE(SUM(sub_total),0),
COALESCE(SUM(discount),0)
INTO
v_subtotal,
v_discount
FROM invoice_items
WHERE invoice_id = v_invoice_id
AND deleted_at IS NULL;
SELECT enable_tax
INTO v_enable_tax
FROM invoices
WHERE id = v_invoice_id;
IF v_enable_tax THEN
v_tax := (v_subtotal - v_discount) * 0.10;
ELSE
v_tax := 0;
END IF;
v_total := (v_subtotal - v_discount) + v_tax;
SELECT COALESCE(SUM(amount),0)
INTO v_paid
FROM payments
WHERE invoice_id = v_invoice_id
AND status = 'SUCCESS';
UPDATE invoices
SET
sub_total = v_subtotal,
discount = v_discount,
tax_amount = v_tax,
total = v_total,
paid_amount = v_paid,
balance = v_total - v_paid
WHERE id = v_invoice_id;
RETURN NULL;
END;
$$ LANGUAGE plpgsql;
`);
// trigger
this.addSql(`
CREATE TRIGGER trg_invoice_items_recalc
AFTER INSERT OR UPDATE OR DELETE
ON invoice_items
FOR EACH ROW
EXECUTE FUNCTION recalc_invoice_totals();
`);
// recalc_invoice_payments
this.addSql(`
CREATE OR REPLACE FUNCTION recalc_invoice_payments()
RETURNS TRIGGER AS $$
DECLARE
v_invoice_id char(26);
v_paid int;
v_total int;
BEGIN
v_invoice_id := COALESCE(NEW.invoice_id, OLD.invoice_id);
SELECT COALESCE(SUM(amount),0)
INTO v_paid
FROM payments
WHERE invoice_id = v_invoice_id
AND status = 'paid';
SELECT total
INTO v_total
FROM invoices
WHERE id = v_invoice_id;
UPDATE invoices
SET
paid_amount = v_paid,
balance = v_total - v_paid
WHERE id = v_invoice_id;
RETURN NULL;
END;
$$ LANGUAGE plpgsql;
`);
// trigger
this.addSql(`
CREATE TRIGGER trg_payments_recalc
AFTER INSERT OR UPDATE OR DELETE
ON payments
FOR EACH ROW
EXECUTE FUNCTION recalc_invoice_payments();
`);
this.addSql(`CREATE SEQUENCE invoice_number_seq START 1;`);
this.addSql(`
ALTER TABLE invoices
ALTER COLUMN invoice_number
SET DEFAULT nextval('invoice_number_seq');
`);
}
override async down(): Promise<void> {
this.addSql(`DROP TRIGGER IF EXISTS trg_invoice_items_recalc ON invoice_items;`);
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;`);
// remove default from invoice_number
this.addSql(`
ALTER TABLE invoices
ALTER COLUMN invoice_number
DROP DEFAULT;
`);
// drop sequence
this.addSql(`DROP SEQUENCE IF EXISTS invoice_number_seq;`);
}
}
@@ -0,0 +1,40 @@
import { Migration } from '@mikro-orm/migrations';
export class Migration20260404112450 extends Migration {
override async up(): Promise<void> {
this.addSql(`alter table "invoices" alter column "invoice_number" type int using ("invoice_number"::int);`);
this.addSql(`alter table "invoices" alter column "discount" type int using ("discount"::int);`);
this.addSql(`alter table "invoices" alter column "discount" set not null;`);
this.addSql(`alter table "invoices" alter column "sub_total" type int using ("sub_total"::int);`);
this.addSql(`alter table "invoices" alter column "sub_total" set not null;`);
this.addSql(`alter table "invoices" alter column "tax_amount" type int using ("tax_amount"::int);`);
this.addSql(`alter table "invoices" alter column "tax_amount" set not null;`);
this.addSql(`alter table "invoices" alter column "total" type int using ("total"::int);`);
this.addSql(`alter table "invoices" alter column "total" set not null;`);
this.addSql(`alter table "invoices" alter column "balance" drop default;`);
this.addSql(`alter table "invoices" alter column "balance" type int using ("balance"::int);`);
this.addSql(`alter table "invoices" alter column "invoice_number" drop default;`);
this.addSql(`alter table "invoices" add constraint "invoices_invoice_number_unique" unique ("invoice_number");`);
}
override async down(): Promise<void> {
this.addSql(`alter table "invoices" drop constraint "invoices_invoice_number_unique";`);
this.addSql(`alter table "invoices" alter column "invoice_number" type int using ("invoice_number"::int);`);
this.addSql(`alter table "invoices" alter column "discount" type int using ("discount"::int);`);
this.addSql(`alter table "invoices" alter column "discount" drop not null;`);
this.addSql(`alter table "invoices" alter column "sub_total" type int using ("sub_total"::int);`);
this.addSql(`alter table "invoices" alter column "sub_total" drop not null;`);
this.addSql(`alter table "invoices" alter column "tax_amount" type int using ("tax_amount"::int);`);
this.addSql(`alter table "invoices" alter column "tax_amount" drop not null;`);
this.addSql(`alter table "invoices" alter column "total" type int using ("total"::int);`);
this.addSql(`alter table "invoices" alter column "total" drop not null;`);
this.addSql(`alter table "invoices" alter column "balance" type int using ("balance"::int);`);
this.addSql(`alter table "invoices" alter column "balance" set default 0;`);
this.addSql(`create sequence if not exists "invoices_invoice_number_seq";`);
this.addSql(`select setval('invoices_invoice_number_seq', (select max("invoice_number") from "invoices"));`);
this.addSql(`alter table "invoices" alter column "invoice_number" set default nextval('invoices_invoice_number_seq');`);
}
}