-
Notifications
You must be signed in to change notification settings - Fork 30
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Display index and foreign key details when hovering over a model #588
base: main
Are you sure you want to change the base?
Display index and foreign key details when hovering over a model #588
Conversation
(you can ignore the CI failures for Rails main: #586) |
model.connection.foreign_keys(model.table_name).map do |key_definition| | ||
key_definition.options[:column] | ||
end | ||
rescue NotImplementedError |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Where does this get raised from?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It will be raised from here if the adapter doest support foreign keys.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there another way we could determine that? e.g. check if some particular method is defined on the adapter?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since SchemaStatements
module which provides indexes
and foreign_keys
methods, is included in AbstractAdapter
and all adapters inherit AbstractAdapter
. So, we can't determine if a method is explicitly defined in an adapter just by checking its existence. Instead, we can decide whether to execute the query based on the adapter name, but this requires keeping track of adapter changes.
To check foreign_keys support we can use supports_foreign_keys?
which defaults to false. However, for indexes, there’s no equivalent method.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we can use ActiveRecord::Base.connection.supports_foreign_keys?
. It's false for AbstractAdapter
and is overwritten as true
for the adapters that support it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I have utilized the same as a guard clause in the collect_model_foreign_keys
method.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, so let's update this one to use supports_foreign_keys?
and continue using rescue
for the other. (it might be worth proposing a PR to Rails to add a supports_indexes?
method on the adapter).
test/ruby_lsp_rails/hover_test.rb
Outdated
@@ -41,21 +43,25 @@ class User < ApplicationRecord | |||
[Schema](#{URI::Generic.from_path(path: dummy_root + "/db/schema.rb")}) | |||
**id**: integer (PK) | |||
### *Columns* |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
### *Columns* | |
### Columns |
Let's avoid italics.
test/ruby_lsp_rails/hover_test.rb
Outdated
@@ -96,21 +104,25 @@ class User < ApplicationRecord | |||
[Schema](#{URI::Generic.from_path(path: dummy_root + "/db/schema.rb")}) | |||
**id**: integer (PK) | |||
### *Columns* |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
### *Columns* | |
### Columns |
…h_indexes_and_fks_on_model_hover
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Gave it a try on a few repos, seems to be working well 👍
Motivation
#394
Implementation
Added methods to collect index and foreign key information from a model
Automated Tests
Updated existing test cases
Manual Tests
Screencast.from.05-03-25.10.04.58.AM.IST.webm