Upgrade schema while upgrade module (UpgradeSchema) – setup script – Magento 2

This is a continuation of the previous tutorial, It helps to alter table into the database while installing the module in Magento 2.

As per our previous tutorials, we summarize the below points

Now we upgrade the table schema while upgrade module.

  • UpgradeSchema.php script also executed while run “bin/magento setup:upgrade” and it additionally check version. We use this features to add a column into our existing custom table.

When we use UpgradeSchema

Consider already you launch your module-version-1.0.0 into public marketplace, If you want to alter schema of your module upcoming version-1.0.1, then you should add logic in UpgradeSchema.

First change the module version in app/code/Bilal/Cruddemo/etc/module.xml

Create UpgradeSchema Script

Create app/code/Vendor/Module/Setup/UpgradeSchema.php

If you keenly observe the UpgradeSchema script, you can find the logic of version comparison

As we discussed UpgradeSchema.php execute while running setup:upgrade so run the below command in your Magento root

If you check your table, you can find our new column.

I hope it helps! Please carry the same source code to follow next tutorial.

About Bilal

I'm a software programmer, I handle Magento2 modifications at any level.

View all posts by Bilal →

Leave a Reply

Your email address will not be published. Required fields are marked *