#Declarative Schema in Magento 2.3
Magento has introduced a new feature called Declarative Schema which aims to simplify the Magento installation and upgrade processes. This new concept will allow a developer to get away from writing updates for each new version in favour of declaring the final result that the developer wants to achieve.
#How to use Declarative Schema in Magento 2.3
we will only discuss file required for a Declarative Schema.
Firstly create a file db_schema.xml
inside the folder Karabiner/Declarative/etc
and write the following code
<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Setup/Declaration/Schema/etc/schema.xsd">
<table name="author_data" resource="default" engine="innodb" comment="Author Table">
<column xsi:type="smallint" name="id" padding="6" unsigned="false" nullable="false" identity="true" comment="Author ID"/>
<column xsi:type="varchar" name="author_name" nullable="false" length="255" comment="Author Name"/>
<column xsi:type="varchar" name="author_email" nullable="false" length="255" comment="Author Email"/>
<column xsi:type="varchar" name="affliation" nullable="false" length="255" comment="Affliation"/>
<column xsi:type="int" name="age" unsigned="true" nullable="true" identity="false" default="" comment="Age"/>
<constraint xsi:type="primary" name="PRIMARY">
<column name="id"/>
</constraint>
</table>
</schema>
Each table node represents a table in the database.
A table node can contain three types of subnodes:
- Column
- Constraint
- Index
So when you run setup:upgrade
command then it will create the table “author_data”
###Adding New Column
If you want to add a new column to the existing table then you need to add a new column node in db_schema.xml and on running upgrade command it will add the new column.
<column xsi:type="int" name="new_column" unsigned="true" nullable="true" identity="false" default="" comment="New column" />
###Remove Existing Column
Now if you want to remove the existing column then you either need to remove the column node (< column >) inside the table node or you can set the disabled
attribute to true
.
<column xsi:type="int" name="age" unsigned="true" nullable="true" identity="false" default="" comment="Age" disabled=”true” />
But before running the upgrade command you need to add your schema to db_whitelist_schema.json file by running the following command-
php bin/magento setup:db-declaration:generate-whitelist --module-name=Karabiner_Declarative
Karabiner_Declarative
is a module name. You need to specify your module name there.
Now on running setup:upgrade
command, it will remove the column.
Changing the column type
You can change the column type, changing its type attribute to int, varchar, text etc.
##Renaming a column
To rename a column you need to first remove the one you don’t want and add another column with your desired name. Now you need to migrate that column data into a newly created one, to do this you need to set an attribute on Create which will migrate data from the old column.
If we want to rename the author_email column to just email, we will remove the author column and add a new one.
<column xsi:type="varchar" name="email" onCreate="migrateDataFrom(author_email)" on_update="false" nullable="false" default="" comment="Author Email"/>
<column xsi:type="int" name="email" unsigned="true" nullable="true" identity="false" default="" comment="Email" disabled=”true” />
So this will create a new column ‘email’ with data from the removed column.
##Drop a Table
To drop a table, either remove the entire table node from db_schema.xml
or set the disabled
attribute to true
.
<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Setup/Declaration/Schema/etc/schema.xsd">
<table name="author_data" resource="default" engine="innodb" comment="Author Table" disabled=”true”>
....
</table>
</schema>