{"id":14985435,"url":"https://github.com/bdcrops/module-declarative","last_synced_at":"2025-04-11T16:30:39.293Z","repository":{"id":62492481,"uuid":"205715641","full_name":"bdcrops/module-declarative","owner":"bdcrops","description":"Magento 2.3x  Declarative Schema Tutorial","archived":false,"fork":false,"pushed_at":"2019-11-06T07:55:14.000Z","size":2967,"stargazers_count":7,"open_issues_count":0,"forks_count":3,"subscribers_count":4,"default_branch":"master","last_synced_at":"2024-10-31T06:51:36.631Z","etag":null,"topics":["composer","configure-declarative-schema","database-schema","db-schema","db-whitelist-schemaon","declarative-schema","declarative-schema-offers","declarative-schema-tutorial","drop","insert-data","magento","magento-2-dbschema","magento-2-declarative-schema","magento-setup","magento2","magento2-exam-preparation","php-framework","professional-developer","schema-whitelist","setup"],"latest_commit_sha":null,"homepage":"http://www.bdcrops.com","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"gpl-3.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/bdcrops.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-09-01T18:21:47.000Z","updated_at":"2023-10-29T15:42:46.000Z","dependencies_parsed_at":"2022-11-02T11:31:05.816Z","dependency_job_id":null,"html_url":"https://github.com/bdcrops/module-declarative","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bdcrops%2Fmodule-declarative","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bdcrops%2Fmodule-declarative/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bdcrops%2Fmodule-declarative/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bdcrops%2Fmodule-declarative/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/bdcrops","download_url":"https://codeload.github.com/bdcrops/module-declarative/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":223472814,"owners_count":17150745,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["composer","configure-declarative-schema","database-schema","db-schema","db-whitelist-schemaon","declarative-schema","declarative-schema-offers","declarative-schema-tutorial","drop","insert-data","magento","magento-2-dbschema","magento-2-declarative-schema","magento-setup","magento2","magento2-exam-preparation","php-framework","professional-developer","schema-whitelist","setup"],"created_at":"2024-09-24T14:10:58.092Z","updated_at":"2024-11-07T07:05:00.819Z","avatar_url":"https://github.com/bdcrops.png","language":"PHP","readme":"## BDC_Declarative Schema Module Declarative Schema Tutorial\n\nDeclarative Schema: Magento 2.3x introduced a new declarative schema feature which aims at eliminating the excessive work and speeding up the installation and upgrade processes.\n\nMost module developers will be familiar with creating php scripts for creating, updating and manipulating custom tables for their modules. These files would live in the module directory here;\n\n- /app/code/NameSpace/ModuleName/Setup/InstallSchema.php\n- /app/code/NameSpace/ModuleName/Setup/UpgradeSchema.php\n\nSince Magento 2.3.0, you can now use the declarative DB schema XML to create and update your DB tables in a single XML file. This file is placed in your module here;\n\n/app/code/NameSpace/ModuleName/etc/db_schema.xml\n\nThis change is incredibly useful once you start playing with it. The php schema scripts can be long winded and difficult to read. The XML is short and neat, making it a relative dream to work with.\n\n\n## Goal:\n\n - Create table (etc/db_schema.xml)\n\n - Schema whitelist (etc/db_schema_whitelist.json)\n\n - DataPatch / Insert data (Installing and upgrading data)\n\n - How to install \u0026 upgrade BDC_Declarative\n\n - FAQ of Declarative Schema\n\n - Certified Associate/Professional Developer Questions \u0026 Solution\n\n## 1. How to install \u0026 upgrade  Declarative Schema Module\n### 1.1. Copy and paste\n  If you don't want to install via composer, you can use this way.\n  - Download [the latest version here](https://github.com/bdcrops/module-declarative/archive/master.zip)\n\n### 1.2. Install via composer\n  ```\n  composer require bdc/module-declarative\n  ```\n\n****\n\n## 2. Declarative Schema Module Step By Step\n\n - Create [app/code/BDC/Declarative/registration.php](registration.php)\n\n   \u003cdetails\u003e\u003csummary\u003eSource\u003c/summary\u003e\n\n     ```\n     \u003c?php\n     \\Magento\\Framework\\Component\\ComponentRegistrar::register(\n         \\Magento\\Framework\\Component\\ComponentRegistrar::MODULE,\n         'BDC_Declarative',\n         __DIR__\n     );\n     ```\n  \u003c/details\u003e\n\n - Create [etc/module.xml](etc/module.xml)\n\n    \u003cdetails\u003e\u003csummary\u003eSource\u003c/summary\u003e\n\n        ```\n        \u003c?xml version=\"1.0\"?\u003e\n        \u003cconfig xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\"urn:magento:framework:Module/etc/module.xsd\"\u003e\n        \u003cmodule name=\"BDC_Declarative\" setup_version=\"1.0.0\"/\u003e\n        \u003c/config\u003e\n        ```\n    \u003c/details\u003e\n\n\n - Create [etc/db_schema.xml](etc/db_schema.xml)\n\n    \u003cdetails\u003e\u003csummary\u003eSource\u003c/summary\u003e\n\n        ```\n        \u003c?xml version=\"1.0\"?\u003e\n        \u003cschema xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\"urn:magento:framework:Setup/Declaration/Schema/etc/schema.xsd\"\u003e\n        \u003ctable name=\"bdc_declarative\" resource=\"default\" engine=\"innodb\" comment=\"bdcrops declarative table\"\u003e\n         \u003ccolumn xsi:type=\"smallint\" name=\"id\" padding=\"6\" unsigned=\"false\" nullable=\"false\" identity=\"true\" comment=\"ID\"/\u003e\n         \u003ccolumn xsi:type=\"varchar\" name=\"name\" nullable=\"false\" length=\"25\" comment=\"Name\"/\u003e\n         \u003ccolumn xsi:type=\"varchar\" name=\"email\" nullable=\"false\" length=\"25\" comment=\"Email\"/\u003e\n         \u003ccolumn xsi:type=\"varchar\" name=\"note\" nullable=\"false\" length=\"255\" comment=\"Descrition\"/\u003e\n         \u003ccolumn xsi:type=\"timestamp\" name=\"created\"  comment=\"Time of event\"/\u003e\n         \u003ccolumn xsi:type=\"timestamp\" name=\"date_closed\"  comment=\"Time of event\"/\u003e\n         \u003cconstraint xsi:type=\"primary\" referenceId=\"PRIMARY\"\u003e   \u003ccolumn name=\"id\"/\u003e \u003c/constraint\u003e\n        \u003c/table\u003e\n        \u003c/schema\u003e\n        ```\n    \u003c/details\u003e\n\n    file “db_schema.xml” inside folder “BDC/Declarative/etc” and write the following code\n\n    \u003cdetails\u003e\u003csummary\u003eShow Image\u003c/summary\u003e\n\n      ![db_schema](https://github.com/bdcrops/BDC_Declarative/blob/master/view/adminhtml/web/images/db_schema.png)\n    \u003c/details\u003e\n\n - Create [Setup/Schema/Sample.php](Setup/Schema/Sample.php)\n\n      \u003cdetails\u003e\u003csummary\u003eSource\u003c/summary\u003e\n\n        ```\n        \u003c?php\n        declare(strict_types=1);\n\n        namespace BDC\\Declarative\\Setup\\Patch\\Schema;\n\n        use Magento\\Framework\\Setup\\Patch\\SchemaPatchInterface;\n        use Magento\\Framework\\Setup\\ModuleDataSetupInterface;\n        /**\n        * Patch is mechanism, that allows to do atomic upgrade data changes\n        */\n        class Sample implements SchemaPatchInterface{\n         /**\n          * @var ModuleDataSetupInterface $moduleDataSetup\n          */\n         private $moduleDataSetup;\n         /**\n          * @param ModuleDataSetupInterface $moduleDataSetup\n          */\n         public function __construct(ModuleDataSetupInterface $moduleDataSetup) {\n             $this-\u003emoduleDataSetup = $moduleDataSetup;\n         }\n         /**\n          * Do Upgrade\n          *\n          * @return void\n          */\n         public function apply() { }\n         /**\n          * {@inheritdoc}\n          */\n         public function getAliases() { return []; }\n         /**\n          * {@inheritdoc}\n          */\n         public static function getDependencies() { return [ ]; }\n        }\n\n        ```\n      \u003c/details\u003e\n\n - Create [Setup/Patch/Data/NonRevertable.php](Setup/Patch/Data/NonRevertable.php)\n\n      \u003cdetails\u003e\u003csummary\u003eSource\u003c/summary\u003e\n\n          ```\n          \u003c?php\n          declare(strict_types=1);\n          namespace BDC\\Declarative\\Setup\\Patch\\Data;\n\n          use Magento\\Framework\\Setup\\Patch\\DataPatchInterface;\n          use Magento\\Framework\\Setup\\ModuleDataSetupInterface;\n          /**\n          * Class NonRevertable\n          * @package BDC\\Declarative\\Setup\\Patch\\Data\n          */\n          class NonRevertable implements DataPatchInterface{\n          /**\n          * @var ModuleDataSetupInterface $moduleDataSetup\n          */\n          private $moduleDataSetup;\n          /**\n          * @param ModuleDataSetupInterface $moduleDataSetup\n          */\n          public function __construct(ModuleDataSetupInterface $moduleDataSetup){\n             $this-\u003emoduleDataSetup = $moduleDataSetup;\n          }\n          /**\n          * Do Upgrade\n          * @return void\n          */\n          public function apply(){\n             $data = ['name' =\u003e 'Matin Rahman', 'email' =\u003e 'matinict@gmail.com','note' =\u003e 'Declarative insert'];\n             $this-\u003emoduleDataSetup-\u003egetConnection()-\u003einsert('bdc_declarative', $data);\n          }\n          /**\n          * {@inheritdoc}\n          */\n          public function getAliases(){ return []; }\n          /**\n          * {@inheritdoc}\n          */\n          public static function getDependencies(){ return []; }\n          }\n\n          ```\n      \u003c/details\u003e\n\n - Create [Setup/Patch/Data/Revertable.php](Setup/Patch/Data/Revertable.php)\n\n    \u003cdetails\u003e\u003csummary\u003eSource\u003c/summary\u003e\n\n         ```\n         \u003c?php\n         declare(strict_types=1);\n\n         namespace BDC\\Declarative\\Setup\\Patch\\Data;\n\n         use Magento\\Framework\\Setup\\Patch\\DataPatchInterface;\n         use Magento\\Framework\\Setup\\ModuleDataSetupInterface;\n         /**\n          * Class Revertable\n          * @package BDC\\Declarative\\Setup\\Patch\\Data\n          */\n         class Revertable implements DataPatchInterface {\n             /**\n              * @var ModuleDataSetupInterface $moduleDataSetup\n              */\n             private $moduleDataSetup;\n\n             /**\n              * @param ModuleDataSetupInterface $moduleDataSetup\n              */\n             public function __construct(ModuleDataSetupInterface $moduleDataSetup){\n                 $this-\u003emoduleDataSetup = $moduleDataSetup;\n             }\n\n             /**\n              * Do Upgrade\n              *\n              * @return void\n              */\n             public function apply() { }\n\n             /**\n              * {@inheritdoc}\n              */\n             public function getAliases() { return []; }\n             /**\n              * {@inheritdoc}\n              */\n             public static function getDependencies() { return [ ]; }\n         }\n\n         ```\n    \u003c/details\u003e\n\n-  Run\n A schema whitelist:You will not be able to run a declarative mode without creating a schema whitelist.\nNote: it is recommended to generate a new whitelist for every release for the double-check purposes. Before running the upgrade command you need to add your schema to db_whitelist_schema.json file by running the following command.\nFor that, you need a //etc/db_schema_whitelist.json file that will store all the content added with declarative schema. To generate this file, run:\n\n    \u003cdetails\u003e\u003csummary\u003eShow images\u003c/summary\u003e\n\n      ![db_schema](https://github.com/bdcrops/BDC_Declarative/blob/master/view/adminhtml/web/images/whitelist.png)\n\n    \u003c/details\u003e\n\n\n    ```\n    php bin/magento setup:db-declaration:generate-whitelist [options]\n    php bin/magento setup:db-declaration:generate-whitelist --module-name=vendor_module\n    php bin/magento setup:db-declaration:generate-whitelist --module-name=BDC_Declarative\n    php bin/magento setup:upgrade --dry-run=1 --keep-generated\n    ```\n\n    Now, there are db_whitelist_schema.json file will be create in /vendor/module/etc folder.\n     Insert data NonRevertable.php \u0026 Revertable.php\n\n    \u003cdetails\u003e\u003csummary\u003eShow images\u003c/summary\u003e\n\n      ![Insertdata](https://github.com/bdcrops/BDC_Declarative/blob/master/view/adminhtml/web/images/data.png)\n\n    \u003c/details\u003e\n\n    ```\n    Run php bin/magento setup:upgrade\n    ```\n\n    \u003cdetails\u003e\u003csummary\u003eShow Image\u003c/summary\u003e\n\n      ![InsertData](https://github.com/bdcrops/BDC_Declarative/blob/master/view/adminhtml/web/images/db-table.png)\n\n    \u003c/details\u003e\n\n***\n\n## 3.  Declarative Schema FAQ\n\n### What is Declarative Schema ?\n\n![declarative-schema](docs/magento-2-declarative-schema-8-638.jpg)\n\n### Why Declarative Schema ?\n\n![Issues with Setup Scripts](docs/magento-2-declarative-schema-10-638.jpg)\n\n### Will Declarative Schema help ?\n\n ![Declarative Schema Offers](docs/magento-2-declarative-schema-12-638.jpg)\n\n### How Declaring/Create a table?\n![Declaring a table](docs/magento-2-declarative-schema-18-638.jpg)\nThe below example creates the declarative_table table with four columns. The id_column column is the primary key.\n```\n\u003cschema xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n                xsi:noNamespaceSchemaLocation=\"urn:magento:framework:Setup/Declaration/Schema/etc/schema.xsd\"\u003e\n+    \u003ctable name=\"declarative_table\"\u003e\n+        \u003ccolumn xsi:type=\"int\" name=\"id_column\" padding=\"10\" unsigned=\"true\" nullable=\"false\" comment=\"Entity Id\"/\u003e\n+        \u003ccolumn xsi:type=\"int\" name=\"severity\" padding=\"10\" unsigned=\"true\" nullable=\"false\" comment=\"Severity code\"/\u003e\n+        \u003ccolumn xsi:type=\"varchar\" name=\"title\" nullable=\"false\" length=\"255\" comment=\"Title\"/\u003e\n+        \u003ccolumn xsi:type=\"timestamp\" name=\"time_occurred\" padding=\"10\" comment=\"Time of event\"/\u003e\n+        \u003cconstraint xsi:type=\"primary\" referenceId=\"PRIMARY\"\u003e\n+            \u003ccolumn name=\"id_column\"/\u003e\n+        \u003c/constraint\u003e\n+    \u003c/table\u003e\n\u003c/schema\u003e\n```\nWhen creating a new table, remember to generate the db_schema_whitelist.json file.\n\n### What is \u003ctable/\u003e Attributes ?\n#### \u003ctable/\u003e Attributes\n\n![Attributes](docs/magento-2-declarative-schema-19-638.jpg)\n#### \u003ctable/\u003e Attributes (Contd)\n\n![Attributes (Contd)](docs/magento-2-declarative-schema-20-638.jpg)\n\n### What is  integer columns?\n\n![](docs/magento-2-declarative-schema-23-638.jpg)\n![](docs/magento-2-declarative-schema-24-638.jpg)\n\n### How Declaring integer columns?\n\n![](docs/magento-2-declarative-schema-22-638.jpg)\n\n\n### How Declaring text columns?\n![](magento-2-declarative-schema-26-638.jpg)\n\n###  What is   \u003ccolumn/\u003e Text Attributes ?\n\n![](docs/magento-2-declarative-schema-27-638.jpg)\n![](docs/magento-2-declarative-schema-28-638.jpg)\n\n###  How Declaring binary columns?\n\n![](docs/magento-2-declarative-schema-30-638.jpg)\n\n### Whats is binary or \u003ccolumn/\u003e Text Attributes ?\n\n![](docs/magento-2-declarative-schema-31-638.jpg)\n![](docs/magento-2-declarative-schema-32-638.jpg)\n\n\n###   Declaring decimal columns?\n![](docs/magento-2-declarative-schema-34-638.jpg)\n\n###  decimal or \u003ccolumn/\u003e Text Attributes ?\n\n![](docs/magento-2-declarative-schema-35-638.jpg)\n![](docs/magento-2-declarative-schema-36-638.jpg)\n\n### Declaring a time column?\n\n![](DOCS/magento-2-declarative-schema-38-638.jpg)\n\n###  time or  \u003ccolumn/\u003e Text Attributes ?\n\n![](docs/magento-2-declarative-schema-39-638.jpg)\n![](docs/magento-2-declarative-schema-40-638.jpg)\n\n###  Declaring a primary key?\n![](docs/magento-2-declarative-schema-42-638.jpg)\n\n###  Declaring a foreign key?\n![](docs/magento-2-declarative-schema-44-638.jpg)\n\n\n###  Declaring a unique key?\n![](docs/magento-2-declarative-schema-46-638.jpg)\n\n###  Declaring an index?\n![](docs/magento-2-declarative-schema-48-638.jpg)\n\n###  \u003cindex/\u003e Attributes?\n![](docs/magento-2-declarative-schema-49-638.jpg)\n\n###  How Generating db_schema_whitelist.json?\n![](docs/magento-2-declarative-schema-51-638.jpg)\n\n###  \u003cmodule_root\u003e/etc/db_schema_whitelist.json?\n![](docs/magento-2-declarative-schema-52-638.jpg)\n![](docs/magento-2-declarative-schema-54-638.jpg)\n\n\n###  Testing with dry-run?\n![](docs/magento-2-declarative-schema-56-638.jpg)\n![](docs/magento-2-declarative-schema-57-638.jpg)\n\n###  var/log/dry-run-installation.log?\n![](docs/magento-2-declarative-schema-59-638.jpg)\n\n###  Applying Database Schema?\n![](docs/magento-2-declarative-schema-61-638.jpg)\n\n### Modifying the table column?\n![](docs/magento-2-declarative-schema-63-638.jpg)\n![](docs/magento-2-declarative-schema-64-638.jpg)\n![](docs/magento-2-declarative-schema-65-638.jpg)\n\n\n###  Testing with safe-mode  Destructive Operations ?\n\n![](docs/magento-2-declarative-schema-67-638.jpg)\n![](docs/magento-2-declarative-schema-68-638.jpg)\n\n###  var/declarative_dumps_csv/ {column_name_column_type_other_dimensions}.csv var/declarative_dumps_csv/{table_name}.csv?\n![](docs/magento-2-declarative-schema-69-638.jpg)\n\n###  Restoring Schema Data?\n![](docs/magento-2-declarative-schema-71-638.jpg)\n\n###  Uninstalling a Module?\n![](docs/magento-2-declarative-schema-73-638.jpg)\n\n###  Converting Setup Script to Declarative Schema?\n![](docs/magento-2-declarative-schema-75-638.jpg)\n\n###  Limitations of Declarative Schema?\n\n![](docs/magento-2-declarative-schema-76-638.jpg)\n\n\n\n### What is Schema Patch?\n\n![](docs/magento-23-schema-and-data-patches-6-638.jpg)\n\n### Where do we define Schema Patch?\n![](docs/magento-23-schema-and-data-patches-8-638.jpg)\n![](docs/magento-23-schema-and-data-patches-9-638.jpg)\n\n###  Methods we will implement?\n![](docs/magento-23-schema-and-data-patches-10-638.jpg)\n![](docs/magento-23-schema-and-data-patches-11-638.jpg)\n![](docs/magento-23-schema-and-data-patches-11-638.jpg)\n![](docs/magento-23-schema-and-data-patches-12-638.jpg)\n![](docs/magento-23-schema-and-data-patches-13-638.jpg)\n![](docs/magento-23-schema-and-data-patches-14-638.jpg)\n![](docs/magento-23-schema-and-data-patches-15-638.jpg)\n![](docs/magento-23-schema-and-data-patches-16-638.jpg)\n\n### How Applying the Schema Patch?   \n![](docs/magento-23-schema-and-data-patches-18-638.jpg)\n\n\n### What happened behind the scenes after Applying the Schema Patch?  \n![](docs/magento-23-schema-and-data-patches-20-638.jpg)\n![](docs/magento-23-schema-and-data-patches-21-638.jpg)\n\n###   What is Data Patch?\n![](docs/magento-23-schema-and-data-patches-23-638.jpg)\n\n### Where do we define Data Patch file ?  \n![](docs/magento-23-schema-and-data-patches-25-638.jpg)\n![](docs/magento-23-schema-and-data-patches-26-638.jpg)\n![](docs/magento-23-schema-and-data-patches-26-638.jpg)\n![](docs/magento-23-schema-and-data-patches-27-638.jpg)\n![](docs/magento-23-schema-and-data-patches-28-638.jpg)\n\n### What is Version Patch?\nVersions Patches\n• It is used for backwards compatibility with older setup scripts\n• A version number can be defined\n• Magento checks if setup module version\n• If the version of the module is higher than the version specified in your patch, then the patch is skipped\n• If the version in the database is equal or lower, then the patch installs.\n• It is marked as deprecated  \n![](docs/magento-23-schema-and-data-patches-30-638.jpg)\n\n### How to convert a schema/patch into a version patch ?\n\n![](docs/magento-23-schema-and-data-patches-32-638.jpg)\n\n###   What is Revertable Patch?\nRevertable Patches\n• Patches can be reverted at the time of removing the module\n• Single patches cannot be reverted\n• Module uninstall needed to be triggered\n• This feature is still buggy and changes are happening\n\n![](docs/magento-23-schema-and-data-patches-34-638.jpg)\n\n### How to convert a schema/patch into a revertable patch ?   \n![](docs/magento-23-schema-and-data-patches-36-638.jpg)\n\n### How to uninstall a module ?\n![](docs/magento-23-schema-and-data-patches-38-638.jpg)\n\n### New CLI Command  Generating Patches?\n![](docs/magento-23-schema-and-data-patches-40-638.jpg)\n![](docs/magento-23-schema-and-data-patches-41-638.jpg)\n\n```\nphp bin/magento help setup:db-declaration:generate-patch\nphp bin/magento setup:db-declaration:generate-patch Bdcrops_TicketingSystem Example --type=data\nphp bin/magento setup:db-declaration:generate-patch Bdcrops_TicketingSystem Example --type=schema\n\n```\n![](docs/generatePatch.png)\n![](docs/generateDataPatch.png)\n![](docs/generateSchemaPatch.png)\n\n\n### How to Drop a table?\n\nTo drop declarative_table table was completely removed from the db-schema.xml file.\n```\n\u003cschema xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\"urn:magento:framework:Setup/Declaration/Schema/etc/schema.xsd\"\u003e\n        //Remove All Content, Table will be Drop after whitelist \u0026 Upgrade run\n\u003c/schema\u003e\n```\n### What is Top node?\nThe top node represents the schema node, which is located in the schema.xsd file.\n\n\u003cschema xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance”        xsi:noNamespaceSchemaLocation=”urn:magento:framework:Setup/Declaration/Schema/etc/schema.xsd”\u003e\n\nThe location of the schema.xsd file is: \u003cmagento_root_directory\u003e/vendor/magento/framework/Setup/Declaration/Schema/etc/schema.xsd\n\n### What is  Table node?\nWe can create more than one table in the same db_schema.xml file, each table node creates a new table in the database. A table node can contain the following attributes:\n- Name: The name of the table\n- Engine: SQL engine, this value must be InnoDB or memory.\n- Resource: The database shard on which to install the table. This value must be default, checkout, or sales.\n- Comment: Table comment\nThe table node contains three different types of subnodes:\n- Column\n- Constraints\n- Index\n\n### What is Column node?\nThe column node defines inside the table node, each column node has its own declaration. A column node can contain the following attributes:\n- Type: the column type should contain one of the blob (includes blob, mediumblob, longblob), boolean, date, datetime, int (includes smallint, bigint, tinyint), real (includes decimal, float, double, real), text (includes text, mediumtext, longtext), timestamp, varbinary, and varchar.\n- Name: name of the column\n- Padding: indicates the size of the integer column\n- Unsigned: indicates if the column contains positive and negative values or only positive values\n- Nullable: indicates if the column can be nullable\n- Comment: indicates the comment of the column\n- Length: indicates the length of a column\n\n### How to Rename a table?\n\n  ```\n  \u003ctable name=\"declarative_table\"\u003e\n   Changed as below\n\u003ctable name=\"new_declarative_table\" onCreate=\"migrateDataFromAnotherTable(declarative_table)\"\u003e\n```\n### How to Add a column to table?\n\nThe following example adds the date_closed column.\n```\n\u003cschema xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n                 xsi:noNamespaceSchemaLocation=\"urn:magento:framework:Setup/Declaration/Schema/etc/schema.xsd\"\u003e\n    \u003ctable name=\"declarative_table\"\u003e\n        \u003ccolumn .../\u003e ...\n\n+       \u003ccolumn xsi:type=\"timestamp\" name=\"date_closed\" padding=\"10\" comment=\"Time of event\"/\u003e\n\n        \u003cconstraint ..\u003e...\u003c/constraint\u003e\n    \u003c/table\u003e\n\u003c/schema\u003e\n```\nWhen adding a new column into table, remember to generate the db_schema_whitelist.json file.\n\n### How to Drop a column from a table?\n\nThe following example removes the date_closed column by deleting its column node. To drop a column declared in another module, redeclare it with the disabled attribute set to true.\n```\n\u003cschema xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n                 xsi:noNamespaceSchemaLocation=\"urn:magento:framework:Setup/Declaration/Schema/etc/schema.xsd\"\u003e\n    \u003ctable name=\"declarative_table\"\u003e\n        \u003ccolumn xsi:type=\"int\" name=\"id_column\" padding=\"10\" unsigned=\"true\" nullable=\"false\" comment=\"Entity Id\"/\u003e\n        \u003ccolumn xsi:type=\"int\" name=\"severity\" padding=\"10\" unsigned=\"true\" nullable=\"false\" comment=\"Severity code\"/\u003e\n        \u003ccolumn xsi:type=\"varchar\" name=\"title\" nullable=\"false\" length=\"255\" comment=\"Title\"/\u003e\n        \u003ccolumn xsi:type=\"timestamp\" name=\"time_occurred\" padding=\"10\" comment=\"Time of event\"/\u003e\n-       \u003ccolumn xsi:type=\"timestamp\" name=\"date_closed\" padding=\"10\" comment=\"Time of event\"/\u003e\n        \u003cconstraint xsi:type=\"primary\" referenceId=\"PRIMARY\"\u003e\n            \u003ccolumn name=\"id_column\"/\u003e\n        \u003c/constraint\u003e\n    \u003c/table\u003e\n\u003c/schema\u003e\n```\nIt is possible to drop a column only if it exists in the db_schema_whitelist.json file.\n\n### How to Change the column type?\n\nThe following example changes the type of the title column from varchar to tinytext.\n\n```\n\u003cschema xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n                 xsi:noNamespaceSchemaLocation=\"urn:magento:framework:Setup/Declaration/Schema/etc/schema.xsd\"\u003e\n    \u003ctable name=\"declarative_table\"\u003e\n        \u003ccolumn .../\u003e\n        \u003ccolumn .../\u003e\n-       \u003ccolumn xsi:type=\"varchar\" name=\"title\" nullable=\"false\" length=\"255\" comment=\"Title\"/\u003e\n+       \u003ccolumn xsi:type=\"tinytext\" name=\"title\" nullable=\"false\" length=\"255\" comment=\"Title\"/\u003e\n        \u003ccolumn xsi:type=\"timestamp\" name=\"time_occurred\" padding=\"10\" comment=\"Time of event\"/\u003e\n        \u003cconstraint ...\u003e....\u003c/constraint\u003e\n    \u003c/table\u003e\n\u003c/schema\u003e\n```\n\n###  How to Rename a column?\n\nTo rename a column, delete the original column declaration and create a new one. In the new column declaration, use the onCreate attribute to specify which column to migrate data from. Use the following construction to migrate data from the same table.\n```\nonCreate=\"migrateDataFrom(entity_id)\"\n```\nWhen renaming a column, remember to regenerate the db_schema_whitelist.json file so it contains the new name in addition to the old one.\n\n###  How to Add an index?\n\nThe following example adds the INDEX_SEVERITY index to the declarative_table table.\n\n```\n\u003cschema xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n                 xsi:noNamespaceSchemaLocation=\"urn:magento:framework:Setup/Declaration/Schema/etc/schema.xsd\"\u003e\n    \u003ctable name=\"declarative_table\"\u003e\n        \u003ccolumn .... /\u003e\n        \u003cconstraint ...\u003e \u003ccolumn name=\"id_column\"/\u003e \u003c/constraint\u003e\n\n+       \u003cindex referenceId=\"INDEX_SEVERITY\" indexType=\"btree\"\u003e\n+           \u003ccolumn name=\"severity\"/\u003e\n+       \u003c/index\u003e\n    \u003c/table\u003e\n\u003c/schema\u003e\n```\n\n### How to Create a foreign key?\n\nIn the following example, the selected constraint node defines the characteristics of the FL_ALLOWED_SEVERITIES foreign key.\n\n```\n\u003cschema xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n                 xsi:noNamespaceSchemaLocation=\"urn:magento:framework:Setup/Declaration/Schema/etc/schema.xsd\"\u003e\n    \u003ctable name=\"declarative_table\"\u003e\n        \u003ccolumn ../\u003e ...\n        \u003cconstraint...\u003e ...    \u003c/constraint\u003e\n\n+     \u003cconstraint xsi:type=\"foreign\" referenceId=\"FL_ALLOWED_SEVERITIES\" table=\"declarative_table\"\n+       column=\"severity\" referenceTable=\"severities\" referenceColumn=\"severity_identifier\"\n+       onDelete=\"CASCADE\"/\u003e\n\n    \u003c/table\u003e\n\u003c/schema\u003e\n```\n\n### How to Drop a foreign key?\n\nThe following example removes the FL_ALLOWED_SEVERITIES foreign key by deleting its constraint node. To drop a constraint declared in another module, redeclare it with the disabled attribute set to true.\n\n```\n\u003cschema xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n                 xsi:noNamespaceSchemaLocation=\"urn:magento:framework:Setup/Declaration/Schema/etc/schema.xsd\"\u003e\n    \u003ctable name=\"declarative_table\"\u003e\n      ......\n-  \u003cconstraint xsi:type=\"foreign\" referenceId=\"FL_ALLOWED_SEVERITIES\" table=\"declarative_table\"\n-  column=\"severity\" referenceTable=\"severities\" referenceColumn=\"severity_identifier\"\n-   onDelete=\"CASCADE\"/\u003e\n\n    \u003c/table\u003e\n\u003c/schema\u003e\n```\n\n### How to Recreate a foreign key?\n\nIn this example, Module A defines a new table with primary key id_column. Module B declares its own schema, in which it creates a new column (new_id_column) and changes the primary index to this column. Module B disables the original primary key and sets a new primary key with a referenceId value that is different from PRIMARY. Although this value is different, the real name of the primary key in the database remains PRIMARY.\n\nModule A declaration\n```\n\u003cschema xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n                 xsi:noNamespaceSchemaLocation=\"urn:magento:framework:Setup/Declaration/Schema/etc/schema.xsd\"\u003e\n    \u003ctable name=\"declarative_table\"\u003e\n        \u003ccolumn xsi:type=\"int\" name=\"id_column\" padding=\"10\" unsigned=\"true\" nullable=\"false\" comment=\"Entity Id\"/\u003e\n        \u003cconstraint xsi:type=\"primary\" referenceId=\"PRIMARY\"\u003e\n            \u003ccolumn name=\"id_column\"/\u003e\n        \u003c/constraint\u003e\n    \u003c/table\u003e\n\u003c/schema\u003e\n```\nModule B declaration\n\n```\n\u003cschema xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n        xsi:noNamespaceSchemaLocation=\"urn:magento:framework:Setup/Declaration/Schema/etc/schema.xsd\"\u003e\n    \u003ctable name=\"declarative_table\"\u003e\n        \u003ccolumn xsi:type=\"int\" name=\"new_id_column\" padding=\"10\" unsigned=\"true\" nullable=\"false\"\n                comment=\"New Entity Id\"/\u003e\n        \u003cconstraint xsi:type=\"primary\" referenceId=\"PRIMARY\" disabled=\"true\"/\u003e\n        \u003cconstraint xsi:type=\"primary\" referenceId=\"NEW_PRIMARY\"\u003e\n            \u003ccolumn name=\"new_id_column\"/\u003e\n        \u003c/constraint\u003e\n    \u003c/table\u003e\n\u003c/schema\u003e\n```\n###  What is Data patch?\n\nA class that contains data modification instructions. It can have dependencies on other data or schema patches.\n\n### What is Revertable data patch?\n\n A patch that can be reverted as a module or path is uninstalled or deleted. Revertable operations are Data Query Language (DQL) and Data Manipulation Language (DML) operations: INSERT, UPDATE.\n\n### What is Migration?\n\nA type of non-revertable data patch that can be applied, but not reverted. Any complex operation, such as one that contains an application layer (for example, Collections or Serializers) is non-revertable. SQL delete operations are non-revertable because they can cause triggering.\n\n### What is Schema patch \u0026  allow  operations?\n\n A class that contains custom schema modification instructions. Schema patches are used along with declarative schema, but these patches allow complex operations such as:\n\n- Adding triggers, stored procedures, functions\n\n- Performing data migration with inside DDL operations\n\n- Renaming tables, columns, and other entities\n\n- Adding partitions and options to a table\n\n\n# Magento 2 Certified Associate Developer Exam\n\n\n(4.4 Demonstrate an ability to use declarative schema)\n\n## How do you add a column using declarative schema?\n\nThe following example adds the date_closed column.\n```\n\u003cschema xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n                 xsi:noNamespaceSchemaLocation=\"urn:magento:framework:Setup/Declaration/Schema/etc/schema.xsd\"\u003e\n    \u003ctable name=\"declarative_table\"\u003e\n        \u003ccolumn .../\u003e ...\n\n+       \u003ccolumn xsi:type=\"timestamp\" name=\"date_closed\" padding=\"10\" comment=\"Time of event\"/\u003e\n\n        \u003cconstraint ..\u003e...\u003c/constraint\u003e\n    \u003c/table\u003e\n\u003c/schema\u003e\n```\nWhen adding a new column into table, remember to generate the db_schema_whitelist.json file.\n\n### How do you modify a table added by another module?\n\nSuppose we want add new column name=\"referred_by\" on core table customer_entity\netc/db_schema.xml\n```\n--\u003c?xml version=\"1.0\"?\u003e\n--\u003cschema xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\"urn:magento:framework:Setup/Declaration/Schema/etc/schema.xsd\"\u003e\n--This below content oly if exist declaration\n    \u003ctable name=\"customer_entity\"\u003e\n    \u003ccolumn xsi:type=\"int\" name=\"referred_by\" padding=\"10\" unsigned=\"true\" nullable=\"false\"\n    comment=\"Referred By\"/\u003e\n\n    \u003c/table\u003e\n\n--\u003c/schema\u003e\n```\nThen run following command to generate db_schema_whitelist.json\n\n```\nphp bin/magento setup:db-declaration:generate-whitelist --module-name=BDC_Declarative\nphp bin/magento setup:upgrade --dry-run=1 --keep-generated\nphp bin/magento setup:upgrade\n```\n![](docs/b4addcolumn.png)\n![](docs/dryRun.png)\n![](docs/afterAddcolumn.png)\n\n You can modify code according to your requirement.\n\n### How do you delete a column?\n\nThe following example removes the date_closed column by deleting its column node. To drop a column declared in another module, redeclare it with the disabled attribute set to true.\n```\n\u003cschema xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n                 xsi:noNamespaceSchemaLocation=\"urn:magento:framework:Setup/Declaration/Schema/etc/schema.xsd\"\u003e\n    \u003ctable name=\"declarative_table\"\u003e\n        \u003ccolumn xsi:type=\"int\" name=\"id_column\" padding=\"10\" unsigned=\"true\" nullable=\"false\" comment=\"Entity Id\"/\u003e\n        \u003ccolumn xsi:type=\"int\" name=\"severity\" padding=\"10\" unsigned=\"true\" nullable=\"false\" comment=\"Severity code\"/\u003e\n        \u003ccolumn xsi:type=\"varchar\" name=\"title\" nullable=\"false\" length=\"255\" comment=\"Title\"/\u003e\n        \u003ccolumn xsi:type=\"timestamp\" name=\"time_occurred\" padding=\"10\" comment=\"Time of event\"/\u003e\n-       \u003ccolumn xsi:type=\"timestamp\" name=\"date_closed\" padding=\"10\" comment=\"Time of event\"/\u003e\n        \u003cconstraint xsi:type=\"primary\" referenceId=\"PRIMARY\"\u003e\n            \u003ccolumn name=\"id_column\"/\u003e\n        \u003c/constraint\u003e\n    \u003c/table\u003e\n\u003c/schema\u003e\n```\nIt is possible to drop a column only if it exists in the db_schema_whitelist.json file.\n\n\n### \"Declarative Schema\" vs \"Extension Attributes\" ?\n\n\"Declarative Schema\" is used when you need to create a NEW table inside Magento. With declarative schema you have the advantages of mutations.\n\"Extension Attributes\" are used to add new fields inside an EXISTING table. In this way you don't extend the original model.So in the above example, the best approach is to use Extension Attributes. magento-2-what-are-extension-attributes\n\n\n### How do you add an index ?\n\nAdd an index: The following example adds the INDEX_SEVERITY index to the declarative_table table.\n```\n\u003cschema xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n                 xsi:noNamespaceSchemaLocation=\"urn:magento:framework:Setup/Declaration/Schema/etc/schema.xsd\"\u003e\n    \u003ctable name=\"declarative_table\"\u003e\n        \u003ccolumn xsi:type=\"int\" name=\"id_column\" padding=\"10\" unsigned=\"true\" nullable=\"false\" comment=\"Entity Id\"/\u003e\n        \u003ccolumn xsi:type=\"int\" name=\"severity\" padding=\"10\" unsigned=\"true\" nullable=\"false\" comment=\"Severity code\"/\u003e\n        \u003ccolumn xsi:type=\"tinytext\" name=\"title\" nullable=\"false\" length=\"255\" comment=\"Title\"/\u003e\n        \u003ccolumn xsi:type=\"timestamp\" name=\"time_occurred\" padding=\"10\" comment=\"Time of event\"/\u003e\n        \u003cconstraint xsi:type=\"primary\" referenceId=\"PRIMARY\"\u003e\n            \u003ccolumn name=\"id_column\"/\u003e\n        \u003c/constraint\u003e\n+       \u003cindex referenceId=\"INDEX_SEVERITY\" indexType=\"btree\"\u003e\n+           \u003ccolumn name=\"severity\"/\u003e\n+       \u003c/index\u003e\n    \u003c/table\u003e\n\u003c/schema\u003e\n```\n\n\n### How do You add  foreign key using declarative schema?\n\n- Create a foreign key\nIn the following example, the selected constraint node defines the characteristics of the FL_ALLOWED_SEVERITIES foreign key.\n\n```\n\u003cschema xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n                 xsi:noNamespaceSchemaLocation=\"urn:magento:framework:Setup/Declaration/Schema/etc/schema.xsd\"\u003e\n    \u003ctable name=\"declarative_table\"\u003e\n        \u003ccolumn xsi:type=\"int\" name=\"id_column\" padding=\"10\" unsigned=\"true\" nullable=\"false\" comment=\"Entity Id\"/\u003e\n        \u003ccolumn xsi:type=\"int\" name=\"severity\" padding=\"10\" unsigned=\"true\" nullable=\"false\" comment=\"Severity code\"/\u003e\n        \u003ccolumn xsi:type=\"varchar\" name=\"title\" nullable=\"false\" length=\"255\" comment=\"Title\"/\u003e\n        \u003ccolumn xsi:type=\"timestamp\" name=\"time_occurred\" padding=\"10\" comment=\"Time of event\"/\u003e\n        \u003cconstraint xsi:type=\"primary\" referenceId=\"PRIMARY\"\u003e\n            \u003ccolumn name=\"id_column\"/\u003e\n        \u003c/constraint\u003e\n+       \u003cconstraint xsi:type=\"foreign\" referenceId=\"FL_ALLOWED_SEVERITIES\" table=\"declarative_table\"\n+               column=\"severity\" referenceTable=\"severities\" referenceColumn=\"severity_identifier\"\n+               onDelete=\"CASCADE\"/\u003e\n    \u003c/table\u003e\n\u003c/schema\u003e\n```\n- Drop a foreign key\nThe following example removes the FL_ALLOWED_SEVERITIES foreign key by deleting its constraint node. To drop a constraint declared in another module, redeclare it with the disabled attribute set to true.\n\n```\n\u003cschema xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n                 xsi:noNamespaceSchemaLocation=\"urn:magento:framework:Setup/Declaration/Schema/etc/schema.xsd\"\u003e\n    \u003ctable name=\"declarative_table\"\u003e\n        \u003ccolumn xsi:type=\"int\" name=\"id_column\" padding=\"10\" unsigned=\"true\" nullable=\"false\" comment=\"Entity Id\"/\u003e\n        \u003ccolumn xsi:type=\"int\" name=\"severity\" padding=\"10\" unsigned=\"true\" nullable=\"false\" comment=\"Severity code\"/\u003e\n        \u003ccolumn xsi:type=\"varchar\" name=\"title\" nullable=\"false\" length=\"255\" comment=\"Title\"/\u003e\n        \u003ccolumn xsi:type=\"timestamp\" name=\"time_occurred\" padding=\"10\" comment=\"Time of event\"/\u003e\n        \u003cconstraint xsi:type=\"primary\" referenceId=\"PRIMARY\"\u003e\n            \u003ccolumn name=\"id_column\"/\u003e\n        \u003c/constraint\u003e\n-       \u003cconstraint xsi:type=\"foreign\" referenceId=\"FL_ALLOWED_SEVERITIES\" table=\"declarative_table\"\n-               column=\"severity\" referenceTable=\"severities\" referenceColumn=\"severity_identifier\"\n-               onDelete=\"CASCADE\"/\u003e\n    \u003c/table\u003e\n\u003c/schema\u003e\n```\nIt is possible to drop a foreign key only if it exists in the db_schema_whitelist.json file.\n\n- Recreate a foreign key\nIn this example, Module A defines a new table with primary key id_column. Module B declares its own schema, in which it creates a new column (new_id_column) and changes the primary index to this column. Module B disables the original primary key and sets a new primary key with a referenceId value that is different from PRIMARY. Although this value is different, the real name of the primary key in the database remains PRIMARY.\n\nModule A declaration\n```\n\u003cschema xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n                 xsi:noNamespaceSchemaLocation=\"urn:magento:framework:Setup/Declaration/Schema/etc/schema.xsd\"\u003e\n    \u003ctable name=\"declarative_table\"\u003e\n        \u003ccolumn xsi:type=\"int\" name=\"id_column\" padding=\"10\" unsigned=\"true\" nullable=\"false\" comment=\"Entity Id\"/\u003e\n        \u003cconstraint xsi:type=\"primary\" referenceId=\"PRIMARY\"\u003e\n            \u003ccolumn name=\"id_column\"/\u003e\n        \u003c/constraint\u003e\n    \u003c/table\u003e\n\u003c/schema\u003e\n```\nModule B declaration\n```\n\u003cschema xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n        xsi:noNamespaceSchemaLocation=\"urn:magento:framework:Setup/Declaration/Schema/etc/schema.xsd\"\u003e\n    \u003ctable name=\"declarative_table\"\u003e\n        \u003ccolumn xsi:type=\"int\" name=\"new_id_column\" padding=\"10\" unsigned=\"true\" nullable=\"false\"\n                comment=\"New Entity Id\"/\u003e\n        \u003cconstraint xsi:type=\"primary\" referenceId=\"PRIMARY\" disabled=\"true\"/\u003e\n        \u003cconstraint xsi:type=\"primary\" referenceId=\"NEW_PRIMARY\"\u003e\n            \u003ccolumn name=\"new_id_column\"/\u003e\n        \u003c/constraint\u003e\n    \u003c/table\u003e\n\u003c/schema\u003e\n```\n\n### How do you manipulate data using data patches?\n\nA data patch is a class that contains data modification instructions. It is defined in a \u003cVendor\u003e/\u003cModule_Name\u003e/Setup/Patch/Data/\u003cPatch_Name\u003e.php file and implements \\Magento\\Framework\\Setup\\Patch\\DataPatchInterface.\n\n\n### What is the purpose of schema patches?\n\nA schema patch contains custom schema modification instructions. These modifications can be complex. It is defined in a \u003cVendor\u003e/\u003cModule_Name\u003e/Setup/Patch/Schema/\u003cPatch_Name\u003e.php file and implements \\Magento\\Framework\\Setup\\Patch\\SchemaPatchInterface.\n\nUnlike the declarative schema approach, patches will only be applied once. A list of applied patches is stored in the patch_list database table. An unapplied patch will be applied when running the setup:upgrade from the Magento CLI.\n\n## Magento 2 Certified Professional Developer exam\n\n### 4.4 Demonstrate an ability to use declarative schema\n- How do you add a column using declarative schema?\n- How do you modify a table added by another module? ||\n- How do you delete a column?  ||\n- How do you add an index or foreign key using declarative schema? ||\n- How do you manipulate data using data patches?  \n- What is the purpose of schema patches?\n\n### How to manipulate columns and keys using declarative schema?  \n\n- Add a column to table\nThe following example adds the date_closed column.\n![](https://magento-devdocs.github.io/devdocs-for-tests/guides/v2.3/extension-dev-guide/declarative-schema/images/add-column.png)\n\n- Drop a column from a table\nThe following example removes the date_closed column by deleting its column node.\nTo drop a column declared in another module, redeclare it with the disabled attribute set to true.\n![](https://magento-devdocs.github.io/devdocs-for-tests/guides/v2.3/extension-dev-guide/declarative-schema/images/remove-column.png)\n\n- Change the column type\nThe following example changes the type of the title column from varchar to tinytext.\n![](https://magento-devdocs.github.io/devdocs-for-tests/guides/v2.3/extension-dev-guide/declarative-schema/images/change-column-type.png)\n\n- Rename a column\nTo rename a column, delete the original column declaration and create a new one.\nIn the new declaration, use the onCreate attribute to specify which column to migrate data from.\nUse the following construction to migrate data from the same table.\nonCreate=\"migrateDataFrom(entity_id)\"\n\nTo migrate data from another table, specify a value similar to the following:\nonCreate=\"migrateDataFromAnotherTable(catalog_category_entity,entity_id)\"\n - Add an index\nThe following example adds the INDEX_SEVERITY index to the table_name table.\n\n![](https://magento-devdocs.github.io/devdocs-for-tests/guides/v2.3/extension-dev-guide/declarative-schema/images/add-index.png)\n- Create a foreign key\nIn the following example, the selected constraint node defines the characteristics of the FL_ALLOWED_SEVERITIES foreign key.\n\n![](https://magento-devdocs.github.io/devdocs-for-tests/guides/v2.3/extension-dev-guide/declarative-schema/images/create-fk.png)\n\n- Drop a foreign key\nThe following example removes the FL_ALLOWED_SEVERITIES foreign key by deleting its constraint node.\nTo drop a constraint declared in another module, redeclare it with the disabled attribute set to true.\n![](https://magento-devdocs.github.io/devdocs-for-tests/guides/v2.3/extension-dev-guide/declarative-schema/images/drop-fk.png)\n\n### What is the purpose of whitelisting?\n\nPurpose of whitelisting:\nYou will not be able to run a declarative mode without creating a schema whitelist.\nSince backward compatibility must be maintained, declarative schema doesn’t automatically delete database tables, columns or keys not defined in db_schema.xml.This is one of the reasons we have db_schema_whitelist.json. It shows a history of all tables, columns and keys added with declarative schema and it’s required for drop operations.  \n\nNote: it is recommended to generate a new whitelist for every release for the double-check purposes. Before running the upgrade command you need to add your schema to db_whitelist_schema.json file by running the following command. For that, you need a //etc/db_schema_whitelist.json file that will store all the content added with declarative schema. To generate this file, run:\n\n![db_schema](https://github.com/bdcrops/BDC_Declarative/blob/master/view/adminhtml/web/images/whitelist.png)\n\n```\nphp bin/magento setup:db-declaration:generate-whitelist [options]\nphp bin/magento setup:db-declaration:generate-whitelist --module-name=vendor_module\nphp bin/magento setup:db-declaration:generate-whitelist --module-name=BDC_Declarative\nphp bin/magento setup:upgrade --dry-run=1 --keep-generated\n```\n\nNow, there are db_whitelist_schema.json file will be create in /vendor/module/etc folder.\n\nThere are options you can add at the end of that command. For instance,  you can use “–module-name=YourModule” to specify the module you want to generate a whitelist for. Similarly, you could also set “–module-name=all” although it will generate a whitelist for all modules by default.  \n\n\n###  How to use Data and Schema patches?\n\n- Data Patches:\n![](https://github.com/bdcrops/module-declarative/raw/master/docs/magento-23-schema-and-data-patches-23-638.jpg)\n![](https://github.com/bdcrops/module-declarative/raw/master/docs/magento-23-schema-and-data-patches-25-638.jpg)\n\n\n- Schema Patches :\n![](https://github.com/bdcrops/module-declarative/raw/master/docs/magento-23-schema-and-data-patches-6-638.jpg)\n\n\n### How to manage dependencies between patch files?\n\nschema patch contains custom schema modification instructions. These modifications can be complex. It is defined in a \u003cVendor\u003e/\u003cModule_Name\u003e/Setup/Patch/Schema/\u003cPatch_Name\u003e.php file and implements \\Magento\\Framework\\Setup\\Patch\\SchemaPatchInterface.\n\nUnlike the declarative schema approach, patches will only be applied once. A list of applied patches is stored in the patch_list database table. An unapplied patch will be applied when running the setup:upgrade from the Magento CLI.\n\nOptionally, if you plan to enable rollback for your patch during module uninstallation, then you must implement \\Magento\\Framework\\Setup\\Patch\\PatchRevertableInterface.\n\nOld scripts will work with new versions of Magento. However, if you want to convert your old scripts to the new format, implement \\Magento\\Framework\\Setup\\Patch\\PatchVersionInterface. This interface allows you to specify the setup version of the module in your database. If the version of the module is higher than or equal to the version specified in your patch, then the patch is skipped. If the version in the database is lower, then the patch installs\n\n### How to EAV attributes Create via Declarative Schema ?\n\n\nIn Magento 2.3 and above, you can create attribute via DataPatch.  For example, create a file: Vendor_Module\\Setup\\Patch\\Data\\ApplyNewAttribute.php\n\n```\n\u003c?php\nnamespace Vendor\\Module\\Setup\\Patch\\Data;\nuse Magento\\Framework\\Setup\\ModuleDataSetupInterface;\nuse Magento\\Framework\\Setup\\Patch\\DataPatchInterface;\nuse Magento\\Framework\\Setup\\Patch\\PatchVersionInterface;\nuse Magento\\Eav\\Setup\\EavSetup;\nuse Magento\\Eav\\Setup\\EavSetupFactory;\n/**\n * Class ApplyNewAttribute\n * @package Vendor\\Module\\Setup\\Patch\\Data\n */\nclass ApplyNewAttribute implements DataPatchInterface, PatchVersionInterface\n{\n    /**\n     * @var ModuleDataSetupInterface\n     */\n    private $moduleDataSetup;\n    /**\n     * @var EavSetupFactory\n     */\n    private $eavSetupFactory;\n    /**\n     * ApplyNewAttribute constructor.\n     *\n     * @param ModuleDataSetupInterface $moduleDataSetup\n     * @param EavSetupFactory $eavSetupFactory\n     */\n    public function __construct(\n        ModuleDataSetupInterface $moduleDataSetup,\n        EavSetupFactory $eavSetupFactory\n    ) {\n        $this-\u003emoduleDataSetup = $moduleDataSetup;\n        $this-\u003eeavSetupFactory = $eavSetupFactory;\n    }\n    /**\n     * {@inheritdoc}\n     * @SuppressWarnings(PHPMD.ExcessiveMethodLength)\n     */\n    public function apply()\n    {\n        /** @var EavSetup $eavSetup */\n        $eavSetup = $this-\u003eeavSetupFactory-\u003ecreate(['setup' =\u003e $this-\u003emoduleDataSetup]);\n        $eavSetup-\u003eaddAttribute(\n            \\Magento\\Catalog\\Model\\Product::ENTITY,\n            /** Add your attribute here.……….*/\n       );\n        $setup-\u003eendSetup();\n    }\n}\n```\n[bsscommerce](https://bsscommerce.com/confluence/magento-2-eav-model-things-you-may-not-know/)\n\n## Ref\n\n- [devdocs](https://devdocs.magento.com/guides/v2.3/extension-dev-guide/declarative-schema/)\n\n- [onilabblog](https://onilab.com/blog/declarative-schema-magento-2-3-and-higher/)\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbdcrops%2Fmodule-declarative","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbdcrops%2Fmodule-declarative","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbdcrops%2Fmodule-declarative/lists"}