LoginSignup
1
1

More than 3 years have passed since last update.

Magento 2.3のDeclarative Schema

Last updated at Posted at 2020-03-16

Magento 2.3のDeclarative Schema

Declarative Schemaは、Magento2.2までのPHPコードを使用したデータベーススキーマ定義作成を置き換える仕組みです。
Magento2.3から新たに導入された仕組みで、XMLファイルを使用してテーブルの構造などを定義します。

Magento 2.3のDeclarative Schemaの使用する方法

Declarative Schemaの必要なファイルについてのみ説明します。

まず、フォルダ「Karabiner/Declarative/etc」内にファイル「db_schema.xml」を作成して、次のコードを記述します

db_schema.xml
<?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> 

各テーブルノードは、データベース内のテーブルを表します。

テーブルノードには、次の3種類のサブノードを含めることができます。

  • Column
  • Constraint
  • Index

setup:upgradeコマンドを実行すると、テーブル「author_data」が作成されます

新しい列を追加

既存のテーブルに新しい列を追加する場合は、db_schema.xmlに新しい列ノード(<column>)を追加する必要があります。setup:upgradeコマンドを実行すると、新しい列が追加されます。

db_schema.xml
<column xsi:type="int" name="new_column" unsigned="true" nullable="true" identity="false" default="" comment="New column"  />

既存の列を削除

既存の列を削除する場合は、テーブルノード内の列ノード(<column>)を削除するか、 disabledアトリビュートをtrueに設定する必要があります。

db_schema.xml
<column xsi:type="int" name="age" unsigned="true" nullable="true" identity="false" default="" comment="Age" disabled=”true” />

しかし、アップグレードコマンドを実行する前に、次のコマンドを実行してスキーマを db_whitelist_schema.jsonファイルに追加する必要があります。

php bin/magento setup:db-declaration:generate-whitelist --module-name=Karabiner_Declarative 

Karabiner_Declarativeはモジュール名です。 そこでモジュール名を指定する必要があります。

setup:upgradeコマンドを実行すると、列が削除されます。

列タイプを変更し、そのtypeアトリビュートをint、varchar、textなどに変更できます。

列の名前を変更

列の名前を変更するには、まず不要な列を削除する必要があります。 別の列を追加します。 その列のデータを新しく作成したものに移行する必要があります。これを行うには、古い列からデータを移行するアトリビュートonCreateを設定する必要があります。
author_email列の名前を「email」に変更する場合は、author_email列を削除して新しい列を追加します。

db_schema.xml

<column xsi:type="varchar" name="email" onCreate="migrateDataFrom(author_email)" on_update="false" nullable="false" default="" comment="Author Email"/>
<column xsi:type="varchar" name="author_email" unsigned="true" nullable="true" identity="false" default="" comment="Email" disabled=”true” />

削除された列のデータで新しい列「email」が作成されます。

テーブルをドロップ

テーブルを削除するには、テーブルノード全体を「db_schema.xml」から削除するか、「disabled」アトリビュートを「true」に設定します。

db_schema.xml
<?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> 
1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1