verbb / events

Craft CMS Plugin for events management and ticketing.
Other
22 stars 13 forks source link

Upgrade to craft 5 fails because of events migrations #155

Closed mihob closed 1 week ago

mihob commented 1 week ago

Describe the bug

It is not possible to upgrade to craft 5 because the database migrations of the events plugin fail.

The following error occurs:

failed to apply m240921_000000_events3

    > update in {{%events_event_types}} ... done (time: 0.001s)
Exception: Call to a member function getType() on null (/app/vendor/verbb/events/src/elements/Session.php:610)
#0 /app/vendor/craftcms/cms/src/services/Elements.php(3516): verbb\events\elements\Session->beforeSave(true)
#1 /app/vendor/craftcms/cms/src/services/Elements.php(1308): craft\services\Elements->_saveElementInternal(Object(verbb\events\elements\Session), true, true, NULL, NULL, false, false, false)
#2 /app/vendor/verbb/events/src/migrations/m240921_000000_events3.php(344): craft\services\Elements->saveElement(Object(verbb\events\elements\Session))
#3 /app/vendor/craftcms/cms/src/db/Migration.php(50): verbb\events\migrations\m240921_000000_events3->safeUp()
#4 /app/vendor/yiisoft/yii2/console/controllers/BaseMigrateController.php(758): craft\db\Migration->up()
#5 /app/vendor/craftcms/cms/src/console/controllers/MigrateController.php(382): yii\console\controllers\BaseMigrateController->migrateUp('m240921_000000_...')
#6 [internal function]: craft\console\controllers\MigrateController->actionAll()
#7 /app/vendor/yiisoft/yii2/base/InlineAction.php(57): call_user_func_array(Array, Array)
#8 /app/vendor/yiisoft/yii2/base/Controller.php(178): yii\base\InlineAction->runWithParams(Array)
#9 /app/vendor/yiisoft/yii2/console/Controller.php(180): yii\base\Controller->runAction('all', Array)
#10 /app/vendor/craftcms/cms/src/console/controllers/MigrateController.php(195): yii\console\Controller->runAction('all', Array)
#11 /app/vendor/yiisoft/yii2/base/Module.php(552): craft\console\controllers\MigrateController->runAction('all', Array)
#12 /app/vendor/yiisoft/yii2/console/Application.php(180): yii\base\Module->runAction('migrate/all', Array)
#13 /app/vendor/craftcms/cms/src/console/Application.php(91): yii\console\Application->runAction('migrate/all', Array)
#14 /app/vendor/yiisoft/yii2/base/Controller.php(212): craft\console\Application->runAction('migrate/all', Array)
#15 /app/vendor/craftcms/cms/src/console/Controller.php(207): yii\base\Controller->run('migrate/all', Array)
#16 /app/vendor/craftcms/cms/src/console/controllers/UpController.php(60): craft\console\Controller->run('migrate/all', Array)
#17 [internal function]: craft\console\controllers\UpController->actionIndex()
#18 /app/vendor/yiisoft/yii2/base/InlineAction.php(57): call_user_func_array(Array, Array)
#19 /app/vendor/yiisoft/yii2/base/Controller.php(178): yii\base\InlineAction->runWithParams(Array)
#20 /app/vendor/yiisoft/yii2/console/Controller.php(180): yii\base\Controller->runAction('', Array)
#21 /app/vendor/craftcms/cms/src/console/ControllerTrait.php(88): yii\console\Controller->runAction('', Array)
#22 /app/vendor/craftcms/cms/src/console/Controller.php(216): craft\console\Controller->traitRunAction('', Array)
#23 /app/vendor/yiisoft/yii2/base/Module.php(552): craft\console\Controller->runAction('', Array)
#24 /app/vendor/yiisoft/yii2/console/Application.php(180): yii\base\Module->runAction('up', Array)
#25 /app/vendor/craftcms/cms/src/console/Application.php(91): yii\console\Application->runAction('up', Array)
#26 /app/vendor/yiisoft/yii2/console/Application.php(147): craft\console\Application->runAction('up', Array)
#27 /app/vendor/craftcms/cms/src/console/Application.php(122): yii\console\Application->handleRequest(Object(craft\console\Request))
#28 /app/vendor/yiisoft/yii2/base/Application.php(384): craft\console\Application->handleRequest(Object(craft\console\Request))
#29 /app/craft(13): yii\base\Application->run()
#30 {main}

As far as I remember, it worked with the last beta release of the plugin.

UPDATE:

I have also tested it with the dev-craft5, because there is a change in the corresponding place. But the migration still fails, this time with the following error:

Exception: verbb\events\elements\Session::updateTitle(): Argument #1 ($event) must be of type verbb\events\elements\Event, null given, called in /app/vendor/verbb/events/src/elements/Session.php on line 659 (/app/vendor/verbb/events/src/elements/Session.php:461)
#0 /app/vendor/verbb/events/src/elements/Session.php(659): verbb\events\elements\Session->updateTitle(NULL)
#1 /app/vendor/craftcms/cms/src/services/Elements.php(3786): verbb\events\elements\Session->afterSave(true)
#2 /app/vendor/craftcms/cms/src/services/Elements.php(1222): craft\services\Elements->craft\services\{closure}()
#3 /app/vendor/craftcms/cms/src/services/Elements.php(3591): craft\services\Elements->ensureBulkOp(Object(Closure))
#4 /app/vendor/craftcms/cms/src/services/Elements.php(1308): craft\services\Elements->_saveElementInternal(Object(verbb\events\elements\Session), true, true, NULL, Array, false, false, false)
#5 /app/vendor/verbb/events/src/migrations/m240921_000000_events3.php(344): craft\services\Elements->saveElement(Object(verbb\events\elements\Session))
#6 /app/vendor/craftcms/cms/src/db/Migration.php(50): verbb\events\migrations\m240921_000000_events3->safeUp()
#7 /app/vendor/yiisoft/yii2/console/controllers/BaseMigrateController.php(758): craft\db\Migration->up()
#8 /app/vendor/craftcms/cms/src/console/controllers/MigrateController.php(382): yii\console\controllers\BaseMigrateController->migrateUp('m240921_000000_...')
#9 [internal function]: craft\console\controllers\MigrateController->actionAll()
#10 /app/vendor/yiisoft/yii2/base/InlineAction.php(57): call_user_func_array(Array, Array)
#11 /app/vendor/yiisoft/yii2/base/Controller.php(178): yii\base\InlineAction->runWithParams(Array)
#12 /app/vendor/yiisoft/yii2/console/Controller.php(180): yii\base\Controller->runAction('all', Array)
#13 /app/vendor/craftcms/cms/src/console/controllers/MigrateController.php(195): yii\console\Controller->runAction('all', Array)
#14 /app/vendor/yiisoft/yii2/base/Module.php(552): craft\console\controllers\MigrateController->runAction('all', Array)
#15 /app/vendor/yiisoft/yii2/console/Application.php(180): yii\base\Module->runAction('migrate/all', Array)
#16 /app/vendor/craftcms/cms/src/console/Application.php(91): yii\console\Application->runAction('migrate/all', Array)
#17 /app/vendor/yiisoft/yii2/base/Controller.php(212): craft\console\Application->runAction('migrate/all', Array)
#18 /app/vendor/craftcms/cms/src/console/Controller.php(207): yii\base\Controller->run('migrate/all', Array)
#19 /app/vendor/craftcms/cms/src/console/controllers/UpController.php(60): craft\console\Controller->run('migrate/all', Array)
#20 [internal function]: craft\console\controllers\UpController->actionIndex()
#21 /app/vendor/yiisoft/yii2/base/InlineAction.php(57): call_user_func_array(Array, Array)
#22 /app/vendor/yiisoft/yii2/base/Controller.php(178): yii\base\InlineAction->runWithParams(Array)
#23 /app/vendor/yiisoft/yii2/console/Controller.php(180): yii\base\Controller->runAction('', Array)
#24 /app/vendor/craftcms/cms/src/console/ControllerTrait.php(88): yii\console\Controller->runAction('', Array)
#25 /app/vendor/craftcms/cms/src/console/Controller.php(216): craft\console\Controller->traitRunAction('', Array)
#26 /app/vendor/yiisoft/yii2/base/Module.php(552): craft\console\Controller->runAction('', Array)
#27 /app/vendor/yiisoft/yii2/console/Application.php(180): yii\base\Module->runAction('up', Array)
#28 /app/vendor/craftcms/cms/src/console/Application.php(91): yii\console\Application->runAction('up', Array)
#29 /app/vendor/yiisoft/yii2/console/Application.php(147): craft\console\Application->runAction('up', Array)
#30 /app/vendor/craftcms/cms/src/console/Application.php(122): yii\console\Application->handleRequest(Object(craft\console\Request))
#31 /app/vendor/yiisoft/yii2/base/Application.php(384): craft\console\Application->handleRequest(Object(craft\console\Request))
#32 /app/craft(13): yii\base\Application->run()
#33 {main}

UPDATE 2:

It looks like primaryOwnerId cannot be set via setAttributes:

$session->setAttributes([
    'primaryOwnerId' => $event['id'],
    'startDate' => $event['startDate'],
    'endDate' => $event['endDate'],
    'allDay' => (bool)$event['allDay'],
], false);

If I use the function setPrimaryOwnerId(...) as follows, the owner event is set correctly:

$session->setPrimaryOwnerId($event['id']);

The same problem exists at the following point and can be solved analogously:

$ticketType->setAttributes([
    'title' => $legacyTicketTypeElement->title ?? 'Ticket Type ' . rand(),
    'primaryOwnerId' => $legacyTicket['eventId'],
    'price' => $legacyTicket['price'],
    'capacity' => $legacyTicket['quantity'],
    'availableFrom' => $legacyTicket['availableFrom'],
    'availableTo' => $legacyTicket['availableTo'],
], false);

$ticketType->setPrimaryOwnerId($legacyTicket['eventId']);

If I adjust both places, the migration runs through without any problems.

Steps to reproduce

  1. Upgrade from the latest craft 4 version to craft 5 including the event plugin

Craft CMS version

5.4.10.1

Plugin version

3.0.0

Multi-site?

Yes

Additional context

No response

engram-design commented 1 week ago

Thanks for the extra updates, I was struggling to reproduce that, but glad to get to the core issue of the owner not set correctly, as that's critically important! Looks like that's due to using the NestedElementTrait.

Fixed in 3.0.1

mihob commented 1 week ago

Glad to help, thanks for the quick fix!

mihob commented 1 week ago

@engram-design Its still not working:

$session->startDate = $event['startDate'];
$session->endDate = $event['endDate'];

The properties must be DateTime or null, $event['startDate'] and $event['endDate'] are strings.

If you use setAttributes as before for these properties it works.

engram-design commented 1 week ago

My mistake, you're right about that. Fixed for the next release. To get this early, run composer require verbb/events:"dev-craft-5 as 3.0.2".