Move migration compatibility behavior to per-adapter modules#57479
Open
yahonda wants to merge 1 commit into
Open
Move migration compatibility behavior to per-adapter modules#57479yahonda wants to merge 1 commit into
yahonda wants to merge 1 commit into
Conversation
4 tasks
272702a to
08a143f
Compare
Adapter-specific migration compatibility logic was scattered across a single monolithic Compatibility class with adapter_name string comparisons. This made it hard to maintain and impossible for third-party adapters to extend without monkey-patching. Each built-in adapter now defines its own MigrationCompatibility module and returns it from the new `migration_compatibility_module_for` hook. Third-party adapters (e.g. cockroachdb) can override this hook to ship per-version legacy behavior without patching Rails internals. The hook is registered on Mysql2Adapter and TrilogyAdapter (not AbstractMysqlAdapter) so third-party adapters subclassing the abstract class are not implicitly opted in. The assembled module is included on the topmost user-defined class in the migration ancestry, preserving the pre-refactor lookup order where user overrides run ahead of adapter-specific code. Migrator#apply_adapter_compatibility_module covers migrations that override #migrate directly. Module caching uses ObjectSpace::WeakMap to avoid leaking anonymous/reloaded classes. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
08a143f to
7d8af0f
Compare
rafaelfranca
left a comment
Member
There was a problem hiding this comment.
I like the idea but this removed all the simplicity of the superclass of the migration containing the version, now, if I get this correctly the code of that class changes the first time a migration is executed so it is hard to predict which behavior that class would have, it depends on modules included at runtime, that depends on which adapter is used. That is a lot of complexity to me.
A common design pattern to solve this problem is the strategy pattern, and we can avoid the inheritance complexity (module being included at runtime)
| end | ||
|
|
||
| def migration_compatibility_module_for(migration_class) | ||
| nil |
Member
Author
|
Agreed. I've started reworking it around the strategy pattern on a separate branch, and I'll open it as a separate pull request once it's ready. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Motivation / Background
Adapter-specific migration compatibility logic was scattered across a single monolithic
Compatibilityclass withadapter_namestring comparisons. This made it hard to maintain and impossible for third-party adapters to extend without monkey-patching (e.g. cockroachdb adapter, oracle-enhanced #2596, oracle-enhanced #2710).Detail
Each built-in adapter now defines its own
MigrationCompatibilitymodule and returns it from the newmigration_compatibility_module_forhook. Third-party adapters can override this hook to ship per-version legacy behavior without patching Rails internals.The hook is registered on
Mysql2AdapterandTrilogyAdapter(notAbstractMysqlAdapter) so third-party adapters subclassing the abstract class are not implicitly opted in.Additional information
Verified that the CockroachDB adapter's monkey patch is no longer needed —
migration_compatibility_module_foris inherited fromPostgreSQLAdapter, and:datetime→:timestampconversion works correctly forMigration[6.1]on CockroachDB without any adapter-side patching.Checklist