Chapters Close

Recurring setup script in Magento 2

The title “setup script” itself demonstrates that script files are utilized to execute some operations on the data or the table while installing or upgrading a module.

You can find all the setup scripts files in a Setup folder which is located at: 

app/code/<Vendor name>/<Module Name>/Setup

Let’s dig deep and master more about what recurring script is and how it is helpful. The recurring script is a new trait of Magento 2 that can be created in your module in Setup/Recurring.php

It serves the same purpose as of an Upgrade script but doesn’t have a version comparison condition. An upgrade script only runs when the module version changes whereas a recurring script can run whenever the CLI calls command: $ bin/magento setup: upgrade

Recurring Schema event

Your module’s recurring schema event class is executed by Magento post each schema installation or upgrade stage. This class makes final changes to the database schema after it has been installed or updated.

Well, one thing about recurring scripts is that it does not implement its interface type, instead, they make use of InstallSchemaInterface and the rest of the code is inside the install function.

In the event that you want to execute any operation on the table or data after installing or upgrading any module, you have to generate the recurring script.

For example,
Setup/Recurring.php

<?php

namespace Aureatelabs\Test\Setup;

use Magento\Framework\Setup\InstallSchemaInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\SchemaSetupInterface;
use Magento\Framework\Stdlib\DateTime\DateTime;

class Recurring implements InstallSchemaInterface
{
	/**
	 * @var \Magento\Framework\Stdlib\DateTime\DateTime
	 */
	protected $dateTime;

	public function __construct(
		DateTime $dateTime
	) {
		$this->dateTime = $dateTime;
	}

	public function install(SchemaSetupInterface $setup, ModuleContextInterface $context)
	{
		$setup->startSetup();
		$setup->getConnection()->query("INSERT INTO aureatelabs_logs SET log_datetime = '" . $this->datetime->gmtTimestamp() . "'");
		$setup->endSetup();
	}
}

In the illustration given above, while $ bin/magento setup:upgrade command is called in CLI the new record will be entered in the log table (aureatelabs_logs).

There’s hardly one example in the prevailing Magento 2 that is the vendor/magento/module-indexer/Setup/Recurring.php class where Magento_Indexer module inspects for the newly defined indexers and appends them into the indexer_state table.

Recurring data event

Your module’s recurring data event class is executed by Magento post each schema installation or upgrade stage. This class then makes the last revisions to the database store after the data has been installed or updated.

For example,
Setup/RecurringData.php

<?php

namespace Aureatelabs\Test\Setup;

use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;

class RecurringData implements InstallDataInterface
{
	public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
	{
		// Recurring data event logic
	}
}

The only example in current Magento 2 is vendor/magento/module-indexer/Setup/RecurringData.php class.

Thanks

Speak your Mind

Post a Comment

Got a question? Have a feedback? Please feel free to leave your ideas, opinions, and questions in the comments section of our post! ❤️

* This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

Thank you for your feedback

Comments (1)

  1. Nice, helped me. Thank you.

    Reply

Grow your online business like 4,191 subscribers

    * This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.
    envelope

    Thank You!

    We are reviewing your submission, and will be in touch shortly.