Skip to content
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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

rogancodes
Copy link

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

@rogancodes rogancodes requested a review from a team as a code owner March 5, 2025 04:37
@andyw8
Copy link
Contributor

andyw8 commented Mar 5, 2025

(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
Copy link
Contributor

@andyw8 andyw8 Mar 5, 2025

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?

Copy link
Author

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.

Copy link
Contributor

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?

Copy link
Author

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.

Copy link
Contributor

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.

Copy link
Author

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.

Copy link
Contributor

@andyw8 andyw8 Mar 8, 2025

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).

@@ -41,21 +43,25 @@ class User < ApplicationRecord
[Schema](#{URI::Generic.from_path(path: dummy_root + "/db/schema.rb")})
**id**: integer (PK)
### *Columns*
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
### *Columns*
### Columns

Let's avoid italics.

@@ -96,21 +104,25 @@ class User < ApplicationRecord
[Schema](#{URI::Generic.from_path(path: dummy_root + "/db/schema.rb")})
**id**: integer (PK)
### *Columns*
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
### *Columns*
### Columns

Copy link
Contributor

@andyw8 andyw8 left a 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 👍

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.

2 participants