0

Can we add or update the database schema and if yes, so can anyone give example?

0

Upgrade Script: UpgradeSchema

UpgradeSchema classes is difference with the Install classes because they will run every time the module upgrade. So we will need to check the attribute setup_version in module.xml at app/code/Mageplaza/HelloWorld/etc/ and separate the script by each version.

In this example, we will change the attrubute setup_version to 1.2.0.

File: app/code/Mageplaza/HelloWorld/etc/module.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="Mageplaza_HelloWorld" setup_version="1.2.0">
    </module>
</config>

File: app/code/Mageplaza/HelloWorld/Setup/UpgradeSchema.php

<?php
namespace Mageplaza\HelloWorld\Setup;

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

class UpgradeSchema implements UpgradeSchemaInterface
{
    public function upgrade( SchemaSetupInterface $setup, ModuleContextInterface $context ) {
        $installer = $setup;

        $installer->startSetup();

        if(version_compare($context->getVersion(), '1.2.0', '<')) {
            $installer->getConnection()->addColumn(
                $installer->getTable( 'mageplaza_helloworld_post' ),
                'test',
                [
                    'type' => \Magento\Framework\DB\Ddl\Table::TYPE_DECIMAL,
                    'nullable' => true,
                    'length' => '12,4',
                    'comment' => 'test',
                    'after' => 'status'
                ]
            );
        }



        $installer->endSetup();
    }
}

In this class, we use the upgrade() method which will be run every time the module is upgraded. We also have to compare the version to add the script for each version.

Reference Links

  1. Add column Upgrade Schema Magento 2
  2. How to Create Setup, Upgrade, Uninstall Script in Magento 2
  3. How to Add an Extra Column in Your Existing Magento 2 Table?

You may checkout below link, if you are using Magento 2.3

Before Magento 2.3, extension developers were required to write code (PHP scripts) to change the database schema. The following types of scripts existed before Magento 2.3:

  • InstallData and InstallSchema scripts, which are executed the first time a module is installed.
  • UpgradeData and UpgradeSchema incremental scripts, which supplement an existing module schema.
  • Recurring scripts, which are executed each time you install or upgrade Magento.

Configure declarative schema

4
  • Supravat , what about db version??? Dec 24 '19 at 5:58
  • What exactly you want me explain? Dec 24 '19 at 6:03
  • I can do the same stuff with s:up after changing setup_version in module.xml then why to use setup:db-schema:upgrade? Dec 24 '19 at 6:24
  • setup:upgrade is uses for Upgrades the Magento application, DB data, and schema & setup:db-schema:upgrade is uses for Installs and upgrades the DB schema Dec 24 '19 at 6:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.