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

No migration for Craft 3? #22

Open
bossanova808 opened this issue Sep 12, 2018 · 8 comments
Open

No migration for Craft 3? #22

bossanova808 opened this issue Sep 12, 2018 · 8 comments

Comments

@bossanova808
Copy link

Hi Lewis

Is there any chance you could add a migration for those of us updating from using this extensively in Craft 2?

I can hack/write a query to deal with changing the component class, but usually there is a bit more to it than that and it would be HUGELY useful if there was a proper migration in place for these.

As part of a huge upgrade, I have to dump by C3 database and re-upgrade my C2 one on fairly regular occasions, and each time these field types / settings don't carry over.

@bossanova808
Copy link
Author

bossanova808 commented Sep 18, 2018

So I went ahead and hacked my own upgrade path here using a console command - there's a gist that has the code if you want to grab any of it:

(see updated code below)

@jamesmacwhite
Copy link

@bossanova808 I noticed your gist link is 404. Would you be willing to share your code? I'm in a similar position to you, except my usage of Craft Dynamic Fields in a Craft 2 project I'm migrating is minimal, so it's probably manageable without native migration.

Thanks in advance!

@bossanova808
Copy link
Author

bossanova808 commented Mar 22, 2019

@jamesmacwhite Right you are - I actually changed from my console command to a regular migration. Note I only use checkboxes and migrate but extrapolating to the other types should be simple:

<?php

namespace craft\contentmigrations;

use craft\db\Migration;
use craft\db\Query;
use craft\helpers\ArrayHelper;
use craft\helpers\Db;
use craft\helpers\Json;
use lewisjenkins\craftdynamicfields\fields\Checkboxes as CheckboxesField;
use yii\db\Schema;

/**
 * m190321_002602_upgrade_dynamic_checkboxes migration.
 */
class m190121_002602_upgrade_dynamic_checkboxes extends Migration
{
    /**
     * @inheritdoc
     */
    public function safeUp()
    {
        // Look for any old LJ Checkbox fields and in this case we also need to update their settings

        $fields = (new Query())
            ->select(['id', 'settings'])
            ->from(['{{%fields}}'])
            ->where(['type' => 'Lj_DynamicFields_Checkboxes'])
            ->all($this->db);

        foreach ($fields as $field) {
            $settings = Json::decode($field['settings']);
            if (!is_array($settings)) {
                continue;
            }

            ArrayHelper::rename($settings, 'json', 'checkboxOptions');

            $this->update('{{%fields}}', [
                'settings' => Json::encode($settings)
            ], ['id' => $field['id']]);
        }
        $this->update('{{%fields}}', ['type' => CheckboxesField::class], ['type' => 'Lj_DynamicFields_Checkboxes']);

    }

    /**
     * @inheritdoc
     */
    public function safeDown()
    {
        echo "m190321_002602_upgrade_dynamic_checkboxes cannot be reverted.\n";
        return false;
    }
}

...it would be great if @lewisjenkins could run with this and add a proper, complete migration.

@lindseydiloreto
Copy link

@bossanova808 Thanks for writing this migration! Would it be possible to submit this as a pull request so @lewisjenkins can easily merge it?

I agree that this feels like a big oversight... it's throwing a wrench into the upgrade I'm currently working on.

@bossanova808
Copy link
Author

...well, given this is now a paid plugin, and given the code has really been provided here, it really feels like the ball is in someone else's court here...I kinda got my own things I need to be doing am now on the other side of all my Craft 3 migration stuff....but if I find a minute I'll see what I can do. But won't be a for a bit, I'm afraid!

@lindseydiloreto
Copy link

Fair enough! Thanks for your original snippet @bossanova808. 🍺

@lewisjenkins Would you consider porting Jeremy's code into an official migration? As he pointed out, this is now a paid plugin... It would be very helpful for your customers who are updating Craft 2 sites.

@theskyfloor
Copy link

Please add this...

@sgtpenguin
Copy link

please please please @lewisjenkins

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

5 participants