Skip to content

Conversation

drwl
Copy link
Owner

@drwl drwl commented Jul 8, 2024

No description provided.

@drwl
Copy link
Owner Author

drwl commented Jul 8, 2024

Taken from ctran/annotate_models#892

Given a Rails application with multiple databases, tables in two databases with the same table name, and corresponding models with different names, some files get annotated with the wrong table's schema.

The issue is that some of the patterns include %TABLE_NAME%, which will be identical between the two tables, even though they have different model names.

This fixes the issue by eliminating the use of the table name when the model is not connected to the primary database.

@drwl
Copy link
Owner Author

drwl commented Jul 8, 2024

The issue lies in building the list of related files for a model file. Using the example Rails project https://github.com/cygnuseducation/annotate-test-app, running bundle exec annotaterb models consecutively led to the same output. This happened because the factory file test/factories/users.rb would get annotated using Secondary::User's annotations and then User's annotations.

[6] pry(#<AnnotateRb::ModelAnnotator::ProjectAnnotator>)> c
file_name: app/models/secondary/user.rb called
file_name: test/factories/users.rb called
file_name: test/factories/secondary/users.rb called
file_name: app/models/user.rb called
file_name: test/factories/users.rb called
Annotated (2): test/factories/users.rb, test/factories/users.rb

@drwl
Copy link
Owner Author

drwl commented Jul 8, 2024

Note for any future viewers, the change may require Rails 6+ support as the behavior for #connection_specification_name changed between Rails 5 and Rails 6.1.

https://apidock.com/rails/v5.0.0.1/ActiveRecord/ConnectionHandling/connection_specification_name

https://apidock.com/rails/v6.1.3.1/ActiveRecord/ConnectionHandling/connection_specification_name

@drwl
Copy link
Owner Author

drwl commented Jul 9, 2024

TODO: Check how this interacts with :include_version option set, as it currently caches the version. In a multi database setup, it looks like it's possible for each database to have their own schema version.

@drwl
Copy link
Owner Author

drwl commented Feb 17, 2025

I currently don't have time for this, so if any future viewer wants to take this on feel free. Closing this PR for now.

@drwl drwl closed this Feb 17, 2025
@drwl drwl deleted the drwl/fix-multi-db branch February 17, 2025 02:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant