Magento2 : How to database schema upgrade

I am working on magento custom module, in module have Setup\InstallSchema.php file which is installed before. i have added some more database fields in InstallSchema.php so i want to update table structure but table have not applied any changes.

How i can apply schema changes to database table?

I have process cli commands to update schema but not success.

php bin/magento setup:db-schema:upgrade

and

php bin/magento setup:upgrade

Solutions

To upgrade Installer Schema you have to write 'UpgradeSchema.php',

example of UpgradeSchema.php:

namespace <namespace>\<modulename>\Setup;

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

/**
* @codeCoverageIgnore
*/
class UpgradeSchema implements UpgradeSchemaInterface
{
    /**
 * {@inheritdoc}
 */
public function upgrade(SchemaSetupInterface $setup, ModuleContextInterface $context)
{
    $installer = $setup;

    $installer->startSetup();

    /*your code here*/

    $installer->endSetup();
}
}

Step 2: In your module you will find module.xml inside etc folder in that file change setup_version value(ex: 1.0.1 to 1.0.2) version value should be higher then current version value.

Step 3: Run php bin/magento setup:upgrade command from CLI

If you want to add more column into existing table of your module you could do following.

Step 1: Create UpgradeSchema.php under Setup folder. Get Idea from following code.

namespace Vendor\ModuleName\Setup;

use Magento\Framework\Setup\UpgradeSchemaInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\SchemaSetupInterface;
class UpgradeSchema implements  UpgradeSchemaInterface
{
    public function upgrade(SchemaSetupInterface $setup,
                            ModuleContextInterface $context){
        $setup->startSetup();
        if (version_compare($context->getVersion(), '1.0.1') < 0) {

            // Get module table
            $tableName = $setup->getTable('table_name');

            // Check if the table already exists
            if ($setup->getConnection()->isTableExists($tableName) == true) {
                // Declare data
                $columns = [
                    'imagename' => [
                        'type' => \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
                        'nullable' => false,
                        'comment' => 'image name',
                    ],
                ];

                $connection = $setup->getConnection();
                foreach ($columns as $name => $definition) {
                    $connection->addColumn($tableName, $name, $definition);
                }

            }
        }

        $setup->endSetup();
    }
}

Step 2: Change the setup_version value in module.xml

Step 3: Run php bin/magento setup:upgrade command from CLI

Similar questions

How to create upgrade schema file second time
I want to create a new upgrade schema file in magento2. I already used the install schema and upgrade schema. I need to create a new upgrade schema.
Error Area Code is not set when Creating a virtual Product from Upgrade Schema
I'm using magento 2.2.5 , i want to make a virtual product programmatically using UpgradeSchema , but i alws got an error when i run bin/magento setup:upgrade here's my UpgradeSchema.php
How to convert install/upgrade schema scripts to db_schema.xml files?
I'm following the guide from Magento DevDocs about the Declarative Schema I created a new module with old setup way (InstallSchema.php, InstallData.php) to test the convert install/upgrade schema scripts to db_schema.xml config. I ran two commands: But, I didn't see the db_schema.xml in my custom module. Why the db_schema.xml didn't generate in my ...
Magento 2 : how can we use of setup:db-schema:upgrade?
Can we add or update the database schema and if yes, so can anyone give example?
Magento 2 create foreign key with upgrade schema
I have 2 table. I want to createForeignKey. I have my code: Please help me!
Batching database schema changes during upgrades
Some background When rehearsing a production deployment to EE 1.11 from 1.10, we encountered an issue where the SQL upgrade routines were adding columns to report, log, sales, customer tables for the Enterprise_RMA module. The log tables on this particular install had ~10M rows, had not been maintained in quite some time. Adding columns proved to t...

Also ask

We use cookies to deliver the best possible experience on our website. By continuing to use this site, accepting or closing this box, you consent to our use of cookies. To learn more, visit our privacy policy.