In part 1 of this article, we discussed what declarative schemes are, why you should use declarative schemes instead of install/upgrade scripts, how to create, edit, and eventually delete a declarative. If you haven’t already read the first part, I suggest you take a moment to read it before you begin this article.

As an example, I created a simple module containing a

InstallSchema.php that looks like this.

<?php /** * Copyright (c) 2016 Magento. All rights reserved. * See COPYING.txt for license details. */ namespace Rick\Example\Setup; use Magento\Framework\DB\Ddl\Table; use Magento\Framework\Setup\InstallSchemaInterface; use Magento\Framework\Setup\ModuleContextInterface; use Magento\Framework\Setup\SchemaSetupInterface; /** * @codeCoverageIgnore */ class InstallSchema implements InstallSchemaInterface { /** * {@inheritdoc} * @SuppressWarnings(PHPMD.ExcessiveMethodLength) */ public function install(SchemaSetupInterface $setup, ModuleContextInterface $context) { /** * Create table 'quote_item_file' */ $table = $setup->getConnection() ->newTable($setup->getTable('quote_item_file')) ->addColumn( 'entity_id', Table::TYPE_INTEGER, null, ['nullable' => false, 'primary' => true, 'identity' => true], 'Entity ID' ) ->addColumn( 'filename', Table::TYPE_TEXT, 255, ['nullable' => false], 'Filename' )->addColumn( 'location', Table::TYPE_TEXT, 255, ['nullable' => false], 'Location of file' )->addColumn( 'quote_item_item_id', Table::TYPE_INTEGER, null, ['padding' => 10, 'nullable' => false, 'unsigned' => true], 'Item Id' )->addForeignKey( $setup->getFkName( $setup->getTable('quote_item_file'), 'quote_item_item_id', 'quote_item', 'item_id' ), 'quote_item_item_id', $setup->getTable('quote_item'), 'item_id', Table::ACTION_CASCADE )->setComment('Quote Item File Table'); $setup->getConnection()->createTable($table); } }

Migrate install/upgrade scripts to a declarative scheme

Now it is also possible to convert existing installation / upgrade scripts to a declarative scheme with the “Schema Listener Tool”. It is useful to know that using the Schema listener tool can only be used in developer mode, as it will make changes to the code.

In addition, it does not take into account raw SQL data, and custom DDL operations outside of

\Magento\Framework\DB\Adapter\Pdo\Mysql.

To perform the migration, run the following command:

bin/magento s:up --convert-old-scripts=1

For Magento versions less than 2.4 use the - convert_old_scripts flag

Note that this only works during an installation or upgrade of your modules. If you have issues generating, try removing the module entry in the setup_module table and then run setup:upgrade again.

This will generate a

db_schema.xmlin your module etc directory.

<?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 name="quote_item_file" resource="default" engine="innodb" comment="Quote Item File Table"> <column xsi:type="int" name="entity_id" padding="11" unsigned="false" nullable="false" identity="true" comment="Entity ID"/> <column xsi:type="varchar" name="filename" nullable="false" length="255" comment="Filename"/> <!--<column xsi:type="varchar" name="location" nullable="false" length="255" comment="Location of file"/>--> <column xsi:type="int" name="quote_item_item_id" padding="10" unsigned="true" nullable="false" identity="false" comment="Item Id"/> <constraint xsi:type="primary" referenceId="PRIMARY"> <column name="entity_id"/> </constraint> <constraint xsi:type="foreign" referenceId="QUOTE_ITEM_FILE_QUOTE_ITEM_ITEM_ID_QUOTE_ITEM_ITEM_ID" table="quote_item_file" column="quote_item_item_id" referenceTable="quote_item" referenceColumn="item_id" onDelete="CASCADE"/> </table> </schema>

Then we add some data

INSERT INTO `magento`.`quote_item_file` (`entity_id`, `filename`, `location`, `quote_item_item_id`) VALUES ('1', 'examole.png', 'media/foo/bar/example.png', '1');

#magento #magento2 #magento-2 #programming #coding #magento-development #magentosupport #coding-skills

The Essential Guide to Declarative Schemas in Magento 2
1.40 GEEK