Last active
July 13, 2022 19:20
-
-
Save tonysm/36d2c413b479b29741af9613fb9808ad to your computer and use it in GitHub Desktop.
Migrate Stored Hashes Using Rich Text Laravel
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Pseudo-code based on the PHP version... | |
namespace :action_text do | |
task refresh_embeds: :environment do | |
ActionText::RichText.where.not(body: nil).find_each do |trix| | |
next unless trix.embeds.size.positive? | |
trix.update_column :body, trix.body.fragment.replace("action-text-attachment[sgid]") do |node| | |
return ActionText::Attachment.from_attachable( | |
GlobalID::Locator.locate_signed( | |
node.attributes['sgid'].value, | |
verifier: Verifier.new(oldVerifierHash), | |
for: "attachable" | |
), | |
node.attributes | |
) | |
end.to_s | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use Illuminate\Support\Facades\Artisan; | |
use Tonysm\GlobalId\Facades\Locator; | |
use Tonysm\GlobalId\Verifier; | |
use Tonysm\RichTextLaravel\Attachment; | |
use Tonysm\RichTextLaravel\Models\RichText; | |
Artisan::command('richtext:rotate', function () { | |
$oldVerifier = new Verifier( | |
fn () => config('rich-text-laravel.old_key'), | |
salt: 'signed_global_ids', | |
); | |
RichText::query()->oldest()->eachById(function (RichText $entry) use ($oldVerifier) { | |
$entry->forceFill([ | |
'body' => $entry->body->fragment->replace('//rich-text-attachment[@sgid]', function (DOMElement $node) use ($oldVerifier) { | |
$attributes = []; | |
foreach ($node->attributes as $attr) { | |
$attributes[$attr->name] = $attr->value; | |
} | |
return Attachment::fromAttachable(Locator::locateSigned($node->getAttribute('sgid'), [ | |
'for' => 'rich-text-laravel', | |
'verifier' => $oldVerifier, | |
]), $attributes); | |
})->toHtml(), | |
])->save(); | |
}); | |
})->purpose('Loops through the Rich Text Models and migrates the attachments.'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment