Exploring Declarative Schema in Magento 2

In earlier versions of Magento 2, developers had to manage database schema changes using PHP scripts like InstallSchema.php and UpgradeData.php. These scripts ran linearly and required careful version management for every change. However, with the introduction of the Declarative Schema in Magento 2.3, these complexities were alleviated, marking a significant advancement in module development.

Declarative schema, represented by a simple XML file in the etc directory of a module, declares the final state of the database. Magento 2 automatically adjusts the database schema based on this declaration. Unlike the previous approach, where developers had to write PHP scripts for each schema change, declarative schema simplifies the process by providing a structured and declarative way to define the database structure.

Key Features of Declarative Schema

  1. Simplified Schema Management: Instead of writing PHP scripts for schema changes, developers define the database structure using XML.
  2. Flexibility: Declarative schema allows for various database operations, including adding new tables, renaming tables, adding or removing columns, updating column attributes, adding indexes, and defining primary and foreign keys.
  3. Dry Run: Magento 2 offers a dry run feature that generates SQL statements without modifying the actual database schema or data. This allows developers to preview the changes before executing them, enhancing development efficiency and reducing the risk of errors.

Sample Declarative Schema

The db_schema.xml configuration file provides information about database table(s) schema, its columns, foreign keys, and indexes. The configuration file is located in the etc directory of a Magento module.

<schema>
   <table name="magemastery_store_credit_balance"
resource="default"
engine="innodb"
           comment="Store Credit Balance Table">

       <column xsi:type="int"
name="balance_id"
unsigned="true"
nullable="false"
identity="true"
                comment="Balance ID"/>

        <column xsi:type="int"
name="customer_id"
unsigned="true"
nullable="true"
identity="false"
                comment="Customer ID"/>

        <column xsi:type="decimal"
name="balance"
scale="4"
precision="20"
unsigned="false"
nullable="true"
                default="0"
comment="Store Credit Balance"/>

        <constraint xsi:type="primary" referenceId="PRIMARY">
            <column name="balance_id"/>
        </constraint>

      <index referenceId="MAGEMASTERY_STORE_CREDIT_BALANCE_CUSTOMER_ID" indexType="btree">
            <column name="customer_id"/>
        </index>

        <constraint xsi:type="foreign"
referenceId="MAGEMASTERY_STORE_CREDIT_BALANCE_CSTR_ID_CSTR_ENTT_ENTT_ID"
                  table="magemastery_store_credit_balance"
column="customer_id"
referenceTable="customer_entity"
                    referenceColumn="entity_id"
onDelete="CASCADE"/>

</table>
</schema>

Summary

Declarative schema simplifies database management in Magento 2, providing a structured and efficient way to define and manage database schema changes. By adopting a declarative schema, developers can streamline module development and improve overall code quality and maintainability. In addition, features like dry run offer enhanced control and confidence during the development process.