How Has Declarative Schema Simplified The Lives Of Magento Developers?

ZealousWeb
4 min readMay 20, 2020

--

How Has Declarative Schema Simplified The Lives Of Magento Developers?

Are you facing low speed while developing a Magento website? Do you find running various commands for the database operation a tedious task? Then, Declarative Schema is here to save the day.

Magento has come up with a new feature called Declarative Schema to work with plenty of databases without writing various scripts for a new module.

It would be a tedious task to have your software install an update only to have it take out in the next update while updating a series of updates. It would seem like a failed effort.

Magento’s declarative Schema saves the developers from the trouble of downloading an update just to have it removed in the next one while doing a long-pending update of the software.

Ideally, Magento only looks for changes in an updated script and is not concerned with the versions. So whenever you are upgrading yourself to another version, it blindly updated the changes.

The declarative schema allows the developers to decide the final states of a particular database and then the system adjusts to those changes automatically; this saves the developers from performing a plethora of redundant functions.

Why Should We Use A Declarative Schema?

Before the introduction of the declarative schema, developers would write a PHP script to update the database table field. Writing the script was not entirely an ideal solution because you also have to update the module version. Updating the module version will execute all the script between the two versions and creates a complex situation.

The new approach of declarative schema allows us to declare the final state of the database, and the system automatically adjusts to it. That means it does not require to run an upgrade command and no need to upgrade the module version.

How To Configure Declarative Schema?

For example, if you have a module version 1.0.0, and if the new version is 1.0.4, then when you run the upgrade command, it executes all the scripts between 1.0.0 and 1.0.2. So, in the first script if you can add one column, and in another script, you can delete it, then this can be eliminated with the help of declarative schema.

In your module, you need to create a db_schema.xml file under [MODULE]/etc/. In this file, you can define your table structure. Below is the demo of the table structure;

We can also perform various database operations like below

  • Drop a table.
  • Rename a table.
  • Add a column to the table.
  • Drop a column from a table.
  • Change the column type.
  • Rename a column.
  • Add an index.
  • Create a foreign key.
  • Drop a foreign key.

Convert Scripts To A Declarative Schema

Magento already defined some commands which will convert your install-and-upgrade script to a declarative schema. This command will also roll back the changes if anything goes wrong.

php bin/magento setup:upgrade -convert-old-scripts=1

Dry Run Mode

In the Dry Run mode, you can check your schema before changing the database. The database state does not change, and data will be the same before-and-after the installation. This is a very useful way to ensure that your code is proper.

php bin/magento setup:upgrade -dry-run=1

How To Create A Schema Whitelist

In the setup upgrade script, it will remove the column and the data from the table directly. Whereas, in a declarative schema, it does not remove data and state directly. Elements can be whitelisted and easily rolled back.

bin/magento setup:db-declaration:generate-whitelist -module-name=[MODULE_NAME]

Create Data And Schema Patches

You can create a patch using a declarative schema. A list of applied patches stored in the patch_list table and an unapplied patch will be applied when you run the setup upgrade command. Also, you do not need to specify the module version.

Conclusion

Declarative Schema is an essential feature introduced by Magento. It enhances the performance and simplifies processes for the Magento developer working with databases. It is a delight for Magento development agencies out there and has simplified the tedious process for the Magento certified developer.

So the next time you are upgrading your Magento system, you might want to pair it up with declarative schema to avoid repetitive work and save a lot of your time.

If you are looking for a Magento development company for your custom Magento development project then click here to get in touch! Nothing better than a happy community of Magento developers working with large databases with absolute ease.

Originally published at https://www.zealousweb.com.

--

--

ZealousWeb
ZealousWeb

Written by ZealousWeb

Helping businesses Solve The Unsolved with a tech-first approach to expedite digital transformation.

No responses yet