3

What is in short words declarative schema? Thanks for any help)

2

2 Answers 2

2

Its a different method of defining database schema.

From the docs:

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

Source: https://developer.adobe.com/commerce/php/development/components/declarative-schema/

Example

Previous method

  • Version 1: add column A to table
  • Version 2: add columns B add C table
  • Version 3: remove column B from table

Net result after version 3 is a table with columns A and C

Declarative schema method

You just define the net result you want.

  • Version 1: table with column A
  • Version 2: table with column A and B
  • Version 3: table with Column A and C

Net result after version 3 is a table with columns A and C

1

Basically, instead of using an InstallSchema class that extends Magento\Framework\Setup\InstallSchemaInterface, you can use xml to define your database schema.

Previous Method:

namespace Vendor\Module\Setup;

use Magento\Framework\Setup\InstallSchemaInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\SchemaSetupInterface;

class InstallSchema implements InstallSchemaInterface
{
    public function install(SchemaSetupInterface $setup, ModuleContextInterface $context)
    {
        $installer = $setup;
        $installer->startSetup();
        
        $table = $installer->getConnection()->newTable($installer->getTable('some_table_name'))
                ->addColumn('example_id',       \Magento\Framework\DB\Ddl\Table::TYPE_INTEGER, 10,  ['unsigned' => true, 'nullable' => false, 'primary' => true,'auto_increment' => true], 'example id')
                ->addColumn('order_id',                 \Magento\Framework\DB\Ddl\Table::TYPE_INTEGER, 10,  ['nullable' => false],  'order id');
                
        $installer->getConnection()->createTable($table);
    }
}

New Method: Vendor/Module/etc/db_schema.xml

<?xml version="1.0" ?>
<schema xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Setup/Declaration/Schema/etc/schema.xsd">
    
    <table comment="some_table_name Table" engine="innodb" name="some_table_name" resource="default">
        
        <column comment="Example Id" identity="true" name="example_id" nullable="false" padding="6" unsigned="true" xsi:type="smallint"/>
        <constraint referenceId="PRIMARY" xsi:type="primary">
            <column name="example_id"/>
        </constraint>
        
        <column name="order_id" nullable="false" xsi:type="int" comment="order id"/>
        
    </table>
</schema>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.