2

I have doubts about the database schema and schema patch so can anyone help me to understand the difference between

1
  • I also want to understand the difference, as we can use both approaches to create/update tables schema.
    – Knight017
    Oct 31, 2022 at 5:17

1 Answer 1

0

DB SChema

DB Schema is also known as declarative schema.

Before Adobe Commerce and Magento Open Source 2.3, extension developers were required to write code (PHP scripts) to change the database schema. The following types of scripts existed before 2.3:

InstallData and InstallSchema scripts, which are executed the first time a module is installed. UpgradeData and UpgradeSchema incremental scripts, which supplement an existing module schema. Recurring scripts, which are executed each time you install or upgrade Magento. Each script iteratively adds changes. During the installation process, upgrade scripts apply only those changes that have not been applied yet. For example, if you have a module with version 2.1.8 installed and the latest version is 2.1.11, then the upgrade script changes for 2.1.9, 2.1.10, and 2.1.11 will be applied, in order, when you upgrade to 2.1.11. Each upgrade script is responsible for checking the required version for each change to apply. The Adobe Commerce or Magento Open Source installation only knows that a module has an upgrade script, not what versions it affected. That procedure is called migration setup or migration scripts.

The main disadvantage of this approach is that the application applies changes blindly. For example, in one version a new database column might be introduced, only to be removed in the next. Declarative setup eliminates this type of unnecessary work.

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

Schema Patches

A schema patch contains custom schema modification instructions. These modifications can be complex. It is defined in a /<Module_Name>/Setup/Patch/Schema/<Patch_Name>.php file and implements \Magento\Framework\Setup\Patch\SchemaPatchInterface.

Unlike 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.

Optionally, if you plan to enable rollback for your patch during module uninstallation, then you must implement \Magento\Framework\Setup\Patch\PatchRevertableInterface.

The declarative schema approach removes the version from the setup_module table (in a backward-compatible way), leaving only the Composer version. Therefore, you can create all new patches and modules without specifying a setup_module version.

The sequence of installing patches is handled through a dependency-based approach. Patches can either be independent or dependent on other patches. Independent patches can be installed in any sequence. A dependent patch requires a minimal number of patches so that it can be installed successfully.

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

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.