Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upgrading from <= v0.2.1 loses all Directive values #4

Open
matt-in-a-hat opened this issue Jun 9, 2021 · 1 comment
Open

Upgrading from <= v0.2.1 loses all Directive values #4

matt-in-a-hat opened this issue Jun 9, 2021 · 1 comment

Comments

@matt-in-a-hat
Copy link

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";
    }
}
@JamesDPC
Copy link
Contributor

Thanks for providing that. I'm planning an update and can incorporate this after testing in a v0.2 release along with v0.3.

Wrapping the query in a try/catch should automatically deal with the Value column not being available.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants