doublesecretagency / craft-adwizard

Ad Wizard plugin for Craft CMS
Other
7 stars 8 forks source link

Ad Wizard migration error: Unable to create "Details" field #36

Closed AmitSenjaliya closed 1 year ago

AmitSenjaliya commented 2 years ago

I have installed Craft CMS 3.7.36 version and tried to install the plugin but the below error occurred. Migration: doublesecretagency\adwizard\migrations\m180925_000002_adWizard_portDetailsField Exception: Ad Wizard migration error: Unable to create "Details" field.

lindseydiloreto commented 2 years ago

Hi @AmitSenjaliya, I've replied to your same question on Discord. Let's discuss the issue over on Discord, since the issue is almost certainly unique to your setup (and unlikely to be a bug in the plugin).

tyssen commented 2 years ago

I'm getting the same problem. In my case, I'm upgrading a Craft 2 site that already has the plugin installed. Could you let me know what the fix was in Amit's case, please?

lindseydiloreto commented 2 years ago

@tyssen We didn't figure out quite why the error occurred, but we did find a workaround...

  1. After updating everything via Composer (but before you let Craft update the database), open up this migration file...
vendor/doublesecretagency/craft-adwizard/src/migrations/m180925_000002_adWizard_portDetailsField.php
  1. Find the following line...

https://github.com/doublesecretagency/craft-adwizard/blob/fe1ce3bf0ae3f8de70d61147322fd86b23361083/src/migrations/m180925_000002_adWizard_portDetailsField.php#L73

  1. Change it to look like this...
if (!$fieldsService->saveField($field, false)) {
  1. Once that's done, you should be able to run the rest of the update successfully. 👍
tyssen commented 2 years ago

@lindseydiloreto Sorry, for the slow reply. After that change, my error changes to:

Column not found: 1054 Unknown column 'field_adWizard_details' in 'field list' The SQL being executed was: UPDATE craft_content SET field_adWizard_details='', dateUpdated='2022-05-31 04:26:21' WHERE elementId='953'

tyssen commented 2 years ago

In my case, in craft_content I've got three fields named field_adWizard_details_xxxxxxxx where xxxxxxxx is a different string for each field.

tyssen commented 2 years ago

Just following up on this one again.

tyssen commented 2 years ago

And again.

lindseydiloreto commented 2 years ago

Hi @tyssen, ping me on Craft Discord. I have a handful of follow-up questions, we can troubleshoot it together in real-time.

lindseydiloreto commented 2 years ago

This is fixed, per our separate conversation on Discord. The issue wasn't really related to the original issue in this thread, other than the fact that they were both migration errors.

Today's bug will only effect people who are migrating directly from Craft 2.x up to Craft 3.7. If you are migrating to a version of Craft lower than 3.7, you probably will not run into this issue. I won't be backporting a fix, since this can be considered an edge case, and we are now focused on Craft 4.

To summarize, this is a result of Craft adding field column suffixes in Craft 3.7.


For reference, I had to make the following hacks to that same migration file...

Added to the _createNewField method:

// Ensure column suffix exists
FieldHelper::ensureColumnSuffix($field);

// Store for transfer of field values
$this->_columnSuffix = $field->columnSuffix;

Added to the _copyFieldValues method:

// Get the field column, with suffix
$fieldColumn = "field_adWizard_details_{$this->_columnSuffix}";

// Port data to new column
foreach ($ads as $row) {
    $this->update(
        '{{%content}}',
        [$fieldColumn => $row['details']],
        ['elementId' => $row['id']]
    );
}
lindseydiloreto commented 2 years ago

And I just realized there is an alternate solution...

  1. Update to Craft 3.6 and a compatible version of Ad Wizard.
  2. Update to Craft 3.7 from there.
engram-design commented 2 years ago

Just hit this myself for some Craft 2 upgrades on my way to Craft 4. I totally get wanting to focus on Craft 4, but it'd still be good to have the Craft 3 upgrade work (from the latest version of Craft 2 to the latest Craft 3 - onward to Craft 4).

It'd be super helpful just to have some more detail on the error:

throw new Exception('Ad Wizard migration error: Unable to create "Details" field ' . Json::encode($field->getErrors()) . ' .');

Which shows:

Exception: Ad Wizard migration error: Unable to create "Details" field {"handle":["Handle \"adWizard_details\" has already been taken."]} .

I'm not quite sure how the field has already been created, but maybe worth a check in the migration? Or at the very least, some feedback on the error in the exception.

Next, another error:

> update in {{%content}} ...Exception: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'field_adWizard_details' in 'field list'
The SQL being executed was: UPDATE `craft_content` SET `field_adWizard_details`='Link to Crystal Blue Lagoon Luxury Villas, Muri Beach, Rarotonga', `dateUpdated`='2022-10-21 09:48:05' WHERE `elementId`='558' 

This is because you assume the content column is named field_adWizard_details which won't be the case due to the new field suffix setup.

I've created a PR for you https://github.com/doublesecretagency/craft-adwizard/pull/40 but I think you might've made it tricky for yourself to create a new version after 3.1.4 just for Craft 3 (we can't go to Craft 4 straight from Craft 2), but maybe up to you to handle how best to deal with a new version.

lindseydiloreto commented 2 years ago

Very interesting... Thanks for the PR @engram-design! 👍

I'll take a look at the nuances, and figure out the best way to tag and release these changes.

lindseydiloreto commented 1 year ago

This is fixed once and for all, thanks @engram-design! Sorry it took so long to get that merged. 🙏

Worth saying one more time, this only affects people migrating from Craft 2 to Craft 3.7. If you stopped at any versions of Craft in between, this change is moot.

For people following that update path, set your Craft version to ^3.7...

"craftcms/cms": "^3.7",

And set your Ad Wizard version to either ^3.1 or 3.1.5...

"doublesecretagency/craft-adwizard": "^3.1", // Relaxed
"doublesecretagency/craft-adwizard": "3.1.5", // Strict

Then run composer update. As long as you are also loading Craft 3.7, both specifications will provide you with the exact same package.

Hope that helps. Thanks again to @engram-design for the pull request! 👏