一键导入
module-setup
Implement declarative schema (db_schema.xml), data patches, and schema patches for Magento 2 database structure and data migrations
用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
菜单
Implement declarative schema (db_schema.xml), data patches, and schema patches for Magento 2 database structure and data migrations
用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
基于 SOC 职业分类
Build Magento 2 admin grids using UI Component listing XML, data providers, and admin controllers with proper ACL integration
Implement Magento 2 GraphQL resolvers — define schema.graphqls types, write query and mutation resolvers, and handle authorization
Run Magento 2 CLI commands through Warden's Docker environment: warden shell, bin/magento, composer, Redis/Valkey, Varnish, OpenSearch, RabbitMQ, n98-magerun2, mutagen sync, and env lifecycle.
Adapt Luma-oriented Magento 2 modules to work with Hyva themes — replace RequireJS and Knockout with Alpine.js, Tailwind CSS, and ViewModels
Configure Magento 2 dependency injection — preferences, virtual types, constructor arguments, and proxy generation via di.xml
Implement Magento 2 plugins (interceptors) following best practices for before, after, and around method interception
| name | module-setup |
| description | Implement declarative schema (db_schema.xml), data patches, and schema patches for Magento 2 database structure and data migrations |
| installed_version | 1.0.0 |
| magehub_version | 0.1.13 |
Magento 2.3+ uses declarative schema (etc/db_schema.xml) to define database
table structures instead of legacy InstallSchema/UpgradeSchema scripts. The
framework compares the declared state against the current database state and
generates the necessary ALTER/CREATE statements automatically. This is the
only supported approach for new modules.
Define tables, columns, constraints, and indexes in etc/db_schema.xml. Each
column requires name, xsi:type, and nullable attributes at minimum.
Primary keys use <constraint> with referenceId="PRIMARY". Foreign keys
reference the parent table and column, and Magento enforces referential
integrity at the database level.
After modifying db_schema.xml, run
bin/magento setup:db-declaration:generate-whitelist --module-name=Vendor_Module
to update etc/db_schema_whitelist.json. The whitelist tracks which schema
elements your module owns so that setup:upgrade can safely drop columns and
tables during uninstallation. Never edit the whitelist manually.
Use DataPatchInterface classes in Setup/Patch/Data/ for inserting or
modifying data — configuration values, attribute creation, seed data. Data
patches run once and are tracked in the patch_list table. Make every patch
idempotent because setup:upgrade may execute patches in any order relative
to other modules.
Use SchemaPatchInterface in Setup/Patch/Schema/ for DDL operations that
cannot be expressed in declarative schema — triggers, stored procedures, or
complex index operations. Schema patches are rare; prefer declarative schema
for standard table changes.
Implement getDependencies() to declare ordering between patches within the
same module. Return getAliases() with the old class name when renaming a
patch to prevent re-execution.
Complete declarative schema defining a custom entity table with primary key, columns, index, and foreign key constraint
<?xml version="1.0"?>
<schema xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:Setup/Declaration/Schema/etc/schema.xsd">
<table name="vendor_module_entity" resource="default" engine="innodb"
comment="Vendor Module Entity Table">
<column xsi:type="int" name="entity_id" unsigned="true" nullable="false"
identity="true" comment="Entity ID"/>
<column xsi:type="varchar" name="name" nullable="false" length="255"
comment="Entity Name"/>
<column xsi:type="smallint" name="store_id" unsigned="true" nullable="false"
default="0" comment="Store ID"/>
<column xsi:type="timestamp" name="created_at" nullable="false"
default="CURRENT_TIMESTAMP" comment="Created At"/>
<constraint xsi:type="primary" referenceId="PRIMARY">
<column name="entity_id"/>
</constraint>
<index referenceId="VENDOR_MODULE_ENTITY_STORE_ID" indexType="btree">
<column name="store_id"/>
</index>
<constraint xsi:type="foreign" referenceId="VENDOR_MODULE_ENTITY_STORE_ID_STORE_STORE_ID"
table="vendor_module_entity" column="store_id"
referenceTable="store" referenceColumn="store_id"
onDelete="CASCADE"/>
</table>
</schema>
Data patch that adds a product attribute, checking for existence before creation to ensure safe re-execution
<?php
declare(strict_types=1);
namespace Vendor\Module\Setup\Patch\Data;
use Magento\Eav\Setup\EavSetup;
use Magento\Eav\Setup\EavSetupFactory;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Framework\Setup\Patch\DataPatchInterface;
class AddCustomProductAttribute implements DataPatchInterface
{
public function __construct(
private readonly ModuleDataSetupInterface $moduleDataSetup,
private readonly EavSetupFactory $eavSetupFactory
) {
}
public function apply(): self
{
/** @var EavSetup $eavSetup */
$eavSetup = $this->eavSetupFactory->create(['setup' => $this->moduleDataSetup]);
// Idempotent: skip if attribute already exists
if ($eavSetup->getAttributeId(\Magento\Catalog\Model\Product::ENTITY, 'custom_flag')) {
return $this;
}
$eavSetup->addAttribute(
\Magento\Catalog\Model\Product::ENTITY,
'custom_flag',
[
'type' => 'int',
'label' => 'Custom Flag',
'input' => 'boolean',
'source' => \Magento\Eav\Model\Entity\Attribute\Source\Boolean::class,
'required' => false,
'default' => '0',
'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_STORE,
'visible' => true,
'searchable' => false,
'filterable' => false,
'comparable' => false,
'used_in_product_listing' => false,
]
);
return $this;
}
public static function getDependencies(): array
{
return [];
}
public function getAliases(): array
{
return [];
}
}
Auto-generated whitelist file that tracks module-owned schema elements for safe uninstallation
{
"vendor_module_entity": {
"column": {
"entity_id": true,
"name": true,
"store_id": true,
"created_at": true
},
"index": {
"VENDOR_MODULE_ENTITY_STORE_ID": true
},
"constraint": {
"PRIMARY": true,
"VENDOR_MODULE_ENTITY_STORE_ID_STORE_STORE_ID": true
}
}
}
Path template:
etc/db_schema.xml
Declarative schema definition for module database tables
<?xml version="1.0"?>
<schema xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:Setup/Declaration/Schema/etc/schema.xsd">
<table name="{{vendor}}_{{module}}_entity" resource="default" engine="innodb"
comment="{{module}} Entity Table">
<column xsi:type="int" name="entity_id" unsigned="true" nullable="false"
identity="true" comment="Entity ID"/>
<constraint xsi:type="primary" referenceId="PRIMARY">
<column name="entity_id"/>
</constraint>
</table>
</schema>
Path template:
Setup/Patch/Data/{{patchName}}.php
Data patch template with idempotent apply method and dependency declarations
<?php
declare(strict_types=1);
namespace {{vendor}}\{{module}}\Setup\Patch\Data;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Framework\Setup\Patch\DataPatchInterface;
class {{patchName}} implements DataPatchInterface
{
public function __construct(
private readonly ModuleDataSetupInterface $moduleDataSetup
) {
}
public function apply(): self
{
// Implement idempotent data migration here
return $this;
}
public static function getDependencies(): array
{
return [];
}
public function getAliases(): array
{
return [];
}
}