lewisjenkins / craft-dynamic-fields

Populate Craft fields with dynamic data using the power of Twig.
Other
147 stars 10 forks source link

No migration for Craft 3? #22

Open bossanova808 opened 6 years ago

bossanova808 commented 6 years ago

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 commented 6 years ago

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 commented 5 years ago

@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 commented 5 years ago

@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 commented 5 years ago

@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 commented 5 years ago

...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 commented 5 years ago

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 commented 5 years ago

Please add this...

sgtpenguin commented 4 years ago

please please please @lewisjenkins