1
- using System . Collections . Generic ;
2
1
using System . Linq ;
3
2
using System . Threading ;
4
3
using System . Threading . Tasks ;
5
4
using GovUk . Education . ExploreEducationStatistics . Admin . Models ;
6
- using GovUk . Education . ExploreEducationStatistics . Common . Extensions ;
7
5
using GovUk . Education . ExploreEducationStatistics . Common . Model ;
8
6
using GovUk . Education . ExploreEducationStatistics . Content . Model ;
9
7
using GovUk . Education . ExploreEducationStatistics . Content . Model . Database ;
@@ -21,18 +19,19 @@ public class MigrationResult
21
19
{
22
20
public bool IsDryRun ;
23
21
public int Processed ;
24
- public string Errors ;
22
+ public int NumLeftToProcess ;
25
23
}
26
24
27
- [ HttpPut ( "bau/migrate-datasetfile-geographiclevels " ) ]
25
+ [ HttpPut ( "bau/meta-geographic-levels-remove " ) ]
28
26
public async Task < MigrationResult > DataSetFileVersionGeographicLevelsMigration (
29
27
[ FromQuery ] bool isDryRun = true ,
30
28
[ FromQuery ] int ? num = null ,
31
29
CancellationToken cancellationToken = default )
32
30
{
33
31
var queryable = contentDbContext . Files
34
32
. Where ( f => f . Type == FileType . Data
35
- && f . DataSetFileVersionGeographicLevels . Count == 0 ) ;
33
+ && f . DataSetFileMetaGeogLvlMigrated == false
34
+ && f . DataSetFileVersionGeographicLevels . Count > 0 ) ; // Paranoidly protecting against data set where geog lvls aren't migrated
36
35
37
36
if ( num != null )
38
37
{
@@ -42,30 +41,21 @@ public async Task<MigrationResult> DataSetFileVersionGeographicLevelsMigration(
42
41
var files = queryable . ToList ( ) ;
43
42
44
43
var numProcessed = 0 ;
45
- List < string > errors = [ ] ;
46
44
47
45
foreach ( var file in files )
48
46
{
49
- var meta = file . DataSetFileMeta ;
50
-
51
- if ( meta == null )
47
+ if ( file . DataSetFileMeta != null )
52
48
{
53
- errors . Add ( $ "No DataSetFileMeta found for File { file . Id } ") ;
54
- continue ;
55
- }
56
-
57
- var dataSetFileVersionGeographicLevels = meta ! . GeographicLevels
58
- . Distinct ( )
59
- . Select ( gl => new DataSetFileVersionGeographicLevel
49
+ file . DataSetFileMeta = new DataSetFileMeta
60
50
{
61
- DataSetFileVersionId = file . Id ,
62
- GeographicLevel = gl ,
63
- } )
64
- . ToList ( ) ;
65
-
66
- contentDbContext . DataSetFileVersionGeographicLevels . AddRange (
67
- dataSetFileVersionGeographicLevels ) ;
51
+ GeographicLevels = null ,
52
+ TimePeriodRange = file . DataSetFileMeta . TimePeriodRange ,
53
+ Filters = file . DataSetFileMeta . Filters ,
54
+ Indicators = file . DataSetFileMeta . Indicators ,
55
+ } ;
56
+ }
68
57
58
+ file . DataSetFileMetaGeogLvlMigrated = true ;
69
59
numProcessed ++ ;
70
60
}
71
61
@@ -78,7 +68,7 @@ public async Task<MigrationResult> DataSetFileVersionGeographicLevelsMigration(
78
68
{
79
69
IsDryRun = isDryRun ,
80
70
Processed = numProcessed ,
81
- Errors = errors . IsNullOrEmpty ( ) ? "No errors" : errors . JoinToString ( " \n " ) ,
71
+ NumLeftToProcess = contentDbContext . Files . Count ( f => f . DataSetFileMetaGeogLvlMigrated == false ) ,
82
72
} ;
83
73
}
84
74
0 commit comments