Class: RuboCop::Cop::DevDoc::Migration::RequireTimestamps

Inherits:
Base
  • Object
show all
Extended by:
AutoCorrector
Defined in:
lib/rubocop/cop/dev_doc/migration/require_timestamps.rb

Overview

Every ‘create_table` migration must include `t.timestamps`.

## Rationale Every table should have ‘created_at` and `updated_at` columns —there is no reason to omit them. They are essential for debugging, auditing, and ordering records, and adding them later is much more painful than including them up front.


create_table :ai_api_failures do |t|
  t.belongs_to :ai_chat_message, null: false, foreign_key: true
  t.string :error_type
  t.datetime :created_at
end

✔️
create_table :ai_api_failures do |t|
  t.belongs_to :ai_chat_message, null: false, foreign_key: true
  t.string :error_type
  t.timestamps
end

NOTE: This cop skips tables declared with ‘id: false` (typically join tables), where omitting timestamps is a deliberate choice.

Examples:

# bad
create_table :users do |t|
  t.string :name
end

# good
create_table :users do |t|
  t.string :name
  t.timestamps
end

Constant Summary collapse

MSG =
'Add `t.timestamps` to this `create_table` migration.'.freeze
RESTRICT_ON_SEND =
%i[create_table].freeze

Instance Method Summary collapse

Instance Method Details

#on_send(node) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
# File 'lib/rubocop/cop/dev_doc/migration/require_timestamps.rb', line 61

def on_send(node)
  return if skip_table?(node)

  block = node.parent
  return unless block&.block_type?
  return if timestamps_present?(block.body)

  add_offense(node.loc.selector) do |corrector|
    autocorrect(corrector, block)
  end
end