-
-
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/MigrationTimestamp
cop
#1044
Open
sambostock
wants to merge
1
commit into
rubocop:master
Choose a base branch
from
sambostock:migration-file-name
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
* [#1044](https://github.com/rubocop/rubocop-rails/pull/1044): Add new `Rails/MigrationTimestamp` cop. ([@sambostock][]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'time' | ||
|
||
module RuboCop | ||
module Cop | ||
module Rails | ||
# Checks that migration file names start with a valid timestamp. | ||
# | ||
# @example | ||
# # bad | ||
# # db/migrate/bad.rb | ||
|
||
# # bad | ||
# # db/migrate/123_bad.rb | ||
|
||
# # bad | ||
# # db/migrate/20171301000000_bad.rb | ||
# | ||
# # good | ||
# # db/migrate/20170101000000_good.rb | ||
# | ||
class MigrationTimestamp < Base | ||
include RangeHelp | ||
|
||
MSG = 'Migration file name must start with a valid `YYYYmmddHHMMSS_` timestamp in the past.' | ||
|
||
def on_new_investigation | ||
file_path = processed_source.file_path | ||
timestamp = File.basename(file_path).split('_', 2).first | ||
return if valid_timestamp?(timestamp) | ||
|
||
add_offense(source_range(processed_source.buffer, 1, 0)) | ||
end | ||
|
||
private | ||
|
||
def valid_timestamp?(timestamp, format: '%Y%m%d%H%M%S') | ||
sambostock marked this conversation as resolved.
Show resolved
Hide resolved
|
||
format_with_utc_suffix = "#{format} %Z" | ||
timestamp_with_utc_suffix = "#{timestamp} UTC" | ||
|
||
timestamp && | ||
# Time.strptime has no way to externally declare what timezone the string is in, so we append it. | ||
(time = Time.strptime(timestamp_with_utc_suffix, format_with_utc_suffix)) && | ||
# Time.strptime fuzzily accepts invalid dates around boundaries | ||
# | Wrong Days per Month | 24th Hour | 60th Minute | 60th Second | ||
# ---------+----------------------+----------------+----------------+---------------- | ||
# 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 commentThe reason will be displayed to describe this comment to others. Learn more. Smart! 💯 |
||
time.strftime(format) == timestamp && | ||
# No timestamps in the future | ||
time <= Time.now.utc | ||
rescue ArgumentError | ||
false | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe RuboCop::Cop::Rails::MigrationTimestamp, :config do | ||
it 'registers no offenses if timestamp is valid' do | ||
expect_no_offenses(<<~RUBY, 'db/migrate/20170101000000_good.rb') | ||
# ... | ||
RUBY | ||
end | ||
|
||
it 'registers an offense if timestamp is impossible' do | ||
expect_offense(<<~RUBY, 'db/migrate/20002222222222_bad.rb') | ||
# ... | ||
^ Migration file name must start with a valid `YYYYmmddHHMMSS_` timestamp in the past. | ||
RUBY | ||
end | ||
|
||
it 'registers an offense if timestamp swaps month and day' do | ||
expect_offense(<<~RUBY, 'db/migrate/20003112000000_bad.rb') | ||
# ... | ||
^ Migration file name must start with a valid `YYYYmmddHHMMSS_` timestamp in the past. | ||
RUBY | ||
end | ||
|
||
it 'registers an offense if timestamp day is wrong' do | ||
expect_offense(<<~RUBY, 'db/migrate/20000231000000_bad.rb') | ||
# ... | ||
^ Migration file name must start with a valid `YYYYmmddHHMMSS_` timestamp in the past. | ||
RUBY | ||
end | ||
|
||
it 'registers an offense if timestamp hours are invalid' do | ||
expect_offense(<<~RUBY, 'db/migrate/20000101240000_bad.rb') | ||
# ... | ||
^ Migration file name must start with a valid `YYYYmmddHHMMSS_` timestamp in the past. | ||
RUBY | ||
end | ||
|
||
it 'registers an offense if timestamp minutes are invalid' do | ||
expect_offense(<<~RUBY, 'db/migrate/20000101006000_bad.rb') | ||
# ... | ||
^ Migration file name must start with a valid `YYYYmmddHHMMSS_` timestamp in the past. | ||
RUBY | ||
end | ||
|
||
it 'registers an offense if timestamp seconds are invalid' do | ||
expect_offense(<<~RUBY, 'db/migrate/20000101000060_bad.rb') | ||
# ... | ||
^ Migration file name must start with a valid `YYYYmmddHHMMSS_` timestamp in the past. | ||
RUBY | ||
end | ||
|
||
it 'registers an offense if timestamp is invalid' do | ||
expect_offense(<<~RUBY, 'db/migrate/123_bad.rb') | ||
# ... | ||
^ Migration file name must start with a valid `YYYYmmddHHMMSS_` timestamp in the past. | ||
RUBY | ||
end | ||
|
||
it 'registers an offense if no timestamp at all' do | ||
expect_offense(<<~RUBY, 'db/migrate/bad.rb') | ||
# ... | ||
^ Migration file name must start with a valid `YYYYmmddHHMMSS_` timestamp in the past. | ||
RUBY | ||
end | ||
|
||
it 'registers an offense if the timestamp is in the future' do | ||
timestamp = (Time.now.utc + 5).strftime('%Y%m%d%H%M%S') | ||
expect_offense(<<~RUBY, "db/migrate/#{timestamp}_bad.rb") | ||
# ... | ||
^ Migration file name must start with a valid `YYYYmmddHHMMSS_` timestamp in the past. | ||
RUBY | ||
end | ||
|
||
it 'registers no offense if the timestamp is in the past' do | ||
timestamp = (Time.now.utc - 5).strftime('%Y%m%d%H%M%S') | ||
expect_no_offenses(<<~RUBY, "db/migrate/#{timestamp}_good.rb") | ||
# ... | ||
RUBY | ||
end | ||
end |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 underdb
, 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.
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 ownApplicationMigration
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.
That is exactly what it does! 😅