Contenu connexe
Similaire à Magento 2 Declarative Schema (20)
Magento 2 Declarative Schema
- 2. © 2019 Magento, Inc.
.
Page | 2
Lead Developer at BORN Group
Atish Goswami
- 4. © 2019 Magento, Inc. Page | 4
Agenda
• What is Declarative Schema ?
• Declaring table schemes
• Updating existing table schemes
• New CLI commands and arguments
• Schema Rollback
- 6. © 2019 Magento, Inc. Page | 6
Not in Scope
• Applying Data Patches
• Patch Rollbacks
• Schema Patches (Trigger/Stored Procedures)
- 8. © 2019 Magento, Inc. Page | 8
Declarative Schema
• Newer way of handling Setup Script Operations
• Setup Scripts are still supported in 2.3.x versions (EOL unknown)
• Might be Backported to 2.2.x newer versions
• Declarative Schema and Setup Scripts don’t work together
• If developing backwards compatible modules use Setup Script
- 10. © 2019 Magento, Inc. Page | 10
Issues with Setup Scripts
• Setup Scripts get complicated to manage overtime
• Module sequencing needs to be handled properly
• Confusing to manage Install vs Upgrade scripts
• Version number mishap happens
• No Rollbacks
- 12. © 2019 Magento, Inc. Page | 12
Declarative Schema Offers
• Avoidance of missed/repeated SQL operations
• Installation testing using dry-run mode
• Performance optimization
• Support for rollbacks *
- 15. © 2019 Magento, Inc. Page | 15
<module_root>/etc/module.xml
Note : Declarative Schema does not require version number
- 16. © 2019 Magento, Inc. Page | 16
<module_root>/etc/db_schema.xml
New xml file introduced by Declarative Schema
- 19. © 2019 Magento, Inc. Page | 19
<table/> Attributes
• name
– Name of the table
– Required
• resource (default)
– Database connection to use for the operation
– Allowed values – default, sales, checkout
• engine (innodb)
– MySQL Table engine
– Allowed values – innodb, memory
- 20. © 2019 Magento, Inc. Page | 20
<table/> Attributes (Contd)
• comment (not comments added)
– Comment Relating the table
• charset (utf8)
– Charset for the table
• collation (utf8_general_ci)
– Collation to match the charset
• onCreate (empty)
– Helps trigger a task after the table is created
- 23. © 2019 Magento, Inc. Page | 23
<column/> Integer Attributes
• name
– Name of the column
– Required
• xsi:type
– Allowed Values – (tinyint, int, smallint, bigint, boolean)
– Required
• default (NULL)
– Provide default column value
– Can provide only value as integer
• padding (tinyint[2], int[11], smallint[5], bigint[20], boolean[1])
– Padding values for the integer type columns
– Allowed Values (2 - 1024)
- 24. © 2019 Magento, Inc. Page | 24
<column/> Integer Attributes
• unsigned (false)
– If column contains negative values need to be true
• identity (false)
– If true column will be auto increment
– The column need to be declared in primary index as well
• nullable (true)
– If column will have NULL values
• comment (no comments are added)
– Comment for the column
• onCreate (empty)
– Helps trigger a task after the column is created
- 27. © 2019 Magento, Inc. Page | 27
<column/> Text Attributes
• name
– Name of the Column
– Required
• xsi:type
– Allowed Values – (varchar, text, mediumtext, longtext)
– Required
• default
– Provide default column value
– Can provided only for varchar
• length
– Length of the field
– varchar allowed max length is 1024
– text mediumtext longtext length can’t be defined
- 28. © 2019 Magento, Inc. Page | 28
<column/> Text Attributes
• comment (no comments are added)
– Comment for the column
• nullable (true)
– If column will have NULL values
• onCreate (empty)
– Helps trigger a task after the column is created
- 31. © 2019 Magento, Inc. Page | 31
<column/> Text Attributes
• xsi:type
– Allowed Values – (varbinary, blob, mediumblob, longblob)
– Required
• default
– Provide default column value
– Can provided only for varbinary
• length
– Length of the field
– varbinary allowed max length is 255
– blob mediumblob longblob length can’t be defined
• comment (no comments are added)
– Comment for the column
- 32. © 2019 Magento, Inc. Page | 32
<column/> Text Attributes
• nullable (true)
– If column will have NULL values
• onCreate (empty)
– Helps trigger a task after the column is created
- 35. © 2019 Magento, Inc. Page | 35
<column/> Text Attributes
• xsi:type
– Allowed Values – (decimal, float, double)
– Required
• default
– Provide default column value
• precision
– Total digits of the decimal numbers
• scale
– Total digits after the decimal point
• comment (no comments are added)
– Comment for the column
- 36. © 2019 Magento, Inc. Page | 36
<column/> Text Attributes
• nullable (true)
– If column will have NULL values
• unsigned (false)
– If column contains negative values need to be true
• onCreate (empty)
– Helps trigger a task after the column is created
- 39. © 2019 Magento, Inc. Page | 39
<column/> Text Attributes
• name
– Name of the column
– Required
• xsi:type
– Allowed Values – (timestamp, datetime, date)
– Required
• default
– Provide default column value
– Allowed Values – (CURRENT_TIMESTAMP, 0, NULL)
• comment (no comments are added)
– Comment for the column
• on_update (false)
– MySQL on update will be implemented
- 40. © 2019 Magento, Inc. Page | 40
<column/> Text Attributes
• nullable (true)
– If column will have NULL values
• onCreate (empty)
– Helps trigger a task after the column is created
- 49. © 2019 Magento, Inc. Page | 49
<index/> Attributes
• indexType
– fulltext
– hash
– btree
- 53. © 2019 Magento, Inc. Page | 53
<module_root>/etc/db_schema_whitelist.json
- 67. © 2019 Magento, Inc. Page | 67
Destructive Operations
• Deleting a table
• Deleting a column
• Reducing column length
• Changing column precision
• Changing column type
- 69. © 2019 Magento, Inc. Page | 69
var/declarative_dumps_csv/
{column_name_column_type_other_dimensions}.csv
var/declarative_dumps_csv/{table_name}.csv
- 74. © 2019 Magento, Inc. Page | 74
Converting Setup Script
to Declarative Schema
- 76. © 2019 Magento, Inc. Page | 76
Limitations
• Custom DDL operations are ignored. It supports only DDL operations that are
present in MagentoFrameworkDBAdapterPdoMysql
• Raw SQL in InstallSchema or UpgradeSchema scripts are ignored.
• DDL statements in the Recurring file won’t be transferred to the new schema
because the file needs to run during each installation or upgrade.