Skip to content

Commit 2c031c0

Browse files
authored
Migrate to new theme settings from previous settings (#991)
* Created mitgrate function for the frontend to update local storage * Wrote the migration.sql script for update events colour on Database * Updated the frontend migrate function to update the colour of a createdEvent * Removed redudant logging * Update the migration file to run migration properly.
1 parent 7d74e8f commit 2c031c0

File tree

2 files changed

+83
-0
lines changed

2 files changed

+83
-0
lines changed

client/src/utils/storage.ts

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,58 @@ import defaults from '../constants/defaults';
22

33
const STORAGE_KEY = 'data';
44

5+
const MIGRATE_COLOR_MAP: Record<string, string> = {
6+
'#137786': 'default-1',
7+
'#a843a4': 'default-2',
8+
'#134e86': 'default-3',
9+
'#138652': 'default-4',
10+
'#861313': 'default-5',
11+
'#868413': 'default-6',
12+
'#2e89ff': 'default-7',
13+
'#3323ad': 'default-8',
14+
};
15+
16+
const migrateTimetables = (timetables: Record<string, any>) => {
17+
return Object.fromEntries(
18+
Object.entries(timetables).map(([termKey, termValue]) => [termKey, termValue.map(migrateTimetable)]),
19+
);
20+
};
21+
22+
const migrateTimetable = (timetable: any) => {
23+
return {
24+
...timetable,
25+
createdEvents: migrateCreatedEvents(timetable.createdEvents),
26+
assignedColors: migrateAssignedColors(timetable.assignedColors),
27+
};
28+
};
29+
30+
const migrateCreatedEvents = (createdEvents: Record<string, any>) => {
31+
return Object.fromEntries(
32+
Object.entries(createdEvents).map(([eventKey, eventValue]) => [
33+
eventKey,
34+
{
35+
...eventValue,
36+
event: {
37+
...eventValue.event,
38+
color:
39+
eventValue.event.color in MIGRATE_COLOR_MAP
40+
? MIGRATE_COLOR_MAP[eventValue.event.color]
41+
: eventValue.event.color,
42+
},
43+
},
44+
]),
45+
);
46+
};
47+
48+
const migrateAssignedColors = (assignedColors: Record<string, string>) => {
49+
return Object.fromEntries(
50+
Object.entries(assignedColors).map(([key, color]) => [
51+
key,
52+
color in MIGRATE_COLOR_MAP ? MIGRATE_COLOR_MAP[color] : color,
53+
]),
54+
);
55+
};
56+
557
const storage = {
658
get: (key: string): any => {
759
const data: Record<string, any> = storage.load();
@@ -28,6 +80,8 @@ const storage = {
2880

2981
if (localStorage[STORAGE_KEY]) {
3082
data = JSON.parse(localStorage[STORAGE_KEY]);
83+
// migrate old data format to new format
84+
data = storage.migrate(data);
3185
} else {
3286
storage.save(data);
3387
}
@@ -38,6 +92,20 @@ const storage = {
3892
save: (data: Record<string, any>) => {
3993
localStorage[STORAGE_KEY] = JSON.stringify(data);
4094
},
95+
96+
migrate: (data: Record<string, any>) => {
97+
// only do this if version does not exist
98+
if (data.version !== 1 || data.version == null) {
99+
let migrated = {
100+
...data,
101+
version: 1,
102+
timetables: migrateTimetables(data.timetables),
103+
};
104+
storage.save(migrated);
105+
return migrated;
106+
}
107+
return data;
108+
},
41109
};
42110

43111
storage.load();
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
-- Update the colour column in the events table based on the MIGRATE_COLOR_MAP
2+
3+
UPDATE "events"
4+
SET "colour" = CASE
5+
WHEN "colour" = '#137786' THEN 'default-1'
6+
WHEN "colour" = '#a843a4' THEN 'default-2'
7+
WHEN "colour" = '#134e86' THEN 'default-3'
8+
WHEN "colour" = '#138652' THEN 'default-4'
9+
WHEN "colour" = '#861313' THEN 'default-5'
10+
WHEN "colour" = '#868413' THEN 'default-6'
11+
WHEN "colour" = '#2e89ff' THEN 'default-7'
12+
WHEN "colour" = '#3323ad' THEN 'default-8'
13+
ELSE "colour"
14+
END
15+
WHERE "colour" IN ('#137786', '#a843a4', '#134e86', '#138652', '#861313', '#868413', '#2e89ff', '#3323ad');

0 commit comments

Comments
 (0)