-
-
Notifications
You must be signed in to change notification settings - Fork 263
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/MigrationTimestamp
cop
#1044
base: master
Are you sure you want to change the base?
Conversation
9969113
to
4ae885e
Compare
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.
I noticed that MigrationClassName
opts to include everything under db
, and uses a node pattern to detect if the file looks like a migration. We could probably do something similar here, if that's preferable.
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.
if the file looks like a migration
I've wondered about that.
What does "look like a migration" mean really? (We can't rely on the filename having a timestamp prefix, considering that's what we want to check here.)
Would you open the migration file and check if it defines a class that inherits from ActiveRecord::Migration
? What if a project implements its own ApplicationMigration
base class?
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.
Would you open the migration file and check if it defines a class that inherits from
ActiveRecord::Migration
?
That is exactly what it does! 😅
4ae885e
to
6c5a722
Compare
Rails/MigrationClassName
copRails/MigrationTimestamp
cop
6c5a722
to
ee8766f
Compare
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.
Now I think about it again, I think my suggestion to split at the first _
would work, since you check time.strftime(format) == timestamp
:
timestamp = File.basename(file_path).split("_", 2).first
time = Time.strptime(timestamp, format) && time.strftime(format) == timestamp
I'm happy with the regular expression anyway!
Thanks a lot for writing this! ❤️
file_path = processed_source.file_path | ||
return unless file_path.include?('db/migrate') | ||
|
||
timestamp = File.basename(file_path)[/\A\d{14}(?=_)/] |
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 was about to suggest splitting at the first _
for a more readable version, but it turns out Time.strptime
will accept more characters than the pattern needs, and non-numeric characters too...
# Proper time
Time.strptime("20230505163015", "%Y%m%d%H%M%S")
#=> 2023-05-05 16:30:15 +0900
# Extra character
Time.strptime("20230505163015a", "%Y%m%d%H%M%S")
#=> 2023-05-05 16:30:15 +0900
# Non-numeric character
irb(main):008:0> Time.strptime("2023050516301a", "%Y%m%d%H%M%S")
=> 2023-05-05 16:30:01 +0900
Regex looks good! 👍🏻
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.
if the file looks like a migration
I've wondered about that.
What does "look like a migration" mean really? (We can't rely on the filename having a timestamp prefix, considering that's what we want to check here.)
Would you open the migration file and check if it defines a class that inherits from ActiveRecord::Migration
? What if a project implements its own ApplicationMigration
base class?
# ---------+----------------------+----------------+----------------+---------------- | ||
# Actual | 20000231000000 | 20000101240000 | 20000101006000 | 20000101000060 | ||
# Expected | 20000302000000 | 20000102000000 | 20000101010000 | 20000101000100 | ||
# We want normalized values, so we can check if Time#strftime matches the original. |
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.
Smart! 💯
ee8766f
to
5a42999
Compare
@davidstosik I ended up switching to the @paracycle I've added the a check against future timestamps. |
This cop enforces that migration file names start with a valid timestamp in the past.
5a42999
to
64e1260
Compare
@koic If you have time, this is ready for review. |
This cop enforces that migration file names start with a valid timestamp.
It checks both for the correct timestamp format (14 digits), as well as actual timestamp validity (
YYYYmmddHHMMSS
), by parsing withTime.strptime
, and checking ifTime#strftime
returns the original timestamp, to ensure normalization of exotic dates like February 31st (i.e. March 2nd, depending on leap years).Before submitting the PR make sure the following are checked:
Commit message starts with[Fix #issue-number]
(if the related issue exists).master
(if not - rebase it).bundle exec rake default
. It executes all tests and runs RuboCop on its own code.{change_type}_{change_description}.md
if the new code introduces user-observable changes. See changelog entry format for details.