-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
table_without_primary_key
checker (#173)
- Loading branch information
Showing
5 changed files
with
106 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
30 changes: 30 additions & 0 deletions
30
lib/active_record_doctor/detectors/table_without_primary_key.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,30 @@ | ||
# frozen_string_literal: true | ||
|
||
require "active_record_doctor/detectors/base" | ||
|
||
module ActiveRecordDoctor | ||
module Detectors | ||
class TableWithoutPrimaryKey < Base # :nodoc: | ||
@description = "detect tables without primary keys" | ||
@config = { | ||
ignore_tables: { | ||
description: "tables whose primary key existense should not be checked", | ||
global: true | ||
} | ||
} | ||
|
||
private | ||
|
||
def message(table:) | ||
"add a primary key to #{table}" | ||
end | ||
|
||
def detect | ||
each_table(except: config(:ignore_tables)) do |table| | ||
column = primary_key(table) | ||
problem!(table: table) unless column | ||
end | ||
end | ||
end | ||
end | ||
end |
47 changes: 47 additions & 0 deletions
47
test/active_record_doctor/detectors/table_without_primary_key_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,47 @@ | ||
# frozen_string_literal: true | ||
|
||
class ActiveRecordDoctor::Detectors::TableWithoutPrimaryKeyTest < Minitest::Test | ||
def test_table_without_primary_key_reported | ||
Context.create_table(:companies, id: false) do |t| | ||
t.string :name | ||
end | ||
|
||
assert_problems(<<~OUTPUT) | ||
add a primary key to companies | ||
OUTPUT | ||
end | ||
|
||
def test_table_with_primary_key_is_not_reported | ||
Context.create_table(:companies) | ||
refute_problems | ||
end | ||
|
||
def test_config_ignore_tables | ||
Context.create_table(:companies, id: false) do |t| | ||
t.string :name | ||
end | ||
|
||
config_file(<<-CONFIG) | ||
ActiveRecordDoctor.configure do |config| | ||
config.detector :table_without_primary_key, | ||
ignore_tables: ["companies"] | ||
end | ||
CONFIG | ||
|
||
refute_problems | ||
end | ||
|
||
def test_global_ignore_tables | ||
Context.create_table(:companies, id: false) do |t| | ||
t.string :name | ||
end | ||
|
||
config_file(<<-CONFIG) | ||
ActiveRecordDoctor.configure do |config| | ||
config.global :ignore_tables, ["companies"] | ||
end | ||
CONFIG | ||
|
||
refute_problems | ||
end | ||
end |