Skip to content

Commit 8a2fa7f

Browse files
Update old broken revisions after updating to Statamic v4.41.0 (#231)
* Update old broken revisions after updating to Statamic v4.41.0 * StyleCI * Save id as string
1 parent 509845e commit 8a2fa7f

File tree

3 files changed

+77
-10
lines changed

3 files changed

+77
-10
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
use Statamic\Eloquent\Database\BaseMigration as Migration;
4+
use Statamic\Eloquent\Revisions\RevisionModel;
5+
6+
return new class extends Migration {
7+
public function up()
8+
{
9+
// Extract the id from the key and add it to the attributes of the revision
10+
RevisionModel::query()
11+
->whereJsonDoesntContainKey('attributes->id')
12+
->chunk(200, function ($revisions) {
13+
foreach ($revisions as $revision) {
14+
$id = str($revision->key)
15+
->afterLast('/')
16+
->toString();
17+
18+
$attributes = $revision->attributes;
19+
$attributes['id'] = $id;
20+
21+
$revision->attributes = $attributes;
22+
$revision->save();
23+
}
24+
});
25+
}
26+
27+
public function down()
28+
{
29+
// Reverse the above
30+
RevisionModel::query()
31+
->whereJsonContainsKey('attributes->id')
32+
->chunk(200, function ($revisions) {
33+
foreach ($revisions as $revision) {
34+
$attributes = $revision->attributes;
35+
unset($attributes['id']);
36+
37+
$revision->attributes = $attributes;
38+
$revision->save();
39+
}
40+
});
41+
}
42+
};

src/ServiceProvider.php

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ class ServiceProvider extends AddonServiceProvider
5252
\Statamic\Eloquent\Updates\ChangeDefaultBlueprint::class,
5353
\Statamic\Eloquent\Updates\DropForeignKeysOnEntriesAndForms::class,
5454
\Statamic\Eloquent\Updates\SplitGlobalsFromVariables::class,
55+
\Statamic\Eloquent\Updates\AddIdToAttributesInRevisionsTable::class,
5556
];
5657

5758
protected $listen = [
@@ -75,20 +76,20 @@ public function boot()
7576
$this->publishes([$config => config_path('statamic/eloquent-driver.php')], 'statamic-eloquent-config');
7677

7778
$this->publishes([
78-
__DIR__.'/../database/migrations/create_taxonomies_table.php.stub' => $this->migrationsPath('create_taxonomies_table.php'),
79-
__DIR__.'/../database/migrations/create_terms_table.php.stub' => $this->migrationsPath('create_terms_table.php'),
80-
__DIR__.'/../database/migrations/create_globals_table.php.stub' => $this->migrationsPath('create_globals_table.php'),
79+
__DIR__.'/../database/migrations/create_taxonomies_table.php.stub' => $this->migrationsPath('create_taxonomies_table.php'),
80+
__DIR__.'/../database/migrations/create_terms_table.php.stub' => $this->migrationsPath('create_terms_table.php'),
81+
__DIR__.'/../database/migrations/create_globals_table.php.stub' => $this->migrationsPath('create_globals_table.php'),
8182
__DIR__.'/../database/migrations/create_global_variables_table.php.stub' => $this->migrationsPath('create_global_variables_table.php'),
82-
__DIR__.'/../database/migrations/create_navigations_table.php.stub' => $this->migrationsPath('create_navigations_table.php'),
83+
__DIR__.'/../database/migrations/create_navigations_table.php.stub' => $this->migrationsPath('create_navigations_table.php'),
8384
__DIR__.'/../database/migrations/create_navigation_trees_table.php.stub' => $this->migrationsPath('create_navigation_trees_table.php'),
84-
__DIR__.'/../database/migrations/create_collections_table.php.stub' => $this->migrationsPath('create_collections_table.php'),
85-
__DIR__.'/../database/migrations/create_blueprints_table.php.stub' => $this->migrationsPath('create_blueprints_table.php'),
86-
__DIR__.'/../database/migrations/create_fieldsets_table.php.stub' => $this->migrationsPath('create_fieldsets_table.php'),
87-
__DIR__.'/../database/migrations/create_forms_table.php.stub' => $this->migrationsPath('create_forms_table.php'),
85+
__DIR__.'/../database/migrations/create_collections_table.php.stub' => $this->migrationsPath('create_collections_table.php'),
86+
__DIR__.'/../database/migrations/create_blueprints_table.php.stub' => $this->migrationsPath('create_blueprints_table.php'),
87+
__DIR__.'/../database/migrations/create_fieldsets_table.php.stub' => $this->migrationsPath('create_fieldsets_table.php'),
88+
__DIR__.'/../database/migrations/create_forms_table.php.stub' => $this->migrationsPath('create_forms_table.php'),
8889
__DIR__.'/../database/migrations/create_form_submissions_table.php.stub' => $this->migrationsPath('create_form_submissions_table.php'),
8990
__DIR__.'/../database/migrations/create_asset_containers_table.php.stub' => $this->migrationsPath('create_asset_containers_table.php'),
90-
__DIR__.'/../database/migrations/create_asset_table.php.stub' => $this->migrationsPath('create_asset_table.php'),
91-
__DIR__.'/../database/migrations/create_revisions_table.php.stub' => $this->migrationsPath('create_revisions_table.php'),
91+
__DIR__.'/../database/migrations/create_asset_table.php.stub' => $this->migrationsPath('create_asset_table.php'),
92+
__DIR__.'/../database/migrations/create_revisions_table.php.stub' => $this->migrationsPath('create_revisions_table.php'),
9293
], 'migrations');
9394

9495
$this->publishes([
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
namespace Statamic\Eloquent\Updates;
4+
5+
use Statamic\UpdateScripts\UpdateScript;
6+
7+
class AddIdToAttributesInRevisionsTable extends UpdateScript
8+
{
9+
public function shouldUpdate($newVersion, $oldVersion)
10+
{
11+
return $this->isUpdatingTo('3.0.2');
12+
}
13+
14+
public function update()
15+
{
16+
$source = __DIR__.'/../../database/migrations/updates/add_id_to_attributes_in_revisions_table.php.stub';
17+
$dest = database_path('migrations/'.date('Y_m_d_His').'_add_id_to_attributes_in_revisions_table.php');
18+
19+
$this->files->copy($source, $dest);
20+
21+
$this->console()->info('Migration created');
22+
$this->console()->comment('Remember to run `php artisan migrate` to apply it to your database.');
23+
}
24+
}

0 commit comments

Comments
 (0)