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

Add new Rails/MigrationTimestamp cop #1044

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

sambostock
Copy link
Contributor

@sambostock sambostock commented Jul 4, 2023

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 with Time.strptime, and checking if Time#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:

  • The PR relates to only one subject with a clear title and description in grammatically correct, complete sentences.
  • Wrote good commit messages.
  • Commit message starts with [Fix #issue-number] (if the related issue exists).
  • Feature branch is up-to-date with master (if not - rebase it).
  • Squashed related commits together.
  • Added tests.
  • Ran bundle exec rake default. It executes all tests and runs RuboCop on its own code.
  • Added an entry (file) to the changelog folder named {change_type}_{change_description}.md if the new code introduces user-observable changes. See changelog entry format for details.
  • If this is a new cop, consider making a corresponding update to the Rails Style Guide.

@sambostock sambostock force-pushed the migration-file-name branch from 9969113 to 4ae885e Compare July 4, 2023 23:38
Comment on lines +663 to +690
Include:
- db/migrate/**/*.rb
Copy link
Contributor Author

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.

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?

Copy link
Contributor Author

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! 😅

@sambostock sambostock force-pushed the migration-file-name branch from 4ae885e to 6c5a722 Compare July 4, 2023 23:41
@sambostock sambostock changed the title Add new Rails/MigrationClassName cop Add new Rails/MigrationTimestamp cop Jul 4, 2023
@sambostock sambostock force-pushed the migration-file-name branch from 6c5a722 to ee8766f Compare July 4, 2023 23:43
Copy link

@davidstosik davidstosik left a 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}(?=_)/]

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! 👍🏻

lib/rubocop/cop/rails/migration_timestamp.rb Outdated Show resolved Hide resolved
Comment on lines +663 to +690
Include:
- db/migrate/**/*.rb

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.

Choose a reason for hiding this comment

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

Smart! 💯

@sambostock sambostock force-pushed the migration-file-name branch from ee8766f to 5a42999 Compare July 5, 2023 16:12
@sambostock
Copy link
Contributor Author

@davidstosik I ended up switching to the split approach. Since we're parsing and round tripping, we implicitly get the length check, so we can go with the more readable option.

@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.
@sambostock
Copy link
Contributor Author

@koic If you have time, this is ready for review.

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.

4 participants