Magento 2 is not generating the db_schema_whitelist.json file via CLI
Found my own answer. Turns out the db_schema.xml
file generated automatically by Magento had an error. The line that declares the name of the table was missing the resource
value.
This is how the tag was generated after running the CLI command:
<table name="my_table_name" resource="" engine="innodb" comment="My Table Comment">
And this is how it should be:
<table name="my_table_name" resource="default" engine="innodb" comment="My Table Comment">
resource
should contain the database shard on which to install the table. This value must be default, quote, or sales. In my case, adding default
did the trick.
Edit (04/25/2019):
Whenever the db_whitelist_schema.json
file is not created automatically when running the bin/magento setup:db-declaration:generate-whitelist --module-name=Vendor_Modulename
command you can safely assume there’s an error in the etc/db_schema.xml
file of your module.
Since the terminal won’t return an error msg to help you fix the issue, you can resort to the following hack to discover the missing piece in your db_schema file.
1) Open the following core file: vendor/magento/framework/Setup/Declaration/Schema/SchemaConfig.php
2) Locate the getDeclarationConfig() method and add a try-catch like this:
/**
* @inheritdoc
*/
public function getDeclarationConfig()
{
try {
$schema = $this->schemaFactory->create();
$data = $this->readerComposite->read(FileResolverByModule::ALL_MODULES);
$this->declarativeSchemaBuilder->addTablesData($data['table']);
$schema = $this->declarativeSchemaBuilder->build($schema);
return $schema;
} catch (\Exception $e) {
$writer = new \Zend\Log\Writer\Stream(BP . '/var/log/schema.log');
$logger = new \Zend\Log\Logger();
$logger->addWriter($writer);
$logger->info($e->getMessage());
}
}
3) Now run the bin/magento setup:db-declaration:generate-whitelist
4) Check for any errors in var/log/schema.log
5) Rinse and repeat until you fix all the errors in your db_schema.xml file
6) Don’t forget to remove the temporary changes you made to the core file!
The db_schema_whitelist.json
will also not be created if you enter a wrong module name or have any typo in the CLI command:
php bin/magento setup:db-declaration:generate-whitelist --module-name=Vendor_Module
In this case, the command will not throw any errors in cli.
Before run below command for generate db_schema_whitelist.json
php bin/magento setup:db-declaration:generate-whitelist --module-name=Vendor_Module
you have to enable module in which your "db_schema.xml" file exists by running below CLI command
bin/magento module:enable Vendor_Module
and of course clean cache.