Magento 2.3
Question
Is there any place where db_schema.xml
for your module is cached/restored from?
Issue
I need to refresh/get the recently changed db_schema.xml changes to apply, which is not happening.
Expected result:
Updated (shortened) names for indexes.
IDX_GM_CTA_CUSTOMER_ENTITY_ID
IDX_GM_CTA_TRADE_ACCOUNT_STATE_TYPE_ID
Current incorrect result:
Ignores my currently updated (shortened) names and somehow **still uses old versions of names:
VENDOR_MODULENAME_TABLENAME_CUSTOMER_ENTITY_ID
IDX_0C25F86D281C81FDAE652EC2E495584F
I confirmed after setup:upgrade (deleted the database tables) that the table generated in database has these OLD index names.
Further information
This is what the db_schema_whitelist.xml
currently shows:
This generated information is old, I have recently updated the index referenceId names (see below):
"vendor_modulename_tablename": {
"column": {
"table_name_id": true,
"customer_entity_id": true,
"table_name_state_type_id": true,
"table_name_discount": true
},
"index": {
"VENDOR_MODULENAME_TABLENAME_CUSTOMER_ENTITY_ID": true,
"IDX_0C25F86D281C81FDAE652EC2E495584F": true
},
"constraint": {
"PRIMARY": true
}
},
The current db_schema.xml for the above generated whitelist:
<table name="vendor_modulename_tablename" resource="default" engine="innodb" comment="vendor_modulename_tablename Table">
<column xsi:type="int" name="table_name_id" padding="10" unsigned="true" nullable="false" identity="true" comment="Table Name ID"/>
<column name="customer_entity_id" nullable="false" xsi:type="int" comment="Customer Entity ID (from customer_entity)" identity="false" unsigned="true"/>
<column name="table_name_state_type_id" nullable="false" xsi:type="smallint" comment="Table Name State Type ID" unsigned="true"/>
<column name="table_name_discount" nullable="false" xsi:type="float" comment="Table Name Discount" default="5.10"/>
<constraint xsi:type="primary" referenceId="PRIMARY">
<column name="table_name_id"/>
</constraint>
<index referenceId="IDX_GM_CTA_CUSTOMER_ENTITY_ID" indexType="btree">
<column name="customer_entity_id"/>
</index>
<index referenceId="IDX_GM_CTA_TRADE_ACCOUNT_STATE_TYPE_ID" indexType="btree">
<column name="table_name_state_type_id"/>
</index>
</table>
The old (previous commit version for the constraints):
<constraint xsi:type="primary" referenceId="PRIMARY">
<column name="table_name_id"/>
</constraint>
<index referenceId="VENDOR_MODULENAME_TABLENAME_CUSTOMER_ENTITY_ID" indexType="btree">
<column name="customer_entity_id"/>
</index>
<index referenceId="VENDOR_MODULENAME_TABLENAME_APPLICATION_ACCOUNT_STATE_ACCOUNT_TYPE_ID" indexType="btree">
<column name="table_name_state_type_id"/>
</index>
MORE INFORMATION
"index": {
"VENDOR_MODULENAME_TABLENAME_CUSTOMER_ENTITY_ID": true,
"IDX_0C25F86D281C81FDAE652EC2E495584F": true
},
OLD version of db_schema.xml
had these:
VENDOR_MODULENAME_TABLENAME_APPLICATION_ACCOUNT_STATE_ACCOUNT_TYPE_ID
index name is too long (more than 64 characters), so Magento 2 shortened it to auto-generated name:IDX_0C25F86D281C81FDAE652EC2E495584F
.VENDOR_MODULENAME_TABLENAME_CUSTOMER_ENTITY_ID
remainsVENDOR_MODULENAME_TABLENAME_CUSTOMER_ENTITY_ID
because the character limit is not reached.
NOTE: When generating the name originally, I wasn't fully aware of the character limit, so I recently updated all field/table/index names in module.
Help needed
Where is db_schema.xml
cached for your module, so that I can delete it and regenerate the updated white list, or what am I missing?
EDIT: What I tried again now
In summary:
Where are these entries cached? I even tried to do text-searches
(grep)
inside the whole folder structure of Magento 2 and could not find anything. I restarted MySQL because I thought maybe it's cached there, even though that would be absolutely weird if that is the case!
I followed these steps:
- Removed module files (deleted
Vendor/Module_Name
). - In other words, there is no generated db_schema_whitelist.
- Ran
bin/magento setup:upgrade
- Deleted database tables related to this module.
- Stopped web services.
- Stopped MySQL service, and restarted.
- Started web services.
- Added module files back (inside
Vendor/Module_Name
) - BEFORE RUNNING
setup:upgrade
I tried to runbin/magento setup:db-declaration-generate-whitelist --module-name=Vendor/Module_Name
but of course nothing was generated since the module has not been reinstalled. - Ran
bin/magento setup:upgrade
. - Checked database and OLD NAMES were back :(
- Ran
bin/magento setup:db-declaration-generate-whitelist --module-name=Vendor/Module_Name
and it had the old generated names.
The behaviour/feature implemented by Magento 2 does not make sense
- After the above steps I went to the **database and ran:
ALTER TABLE database.tablename RENAME INDEX old_index_name TO new_index_name
- So I manually renamed these indexes from the Database side: These indexes are then changed, but **as soon as I run
bin/magento setup:upgrade
it adds those old names back, so now I haveold_index_name
and _new_index_name` indexes for the same coluns.
Every single time I am applying changes and need to run bin/magento setup:upgrade
I currently would need to rename the OLD indexes to NEW index names.
EDIT
I investigated further, and on debugging I discovered that the core code does not apply referenceId
in any way for naming indexes in the database.
I ended up creating a new module to create Plugins for features to use referenceId
as my custom names for any modules I make in the future. I created filter information to ensure I only apply these renamed index names to the desired modules (in other words target column tables for the module/s I want to use this custom plugin for)
db_schema.xml
; it is still using the old names.