Skip to content

Commit d323da1

Browse files
authored
Merge pull request #402 from Resgrid/develop
RE1-T117 migration fix
2 parents 6f9eedb + 1a92bea commit d323da1

3 files changed

Lines changed: 58 additions & 40 deletions

File tree

Providers/Resgrid.Providers.Migrations/Migrations/M0073_AddingReportingIndexes.cs

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -9,31 +9,36 @@ namespace Resgrid.Providers.Migrations.Migrations
99
/// live dashboard aggregates GROUP BY / filter on. Index creation is guarded so it is safe if a
1010
/// matching index already exists.
1111
/// </summary>
12-
[Migration(73)]
12+
[Migration(73, TransactionBehavior.None)]
1313
public class M0073_AddingReportingIndexes : Migration
1414
{
1515
public override void Up()
1616
{
1717
// Pre-aggregated daily rollup (DepartmentId null => system-wide row).
18-
Create.Table("ReportingDailyRollup")
19-
.WithColumn("ReportingDailyRollupId").AsInt64().NotNullable().PrimaryKey().Identity()
20-
.WithColumn("DepartmentId").AsInt32().Nullable()
21-
.WithColumn("BucketDateUtc").AsDateTime2().NotNullable()
22-
.WithColumn("Metric").AsString(128).NotNullable()
23-
.WithColumn("Dimension").AsString(256).Nullable()
24-
.WithColumn("ItemCount").AsInt64().NotNullable().WithDefaultValue(0)
25-
.WithColumn("SumValue").AsDecimal(18, 4).Nullable()
26-
.WithColumn("MinValue").AsDecimal(18, 4).Nullable()
27-
.WithColumn("MaxValue").AsDecimal(18, 4).Nullable()
28-
.WithColumn("P50").AsDecimal(18, 4).Nullable()
29-
.WithColumn("P90").AsDecimal(18, 4).Nullable()
30-
.WithColumn("CreatedOnUtc").AsDateTime2().NotNullable().WithDefault(SystemMethods.CurrentUTCDateTime);
18+
// TransactionBehavior.None means each statement self-commits, so every create below is
19+
// guarded with an existence check to stay safe on a re-run after a partial apply
20+
// (e.g. when a later large index build times out and the migration is retried).
21+
if (!Schema.Table("ReportingDailyRollup").Exists())
22+
Create.Table("ReportingDailyRollup")
23+
.WithColumn("ReportingDailyRollupId").AsInt64().NotNullable().PrimaryKey().Identity()
24+
.WithColumn("DepartmentId").AsInt32().Nullable()
25+
.WithColumn("BucketDateUtc").AsDateTime2().NotNullable()
26+
.WithColumn("Metric").AsString(128).NotNullable()
27+
.WithColumn("Dimension").AsString(256).Nullable()
28+
.WithColumn("ItemCount").AsInt64().NotNullable().WithDefaultValue(0)
29+
.WithColumn("SumValue").AsDecimal(18, 4).Nullable()
30+
.WithColumn("MinValue").AsDecimal(18, 4).Nullable()
31+
.WithColumn("MaxValue").AsDecimal(18, 4).Nullable()
32+
.WithColumn("P50").AsDecimal(18, 4).Nullable()
33+
.WithColumn("P90").AsDecimal(18, 4).Nullable()
34+
.WithColumn("CreatedOnUtc").AsDateTime2().NotNullable().WithDefault(SystemMethods.CurrentUTCDateTime);
3135

32-
Create.Index("IX_ReportingDailyRollup_Dept_Date_Metric")
33-
.OnTable("ReportingDailyRollup")
34-
.OnColumn("DepartmentId").Ascending()
35-
.OnColumn("BucketDateUtc").Ascending()
36-
.OnColumn("Metric").Ascending();
36+
if (!Schema.Table("ReportingDailyRollup").Index("IX_ReportingDailyRollup_Dept_Date_Metric").Exists())
37+
Create.Index("IX_ReportingDailyRollup_Dept_Date_Metric")
38+
.OnTable("ReportingDailyRollup")
39+
.OnColumn("DepartmentId").Ascending()
40+
.OnColumn("BucketDateUtc").Ascending()
41+
.OnColumn("Metric").Ascending();
3742

3843
// Source-table covering indexes for the live dashboard aggregates.
3944
if (!Schema.Table("Calls").Index("IX_Calls_DepartmentId_LoggedOn").Exists())
@@ -100,7 +105,8 @@ public override void Down()
100105
if (Schema.Table("Calls").Index("IX_Calls_DepartmentId_LoggedOn").Exists())
101106
Delete.Index("IX_Calls_DepartmentId_LoggedOn").OnTable("Calls");
102107

103-
Delete.Table("ReportingDailyRollup");
108+
if (Schema.Table("ReportingDailyRollup").Exists())
109+
Delete.Table("ReportingDailyRollup");
104110
}
105111
}
106112
}

Providers/Resgrid.Providers.MigrationsPg/Migrations/M0073_AddingReportingIndexesPg.cs

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

Workers/Resgrid.Workers.Console/Program.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -562,6 +562,9 @@ private static IServiceProvider CreateServices()
562562
.AddPostgres11_0()
563563
// Set the connection string
564564
.WithGlobalConnectionString(Config.DataConfig.CoreConnectionString)
565+
// Index builds on large tables (e.g. ActionLogs) exceed the 30s default;
566+
// allow up to 30 minutes per command so migrations don't time out.
567+
.WithGlobalCommandTimeout(TimeSpan.FromMinutes(30))
565568
// Define the assembly containing the migrations
566569
.ScanIn(typeof(M0001_InitialMigrationPg).Assembly).For.Migrations().For.EmbeddedResources())
567570
// Enable logging to console in the FluentMigrator way
@@ -580,6 +583,9 @@ private static IServiceProvider CreateServices()
580583
.AddSqlServer()
581584
// Set the connection string
582585
.WithGlobalConnectionString(Config.DataConfig.CoreConnectionString)
586+
// Index builds on large tables (e.g. ActionLogs) exceed the 30s default;
587+
// allow up to 30 minutes per command so migrations don't time out.
588+
.WithGlobalCommandTimeout(TimeSpan.FromMinutes(30))
583589
// Define the assembly containing the migrations
584590
.ScanIn(typeof(M0001_InitialMigration).Assembly).For.Migrations().For.EmbeddedResources())
585591
// Enable logging to console in the FluentMigrator way

0 commit comments

Comments
 (0)