-
-
Notifications
You must be signed in to change notification settings - Fork 266
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
Add new Rails/DateAndTimeColumnName
cop
#335
base: master
Are you sure you want to change the base?
Conversation
There is reason why it is hard to apply by default. Updating db/migrate files that has already been migrated will result in schema mismatch. |
#277 also has the same problem. |
We can add a config option (something like |
Yeah, I also considered the timestamp value. Perhaps setting to .rubocop.yml is expensive to maintain. It may be better to use the timestamp value from db/schema.rb. But I have not yet fully considered false positives 😅 |
@fatkodima ping |
So I need to implement this? |
Another option that does not require configuration is inspecting |
Oh, that won't work, as schemas have all legacy columns. Yes, I believe the path taken in #277 is fine. |
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.
Looks good, thank you.
Some minor notes only.
PS Plus the StartAfterMigrationVersion
options is needed.
config/default.yml
Outdated
Enabled: pending | ||
VersionAdded: '2.8' | ||
Include: | ||
- db/migrate/**/**.rb |
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.
Multiple db setup reads migrations_paths:
from config/database.yml
. I'm not sure if there's a convention on how to place them. Mine are usually in db/secondary_migrate/*.rb
.
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 changed it to
Include:
- db/**/*.rb
like in #277
That way it will check all migration files (I hope no one places migration files using migrations_paths:
into other folders than db/
😄 )
def_node_matcher :migration_base?, <<~PATTERN | ||
{ | ||
(send (const (const nil? :ActiveRecord) :Migration) :[] _) | ||
(const (const nil? :ActiveRecord) :Migration) |
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.
Wondering if some projects use a common ApplicationMigration
🤔
Is this check really necessary if we already know that we're inspecting db/migrations/*.rb
?
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.
With this change
Include:
- db/**/*.rb
we should at least ignore schema.rb files. It is also possible (but very unlikely) to have some other files there, except migration files. Better to double check, I think.
subject(:cop) { described_class.new } | ||
|
||
context 'when `add_column` method' do | ||
described_class::TYPE_TO_SUFFIX.each do |type, suffix| |
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.
This won't fail if you remove or add entries to TYPE_TO_SUFFIX
. It also requires the reader of the spec to look it up in the implementation.
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.
That is the way how similar things are tested in many places in the rubocop
and rubocop-rails
(for example,
described_class::TYPES.each do |type| |
end | ||
end | ||
|
||
context 'when `<type>` method' do |
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 this supposed to be interpolated with a real type
?
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.
That was meant method calls like t.integer ...
or t.text ...
etc. Changed it to t.<type>
to be a little more clear.
a880294
to
d3ab782
Compare
config/default.yml
Outdated
`time` - with `_time` suffix. | ||
Enabled: pending | ||
VersionAdded: '<<next>>' | ||
StartAfterMigrationVersion: null |
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.
StartAfterMigrationVersion: null |
end | ||
end | ||
end | ||
end |
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.
#1383 was integrated. Can you remove this module?
# t.column :visited_on, :date | ||
# t.time :start_time | ||
# | ||
# @example StartAfterMigrationVersion: 20211007000001 |
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.
Can you remove this example?
# add_column :orders, :created_on, :datetime | ||
# | ||
class DateAndTimeColumnName < Base | ||
include StartAfterMigrationVersion |
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.
include StartAfterMigrationVersion | |
prepend MigrationsHelper |
def in_migration?(node) | ||
class_node = node.each_ancestor(:class).first | ||
migration_base?(class_node&.parent_class) | ||
end |
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.
This method can be removed and replaced with RuboCop::Cop::MigrationsHelper#in_migration?
.
This naming rule can probably be added to the Rails Style Guide. |
d3ab782
to
d7f6c33
Compare
Updated PR with the suggestions. |
Inspired by https://github.com/thoughtbot/guides/tree/master/rails
This enforces consistency and it is easier to tell the type from name without referring to db schema.