Skip to content

Commit 5130b31

Browse files
committed
Refactor migration logic for preview URLs and add fix for orphaned previews,
It considts of 2 parts, first the fix for migration 107, in which the problem was that the update only happened when the new preview has a value, which ends up with not updating those previews that have no search/player. The second part, is a fix via a new migration step, by which it reads the previews again and try to locate the orphaned previews with json decoded string and then update them either with null or an URL value.
1 parent 2276039 commit 5130b31

File tree

2 files changed

+51
-10
lines changed

2 files changed

+51
-10
lines changed

migrations/107_migrate_previews.php

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,27 +16,26 @@ public function up()
1616

1717

1818
while ($data = $result->fetch()) {
19-
$new_preview = null;
19+
$new_preview_url = null;
2020
$previews = json_decode($data['preview'], true);
2121

2222
if (!empty($previews)) {
2323
if (!empty($previews['player'])) {
24-
$new_preview = $previews['player'];
24+
$new_preview_url = $previews['player'];
2525
} else if (!empty($previews['search'])) {
26-
$new_preview = $previews['search'];
26+
$new_preview_url = $previews['search'];
2727
}
2828
}
2929

30-
if (!empty($new_preview)) {
31-
$stmt->execute([
32-
':preview' => $new_preview,
33-
':id' => $data['id']
34-
]);
35-
}
30+
// Since we are migrating previews to preview URL, we update the preview regardless of the value of the new preview url, whether it is empty or not!
31+
$stmt->execute([
32+
':preview' => $new_preview_url,
33+
':id' => $data['id']
34+
]);
3635
}
3736
}
3837

3938
public function down()
4039
{
4140
}
42-
}
41+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
class FixOrphanedPreviews extends Migration
4+
{
5+
public function description()
6+
{
7+
return 'Fix for previews that could not get migrated in 107.';
8+
}
9+
10+
public function up()
11+
{
12+
$db = DBManager::get();
13+
14+
$stmt = $db->prepare('UPDATE oc_video SET preview = :preview WHERE id = :id');
15+
$result = $db->query('SELECT id, preview FROM oc_video WHERE preview IS NOT NULL');
16+
17+
18+
while ($data = $result->fetch()) {
19+
$new_preview_url = null;
20+
$previews = json_decode($data['preview'], true);
21+
22+
// We want to handle the case where the preview is still a json object.
23+
if (!empty($previews)) {
24+
// Last chance to present the preview URL.
25+
if (!empty($previews['player'])) {
26+
$new_preview_url = $previews['player'];
27+
} else if (!empty($previews['search'])) {
28+
$new_preview_url = $previews['search'];
29+
}
30+
31+
$stmt->execute([
32+
':preview' => $new_preview_url,
33+
':id' => $data['id']
34+
]);
35+
}
36+
}
37+
}
38+
39+
public function down()
40+
{
41+
}
42+
}

0 commit comments

Comments
 (0)