Skip to content

Commit 10df27c

Browse files
committed
Update existing redirects when updating
1 parent 56d82bd commit 10df27c

File tree

4 files changed

+23
-8
lines changed

4 files changed

+23
-8
lines changed

src/Data/Redirect.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@ public function fileData()
145145
'id' => $this->id(),
146146
'enabled' => $this->enabled(),
147147
'source' => $this->source(),
148+
'source_md5' => $this->source_md5(),
148149
'destination' => $this->destination(),
149150
'type' => $this->type(),
150151
'site' => $this->site(),

src/Stache/Redirects/RedirectStore.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ public function makeItemFromFile($path, $contents)
3131
$redirect = Redirect::make()
3232
->id($id)
3333
->source(Arr::pull($data, 'source'))
34+
->source_md5(Arr::pull($data, 'source_md5'))
3435
->destination(Arr::pull($data, 'destination'))
3536
->type(Arr::pull($data, 'type'))
3637
->matchType(Arr::pull($data, 'match_type'))

src/UpdateScripts/IncreaseUrlSizeOnErrors.php

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,7 @@ public function shouldUpdate($newVersion, $oldVersion)
1919
$errorConnection = config('database.default');
2020
}
2121

22-
try {
23-
return ! Schema::connection($errorConnection)->hasColumn('errors', 'url_md5');
24-
} catch (QueryException) {
25-
// Query exception happens when database is not set up
26-
return false;
27-
}
22+
return ! Schema::connection($errorConnection)->hasColumn('errors', 'url_md5');
2823
}
2924

3025
public function update()

src/UpdateScripts/IncreaseUrlSizeOnRedirects.php

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
use Illuminate\Support\Facades\Artisan;
88
use Illuminate\Support\Facades\Schema;
99
use Rias\StatamicRedirect\Eloquent\Redirects\RedirectModel;
10+
use Rias\StatamicRedirect\Facades\Redirect;
11+
use Statamic\Facades\Stache;
1012
use Statamic\UpdateScripts\UpdateScript;
1113

1214
class IncreaseUrlSizeOnRedirects extends UpdateScript
@@ -15,8 +17,8 @@ public function shouldUpdate($newVersion, $oldVersion)
1517
{
1618
$redirectConnection = config('statamic.redirect.redirect_connection');
1719

18-
if ($redirectConnection === 'stache') {
19-
return false;
20+
if ($redirectConnection === 'stache' && ! Redirect::all()->first()->source_md5()) {
21+
return true;
2022
}
2123

2224
if ($redirectConnection === 'default') {
@@ -35,6 +37,22 @@ public function update()
3537
{
3638
$redirectConnection = config('statamic.redirect.redirect_connection');
3739

40+
if ($redirectConnection === 'stache') {
41+
if (Redirect::all()->first()->source_md5()) {
42+
return;
43+
}
44+
45+
Redirect::all()->each(function (\Rias\StatamicRedirect\Data\Redirect $redirect) {
46+
$redirect->save();
47+
});
48+
49+
Stache::clear();
50+
51+
$this->console()->info('Updated redirects with md5 index.');
52+
53+
return;
54+
}
55+
3856
if ($redirectConnection === 'redirect-sqlite') {
3957
if (Schema::connection($redirectConnection)->hasIndex('redirects', 'redirects_source_index')) {
4058
Schema::connection($redirectConnection)->table('redirects', function (Blueprint $table): void {

0 commit comments

Comments
 (0)