将索引缩短为 IX_CE_Offering_Status_WaitlistedAt。
将候补迁移改为可重入:先检查列和索引是否存在,再执行 DDL。 附件中已经成功添加 WaitlistedAt、但索引创建失败的数据库可直接重试,不会报重复列。 运行时和 dotnet ef 连接统一开启 AllowUserVariables;Connector/NET 默认关闭该选项。Connector/NET 官方说明 增加所有 MySQL 索引名不得超过 64 字符的回归测试。
This commit is contained in:
+80
-15
@@ -12,26 +12,91 @@ public partial class CourseSelectionWaitlist : Migration
|
||||
{
|
||||
protected override void Up(MigrationBuilder migrationBuilder)
|
||||
{
|
||||
migrationBuilder.AddColumn<DateTime>(
|
||||
name: "WaitlistedAt",
|
||||
table: "CourseEnrollments",
|
||||
type: "datetime(6)",
|
||||
nullable: true);
|
||||
migrationBuilder.Sql("""
|
||||
SET @waitlisted_column_exists = (
|
||||
SELECT COUNT(*)
|
||||
FROM `INFORMATION_SCHEMA`.`COLUMNS`
|
||||
WHERE `TABLE_SCHEMA` = DATABASE()
|
||||
AND `TABLE_NAME` = 'CourseEnrollments'
|
||||
AND `COLUMN_NAME` = 'WaitlistedAt'
|
||||
);
|
||||
""");
|
||||
migrationBuilder.Sql("""
|
||||
SET @add_waitlisted_column_sql = IF(
|
||||
@waitlisted_column_exists = 0,
|
||||
'ALTER TABLE `CourseEnrollments` ADD COLUMN `WaitlistedAt` datetime(6) NULL',
|
||||
'SELECT 1'
|
||||
);
|
||||
""");
|
||||
migrationBuilder.Sql(
|
||||
"PREPARE add_waitlisted_column_stmt FROM @add_waitlisted_column_sql;");
|
||||
migrationBuilder.Sql("EXECUTE add_waitlisted_column_stmt;");
|
||||
migrationBuilder.Sql("DEALLOCATE PREPARE add_waitlisted_column_stmt;");
|
||||
|
||||
migrationBuilder.CreateIndex(
|
||||
name: "IX_CourseEnrollments_CourseSelectionOfferingId_Status_WaitlistedAt",
|
||||
table: "CourseEnrollments",
|
||||
columns: new[] { "CourseSelectionOfferingId", "Status", "WaitlistedAt" });
|
||||
migrationBuilder.Sql("""
|
||||
SET @waitlist_index_exists = (
|
||||
SELECT COUNT(*)
|
||||
FROM `INFORMATION_SCHEMA`.`STATISTICS`
|
||||
WHERE `TABLE_SCHEMA` = DATABASE()
|
||||
AND `TABLE_NAME` = 'CourseEnrollments'
|
||||
AND `INDEX_NAME` = 'IX_CE_Offering_Status_WaitlistedAt'
|
||||
);
|
||||
""");
|
||||
migrationBuilder.Sql("""
|
||||
SET @add_waitlist_index_sql = IF(
|
||||
@waitlist_index_exists = 0,
|
||||
'CREATE INDEX `IX_CE_Offering_Status_WaitlistedAt` ON `CourseEnrollments` (`CourseSelectionOfferingId`, `Status`, `WaitlistedAt`)',
|
||||
'SELECT 1'
|
||||
);
|
||||
""");
|
||||
migrationBuilder.Sql(
|
||||
"PREPARE add_waitlist_index_stmt FROM @add_waitlist_index_sql;");
|
||||
migrationBuilder.Sql("EXECUTE add_waitlist_index_stmt;");
|
||||
migrationBuilder.Sql("DEALLOCATE PREPARE add_waitlist_index_stmt;");
|
||||
}
|
||||
|
||||
protected override void Down(MigrationBuilder migrationBuilder)
|
||||
{
|
||||
migrationBuilder.DropIndex(
|
||||
name: "IX_CourseEnrollments_CourseSelectionOfferingId_Status_WaitlistedAt",
|
||||
table: "CourseEnrollments");
|
||||
migrationBuilder.Sql("""
|
||||
SET @waitlist_index_exists = (
|
||||
SELECT COUNT(*)
|
||||
FROM `INFORMATION_SCHEMA`.`STATISTICS`
|
||||
WHERE `TABLE_SCHEMA` = DATABASE()
|
||||
AND `TABLE_NAME` = 'CourseEnrollments'
|
||||
AND `INDEX_NAME` = 'IX_CE_Offering_Status_WaitlistedAt'
|
||||
);
|
||||
""");
|
||||
migrationBuilder.Sql("""
|
||||
SET @drop_waitlist_index_sql = IF(
|
||||
@waitlist_index_exists > 0,
|
||||
'DROP INDEX `IX_CE_Offering_Status_WaitlistedAt` ON `CourseEnrollments`',
|
||||
'SELECT 1'
|
||||
);
|
||||
""");
|
||||
migrationBuilder.Sql(
|
||||
"PREPARE drop_waitlist_index_stmt FROM @drop_waitlist_index_sql;");
|
||||
migrationBuilder.Sql("EXECUTE drop_waitlist_index_stmt;");
|
||||
migrationBuilder.Sql("DEALLOCATE PREPARE drop_waitlist_index_stmt;");
|
||||
|
||||
migrationBuilder.DropColumn(
|
||||
name: "WaitlistedAt",
|
||||
table: "CourseEnrollments");
|
||||
migrationBuilder.Sql("""
|
||||
SET @waitlisted_column_exists = (
|
||||
SELECT COUNT(*)
|
||||
FROM `INFORMATION_SCHEMA`.`COLUMNS`
|
||||
WHERE `TABLE_SCHEMA` = DATABASE()
|
||||
AND `TABLE_NAME` = 'CourseEnrollments'
|
||||
AND `COLUMN_NAME` = 'WaitlistedAt'
|
||||
);
|
||||
""");
|
||||
migrationBuilder.Sql("""
|
||||
SET @drop_waitlisted_column_sql = IF(
|
||||
@waitlisted_column_exists > 0,
|
||||
'ALTER TABLE `CourseEnrollments` DROP COLUMN `WaitlistedAt`',
|
||||
'SELECT 1'
|
||||
);
|
||||
""");
|
||||
migrationBuilder.Sql(
|
||||
"PREPARE drop_waitlisted_column_stmt FROM @drop_waitlisted_column_sql;");
|
||||
migrationBuilder.Sql("EXECUTE drop_waitlisted_column_stmt;");
|
||||
migrationBuilder.Sql("DEALLOCATE PREPARE drop_waitlisted_column_stmt;");
|
||||
}
|
||||
}
|
||||
|
||||
+2
-1
@@ -748,7 +748,8 @@ namespace Jiaowu.Api.Infrastructure.Persistence.Migrations.MySql
|
||||
|
||||
b.HasIndex("StudentId", "Status");
|
||||
|
||||
b.HasIndex("CourseSelectionOfferingId", "Status", "WaitlistedAt");
|
||||
b.HasIndex("CourseSelectionOfferingId", "Status", "WaitlistedAt")
|
||||
.HasDatabaseName("IX_CE_Offering_Status_WaitlistedAt");
|
||||
|
||||
b.ToTable("CourseEnrollments");
|
||||
});
|
||||
|
||||
+2
-1
@@ -745,7 +745,8 @@ namespace Jiaowu.Api.Infrastructure.Persistence.Migrations.MySql
|
||||
|
||||
b.HasIndex("StudentId", "Status");
|
||||
|
||||
b.HasIndex("CourseSelectionOfferingId", "Status", "WaitlistedAt");
|
||||
b.HasIndex("CourseSelectionOfferingId", "Status", "WaitlistedAt")
|
||||
.HasDatabaseName("IX_CE_Offering_Status_WaitlistedAt");
|
||||
|
||||
b.ToTable("CourseEnrollments");
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user