10

I’m updating a module to Magento 2.3 and need to migrate the old setup install scripts to the new db_schema.xml file (the new declarative schema). The db_schema.xml file was correctly generated after running:

php bin/magento setup:install --convert-old-scripts=1

But now I need to generate the db_schema_whitelist.json file and running the following command does nothing:

php bin/magento setup:db-declaration:generate-whitelist --module-name=[module_name]

I followed all the steps outlined in the docs and checked the permissions of the etc folder.

But the result is always the same: no errors, no response, nothing. Any ideas?

8 Answers 8

22

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!

3
  • Great post @Daniel Kratohvil!
    – Asrar
    Jul 25, 2019 at 12:21
  • @Daniel Kratohvil - Great answer . Like a way to find out the error in db_schema.xml file. Dec 16, 2019 at 9:14
  • Thanks for this very good answer, nice to know why whitelist db_schema.xml sometimes are not generated. Recent versions (M2.3.5) seems to have slightly better error reporting when trying to generate whitelist, (unique keys)... but not everything reports immediately. Aug 16, 2021 at 8:35
9

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.

7

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.

1

Based on the OP's own answer EDIT, I propose a more global way to debug executing the bin/magento setup:db-declaration:generate-whitelist --module-name=Vendor_Modulename command, which should work for all kind of error, not only ones about errors in db_schema.xml:

  1. Open the file that defines the setup:db-declaration:generate-whitelist command in vendor/magento/module-developer/Console/Command/TablesWhitelistGenerateCommand.php

  2. Locate the execute(...) method, and add the following code:

/**
 * @inheritdoc
 */
protected function execute(InputInterface $input, OutputInterface $output): int
{
    // ...
    try {
        // Original code
    } catch (ConfigurationMismatchException $e) {
        // Original code
    } catch (\Exception $e) {
        // ADD FOLLOWING LINE:
        $output->writeln('<error>' . $e->getMessage() . '</error>');
        return \Magento\Framework\Console\Cli::RETURN_FAILURE;
    }
    // ...
}

This way, you won't have to manually check a log file, as the error will be displayed in the CLI.

(In my opinion, this simple line of code should already be in the code base, anyway...)

EDIT: Actually, this exact line of code has been added in the core code. I was simply not up-to-date...

0

One really quick thing I've found worth checking. If Magento 2 is not generating your db_schema_whitelist.json, make sure your module is actually enabled by checking bin/magento mod:status module where module is the name of your module, and then bin/magento mod:ena module to enable your module. It's super easy to create a module and forget to enable it before trying to use it. This issue applies to more than just db_schema_whitelist.json, of course.

0

For me It only works after I run s:up first then the module installed and after that the whitelist json was added just fine.

0

The answer provided by @Danial is great but still if it is not working then check the below point as well

1- Check if module is enable or enable it using

bin/magento module:enable Vendor_Module

2- Check if you have the correct permission, maybe the file is not generating because you do not have the required permission.

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.