This commit is contained in:
2026-07-03 17:18:45 +03:30
parent 5042cadb32
commit 74d5b7a768
20 changed files with 1032 additions and 19 deletions
@@ -0,0 +1,99 @@
import {
MigrationInterface,
QueryRunner,
Table,
TableForeignKey,
TableIndex,
} from 'typeorm';
export class AddPayments1720600000000 implements MigrationInterface {
name = 'AddPayments1720600000000';
public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`
CREATE TYPE "payments_type_enum" AS ENUM('deposit', 'remaining')
`);
await queryRunner.query(`
CREATE TYPE "payments_status_enum" AS ENUM(
'pending',
'paid',
'failed',
'cancelled'
)
`);
await queryRunner.createTable(
new Table({
name: 'payments',
columns: [
{
name: 'id',
type: 'uuid',
isPrimary: true,
generationStrategy: 'uuid',
default: 'gen_random_uuid()',
},
{ name: 'reserveId', type: 'uuid' },
{
name: 'type',
type: 'payments_type_enum',
},
{ name: 'amountRial', type: 'int' },
{
name: 'authority',
type: 'varchar',
length: '36',
isNullable: true,
},
{ name: 'refId', type: 'bigint', isNullable: true },
{
name: 'status',
type: 'payments_status_enum',
default: `'pending'`,
},
{
name: 'cardPan',
type: 'varchar',
length: '32',
isNullable: true,
},
{ name: 'description', type: 'text' },
{ name: 'createdAt', type: 'timestamp', default: 'now()' },
{ name: 'updatedAt', type: 'timestamp', default: 'now()' },
],
}),
true,
);
await queryRunner.createForeignKey(
'payments',
new TableForeignKey({
columnNames: ['reserveId'],
referencedTableName: 'reserves',
referencedColumnNames: ['id'],
onDelete: 'CASCADE',
}),
);
await queryRunner.createIndex(
'payments',
new TableIndex({
name: 'IDX_payments_reserveId',
columnNames: ['reserveId'],
}),
);
await queryRunner.query(`
CREATE UNIQUE INDEX "IDX_payments_authority"
ON "payments" ("authority")
WHERE "authority" IS NOT NULL
`);
}
public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.dropTable('payments', true);
await queryRunner.query(`DROP TYPE "payments_status_enum"`);
await queryRunner.query(`DROP TYPE "payments_type_enum"`);
}
}
@@ -0,0 +1,83 @@
import { MigrationInterface, QueryRunner, TableForeignKey, TableIndex } from 'typeorm';
export class ExtendPaymentsForSubscriptions1720700000000
implements MigrationInterface
{
name = 'ExtendPaymentsForSubscriptions1720700000000';
public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`
ALTER TYPE "payments_type_enum" ADD VALUE 'subscription'
`);
await queryRunner.query(`
ALTER TABLE "payments" ALTER COLUMN "reserveId" DROP NOT NULL
`);
await queryRunner.query(`
ALTER TABLE "payments" ADD COLUMN "salonId" uuid
`);
await queryRunner.query(`
ALTER TABLE "payments" ADD COLUMN "planId" uuid
`);
await queryRunner.createForeignKey(
'payments',
new TableForeignKey({
columnNames: ['salonId'],
referencedTableName: 'salons',
referencedColumnNames: ['id'],
onDelete: 'CASCADE',
}),
);
await queryRunner.createForeignKey(
'payments',
new TableForeignKey({
columnNames: ['planId'],
referencedTableName: 'subscription_plans',
referencedColumnNames: ['id'],
onDelete: 'RESTRICT',
}),
);
await queryRunner.createIndex(
'payments',
new TableIndex({
name: 'IDX_payments_salonId',
columnNames: ['salonId'],
}),
);
}
public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.dropIndex('payments', 'IDX_payments_salonId');
const table = await queryRunner.getTable('payments');
const salonFk = table?.foreignKeys.find((fk) =>
fk.columnNames.includes('salonId'),
);
const planFk = table?.foreignKeys.find((fk) =>
fk.columnNames.includes('planId'),
);
if (salonFk) {
await queryRunner.dropForeignKey('payments', salonFk);
}
if (planFk) {
await queryRunner.dropForeignKey('payments', planFk);
}
await queryRunner.query(`ALTER TABLE "payments" DROP COLUMN "planId"`);
await queryRunner.query(`ALTER TABLE "payments" DROP COLUMN "salonId"`);
await queryRunner.query(`
DELETE FROM "payments" WHERE "reserveId" IS NULL
`);
await queryRunner.query(`
ALTER TABLE "payments" ALTER COLUMN "reserveId" SET NOT NULL
`);
}
}