SchemaBuilder 迁移指南:在 OrchardCore 中构建与修改 YesSql 索引表
CMS后端Web框架【免费下载链接】OrchardCoreOrchard Core is an open-source modular and multi-tenant application framework built with ASP.NET Core, and a content management system (CMS) built on top of that framework.项目地址https://gitcode.com/gh_mirrors/or/OrchardCore点击查看免费下载导读SchemaBuilder是 OrchardCore 数据迁移体系中的核心工具用于创建和修改支撑 YesSql map/reduce 索引的 SQL 索引表。本指南以 OrchardCore 数据迁移技能文档为骨架结合仓库源码DataMigration.cs、ContentItemIndex.cs 等深度解析其用法、列定义规则、索引管理及不同数据库提供商的兼容性陷阱。读完本文你将能够独立编写健壮、跨数据库可用的索引表迁移代码。SchemaBuilder 从何而来DataMigration 基类SchemaBuilder由DataMigration基类提供通过SchemaBuilder属性暴露永远不要手动注入它。它专门用于构建和修改支撑 YesSql map/reduce 索引的 SQL索引表。源码佐证DataMigration.cs 中定义public abstract class DataMigration : IDataMigration { /// inheritdocs / public ISchemaBuilder SchemaBuilder { get; set; } }该属性来自IDataMigration接口见 IDataMigration.cs由迁移管理器负责赋值。一个典型的迁移类以 OrchardCore.ContentManagement/Records/Migrations.cs 为参考public sealed class Migrations : DataMigration { public async Taskint CreateAsync() { await SchemaBuilder.CreateMapIndexTableAsyncContentItemIndex(table table .Columnstring(ContentItemId, c c.WithLength(26)) .Columnstring(ContentItemVersionId, c c.WithLength(26)) .Columnbool(Latest) .Columnbool(Published) .Columnstring(ContentType, column column.WithLength(ContentItemIndex.MaxContentTypeSize)) .ColumnDateTime(ModifiedUtc, column column.Nullable()) // ... ); // ... return 1; } }关于同步/异步方法所有SchemaBuilder方法都有对应的异步形式...Async新代码中应优先使用异步形式。迁移的入口方法也遵循同样的约定——DataMigrationExtensions见 DataMigrationExtensions.cs会优先查找同步Create()/UpdateFromX()找不到时才回退到CreateAsync()/UpdateFromXAsync()。创建 map 索引表CreateMapIndexTableAsyncTIndex用于为 map 索引创建对应的索引表泛型参数指向模块中定义的索引 POCO。索引 POCOMemberIndex : MapIndex和它的IndexProvider在模块中单独定义迁移只负责创建表不涉及映射逻辑。await SchemaBuilder.CreateMapIndexTableAsyncMemberIndex(table table .Columnstring(ContentItemId, column column.WithLength(26)) .Columnstring(nameof(MemberIndex.SocialSecurityNumber), column column.WithLength(11)) .Columnstring(nameof(MemberIndex.Name), column column.WithLength(26)) .Columnbool(Published, column column.Nullable()) .Columndecimal(Amount, column column.Nullable()) .Columnstring(BigText, column column.Nullable().Unlimited()) );要点说明列名可以直接写字符串字面量也可以用nameof(索引POCO属性)保持与索引定义同步避免改名时遗漏布尔列Latest/Published是 YesSql 索引的常见约定列用于区分草稿与已发布版本CreateReduceIndexTableAsyncTIndex用于 reduce 索引用法一致。仓库中的 OpenIdMigrations.cs 就同时展示了 map 与 reduce 索引表的创建以及collection:命名参数用于将索引表放入指定的集合/文档容器。列定义规则需求代码带长度的字符串.Columnstring(Name, c c.WithLength(26))无长度限制的文本.Columnstring(Body, c c.Nullable().Unlimited())可空值类型.Columnbool(Latest, c c.Nullable())数值类型.Columndecimal(...),.Columnint(...),.ColumnDateTime(...),.ColumnTimeSpan(...)务必为所有字符串列显式指定.WithLength(n)或.Unlimited()。未指定长度的字符串列在不同数据库提供商之间的默认长度不一致会导致某些数据库上的迁移失败。此外需要唯一约束的列可以追加.Unique()见 OpenIdMigrations.cs 中ClientId列的定义。OrchardCore 的标准长度约定ContentItemId/ContentItemVersionId 26内容项标识符的固定长度内容类型、部件、字段名称等使用ContentItemIndex.Max*Size常量见 ContentItemIndex.cspublic const int MaxContentTypeSize 255; public const int MaxContentPartSize 255; public const int MaxContentFieldSize 255; public const int MaxOwnerSize 255; public const int MaxAuthorSize 255; public const int MaxDisplayTextSize 255;实践中推荐直接引用这些常量例如.Columnstring(ContentType, column column.WithLength(ContentItemIndex.MaxContentTypeSize))避免魔法数字散落各处。修改表结构添加 / 删除列与索引AlterIndexTableAsyncTIndex是升级步骤的核心工具支持对已有索引表进行增量修改。升级步骤中新增列await SchemaBuilder.AlterIndexTableAsyncLinkFieldIndex(table table .AddColumnstring(BigUrl, column column.Nullable().Unlimited()));为加速查询创建 SQL 索引await SchemaBuilder.AlterIndexTableAsyncLinkFieldIndex(table table .CreateIndex(IDX_LinkFieldIndex_DocumentId, DocumentId, ContentItemId, ContentItemVersionId, Published, Latest));真实仓库中ContentManagement 的 Migrations.cs 在创建ContentItemIndex表后立即创建了IDX_ContentItemIndex_DocumentId、IDX_ContentItemIndex_DocumentId_ContentType等复合索引这是 OrchardCore 的标准做法——索引表创建后马上为高频查询路径建立复合索引。删除索引await SchemaBuilder.AlterIndexTableAsyncTimeFieldIndex(table table .DropIndex(IDX_TimeFieldIndex_DocumentId_Time));数据库提供商兼容性陷阱SQLite 无法删除列SQLite 对DROP COLUMN支持有限旧版本不支持删除列及其后的列重建操作必须包裹在try/catch中try { await SchemaBuilder.AlterIndexTableAsyncTimeFieldIndex(table table .DropColumn(Time)); await SchemaBuilder.AlterIndexTableAsyncTimeFieldIndex(table table .AddColumnTimeSpan(Time, column column.Nullable())); } catch { _logger.LogWarning(Failed to alter Time column. This is not an error when using SqLite); }这是刻意为之的设计在 SQLite 下失败属于预期行为捕获后仅记录警告不中断迁移流程。删除可能不存在的索引某些升级场景中索引可能已在先前步骤或不同版本中被删除。对“幂等性”不确定的索引删除操作同样建议用try/catch包裹try { await SchemaBuilder.AlterIndexTableAsyncTimeFieldIndex(table table .DropIndex(IDX_TimeFieldIndex_Time)); } catch { _logger.LogWarning(Failed to drop an index that does not exist IDX_TimeFieldIndex_Time); }MySQL 索引键长度限制MySQL 中复合索引键上限为768 字符 / 3072 字节。当索引中包含长字符串列时必须在索引定义中使用前缀长度限制await SchemaBuilder.AlterIndexTableAsyncTextFieldIndex(table table .CreateIndex(IDX_TextFieldIndex_DocumentId_Text, DocumentId, Text(764), // prefix length keeps the key under the limit Published, Latest));OrchardCore 会在每个此类索引上方以注释形式记录其长度算术例如// DocumentId (2) Text (764) Published and Latest (1) 767 ( 768).注意Text(764)这种“列名前缀长度”的写法仅在CreateIndex的列参数中生效用于指示数据库只对Text列的前 764 个字符建索引.Columnstring定义列本身时不能用这种写法。删除整张索引表await SchemaBuilder.DropMapIndexTableAsyncMemberIndex(); // or await SchemaBuilder.DropTableAsync(MyCustomTable);两种方式的区别DropMapIndexTableAsyncTIndex()按索引 POCO 类型定位并删除对应索引表类型安全、推荐优先使用DropTableAsync(表名)直接按表名删除适用于删除自定义表非索引表或泛型方式难以表达的场景。典型使用场景是模块的UninstallAsync()卸载步骤在功能卸载时清理数据库残留。可参考 DataMigration.cs 中关于Uninstall()/UninstallAsync()方法的文档注释约定。迁移方法命名约定速查迁移类的每个方法名承载了版本语义由 DataMigrationManager 通过UpdateFrom前缀与Async后缀的反射机制识别方法签名语义int Create()/Taskint CreateAsync()初始迁移返回 1int UpdateFrom1()/Taskint UpdateFrom1Async()1 → 2 的升级返回 2void Uninstall()/Task UninstallAsync()卸载清理如删除索引表方法的返回值代表迁移后的版本号OrchardCore 将其记录在DataMigrationRecord见 Records/DataMigrationRecord.cs中作为后续升级判断的依据。小结SchemaBuilder是DataMigration基类内置属性负责 YesSql 索引表的建表、改表与删表新代码优先使用...Async形式字符串列必须显式声明.WithLength(n)或.Unlimited()标准标识符用 26类型/字段名用ContentItemIndex.Max*Size常量255升级用AlterIndexTableAsync增量添加列、创建/删除复合索引记住三大兼容性陷阱SQLite 删列需try/catch、删除不存在索引需容错、MySQL 复合索引键需前缀限长如Text(764)卸载时用DropMapIndexTableAsync或DropTableAsync清理索引表。将以上规则与真实迁移如 ContentManagement Migrations、OpenIdMigrations对照阅读即可快速掌握 OrchardCore 数据迁移的完整实践。赞分享CMS后端Web框架【免费下载链接】OrchardCoreOrchard Core is an open-source modular and multi-tenant application framework built with ASP.NET Core, and a content management system (CMS) built on top of that framework.项目地址https://gitcode.com/gh_mirrors/or/OrchardCore点击查看免费下载相关推荐OrchardCore 数据迁移实战DataMigration 版本链、SchemaBuilder 与内容修补全指南OrchardCore 数据迁移实战DataMigration 版本链、SchemaBuilder 与内容修补全指南 OrchardCore 作为基于 ASPCMS后端Web框架GRDB.swift 数据库 Schema 修改实战指南建表、改表与索引的类型安全方案GRDB.swift 数据库 Schema 修改实战指南建表、改表与索引的类型安全方案 GRDB.swift 为 SQLite 数据库提供了完整的、以 Swi数据库ORMdrizzle-kit 0.30.3SingleStore 表重建迁移与索引同名类型错误的修复解析drizzle kit 0.30.3SingleStore 表重建迁移与索引同名类型错误的修复解析 本篇技术指南以 drizzle kit 0.30.3 版本后端数据库ORM创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考