Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

v3.1.2迁移操作说明 #145

Closed
gmf520 opened this issue Mar 28, 2020 · 0 comments · Fixed by #144
Closed

v3.1.2迁移操作说明 #145

gmf520 opened this issue Mar 28, 2020 · 0 comments · Fixed by #144
Labels
Feature 🔨 新功能,新特性 Useful 💯 对于解决问题有帮助
Milestone

Comments

@gmf520
Copy link
Member

gmf520 commented Mar 28, 2020

v3.1.2迁移操作说明

v3.1.2版本进行了较大的改动,授权(原OSharp.Security,现OSharp.Authorization)相关的模块进行了命名空间的更改,代码和数据库迁移都存在比较大的难度。下面,将对此版本的对业务层的影响及迁移方法进行一一说明:

代码迁移

代码迁移的难度不大,受影响的主要是OSharp.Security到OSharp.Authorization的命名空间转移,迁移步骤如下:

  1. 进行旧代码备份,推荐使用git单建分支来进行迁移,出问题可随时还原
  2. 使用 OSharpNS.Template.Mvc_Angular v3.1.2.326 代码模板脚手架生成新版本代码,移除Migration相关的代码及其他不想变更的文件,覆盖现有代码,删掉如下三个地方的代码
/src/YouNamespace.Core/Security/*.*
/src/YouNamespace.EntityConfiguration/Security/*.*
/src/YouNamespace.Web/Areas/Admin/Controllers/Security/*.*
/src/YouNamespace.Web/Controllers/SecurityController.cs

重要提示:
进行代码覆盖时,请注意不要覆盖原项目的数据迁移Migration相关代码,否则无法进行数据迁移

  1. 更新其他代码上相关的命名空间引用直至能生成成功

数据库迁移

因为我们改动了实体类的命名空间,并且v3.1.2版本引入了表前缀功能,会对现有数据表进行重命名,对数据库,迁移难度比较大,请在测试库迁移明白了再对线上数据库进行操作。

  1. 进行数据库备份,这个很重要

  2. 更新原有DefaultDbContextModelSnapshot.cs中的实体类的命名空间字符串,这一步骤很重要,因为实体类改了命名空间,会被当作新的实体来对待,旧的实体会被认为是已删除的实体,生成的迁移代码时会删除旧表并创建新表,而不会改表名。需要更改的表名命名空间变更如下:

OSharp.Core.EntityInfos 变更为 OSharp.Authorization.EntityInfos
OSharp.Core.Functions 变更为 OSharp.Authorization.Functions
YourNamespace.Security.Entities 变更为 YourNamespace.Authorization.Entities

如果后边执行迁移失败,在还原迁移记录的时候,DefaultDbContextModelSnapshot.cs的内容会被还原到最初状态,所以,改好后最好备份一下DefaultDbContextModelSnapshot.cs文件的内容

  1. 执行Add-Migration YourName 创建迁移记录

  2. 删除迁移记录中的删除数据操作
    因为此版本我们把种子数据初始化的功能从EntityConfiguration中抽离了,XXXEntityCofniguration中不再包含种子数据的HasData相关代码,生成的迁移记录中会存在删除种子数据的操作,这个数据我们是不需要删除的。
    打开迁移记录的文件(命名为 2020xxxxx_YourName.cs 的文件),搜索migrationBuilder.DeleteData的行,删除掉

migrationBuilder.DeleteData(
    table: "KeyValue",
    keyColumn: "Id",
    keyValue: new Guid("534d7813-0eea-44cc-b88e-a9cb010c6981"));
migrationBuilder.DeleteData(
    table: "KeyValue",
    keyColumn: "Id",
    keyValue: new Guid("977e4bba-97b2-4759-a768-a9cb010c698c"));
migrationBuilder.DeleteData(
    table: "Module",
    keyColumn: "Id",
    keyValue: 1);
migrationBuilder.DeleteData(
    table: "Role",
    keyColumn: "Id",
    keyValue: 1);

相应的,还原时插入数据 migrationBuilder.InsertData 的代码也是不需要的,找出来删掉

migrationBuilder.InsertData(
    table: "KeyValue",
    columns: new[] { "Id", "IsLocked", "Key", "ValueJson", "ValueType" },
    values: new object[,]
    {
        { new Guid("534d7813-0eea-44cc-b88e-a9cb010c6981"), false, "Site.Name", "\"OSHARP\"", "System.String,System.Private.CoreLib" },
        { new Guid("977e4bba-97b2-4759-a768-a9cb010c698c"), false, "Site.Description", "\"Osharp with .NetStandard2.0 & Angular6\"", "System.String,System.Private.CoreLib" }
    });
migrationBuilder.InsertData(
    table: "Module",
    columns: new[] { "Id", "Code", "Name", "OrderCode", "ParentId", "Remark", "TreePathString" },
    values: new object[] { 1, "Root", "根节点", 1.0, null, "系统根节点", "$1$" });
migrationBuilder.InsertData(
    table: "Role",
    columns: new[] { "Id", "ConcurrencyStamp", "CreatedTime", "DeletedTime", "IsAdmin", "IsDefault", "IsLocked", "IsSystem", "MessageId", "Name", "NormalizedName", "Remark" },
    values: new object[] { 1, "97313840-7874-47e5-81f2-565613c8cdcc", new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Unspecified), null, true, false, false, true, null, "系统管理员", "系统管理员", "系统最高权限管理角色" });

  1. 执行Update-Database命名,提交数据库迁移,无意外的话,迁移会执行成功

特别说明:上面的迁移步骤,是仅涉及此次osharp v3.1.2变更的更改,不涉及各自实际项目的相关变更,如果项目代码受影响,也要进行相应的操作。

@gmf520 gmf520 added Feature 🔨 新功能,新特性 Useful 💯 对于解决问题有帮助 labels Mar 28, 2020
@gmf520 gmf520 added this to the v3.1.2 milestone Mar 28, 2020
@gmf520 gmf520 linked a pull request Mar 28, 2020 that will close this issue
@gmf520 gmf520 pinned this issue Mar 29, 2020
@gmf520 gmf520 closed this as completed Aug 27, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Feature 🔨 新功能,新特性 Useful 💯 对于解决问题有帮助
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant