@@ -8,19 +8,18 @@ namespace Revo.Infrastructure.DataAccess.Migrations
88 public class DatabaseMigrationSelector : IDatabaseMigrationSelector
99 {
1010 private readonly IDatabaseMigrationRegistry migrationRegistry ;
11- private readonly IDatabaseMigrationProvider migrationProvider ;
1211 private readonly IDatabaseMigrationSelectorOptions selectorOptions ;
1312 private IReadOnlyCollection < IDatabaseMigrationRecord > history ;
1413
15- public DatabaseMigrationSelector ( IDatabaseMigrationRegistry migrationRegistry , IDatabaseMigrationProvider migrationProvider ,
14+ public DatabaseMigrationSelector ( IDatabaseMigrationRegistry migrationRegistry ,
1615 IDatabaseMigrationSelectorOptions selectorOptions )
1716 {
1817 this . migrationRegistry = migrationRegistry ;
19- this . migrationProvider = migrationProvider ;
2018 this . selectorOptions = selectorOptions ;
2119 }
2220
23- public async Task < IReadOnlyCollection < SelectedModuleMigrations > > SelectMigrationsAsync ( DatabaseMigrationSpecifier [ ] modules , string [ ] tags )
21+ public async Task < IReadOnlyCollection < SelectedModuleMigrations > > SelectMigrationsAsync ( IDatabaseMigrationProvider migrationProvider ,
22+ DatabaseMigrationSpecifier [ ] modules , string [ ] tags )
2423 {
2524 try
2625 {
@@ -33,7 +32,7 @@ public async Task<IReadOnlyCollection<SelectedModuleMigrations>> SelectMigration
3332
3433 foreach ( var module in sortedModules )
3534 {
36- var migrations = await DoSelectMigrationsAsync ( module , tags , queuedMigrations ) ;
35+ var migrations = await DoSelectMigrationsAsync ( module , tags , queuedMigrations , false ) ;
3736
3837 if ( migrations . Count > 0 )
3938 {
@@ -58,10 +57,10 @@ private bool IsRepeatableMigration(DatabaseMigrationSpecifier specifier)
5857 }
5958
6059 private async Task < IReadOnlyCollection < IDatabaseMigration > > DoSelectMigrationsAsync ( DatabaseMigrationSpecifier specifier , string [ ] tags ,
61- List < DatabaseMigrationSpecifier > queuedMigrations )
60+ List < DatabaseMigrationSpecifier > queuedMigrations , bool required )
6261 {
63- var result = await SelectModuleMigrationsNoDependenciesAsync ( specifier , tags , queuedMigrations ) ;
64-
62+ var result = await SelectModuleMigrationsNoDependenciesAsync ( specifier , tags , queuedMigrations , required ) ;
63+
6564 foreach ( var migration in result )
6665 {
6766 queuedMigrations . Add ( new DatabaseMigrationSpecifier ( migration . ModuleName , migration . Version ) ) ;
@@ -81,7 +80,7 @@ private async Task AddRequiredDependenciesAsync(List<IDatabaseMigration> migrati
8180 var dependencySpecs = await GetRequiredMigrationDependenciesAsync ( migration , queuedMigrations , tags ) ;
8281 foreach ( var dependencySpec in dependencySpecs )
8382 {
84- var dependencyMigrations = await DoSelectMigrationsAsync ( dependencySpec , tags , queuedMigrations ) ;
83+ var dependencyMigrations = await DoSelectMigrationsAsync ( dependencySpec , tags , queuedMigrations , true ) ;
8584 migrations . InsertRange ( i , dependencyMigrations ) ;
8685 queuedMigrations . AddRange ( dependencyMigrations . Select ( x => new DatabaseMigrationSpecifier ( x . ModuleName , x . Version ) ) ) ;
8786 i += dependencyMigrations . Count ;
@@ -96,7 +95,7 @@ private async Task<List<DatabaseMigrationSpecifier>> GetRequiredMigrationDepende
9695
9796 foreach ( var dependency in migration . Dependencies )
9897 {
99- var dependencyMigrations = await SelectModuleMigrationsNoDependenciesAsync ( dependency , tags , queuedMigrations ) ;
98+ var dependencyMigrations = await SelectModuleMigrationsNoDependenciesAsync ( dependency , tags , queuedMigrations , true ) ;
10099 if ( dependencyMigrations . Count > 0 )
101100 {
102101 dependencies . Add ( new DatabaseMigrationSpecifier (
@@ -109,18 +108,19 @@ private async Task<List<DatabaseMigrationSpecifier>> GetRequiredMigrationDepende
109108 }
110109
111110 private async Task < List < IDatabaseMigration > > SelectModuleMigrationsNoDependenciesAsync ( DatabaseMigrationSpecifier specifier ,
112- string [ ] tags , List < DatabaseMigrationSpecifier > queuedMigrations )
111+ string [ ] tags , List < DatabaseMigrationSpecifier > queuedMigrations , bool required )
113112 {
114113 var moduleMigrations = migrationRegistry . Migrations
115- . Where ( x => string . Equals ( x . ModuleName , specifier . ModuleName , StringComparison . InvariantCultureIgnoreCase ) )
114+ . Where ( x => string . Equals ( x . ModuleName , specifier . ModuleName , StringComparison . InvariantCultureIgnoreCase ) ) ;
115+ var providerModuleMigrations = moduleMigrations
116116 . Where ( x => x . Tags . All ( tagGroup => tags . Any ( tagGroup . Contains ) ) ) ;
117117
118118 if ( specifier . Version != null )
119119 {
120- moduleMigrations = moduleMigrations . Where ( x => x . Version . CompareTo ( specifier . Version ) <= 0 ) ;
120+ providerModuleMigrations = providerModuleMigrations . Where ( x => x . Version . CompareTo ( specifier . Version ) <= 0 ) ;
121121 }
122122
123- moduleMigrations = moduleMigrations
123+ providerModuleMigrations = providerModuleMigrations
124124 . OrderBy ( x => x . Version )
125125 . ToArray ( ) ;
126126
@@ -131,14 +131,14 @@ private async Task<List<IDatabaseMigration>> SelectModuleMigrationsNoDependencie
131131
132132 if ( specifier . Version != null )
133133 {
134- if ( moduleMigrations . Any ( ) && moduleMigrations . Last ( ) . IsRepeatable )
134+ if ( providerModuleMigrations . Any ( ) && providerModuleMigrations . Last ( ) . IsRepeatable )
135135 {
136136 throw new DatabaseMigrationException ( $ "Cannot select database migrations for module { specifier } because it is a repeatable migration module, which means it is versioned only by checksums") ;
137137 }
138138 }
139- else if ( ! moduleMigrations . Any ( ) )
139+ else if ( ! providerModuleMigrations . Any ( ) )
140140 {
141- if ( ! historyMigrations . Any ( ) )
141+ if ( ! historyMigrations . Any ( ) && ( required || ! moduleMigrations . Any ( ) ) )
142142 {
143143 // TODO maybe return without errors if there are migrations for this module with different tags?
144144 throw new DatabaseMigrationException ( $ "Cannot select database migrations for module { specifier } : no migrations for specified module were found") ;
@@ -147,10 +147,10 @@ private async Task<List<IDatabaseMigration>> SelectModuleMigrationsNoDependencie
147147 return new List < IDatabaseMigration > ( ) ;
148148 }
149149
150- if ( historyMigrations . Any ( ) && moduleMigrations . Any ( ) )
150+ if ( historyMigrations . Any ( ) && providerModuleMigrations . Any ( ) )
151151 {
152152 bool wasRepeatable = historyMigrations . First ( ) . Version == null ;
153- bool isRepeatable = moduleMigrations . First ( ) . IsRepeatable ;
153+ bool isRepeatable = providerModuleMigrations . First ( ) . IsRepeatable ;
154154
155155 if ( wasRepeatable != isRepeatable )
156156 {
@@ -166,7 +166,7 @@ private async Task<List<IDatabaseMigration>> SelectModuleMigrationsNoDependencie
166166 }
167167
168168 // repeatable-migration modules
169- if ( moduleMigrations . Any ( ) && moduleMigrations . First ( ) . IsRepeatable )
169+ if ( providerModuleMigrations . Any ( ) && providerModuleMigrations . First ( ) . IsRepeatable )
170170 {
171171 if ( moduleQueuedMigrations . Any ( ) )
172172 {
@@ -177,7 +177,7 @@ private async Task<List<IDatabaseMigration>> SelectModuleMigrationsNoDependencie
177177 . OrderByDescending ( x => x . TimeApplied )
178178 . FirstOrDefault ( ) ;
179179
180- var migration = moduleMigrations . SingleOrDefault ( )
180+ var migration = providerModuleMigrations . SingleOrDefault ( )
181181 ?? throw new DatabaseMigrationException ( $ "Cannot select database migrations for repeatable module { specifier } : multiple migrations found") ;
182182
183183 // return if same and no dependencies got updated
@@ -214,21 +214,21 @@ private async Task<List<IDatabaseMigration>> SelectModuleMigrationsNoDependencie
214214
215215 if ( version == null )
216216 {
217- var baseline = moduleMigrations . FirstOrDefault ( x => x . IsBaseline ) ;
217+ var baseline = providerModuleMigrations . FirstOrDefault ( x => x . IsBaseline ) ;
218218 if ( baseline != null )
219219 {
220220 result . Add ( baseline ) ;
221- result . AddRange ( moduleMigrations
221+ result . AddRange ( providerModuleMigrations
222222 . Where ( x => x . Version . CompareTo ( baseline . Version ) > 0 ) ) ;
223223 }
224224 else
225225 {
226- result . AddRange ( moduleMigrations ) ;
226+ result . AddRange ( providerModuleMigrations ) ;
227227 }
228228 }
229229 else
230230 {
231- result . AddRange ( moduleMigrations
231+ result . AddRange ( providerModuleMigrations
232232 . Where ( x => ! x . IsBaseline && x . Version . CompareTo ( version ) > 0 ) ) ;
233233 }
234234
@@ -242,7 +242,7 @@ private async Task<List<IDatabaseMigration>> SelectModuleMigrationsNoDependencie
242242 }
243243 else
244244 {
245- var maxVersion = moduleMigrations . Select ( x => x . Version ) . Max ( ) ;
245+ var maxVersion = providerModuleMigrations . Select ( x => x . Version ) . Max ( ) ;
246246
247247 if ( ( ! result . Any ( ) && ( version == null || version . CompareTo ( maxVersion ) < 0 ) )
248248 || ( result . Any ( ) && ! result . Last ( ) . Version . Equals ( maxVersion ) ) )
0 commit comments