Install

Install is used for creating database tables and adding data to Magento 2. It uses Schema and Data install and upgrade classes.

  • from_version: Add sample upgrade from version statement

Snippet generation

When you generate a module, the following classes will be created:

Install scripts

  • Setup/InstallSchema
  • Setup/InstallData

Upgrade scripts (With the sample from_version statement)

  • Setup/UpgradeSchema
  • Setup/UpgradeData

Use the Install snippet in the Magento 2 module creator.

Files

Setup/UpgradeData.php

<?php


namespace Mage2Gen\Module\Setup;

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

class UpgradeData implements UpgradeDataInterface
{

    /**
     * {@inheritdoc}
     */
    public function upgrade(
        ModuleDataSetupInterface $setup,
        ModuleContextInterface $context
    ) {
        $setup->startSetup();
        if (version_compare($context->getVersion(), "1.0.0", "<")) {
        //Your upgrade script
        }
        $setup->endSetup();
    }
}

Setup/InstallData.php

<?php


namespace Mage2Gen\Module\Setup;

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

class InstallData implements InstallDataInterface
{

    /**
     * {@inheritdoc}
     */
    public function install(
        ModuleDataSetupInterface $setup,
        ModuleContextInterface $context
    ) {
        //Your install script
    }
}

Setup/InstallSchema.php

<?php


namespace Mage2Gen\Module\Setup;

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

class InstallSchema implements InstallSchemaInterface
{

    /**
     * {@inheritdoc}
     */
    public function install(
        SchemaSetupInterface $setup,
        ModuleContextInterface $context
    ) {
        $installer = $setup;
        $installer->startSetup();

        $setup->endSetup();
    }
}

Setup/UpgradeSchema.php

<?php


namespace Mage2Gen\Module\Setup;

use Magento\Framework\Setup\UpgradeSchemaInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\SchemaSetupInterface;

class UpgradeSchema implements UpgradeSchemaInterface
{

    /**
     * {@inheritdoc}
     */
    public function upgrade(
        SchemaSetupInterface $setup,
        ModuleContextInterface $context
    ) {
        $setup->startSetup();
        if (version_compare($context->getVersion(), "1.0.0", "<")) {
        //Your upgrade script
        }
        $setup->endSetup();
    }
}