Skip to main content

What is Declarative Schema in Magento?

Declarative schema is the new feature in Magento to do database operations of creating and updating database tables. This is one of the major changes introduced by the Magento team in 2.3 and it replaces the old approach of creating InstallSchema & UpgradeSchema scripts.

Why Declarative Schema?

Before declarative schema, developers need to write different schema scripts in each version of a module. And when a customer upgrades Magento to a version several releases ahead of the installed version, the upgrade scripts between those two versions will still be executed. This increases the complexity to track changes with every upgrade script. And new declarative schema approach allows developers to declare the final desired state of the database and has the system adjust to it automatically.

What is the use of db_schema.xml?

Before Magento 2.3, whatever database operations developers were writing in InstallSchema & UpgradeSchema scripts are replaced by the db_schema.xml file. So now, the below operations will be done using db_schema:

  • Create or drop tables in the database.
  • Alter table columns.
  • Add new constraints or indexes to the tables.

Let’s see an example of how to create a custom table in a module. Create a new db_schema.xml file inside the VendorName/ModuleName/etc/ folder of a custom module.

<schema xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Setup/Declaration/Schema/etc/schema.xsd">
    <table name="declarative_table">
        <column xsi:type="int" name="id_column" padding="10" unsigned="true" nullable="false" comment="Entity Id"/>
        <column xsi:type="int" name="severity" padding="10" unsigned="true" nullable="false" comment="Severity code"/>
        <column xsi:type="varchar" name="title" nullable="false" length="255" comment="Title"/>
        <column xsi:type="timestamp" name="time_occurred" padding="10" comment="Time of event"/>
        <constraint xsi:type="primary" referenceId="PRIMARY">
            <column name="id_column"/>
        </constraint>
    </table>
</schema>

To apply changes in the Magento, you need to run the upgrade command as below:

php bin/magento setup:upgrade

 For all other database operations please follow the link.

What is the db_schema_whitelist.json file? What is it used?


All operations performed in the system should be backward compatible. Because of this declarative schema doesn’t drop tables or columns or keys which are not available in db_schema.xml.

Comments

Popular posts from this blog

Unlocking Success: The Vital Role of the Contact Us Page in E-commerce

In the dynamic realm of e-commerce, where digital transactions reign supreme, the significance of customer communication cannot be overstated. Amidst the plethora of factors influencing the success of an online store, one often overlooked yet fundamentally important element is the Contact Us page. This seemingly humble corner of a website holds immense power, serving as a linchpin in fostering trust, resolving issues, and nurturing customer relationships. Let's delve deeper into why the Contact Us page is not just an afterthought but a strategic asset for e-commerce businesses, backed by proven data. Building Trust and Credibility Trust is the cornerstone of any successful e-commerce venture. According to a survey conducted by Edelman, 81% of consumers say that trusting a brand to do what is right is a deciding factor in their purchasing decisions. A prominently displayed Contact Us page with clear contact information, including a physical address, phone number, and email address, ...

Magento - LogRocket Integration

In today’s competitive eCommerce landscape, understanding user behavior is crucial for optimizing customer experiences and improving conversion rates. Magento 2, a powerful and flexible eCommerce platform, allows merchants to customize their online stores extensively. However, monitoring how users interact with these customizations is often challenging. This is where LogRocket, a modern session replay tool, comes into play. Integrating LogRocket with Magento 2 can provide invaluable insights into user behavior, performance bottlenecks, and UX issues. In this blog post, we’ll walk you through the steps to integrate LogRocket with Magento 2, and how this integration can help you improve your store’s performance and user experience. What is LogRocket? LogRocket is a session replay tool that enables you to record and playback user activity on your website. It tracks interactions such as clicks, scrolls, and form inputs, giving you a clear view of how users navigate your store. In addition,...

Missing crypt key for upgrading Magento

This is my first experience setting up a local docker environment for my project which is in Magento 2 Cloud edition. While doing setup by following Magento docs, I got stuck in the step "Deploy adobe commerce in the Docker Container". Here I was facing the issue: "Missing crypt key for upgrading Magento". Reason : I have taken a DB backup from my staging environment, it has a crypt_key stored which is used by Magento for encrypting the secured data. Solution : If you are not using docker for local setup then, you can simply update the crypt_key value in the app/etc/env.php file. But if you are using docker for your local setup, then you need to include your crypt_key in the config.php inside the .docker folder in your Magento root directory. Open the  .docker/config.php file. Then, search for the key MAGENTO_CLOUD_VARIABLES and add your crypt_key like below: Once you add the crypt_key in the file, try to again execute the  docker-compose run --rm deploy cloud-d...