Magento 2.3 Declarative Schema

In an effort to simplify the Magento installation and upgrade process, 2.3 introduces declarative schema.

With the new declarative schema approach allows,  developers are no longer forced to write scripts for each new version of a module. Using declarative schema, developers declare the final desired state of the database, and the system adjusts to it automatically, without performing redundant operations. In addition, this approach allows data to be easily deleted when a module is uninstalled.

There are two parts to using declarative schema: the db_schema.xml file and the bin/magento command to register it.

Once you use declarative schema in a module, you cannot use setup/upgrade scripts again. In the future, upgrade scripts will be phased out in favor of declarative schema.

The following example, extracted from the Catalog/etc/db_schema.xml file, defines the catalog_product_entity_datetime table:

<table name="catalog_product_entity_datetime" resource="default" engine="innodb"
           comment="Catalog Product Datetime Attribute Backend Table">
        <column xsi:type="int" name="value_id" padding="11" unsigned="false" nullable="false" identity="true" comment="Value ID"/>
        <column xsi:type="smallint" name="attribute_id" padding="5" unsigned="true" nullable="false" identity="false"default="0" comment="Attribute ID"/>
        <column xsi:type="smallint" name="store_id" padding="5" unsigned="true" nullable="false" identity="false" default="0" comment="Store ID"/>
        <column xsi:type="int" name="entity_id" padding="10" unsigned="true" nullable="false" identity="false" default="0"/>
        <column xsi:type="datetime" name="value" on_update="false" nullable="true" comment="Value"/>
        <constraint xsi:type="primary" name="PRIMARY">
            <column name="value_id"/>
        </constraint>
</table>

https://magento-devdocs.github.io/devdocs-for-tests/guides/v2.3/extension-dev-guide/declarative-schema/db-schema.html

Leave a Reply

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