File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 77 DeleteDateColumn ,
88 OneToMany ,
99 VersionColumn ,
10+ Index ,
1011} from 'typeorm' ;
1112import { ApiProperty } from '@nestjs/swagger' ;
1213import { SegmentRule } from './segment-rule.entity' ;
@@ -15,6 +16,7 @@ import { SegmentRule } from './segment-rule.entity';
1516 * Represents the segment entity.
1617 */
1718@Entity ( 'segments' )
19+ @Index ( 'IDX_segments_isDynamic_createdAt' , [ 'isDynamic' , 'createdAt' ] )
1820export class Segment {
1921 @ApiProperty ( )
2022 @PrimaryGeneratedColumn ( 'uuid' )
@@ -24,6 +26,7 @@ export class Segment {
2426 version : number ;
2527
2628 @ApiProperty ( )
29+ @Index ( 'IDX_segments_name' )
2730 @Column ( )
2831 name : string ;
2932
@@ -53,6 +56,9 @@ export class Segment {
5356 @UpdateDateColumn ( )
5457 updatedAt : Date ;
5558
59+ @Index ( 'IDX_segments_deletedAt' , [ 'deletedAt' ] , {
60+ where : '"deletedAt" IS NULL' ,
61+ } )
5662 @DeleteDateColumn ( )
5763 deletedAt ?: Date ;
5864}
Original file line number Diff line number Diff line change 1+ import { MigrationInterface , QueryRunner } from 'typeorm' ;
2+
3+ /**
4+ * Adds indexes to the `segments` table for the common list and lookup paths:
5+ *
6+ * - `name` lookup / search
7+ * - `isDynamic` + `createdAt` for active/dynamic segment listings ordered by newest first
8+ * - `deletedAt IS NULL` for soft-delete filtering while retaining an efficient active-only index
9+ *
10+ * These cover the primary query paths without introducing redundant single-column indexes
11+ * on the already-prefixed composite `isDynamic, createdAt` index.
12+ */
13+ export class AddSegmentIndexes1806000000000 implements MigrationInterface {
14+ public async up ( queryRunner : QueryRunner ) : Promise < void > {
15+ await queryRunner . query (
16+ 'CREATE INDEX IF NOT EXISTS "IDX_segments_name" ON "segments" ("name")' ,
17+ ) ;
18+
19+ await queryRunner . query (
20+ 'CREATE INDEX IF NOT EXISTS "IDX_segments_isDynamic_createdAt" ON "segments" ("isDynamic", "createdAt")' ,
21+ ) ;
22+
23+ await queryRunner . query (
24+ 'CREATE INDEX IF NOT EXISTS "IDX_segments_deletedAt" ON "segments" ("deletedAt") WHERE "deletedAt" IS NULL' ,
25+ ) ;
26+ }
27+
28+ public async down ( queryRunner : QueryRunner ) : Promise < void > {
29+ await queryRunner . query ( 'DROP INDEX IF EXISTS "IDX_segments_deletedAt"' ) ;
30+ await queryRunner . query ( 'DROP INDEX IF EXISTS "IDX_segments_isDynamic_createdAt"' ) ;
31+ await queryRunner . query ( 'DROP INDEX IF EXISTS "IDX_segments_name"' ) ;
32+ }
33+ }
You can’t perform that action at this time.
0 commit comments