@@ -8,31 +8,36 @@ namespace Resgrid.Providers.MigrationsPg.Migrations
88 /// covering indexes on the source tables. All identifiers are lowercased per the PG convention.
99 /// Index creation is guarded so it is safe if a matching index already exists.
1010 /// </summary>
11- [ Migration ( 73 ) ]
11+ [ Migration ( 73 , TransactionBehavior . None ) ]
1212 public class M0073_AddingReportingIndexesPg : Migration
1313 {
1414 public override void Up ( )
1515 {
1616 // Pre-aggregated daily rollup (DepartmentId null => system-wide row).
17- Create . Table ( "ReportingDailyRollup" . ToLower ( ) )
18- . WithColumn ( "ReportingDailyRollupId" . ToLower ( ) ) . AsInt64 ( ) . NotNullable ( ) . PrimaryKey ( ) . Identity ( )
19- . WithColumn ( "DepartmentId" . ToLower ( ) ) . AsInt32 ( ) . Nullable ( )
20- . WithColumn ( "BucketDateUtc" . ToLower ( ) ) . AsDateTime2 ( ) . NotNullable ( )
21- . WithColumn ( "Metric" . ToLower ( ) ) . AsCustom ( "citext" ) . NotNullable ( )
22- . WithColumn ( "Dimension" . ToLower ( ) ) . AsCustom ( "citext" ) . Nullable ( )
23- . WithColumn ( "ItemCount" . ToLower ( ) ) . AsInt64 ( ) . NotNullable ( ) . WithDefaultValue ( 0 )
24- . WithColumn ( "SumValue" . ToLower ( ) ) . AsDecimal ( 18 , 4 ) . Nullable ( )
25- . WithColumn ( "MinValue" . ToLower ( ) ) . AsDecimal ( 18 , 4 ) . Nullable ( )
26- . WithColumn ( "MaxValue" . ToLower ( ) ) . AsDecimal ( 18 , 4 ) . Nullable ( )
27- . WithColumn ( "P50" . ToLower ( ) ) . AsDecimal ( 18 , 4 ) . Nullable ( )
28- . WithColumn ( "P90" . ToLower ( ) ) . AsDecimal ( 18 , 4 ) . Nullable ( )
29- . WithColumn ( "CreatedOnUtc" . ToLower ( ) ) . AsDateTime2 ( ) . NotNullable ( ) . WithDefault ( SystemMethods . CurrentUTCDateTime ) ;
17+ // TransactionBehavior.None means each statement self-commits, so every create below is
18+ // guarded with an existence check to stay safe on a re-run after a partial apply
19+ // (e.g. when a later large index build times out and the migration is retried).
20+ if ( ! Schema . Table ( "ReportingDailyRollup" . ToLower ( ) ) . Exists ( ) )
21+ Create . Table ( "ReportingDailyRollup" . ToLower ( ) )
22+ . WithColumn ( "ReportingDailyRollupId" . ToLower ( ) ) . AsInt64 ( ) . NotNullable ( ) . PrimaryKey ( ) . Identity ( )
23+ . WithColumn ( "DepartmentId" . ToLower ( ) ) . AsInt32 ( ) . Nullable ( )
24+ . WithColumn ( "BucketDateUtc" . ToLower ( ) ) . AsDateTime2 ( ) . NotNullable ( )
25+ . WithColumn ( "Metric" . ToLower ( ) ) . AsCustom ( "citext" ) . NotNullable ( )
26+ . WithColumn ( "Dimension" . ToLower ( ) ) . AsCustom ( "citext" ) . Nullable ( )
27+ . WithColumn ( "ItemCount" . ToLower ( ) ) . AsInt64 ( ) . NotNullable ( ) . WithDefaultValue ( 0 )
28+ . WithColumn ( "SumValue" . ToLower ( ) ) . AsDecimal ( 18 , 4 ) . Nullable ( )
29+ . WithColumn ( "MinValue" . ToLower ( ) ) . AsDecimal ( 18 , 4 ) . Nullable ( )
30+ . WithColumn ( "MaxValue" . ToLower ( ) ) . AsDecimal ( 18 , 4 ) . Nullable ( )
31+ . WithColumn ( "P50" . ToLower ( ) ) . AsDecimal ( 18 , 4 ) . Nullable ( )
32+ . WithColumn ( "P90" . ToLower ( ) ) . AsDecimal ( 18 , 4 ) . Nullable ( )
33+ . WithColumn ( "CreatedOnUtc" . ToLower ( ) ) . AsDateTime2 ( ) . NotNullable ( ) . WithDefault ( SystemMethods . CurrentUTCDateTime ) ;
3034
31- Create . Index ( "IX_ReportingDailyRollup_Dept_Date_Metric" . ToLower ( ) )
32- . OnTable ( "ReportingDailyRollup" . ToLower ( ) )
33- . OnColumn ( "DepartmentId" . ToLower ( ) ) . Ascending ( )
34- . OnColumn ( "BucketDateUtc" . ToLower ( ) ) . Ascending ( )
35- . OnColumn ( "Metric" . ToLower ( ) ) . Ascending ( ) ;
35+ if ( ! Schema . Table ( "ReportingDailyRollup" . ToLower ( ) ) . Index ( "IX_ReportingDailyRollup_Dept_Date_Metric" . ToLower ( ) ) . Exists ( ) )
36+ Create . Index ( "IX_ReportingDailyRollup_Dept_Date_Metric" . ToLower ( ) )
37+ . OnTable ( "ReportingDailyRollup" . ToLower ( ) )
38+ . OnColumn ( "DepartmentId" . ToLower ( ) ) . Ascending ( )
39+ . OnColumn ( "BucketDateUtc" . ToLower ( ) ) . Ascending ( )
40+ . OnColumn ( "Metric" . ToLower ( ) ) . Ascending ( ) ;
3641
3742 // Source-table covering indexes for the live dashboard aggregates.
3843 if ( ! Schema . Table ( "Calls" . ToLower ( ) ) . Index ( "IX_Calls_DepartmentId_LoggedOn" . ToLower ( ) ) . Exists ( ) )
@@ -98,7 +103,8 @@ public override void Down()
98103 if ( Schema . Table ( "Calls" . ToLower ( ) ) . Index ( "IX_Calls_DepartmentId_LoggedOn" . ToLower ( ) ) . Exists ( ) )
99104 Delete . Index ( "IX_Calls_DepartmentId_LoggedOn" . ToLower ( ) ) . OnTable ( "Calls" . ToLower ( ) ) ;
100105
101- Delete . Table ( "ReportingDailyRollup" . ToLower ( ) ) ;
106+ if ( Schema . Table ( "ReportingDailyRollup" . ToLower ( ) ) . Exists ( ) )
107+ Delete . Table ( "ReportingDailyRollup" . ToLower ( ) ) ;
102108 }
103109 }
104110}
0 commit comments