© 2019 Magento, Inc. Page | 1
Magento 2.3
Schema and Data
Patches
© 2019 Magento, Inc.
.
Page | 2
Technical Architect at BORN Group
Atish Goswami
© 2019 Magento, Inc. Page | 3
Agenda
© 2019 Magento, Inc. Page | 4
Agenda
• Schema Patches
• Data Patches
• Version Patches
• Revertible Patches
• New CLI Commands
© 2019 Magento, Inc. Page | 5
What is Schema Patch?
© 2019 Magento, Inc. Page | 6
Schema Patches
• A class that contains custom schema modification instructions
• Schema patches are used along with declarative schema
• Schema patches allow complex operations
– Adding triggers, stored procedures, functions
– Performing data migration with inside DDL operations
– Renaming tables, columns, and other entities
– Adding partitions and options to a table
© 2019 Magento, Inc. Page | 7
Where do we define this
file ?
© 2019 Magento, Inc. Page | 8
<module_root>/Setup/Patch/Schema/<Patch_Name>.php
© 2019 Magento, Inc. Page | 9
MagentoFrameworkSetupPatchSchemaPatchInterface
© 2019 Magento, Inc. Page | 10
Methods we will implement
© 2019 Magento, Inc. Page | 11
MagentoFrameworkSetupPatchPatchInterface::getAliases()
• Patch class names can change over time
• But changing name should not affect installation process
• If we will change name of the patch we will add alias here
• This method returns an array
© 2019 Magento, Inc. Page | 12
MagentoFrameworkSetupPatchPatchInterface::getAliases()
© 2019 Magento, Inc. Page | 13
MagentoFrameworkSetupPatchPatchInterface::apply()
• The code to apply the patch is written inside here
• One patch is responsible only for one setup version
© 2019 Magento, Inc. Page | 14
MagentoFrameworkSetupPatchPatchInterface::apply()
© 2019 Magento, Inc. Page | 15
MagentoFrameworkSetupPatchDependentPatchInterface::getDependencies()
• Patches can be dependent on other patches
• Other patches needs to be applied before your patch is applied
• One patch can have can have many dependences
• If the patch is dependent on some other modules patch use
sequencing in your module.xml
• It is recommended to declare dependencies of patches present in
the same module
© 2019 Magento, Inc. Page | 16
MagentoFrameworkSetupPatchDependentPatchInterface::getDependencies()
© 2019 Magento, Inc. Page | 17
Applying the Schema
Patch
© 2019 Magento, Inc. Page | 18
Applying the Schema Patch
© 2019 Magento, Inc. Page | 19
What happened behind
the scenes
© 2019 Magento, Inc. Page | 20
What happens behind the scenes
• Magento runs the schema patch
• If it is successfully executed the class name is stored in the patch_list
table
• Next time when the setup upgrade runs it refers the patch_list table
and if the patch class if already applied it is not executed again
© 2019 Magento, Inc. Page | 21
Patch List Storage
© 2019 Magento, Inc. Page | 22
What is Data Patch?
© 2019 Magento, Inc. Page | 23
Data Patches
• A class that contains data modification instructions
• Majorly will be used to create tables records, EAV attributes
© 2019 Magento, Inc. Page | 24
Where do we define this
file ?
© 2019 Magento, Inc. Page | 25
<module_root>/Setup/Patch/Data/<Patch_Name>.php
© 2019 Magento, Inc. Page | 26
MagentoFrameworkSetupPatchDataPatchInterface
© 2019 Magento, Inc. Page | 27
Methods we will implement
© 2019 Magento, Inc. Page | 28
MagentoFrameworkSetupPatchPatchInterface::apply()
© 2019 Magento, Inc. Page | 29
What is Version Patch?
© 2019 Magento, Inc. Page | 30
Versions Patches
• It is used for backwards compatibility with older setup scripts
• A version number can be defined
• Magento checks if setup module version
• If the version of the module is higher than the version specified in
your patch, then the patch is skipped
• If the version in the database is equal or lower, then the patch
installs.
• It is marked as deprecated
© 2019 Magento, Inc. Page | 31
How to convert a
schema/patch into a
version patch ?
© 2019 Magento, Inc. Page | 32
MagentoFrameworkSetupPatchPatchVersionInterface
© 2019 Magento, Inc. Page | 33
What is Revertable
Patch?
© 2019 Magento, Inc. Page | 34
Revertable Patches
• Patches can be reverted at the time of removing the module
• Single patches cannot be reverted
• Module uninstall needed to be triggered
• This feature is still buggy and changes are happening
© 2019 Magento, Inc. Page | 35
How to convert a
schema/patch into a
revertable patch ?
© 2019 Magento, Inc. Page | 36
MagentoFrameworkSetupPatchPatchRevertableInterface
© 2019 Magento, Inc. Page | 37
How to uninstall a
module ?
© 2019 Magento, Inc. Page | 38
Uninstall a composer module
Uninstall a non-composer module
© 2019 Magento, Inc. Page | 39
New CLI Command
© 2019 Magento, Inc. Page | 40
Generating Patches
© 2019 Magento, Inc. Page | 41
Generating Patches
© 2019 Magento, Inc. Page | 42
Questions ?
© 2019 Magento, Inc. Page | 43
Thank You

Magento 2.3 Schema and Data Patches

  • 1.
    © 2019 Magento,Inc. Page | 1 Magento 2.3 Schema and Data Patches
  • 2.
    © 2019 Magento,Inc. . Page | 2 Technical Architect at BORN Group Atish Goswami
  • 3.
    © 2019 Magento,Inc. Page | 3 Agenda
  • 4.
    © 2019 Magento,Inc. Page | 4 Agenda • Schema Patches • Data Patches • Version Patches • Revertible Patches • New CLI Commands
  • 5.
    © 2019 Magento,Inc. Page | 5 What is Schema Patch?
  • 6.
    © 2019 Magento,Inc. Page | 6 Schema Patches • A class that contains custom schema modification instructions • Schema patches are used along with declarative schema • Schema patches allow complex operations – Adding triggers, stored procedures, functions – Performing data migration with inside DDL operations – Renaming tables, columns, and other entities – Adding partitions and options to a table
  • 7.
    © 2019 Magento,Inc. Page | 7 Where do we define this file ?
  • 8.
    © 2019 Magento,Inc. Page | 8 <module_root>/Setup/Patch/Schema/<Patch_Name>.php
  • 9.
    © 2019 Magento,Inc. Page | 9 MagentoFrameworkSetupPatchSchemaPatchInterface
  • 10.
    © 2019 Magento,Inc. Page | 10 Methods we will implement
  • 11.
    © 2019 Magento,Inc. Page | 11 MagentoFrameworkSetupPatchPatchInterface::getAliases() • Patch class names can change over time • But changing name should not affect installation process • If we will change name of the patch we will add alias here • This method returns an array
  • 12.
    © 2019 Magento,Inc. Page | 12 MagentoFrameworkSetupPatchPatchInterface::getAliases()
  • 13.
    © 2019 Magento,Inc. Page | 13 MagentoFrameworkSetupPatchPatchInterface::apply() • The code to apply the patch is written inside here • One patch is responsible only for one setup version
  • 14.
    © 2019 Magento,Inc. Page | 14 MagentoFrameworkSetupPatchPatchInterface::apply()
  • 15.
    © 2019 Magento,Inc. Page | 15 MagentoFrameworkSetupPatchDependentPatchInterface::getDependencies() • Patches can be dependent on other patches • Other patches needs to be applied before your patch is applied • One patch can have can have many dependences • If the patch is dependent on some other modules patch use sequencing in your module.xml • It is recommended to declare dependencies of patches present in the same module
  • 16.
    © 2019 Magento,Inc. Page | 16 MagentoFrameworkSetupPatchDependentPatchInterface::getDependencies()
  • 17.
    © 2019 Magento,Inc. Page | 17 Applying the Schema Patch
  • 18.
    © 2019 Magento,Inc. Page | 18 Applying the Schema Patch
  • 19.
    © 2019 Magento,Inc. Page | 19 What happened behind the scenes
  • 20.
    © 2019 Magento,Inc. Page | 20 What happens behind the scenes • Magento runs the schema patch • If it is successfully executed the class name is stored in the patch_list table • Next time when the setup upgrade runs it refers the patch_list table and if the patch class if already applied it is not executed again
  • 21.
    © 2019 Magento,Inc. Page | 21 Patch List Storage
  • 22.
    © 2019 Magento,Inc. Page | 22 What is Data Patch?
  • 23.
    © 2019 Magento,Inc. Page | 23 Data Patches • A class that contains data modification instructions • Majorly will be used to create tables records, EAV attributes
  • 24.
    © 2019 Magento,Inc. Page | 24 Where do we define this file ?
  • 25.
    © 2019 Magento,Inc. Page | 25 <module_root>/Setup/Patch/Data/<Patch_Name>.php
  • 26.
    © 2019 Magento,Inc. Page | 26 MagentoFrameworkSetupPatchDataPatchInterface
  • 27.
    © 2019 Magento,Inc. Page | 27 Methods we will implement
  • 28.
    © 2019 Magento,Inc. Page | 28 MagentoFrameworkSetupPatchPatchInterface::apply()
  • 29.
    © 2019 Magento,Inc. Page | 29 What is Version Patch?
  • 30.
    © 2019 Magento,Inc. Page | 30 Versions Patches • It is used for backwards compatibility with older setup scripts • A version number can be defined • Magento checks if setup module version • If the version of the module is higher than the version specified in your patch, then the patch is skipped • If the version in the database is equal or lower, then the patch installs. • It is marked as deprecated
  • 31.
    © 2019 Magento,Inc. Page | 31 How to convert a schema/patch into a version patch ?
  • 32.
    © 2019 Magento,Inc. Page | 32 MagentoFrameworkSetupPatchPatchVersionInterface
  • 33.
    © 2019 Magento,Inc. Page | 33 What is Revertable Patch?
  • 34.
    © 2019 Magento,Inc. Page | 34 Revertable Patches • Patches can be reverted at the time of removing the module • Single patches cannot be reverted • Module uninstall needed to be triggered • This feature is still buggy and changes are happening
  • 35.
    © 2019 Magento,Inc. Page | 35 How to convert a schema/patch into a revertable patch ?
  • 36.
    © 2019 Magento,Inc. Page | 36 MagentoFrameworkSetupPatchPatchRevertableInterface
  • 37.
    © 2019 Magento,Inc. Page | 37 How to uninstall a module ?
  • 38.
    © 2019 Magento,Inc. Page | 38 Uninstall a composer module Uninstall a non-composer module
  • 39.
    © 2019 Magento,Inc. Page | 39 New CLI Command
  • 40.
    © 2019 Magento,Inc. Page | 40 Generating Patches
  • 41.
    © 2019 Magento,Inc. Page | 41 Generating Patches
  • 42.
    © 2019 Magento,Inc. Page | 42 Questions ?
  • 43.
    © 2019 Magento,Inc. Page | 43 Thank You