Unpacking the Magento 2 `setup:upgrade --keep-generated` Bug: Why Your New Module Tables Might Go Missing

Unpacking the Magento 2 setup:upgrade --keep-generated Bug: Why Your New Module Tables Might Go Missing

As e-commerce migration experts at Shopping Mover, we often encounter intricate technical challenges within the Magento 2 ecosystem. A recent GitHub issue (#41867) sheds light on a particularly subtle and frustrating bug related to the bin/magento setup:upgrade --keep-generated command. This issue can cause newly enabled modules with declarative schema definitions and data patches to fail during their initial setup, leading to "table doesn't exist" errors.

The Core Problem: A Discrepancy in Module Recognition

The bug manifests when a new Magento 2 module, which includes both a db_schema.xml file declaring a new database table and a data patch that attempts to insert data into that table, is enabled for the first time using setup:upgrade --keep-generated in production mode. The expected behavior is that the command should create the table and then run the data patch successfully. However, the actual result is a fatal error:

SQLSTATE[42S02]: Base table or view not found: 1146 Table '.keepgen_repro' doesn't exist, query was: INSERT INTO `keepgen_repro` (`code`) VALUES (?)

Interestingly, if the command is run a second time, it completes successfully, creating the table and inserting the data. This "second run fixes it" behavior is a key indicator of a caching or state management issue within Magento's setup process.

Deep Dive into the Root Cause

The author of the issue, bluefairydevelopment, provided an excellent analysis of the problem's origin within Magento's core code. The discrepancy lies in how the Installer class handles module lists and object manager resets, particularly when the --keep-generated flag is used:

  • When setup:upgrade --keep-generated is executed, the updateModulesSequence() method is called.
  • This method cleans caches and then writes the new module list to app/etc/config.php via createModulesConfig().
  • Crucially, with --keep-generated, there is no subsequent call to $this->objectManagerProvider->reset(); after createModulesConfig().
  • Consequently, when installSchema() and its underlying declarativeInstallSchema() methods are invoked, the object manager's declarative schema reader still holds an outdated module list (from before the config.php update). This means it never reads the db_schema.xml for the newly enabled module, thus failing to create its table.
  • However, the installDataFixtures() loop, which runs subsequently, does use the Installer's updated module list, recognizing the new module and attempting to execute its data patch against a table that was never created.

In contrast, a plain setup:upgrade command includes a file system cleanup that does reset the object manager, ensuring that the schema reader gets the correct, updated module list from the start.

The Proposed Solution

The issue author suggests a straightforward fix: ensuring that the object manager provider, or at least the module list and config reader caches, are reset after createModulesConfig() in updateModulesSequence(), regardless of the --keep-generated flag. This aligns with how the Installer::installDeploymentConfig() method already handles object manager resets after updating the module configuration.

// Suggested fix:
// In setup/src/Magento/Setup/Model/Installer.php, updateModulesSequence()
// After line 1426: $this->createModulesConfig([]);
// Add: $this->objectManagerProvider->reset(); // Or similar logic to clear module list/config reader caches

This bug highlights the critical importance of Magento's internal state management during deployment processes. For developers and system administrators, understanding such nuances is key to smooth module installations and upgrades, especially in production environments where --keep-generated is often used to speed up deployments by avoiding regeneration of static content and compiled code.

While the issue is marked "ready for confirmation" and has related open PRs, it's a vital piece of information for anyone debugging deployment failures involving new modules and declarative schema in Magento 2.4.x and potentially earlier versions.

Start with the tools

Explore migration tools

See options, compare methods, and pick the path that fits your store.

Explore migration tools