|
1 | 1 | #if EFCORE6
|
2 |
| -using System; |
3 |
| -using System.Collections.Generic; |
4 |
| -using System.Linq; |
5 |
| -using System.Text; |
6 |
| -using System.Threading; |
7 |
| -using System.Threading.Tasks; |
8 |
| -using Microsoft.EntityFrameworkCore; |
9 |
| -using Microsoft.EntityFrameworkCore.Infrastructure; |
10 | 2 | using Microsoft.EntityFrameworkCore.Migrations;
|
11 |
| -using ShardingCore.Core.DbContextCreator; |
12 | 3 | using ShardingCore.Core.RuntimeContexts;
|
13 |
| -using ShardingCore.Exceptions; |
14 |
| -using ShardingCore.Extensions; |
15 |
| -using ShardingCore.Helpers; |
16 |
| -using ShardingCore.Sharding.ShardingDbContextExecutors; |
17 | 4 |
|
18 | 5 | namespace ShardingCore.EFCores
|
19 | 6 | {
|
20 |
| - public sealed class ScriptMigrationGenerator |
| 7 | + public sealed class ScriptMigrationGenerator:AbstractScriptMigrationGenerator |
21 | 8 | {
|
22 |
| - private readonly IShardingRuntimeContext _shardingRuntimeContext; |
23 | 9 | private readonly string _fromMigration;
|
24 | 10 | private readonly string _toMigration;
|
25 | 11 | private readonly MigrationsSqlGenerationOptions _options;
|
26 | 12 |
|
27 | 13 | public ScriptMigrationGenerator(IShardingRuntimeContext shardingRuntimeContext, string fromMigration = null,
|
28 | 14 | string toMigration = null,
|
29 |
| - MigrationsSqlGenerationOptions options = MigrationsSqlGenerationOptions.Default) |
| 15 | + MigrationsSqlGenerationOptions options = MigrationsSqlGenerationOptions.Default) : base(shardingRuntimeContext) |
30 | 16 | {
|
31 |
| - _shardingRuntimeContext = shardingRuntimeContext; |
32 | 17 | _fromMigration = fromMigration;
|
33 | 18 | _toMigration = toMigration;
|
34 | 19 | _options = options;
|
35 | 20 | }
|
36 | 21 |
|
37 |
| - public string GenerateScript() |
| 22 | + protected override string GenerateScriptSql(IMigrator migrator) |
38 | 23 | {
|
39 |
| - var virtualDataSource = _shardingRuntimeContext.GetVirtualDataSource(); |
40 |
| - var allDataSourceNames = virtualDataSource.GetAllDataSourceNames(); |
41 |
| - var dbContextCreator = _shardingRuntimeContext.GetDbContextCreator(); |
42 |
| - var shardingProvider = _shardingRuntimeContext.GetShardingProvider(); |
43 |
| - var shardingConfigOptions = _shardingRuntimeContext.GetShardingConfigOptions(); |
44 |
| - var defaultDataSourceName = virtualDataSource.DefaultDataSourceName; |
45 |
| - |
46 |
| - using (var scope = shardingProvider.CreateScope()) |
47 |
| - { |
48 |
| - using (var shellDbContext = dbContextCreator.GetShellDbContext(scope.ServiceProvider)) |
49 |
| - { |
50 |
| - var parallelCount = shardingConfigOptions.MigrationParallelCount; |
51 |
| - if (parallelCount <= 0) |
52 |
| - { |
53 |
| - throw new ShardingCoreInvalidOperationException($"migration parallel count must >0"); |
54 |
| - } |
55 |
| - |
56 |
| - //默认数据源需要最后执行 否则可能会导致异常的情况下GetPendingMigrations为空 |
57 |
| - var partitionMigrationUnits = allDataSourceNames.Where(o => o != defaultDataSourceName) |
58 |
| - .Partition(parallelCount); |
59 |
| - var scriptStringBuilder = new StringBuilder(); |
60 |
| - foreach (var migrationUnits in partitionMigrationUnits) |
61 |
| - { |
62 |
| - var migrateUnits = migrationUnits.Select(o => new MigrateUnit(shellDbContext, o)).ToList(); |
63 |
| - var scriptSql = ExecuteMigrateUnits(_shardingRuntimeContext, migrateUnits); |
64 |
| - scriptStringBuilder.AppendLine(scriptSql); |
65 |
| - } |
66 |
| - |
67 |
| - //包含默认默认的单独最后一次处理 |
68 |
| - if (allDataSourceNames.Contains(defaultDataSourceName)) |
69 |
| - { |
70 |
| - var scriptSql = ExecuteMigrateUnits(_shardingRuntimeContext, |
71 |
| - new List<MigrateUnit>() { new MigrateUnit(shellDbContext, defaultDataSourceName) }); |
72 |
| - scriptStringBuilder.AppendLine(scriptSql); |
73 |
| - } |
74 |
| - |
75 |
| - return scriptStringBuilder.ToString(); |
76 |
| - } |
77 |
| - } |
78 |
| - } |
79 |
| - |
80 |
| - private string ExecuteMigrateUnits(IShardingRuntimeContext shardingRuntimeContext, |
81 |
| - List<MigrateUnit> migrateUnits) |
82 |
| - { |
83 |
| - var shardingMigrationManager = shardingRuntimeContext.GetShardingMigrationManager(); |
84 |
| - var dbContextCreator = shardingRuntimeContext.GetDbContextCreator(); |
85 |
| - var routeTailFactory = shardingRuntimeContext.GetRouteTailFactory(); |
86 |
| - var migrateTasks = migrateUnits.Select(migrateUnit => |
87 |
| - { |
88 |
| - return Task.Run(() => |
89 |
| - { |
90 |
| - using (shardingMigrationManager.CreateScope()) |
91 |
| - { |
92 |
| - shardingMigrationManager.Current.CurrentDataSourceName = migrateUnit.DataSourceName; |
93 |
| - |
94 |
| - var dbContextOptions = CreateDbContextOptions(shardingRuntimeContext, |
95 |
| - migrateUnit.ShellDbContext.GetType(), |
96 |
| - migrateUnit.DataSourceName); |
97 |
| - |
98 |
| - using (var dbContext = dbContextCreator.CreateDbContext(migrateUnit.ShellDbContext, |
99 |
| - new ShardingDbContextOptions(dbContextOptions, |
100 |
| - routeTailFactory.Create(string.Empty, false)))) |
101 |
| - { |
102 |
| - var migrator = dbContext.GetService<IMigrator>(); |
103 |
| - return $"-- DataSource:{migrateUnit.DataSourceName}" + Environment.NewLine + |
104 |
| - migrator.GenerateScript(_fromMigration, _toMigration, _options) + |
105 |
| - Environment.NewLine; |
106 |
| - } |
107 |
| - } |
108 |
| - }); |
109 |
| - }).ToArray(); |
110 |
| - var scripts = TaskHelper.WhenAllFastFail(migrateTasks).WaitAndUnwrapException(); |
111 |
| - return string.Join(Environment.NewLine, scripts); |
112 |
| - } |
113 |
| - |
114 |
| - private DbContextOptions CreateDbContextOptions(IShardingRuntimeContext shardingRuntimeContext, |
115 |
| - Type dbContextType, string dataSourceName) |
116 |
| - { |
117 |
| - var virtualDataSource = shardingRuntimeContext.GetVirtualDataSource(); |
118 |
| - var shardingConfigOptions = shardingRuntimeContext.GetShardingConfigOptions(); |
119 |
| - var dbContextOptionBuilder = DataSourceDbContext.CreateDbContextOptionBuilder(dbContextType); |
120 |
| - var connectionString = virtualDataSource.GetConnectionString(dataSourceName); |
121 |
| - virtualDataSource.UseDbContextOptionsBuilder(connectionString, dbContextOptionBuilder); |
122 |
| - shardingConfigOptions.ShardingMigrationConfigure?.Invoke(dbContextOptionBuilder); |
123 |
| - //迁移 |
124 |
| - dbContextOptionBuilder.UseShardingOptions(shardingRuntimeContext); |
125 |
| - return dbContextOptionBuilder.Options; |
| 24 | + return migrator.GenerateScript(_fromMigration, _toMigration, _options); |
126 | 25 | }
|
127 | 26 | }
|
128 | 27 | }
|
|
0 commit comments