{{announcement.body}}
{{announcement.title}}

Declarative Schema: Simplifying Magneto Development

DZone 's Guide to

Declarative Schema: Simplifying Magneto Development

Performance optimization and database management are common issues amongst Magneto devs. Use Declarative Schema to solve this wide-spread problem.

· Web Dev Zone ·
Free Resource

Are you facing low speed while developing a Magento website? Do you find running various commands for database operations 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 removed in the next update while updating a series of updates. It would seem like a failed effort.

Magento’s declarative Schema saves developers 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.

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 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;

XML
 







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 that 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 Magento developers.

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.

Topics:
magento, schema

Published at DZone with permission of Kandarp Bhatt . See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}