What is Declarative Schema in Magento 2.x

Before Release Magento 2.3 we are using in custom module development, Tables created by InstallSchema & upgraded by UpgradeSchema, similarly InstallData to install data in tables & UpgradeData to upgrade data in tables, this process was time-consuming

Once Release Magento 2.3 [ 25th Nov, 2018 ], introduced new concept of Database Creation is declarative schema

Replacement of [ InstallSchema / InstallData ,UpgradeSchema / UpgradeData] is declarative schema

“Instead of using Four Files [InstallSchema.php / InstallData.php ,UpgradeSchema.php / UpgradeData.php], Using Single File (db_schema.xml) used inside app/code/vendorname/module/etc/db_schema.xml”

sample db_schema.xml created as below

<schema xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Setup/Declaration/Schema/etc/schema.xsd">
 <table name="Emp_Records" resource="default" engine="innodb" comment="Employee Records">
 <column xsi:type="int" name="id" padding="7" unsigned="false" nullable="false" identity="true" comment="ID" />
	  
<column xsi:type="varchar" name="emp_name"  nullable="false" length="100"  comment="Employee Name" />
		
 <column xsi:type="varchar" name="emp_dept"     nullable="false" length="200"  comment="Employee Department" />
	  
 <column xsi:type="varchar" name="emp_address"       nullable="false" length="200"  comment="Employee Address" />
	 
<column xsi:type="decimal" name="emp_salary" nullable="false" scale="2" precision="10" comment="Employee Salary"/>
	  
<column xsi:type="smallint" name="is_active"   nullable="false"  comment="Employee Records Is Active or not" />

<column xsi:type="datetime" name="created_at"   comment="Employee Records Created Date" />

<column xsi:type="datetime" name="updated_at"   comment="Employee Records Updated Date" />
	  
      <constraint xsi:type="primary" referenceId="PRIMARY">
         <column name="id" />
      </constraint>
	  
    </table>
</schema>

Leave a Reply

Your email address will not be published. Required fields are marked *