Skip to content

Commit ac85313

Browse files
committed
Move photo subject migration out of Refresh.Database, don't migrate already migrated photos
1 parent fa30e48 commit ac85313

2 files changed

Lines changed: 45 additions & 60 deletions

File tree

Refresh.Database/GameDatabaseContext.Photos.cs

Lines changed: 14 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -119,67 +119,20 @@ public GamePhoto UploadPhoto(IPhotoUpload photo, IEnumerable<IPhotoUploadSubject
119119
return newPhoto;
120120
}
121121

122-
/// <remarks>
123-
/// Migration only!!
124-
/// </remarks>
125-
public void MigratePhotoSubjects(GamePhoto photo, bool saveChanges)
122+
public GamePhotoSubject AddSubjectForPhoto(GamePhoto photo, int playerId, string displayName, GameUser? user, List<float> bounds, bool save = true)
126123
{
127-
List<GamePhotoSubject> subjects = [];
128-
129-
#pragma warning disable CS0618 // obsoletion
130-
131-
// If DisplayName is not null, there is a subject in that spot
132-
if (photo.Subject1DisplayName != null)
133-
{
134-
subjects.Add(new()
135-
{
136-
Photo = photo,
137-
PlayerId = 1,
138-
DisplayName = photo.Subject1DisplayName,
139-
User = photo.Subject1User,
140-
Bounds = photo.Subject1Bounds,
141-
});
142-
}
143-
144-
if (photo.Subject2DisplayName != null)
145-
{
146-
subjects.Add(new()
147-
{
148-
Photo = photo,
149-
PlayerId = 2,
150-
DisplayName = photo.Subject2DisplayName,
151-
User = photo.Subject2User,
152-
Bounds = photo.Subject2Bounds,
153-
});
154-
}
155-
156-
if (photo.Subject3DisplayName != null)
157-
{
158-
subjects.Add(new()
159-
{
160-
Photo = photo,
161-
PlayerId = 3,
162-
DisplayName = photo.Subject3DisplayName,
163-
User = photo.Subject3User,
164-
Bounds = photo.Subject3Bounds,
165-
});
166-
}
167-
168-
if (photo.Subject4DisplayName != null)
124+
GamePhotoSubject subject = new()
169125
{
170-
subjects.Add(new()
171-
{
172-
Photo = photo,
173-
PlayerId = 4,
174-
DisplayName = photo.Subject4DisplayName,
175-
User = photo.Subject4User,
176-
Bounds = photo.Subject4Bounds,
177-
});
178-
}
179-
#pragma warning restore CS0618
126+
Photo = photo,
127+
PlayerId = playerId,
128+
DisplayName = displayName,
129+
User = user,
130+
Bounds = bounds,
131+
};
180132

181-
this.GamePhotoSubjects.AddRange(subjects);
182-
if (saveChanges) this.SaveChanges();
133+
this.GamePhotoSubjects.Add(subject);
134+
if (save) this.SaveChanges();
135+
return subject;
183136
}
184137

185138
public void RemovePhoto(GamePhoto photo)
@@ -227,6 +180,9 @@ public IQueryable<GamePhotoSubject> GetSubjectsInPhoto(GamePhoto photo)
227180
.Where(s => s.PhotoId == photo.PhotoId)
228181
.OrderBy(s => s.PlayerId);
229182

183+
public int GetTotalSubjectsInPhoto(GamePhoto photo)
184+
=> this.GamePhotoSubjects.Count(s => s.PhotoId == photo.PhotoId);
185+
230186
public IQueryable<GameUser> GetUsersInPhoto(GamePhoto photo)
231187
=> this.GetSubjectsInPhoto(photo)
232188
.Where(s => s.User != null)

Refresh.Interfaces.Workers/Migrations/MoveSubjectsOutOfGamePhotosMigration.cs

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,43 @@ public class MoveSubjectsOutOfGamePhotosMigration : MigrationJob<GamePhoto>
77
{
88
protected override IQueryable<GamePhoto> SortAndFilter(IQueryable<GamePhoto> query)
99
{
10-
return query.OrderBy(p => p.PhotoId);
10+
return query
11+
.Where(p => p.Subjects.Count == 0)
12+
.OrderBy(p => p.PhotoId);
1113
}
1214

1315
protected override void Migrate(WorkContext context, GamePhoto[] batch)
1416
{
1517
foreach (GamePhoto photo in batch)
1618
{
17-
context.Database.MigratePhotoSubjects(photo, false);
19+
// Extra check to be sure
20+
if (context.Database.GetTotalSubjectsInPhoto(photo) > 0)
21+
continue;
22+
23+
#pragma warning disable CS0618 // obsoletion
24+
25+
// If DisplayName is not null, there is a subject in that spot
26+
if (photo.Subject1DisplayName != null)
27+
{
28+
context.Database.AddSubjectForPhoto(photo, 1, photo.Subject1DisplayName, photo.Subject1User, photo.Subject1Bounds, false);
29+
}
30+
31+
if (photo.Subject2DisplayName != null)
32+
{
33+
context.Database.AddSubjectForPhoto(photo, 2, photo.Subject2DisplayName, photo.Subject2User, photo.Subject2Bounds, false);
34+
}
35+
36+
if (photo.Subject3DisplayName != null)
37+
{
38+
context.Database.AddSubjectForPhoto(photo, 3, photo.Subject3DisplayName, photo.Subject3User, photo.Subject3Bounds, false);
39+
}
40+
41+
if (photo.Subject4DisplayName != null)
42+
{
43+
context.Database.AddSubjectForPhoto(photo, 4, photo.Subject4DisplayName, photo.Subject4User, photo.Subject4Bounds, false);
44+
}
45+
46+
#pragma warning restore CS0618
1847
}
1948

2049
context.Database.SaveChanges();

0 commit comments

Comments
 (0)