|
| 1 | +import { |
| 2 | + MigrationInterface, |
| 3 | + QueryRunner, |
| 4 | + Table, |
| 5 | + TableForeignKey, |
| 6 | + TableIndex, |
| 7 | +} from 'typeorm'; |
| 8 | + |
| 9 | +export class CreateSavingsGoalsTable1775100000000 implements MigrationInterface { |
| 10 | + public async up(queryRunner: QueryRunner): Promise<void> { |
| 11 | + await queryRunner.createTable( |
| 12 | + new Table({ |
| 13 | + name: 'savings_goals', |
| 14 | + columns: [ |
| 15 | + { |
| 16 | + name: 'id', |
| 17 | + type: 'uuid', |
| 18 | + isPrimary: true, |
| 19 | + generationStrategy: 'uuid', |
| 20 | + default: 'uuid_generate_v4()', |
| 21 | + }, |
| 22 | + { |
| 23 | + name: 'userId', |
| 24 | + type: 'uuid', |
| 25 | + isNullable: false, |
| 26 | + }, |
| 27 | + { |
| 28 | + name: 'goalName', |
| 29 | + type: 'varchar', |
| 30 | + length: '255', |
| 31 | + isNullable: false, |
| 32 | + }, |
| 33 | + { |
| 34 | + name: 'targetAmount', |
| 35 | + type: 'decimal', |
| 36 | + precision: 14, |
| 37 | + scale: 2, |
| 38 | + isNullable: false, |
| 39 | + }, |
| 40 | + { |
| 41 | + name: 'targetDate', |
| 42 | + type: 'date', |
| 43 | + isNullable: false, |
| 44 | + }, |
| 45 | + { |
| 46 | + name: 'status', |
| 47 | + type: 'enum', |
| 48 | + enum: ['IN_PROGRESS', 'COMPLETED'], |
| 49 | + default: "'IN_PROGRESS'", |
| 50 | + isNullable: false, |
| 51 | + }, |
| 52 | + { |
| 53 | + name: 'metadata', |
| 54 | + type: 'jsonb', |
| 55 | + isNullable: true, |
| 56 | + }, |
| 57 | + { |
| 58 | + name: 'createdAt', |
| 59 | + type: 'timestamp', |
| 60 | + default: 'now()', |
| 61 | + }, |
| 62 | + { |
| 63 | + name: 'updatedAt', |
| 64 | + type: 'timestamp', |
| 65 | + default: 'now()', |
| 66 | + }, |
| 67 | + ], |
| 68 | + }), |
| 69 | + true, |
| 70 | + ); |
| 71 | + |
| 72 | + // Create foreign key to users table |
| 73 | + await queryRunner.createForeignKey( |
| 74 | + 'savings_goals', |
| 75 | + new TableForeignKey({ |
| 76 | + columnNames: ['userId'], |
| 77 | + referencedTableName: 'users', |
| 78 | + referencedColumnNames: ['id'], |
| 79 | + onDelete: 'CASCADE', |
| 80 | + }), |
| 81 | + ); |
| 82 | + |
| 83 | + // Create index on userId for faster lookups |
| 84 | + await queryRunner.createIndex( |
| 85 | + 'savings_goals', |
| 86 | + new TableIndex({ |
| 87 | + name: 'IDX_SAVINGS_GOALS_USER_ID', |
| 88 | + columnNames: ['userId'], |
| 89 | + }), |
| 90 | + ); |
| 91 | + |
| 92 | + // Create index on status for filtering active/completed goals |
| 93 | + await queryRunner.createIndex( |
| 94 | + 'savings_goals', |
| 95 | + new TableIndex({ |
| 96 | + name: 'IDX_SAVINGS_GOALS_STATUS', |
| 97 | + columnNames: ['status'], |
| 98 | + }), |
| 99 | + ); |
| 100 | + |
| 101 | + // Create index on targetDate for date-based queries |
| 102 | + await queryRunner.createIndex( |
| 103 | + 'savings_goals', |
| 104 | + new TableIndex({ |
| 105 | + name: 'IDX_SAVINGS_GOALS_TARGET_DATE', |
| 106 | + columnNames: ['targetDate'], |
| 107 | + }), |
| 108 | + ); |
| 109 | + } |
| 110 | + |
| 111 | + public async down(queryRunner: QueryRunner): Promise<void> { |
| 112 | + await queryRunner.dropTable('savings_goals'); |
| 113 | + } |
| 114 | +} |
0 commit comments