-
Notifications
You must be signed in to change notification settings - Fork 4
Open
Description
To get around this I created a migration task. Not sure if this could be useful for others
<?php
use NSWDPC\Utilities\ContentSecurityPolicy\Directive;
use SilverStripe\Dev\BuildTask;
use SilverStripe\ORM\DB;
/**
* Migrates CSP directives due to changed structure on upgrade
*/
class MigrateCSPDirectives extends BuildTask
{
protected $title = 'Migrate CSP directives';
protected $description = 'Migrate CSP directives from <= v0.2.1 to >= v0.2.2';
public function run($request)
{
echo "Migrating directives. \n";
echo "If this throws an error regarding the Value column then no migration is available. \n";
$updated = 0;
$invalidSkipped = 0;
$alreadySetSkipped = 0;
foreach (Directive::get() as $directive) {
if (!$directive->dbObject('RulesValue')->getValue()) {
$values = DB::query("SELECT `Value`
FROM CspDirective
WHERE ID = {$directive->ID}
")->column('Value');
if (isset($values[0])) {
$newValues = [];
foreach (explode(' ', $values[0]) as $value) {
if (trim($value)) {
$newValues[$value] = $value;
}
}
$directive->RulesValue = json_encode($newValues);
$directive->write();
$updated += 1;
} else {
$invalidSkipped += 1;
}
} else {
$alreadySetSkipped += 1;
}
}
if ($invalidSkipped) {
echo "Skipped $invalidSkipped values that were empty or invalid? \n";
}
if ($alreadySetSkipped) {
echo "Skipped $alreadySetSkipped values that already had a value in the new field. \n";
}
echo "Done - updated $updated items. \n";
}
}Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels