-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add new checker for tables without default timestamp columns
- Loading branch information
Showing
5 changed files
with
121 additions
and
0 deletions.
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
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
38 changes: 38 additions & 0 deletions
38
lib/active_record_doctor/detectors/table_without_timestamps.rb
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,38 @@ | ||
# frozen_string_literal: true | ||
|
||
require "active_record_doctor/detectors/base" | ||
|
||
module ActiveRecordDoctor | ||
module Detectors | ||
class TableWithoutTimestamps < Base # :nodoc: | ||
@description = "detect tables without created_at/updated_at columns" | ||
@config = { | ||
ignore_tables: { | ||
description: "tables whose timestamp columns existence should not be checked", | ||
global: true | ||
} | ||
} | ||
|
||
private | ||
|
||
TIMESTAMPS = { | ||
"created_at" => "created_on", | ||
"updated_at" => "updated_on" | ||
}.freeze | ||
|
||
def message(table:, column:) | ||
"add a #{column} column to #{table}" | ||
end | ||
|
||
def detect | ||
each_table(except: config(:ignore_tables)) do |table| | ||
TIMESTAMPS.each do |column, alternative_column| | ||
unless column(table, column) || column(table, alternative_column) | ||
problem!(table: table, column: column) | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
54 changes: 54 additions & 0 deletions
54
test/active_record_doctor/detectors/table_without_timestamps_test.rb
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,54 @@ | ||
# frozen_string_literal: true | ||
|
||
class ActiveRecordDoctor::Detectors::TableWithoutTimestampsTest < Minitest::Test | ||
def test_table_without_timestamps_reported | ||
Context.create_table(:companies) do |t| | ||
t.string :name | ||
end | ||
|
||
assert_problems(<<~OUTPUT) | ||
add a created_at column to companies | ||
add a updated_at column to companies | ||
OUTPUT | ||
end | ||
|
||
def test_table_with_timestamps_is_not_reported | ||
Context.create_table(:companies) do |t| | ||
t.timestamps | ||
end | ||
refute_problems | ||
end | ||
|
||
def test_table_with_alternative_timestamps_is_not_reported | ||
Context.create_table(:companies) do |t| | ||
t.timestamp :created_on | ||
t.timestamp :updated_on | ||
end | ||
refute_problems | ||
end | ||
|
||
def test_config_ignore_tables | ||
Context.create_table(:companies) | ||
|
||
config_file(<<-CONFIG) | ||
ActiveRecordDoctor.configure do |config| | ||
config.detector :table_without_timestamps, | ||
ignore_tables: ["companies"] | ||
end | ||
CONFIG | ||
|
||
refute_problems | ||
end | ||
|
||
def test_global_ignore_tables | ||
Context.create_table(:companies) | ||
|
||
config_file(<<-CONFIG) | ||
ActiveRecordDoctor.configure do |config| | ||
config.global :ignore_tables, ["companies"] | ||
end | ||
CONFIG | ||
|
||
refute_problems | ||
end | ||
end |