|
1 | 1 | using System; |
2 | 2 | using System.Collections.Generic; |
3 | 3 | using System.Data.Entity.Migrations; |
| 4 | +using System.IO; |
4 | 5 | using System.Linq; |
5 | 6 | using System.Text; |
6 | 7 | using System.Text.RegularExpressions; |
7 | 8 | using System.Threading.Tasks; |
| 9 | +using SmartStore.Data.Migrations; |
8 | 10 |
|
9 | 11 | namespace SmartStore.Data.Setup |
10 | 12 | { |
@@ -101,5 +103,72 @@ private static TType CreateTypeInstance<TType>(string typeName) where TType : cl |
101 | 103 | return newType as TType; |
102 | 104 | } |
103 | 105 |
|
| 106 | + |
| 107 | + public static void ExecutePendingResourceMigrations(string resPath, SmartObjectContext dbContext) |
| 108 | + { |
| 109 | + Guard.ArgumentNotNull(() => dbContext); |
| 110 | + |
| 111 | + string headPath = Path.Combine(resPath, "head.txt"); |
| 112 | + if (!File.Exists(headPath)) |
| 113 | + return; |
| 114 | + |
| 115 | + string resHead = File.ReadAllText(headPath); |
| 116 | + if (!MigratorUtils.IsValidMigrationId(resHead)) |
| 117 | + return; |
| 118 | + |
| 119 | + var migrator = new DbMigrator(new MigrationsConfiguration()); |
| 120 | + var migrations = GetPendingResourceMigrations(migrator, resHead); |
| 121 | + |
| 122 | + foreach (var id in migrations) |
| 123 | + { |
| 124 | + if (IsAutomaticMigration(id)) |
| 125 | + continue; |
| 126 | + |
| 127 | + if (!IsValidMigrationId(id)) |
| 128 | + continue; |
| 129 | + |
| 130 | + // Resolve and instantiate the DbMigration instance from the assembly |
| 131 | + var migration = CreateMigrationInstanceByMigrationId(id, migrator.Configuration); |
| 132 | + |
| 133 | + var provider = migration as ILocaleResourcesProvider; |
| 134 | + if (provider == null) |
| 135 | + continue; |
| 136 | + |
| 137 | + var builder = new LocaleResourcesBuilder(); |
| 138 | + provider.MigrateLocaleResources(builder); |
| 139 | + |
| 140 | + var resEntries = builder.Build(); |
| 141 | + var resMigrator = new LocaleResourcesMigrator(dbContext); |
| 142 | + resMigrator.Migrate(resEntries); |
| 143 | + } |
| 144 | + } |
| 145 | + |
| 146 | + private static IEnumerable<string> GetPendingResourceMigrations(DbMigrator migrator, string resHead) |
| 147 | + { |
| 148 | + var local = migrator.GetLocalMigrations(); |
| 149 | + var atHead = false; |
| 150 | + |
| 151 | + if (local.Last().IsCaseInsensitiveEqual(resHead)) |
| 152 | + yield break; |
| 153 | + |
| 154 | + foreach (var id in local) |
| 155 | + { |
| 156 | + if (!atHead) |
| 157 | + { |
| 158 | + if (!id.IsCaseInsensitiveEqual(resHead)) |
| 159 | + { |
| 160 | + continue; |
| 161 | + } |
| 162 | + else |
| 163 | + { |
| 164 | + atHead = true; |
| 165 | + continue; |
| 166 | + } |
| 167 | + } |
| 168 | + |
| 169 | + yield return id; |
| 170 | + } |
| 171 | + } |
| 172 | + |
104 | 173 | } |
105 | 174 | } |
0 commit comments