Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,31 +9,36 @@ namespace Resgrid.Providers.Migrations.Migrations
/// live dashboard aggregates GROUP BY / filter on. Index creation is guarded so it is safe if a
/// matching index already exists.
/// </summary>
[Migration(73)]
[Migration(73, TransactionBehavior.None)]
public class M0073_AddingReportingIndexes : Migration
{
public override void Up()
{
// Pre-aggregated daily rollup (DepartmentId null => system-wide row).
Create.Table("ReportingDailyRollup")
.WithColumn("ReportingDailyRollupId").AsInt64().NotNullable().PrimaryKey().Identity()
.WithColumn("DepartmentId").AsInt32().Nullable()
.WithColumn("BucketDateUtc").AsDateTime2().NotNullable()
.WithColumn("Metric").AsString(128).NotNullable()
.WithColumn("Dimension").AsString(256).Nullable()
.WithColumn("ItemCount").AsInt64().NotNullable().WithDefaultValue(0)
.WithColumn("SumValue").AsDecimal(18, 4).Nullable()
.WithColumn("MinValue").AsDecimal(18, 4).Nullable()
.WithColumn("MaxValue").AsDecimal(18, 4).Nullable()
.WithColumn("P50").AsDecimal(18, 4).Nullable()
.WithColumn("P90").AsDecimal(18, 4).Nullable()
.WithColumn("CreatedOnUtc").AsDateTime2().NotNullable().WithDefault(SystemMethods.CurrentUTCDateTime);
// TransactionBehavior.None means each statement self-commits, so every create below is
// guarded with an existence check to stay safe on a re-run after a partial apply
// (e.g. when a later large index build times out and the migration is retried).
if (!Schema.Table("ReportingDailyRollup").Exists())
Create.Table("ReportingDailyRollup")
.WithColumn("ReportingDailyRollupId").AsInt64().NotNullable().PrimaryKey().Identity()
.WithColumn("DepartmentId").AsInt32().Nullable()
.WithColumn("BucketDateUtc").AsDateTime2().NotNullable()
.WithColumn("Metric").AsString(128).NotNullable()
.WithColumn("Dimension").AsString(256).Nullable()
.WithColumn("ItemCount").AsInt64().NotNullable().WithDefaultValue(0)
.WithColumn("SumValue").AsDecimal(18, 4).Nullable()
.WithColumn("MinValue").AsDecimal(18, 4).Nullable()
.WithColumn("MaxValue").AsDecimal(18, 4).Nullable()
.WithColumn("P50").AsDecimal(18, 4).Nullable()
.WithColumn("P90").AsDecimal(18, 4).Nullable()
.WithColumn("CreatedOnUtc").AsDateTime2().NotNullable().WithDefault(SystemMethods.CurrentUTCDateTime);

Create.Index("IX_ReportingDailyRollup_Dept_Date_Metric")
.OnTable("ReportingDailyRollup")
.OnColumn("DepartmentId").Ascending()
.OnColumn("BucketDateUtc").Ascending()
.OnColumn("Metric").Ascending();
if (!Schema.Table("ReportingDailyRollup").Index("IX_ReportingDailyRollup_Dept_Date_Metric").Exists())
Create.Index("IX_ReportingDailyRollup_Dept_Date_Metric")
.OnTable("ReportingDailyRollup")
.OnColumn("DepartmentId").Ascending()
.OnColumn("BucketDateUtc").Ascending()
.OnColumn("Metric").Ascending();

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

Delete.Table("ReportingDailyRollup");
if (Schema.Table("ReportingDailyRollup").Exists())
Delete.Table("ReportingDailyRollup");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,31 +8,36 @@ namespace Resgrid.Providers.MigrationsPg.Migrations
/// covering indexes on the source tables. All identifiers are lowercased per the PG convention.
/// Index creation is guarded so it is safe if a matching index already exists.
/// </summary>
[Migration(73)]
[Migration(73, TransactionBehavior.None)]
public class M0073_AddingReportingIndexesPg : Migration
{
public override void Up()
{
// Pre-aggregated daily rollup (DepartmentId null => system-wide row).
Create.Table("ReportingDailyRollup".ToLower())
.WithColumn("ReportingDailyRollupId".ToLower()).AsInt64().NotNullable().PrimaryKey().Identity()
.WithColumn("DepartmentId".ToLower()).AsInt32().Nullable()
.WithColumn("BucketDateUtc".ToLower()).AsDateTime2().NotNullable()
.WithColumn("Metric".ToLower()).AsCustom("citext").NotNullable()
.WithColumn("Dimension".ToLower()).AsCustom("citext").Nullable()
.WithColumn("ItemCount".ToLower()).AsInt64().NotNullable().WithDefaultValue(0)
.WithColumn("SumValue".ToLower()).AsDecimal(18, 4).Nullable()
.WithColumn("MinValue".ToLower()).AsDecimal(18, 4).Nullable()
.WithColumn("MaxValue".ToLower()).AsDecimal(18, 4).Nullable()
.WithColumn("P50".ToLower()).AsDecimal(18, 4).Nullable()
.WithColumn("P90".ToLower()).AsDecimal(18, 4).Nullable()
.WithColumn("CreatedOnUtc".ToLower()).AsDateTime2().NotNullable().WithDefault(SystemMethods.CurrentUTCDateTime);
// TransactionBehavior.None means each statement self-commits, so every create below is
// guarded with an existence check to stay safe on a re-run after a partial apply
// (e.g. when a later large index build times out and the migration is retried).
if (!Schema.Table("ReportingDailyRollup".ToLower()).Exists())
Create.Table("ReportingDailyRollup".ToLower())
.WithColumn("ReportingDailyRollupId".ToLower()).AsInt64().NotNullable().PrimaryKey().Identity()
.WithColumn("DepartmentId".ToLower()).AsInt32().Nullable()
.WithColumn("BucketDateUtc".ToLower()).AsDateTime2().NotNullable()
.WithColumn("Metric".ToLower()).AsCustom("citext").NotNullable()
.WithColumn("Dimension".ToLower()).AsCustom("citext").Nullable()
.WithColumn("ItemCount".ToLower()).AsInt64().NotNullable().WithDefaultValue(0)
.WithColumn("SumValue".ToLower()).AsDecimal(18, 4).Nullable()
.WithColumn("MinValue".ToLower()).AsDecimal(18, 4).Nullable()
.WithColumn("MaxValue".ToLower()).AsDecimal(18, 4).Nullable()
.WithColumn("P50".ToLower()).AsDecimal(18, 4).Nullable()
.WithColumn("P90".ToLower()).AsDecimal(18, 4).Nullable()
.WithColumn("CreatedOnUtc".ToLower()).AsDateTime2().NotNullable().WithDefault(SystemMethods.CurrentUTCDateTime);

Create.Index("IX_ReportingDailyRollup_Dept_Date_Metric".ToLower())
.OnTable("ReportingDailyRollup".ToLower())
.OnColumn("DepartmentId".ToLower()).Ascending()
.OnColumn("BucketDateUtc".ToLower()).Ascending()
.OnColumn("Metric".ToLower()).Ascending();
if (!Schema.Table("ReportingDailyRollup".ToLower()).Index("IX_ReportingDailyRollup_Dept_Date_Metric".ToLower()).Exists())
Create.Index("IX_ReportingDailyRollup_Dept_Date_Metric".ToLower())
.OnTable("ReportingDailyRollup".ToLower())
.OnColumn("DepartmentId".ToLower()).Ascending()
.OnColumn("BucketDateUtc".ToLower()).Ascending()
.OnColumn("Metric".ToLower()).Ascending();

// Source-table covering indexes for the live dashboard aggregates.
if (!Schema.Table("Calls".ToLower()).Index("IX_Calls_DepartmentId_LoggedOn".ToLower()).Exists())
Expand Down Expand Up @@ -98,7 +103,8 @@ public override void Down()
if (Schema.Table("Calls".ToLower()).Index("IX_Calls_DepartmentId_LoggedOn".ToLower()).Exists())
Delete.Index("IX_Calls_DepartmentId_LoggedOn".ToLower()).OnTable("Calls".ToLower());

Delete.Table("ReportingDailyRollup".ToLower());
if (Schema.Table("ReportingDailyRollup".ToLower()).Exists())
Delete.Table("ReportingDailyRollup".ToLower());
}
}
}
6 changes: 6 additions & 0 deletions Workers/Resgrid.Workers.Console/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -562,6 +562,9 @@ private static IServiceProvider CreateServices()
.AddPostgres11_0()
// Set the connection string
.WithGlobalConnectionString(Config.DataConfig.CoreConnectionString)
// Index builds on large tables (e.g. ActionLogs) exceed the 30s default;
// allow up to 30 minutes per command so migrations don't time out.
.WithGlobalCommandTimeout(TimeSpan.FromMinutes(30))
// Define the assembly containing the migrations
.ScanIn(typeof(M0001_InitialMigrationPg).Assembly).For.Migrations().For.EmbeddedResources())
// Enable logging to console in the FluentMigrator way
Expand All @@ -580,6 +583,9 @@ private static IServiceProvider CreateServices()
.AddSqlServer()
// Set the connection string
.WithGlobalConnectionString(Config.DataConfig.CoreConnectionString)
// Index builds on large tables (e.g. ActionLogs) exceed the 30s default;
// allow up to 30 minutes per command so migrations don't time out.
.WithGlobalCommandTimeout(TimeSpan.FromMinutes(30))
// Define the assembly containing the migrations
.ScanIn(typeof(M0001_InitialMigration).Assembly).For.Migrations().For.EmbeddedResources())
// Enable logging to console in the FluentMigrator way
Expand Down
Loading