Skip to content

Commit

Permalink
Merge pull request #203 from pocke/rails5-expression-index-for-Unique…
Browse files Browse the repository at this point in the history
…ValidationWithoutIndex-cop

[Fix #200] Make SchemaLoader aware of Rails 5 expression index
  • Loading branch information
koic authored Feb 20, 2020
2 parents 49d09f7 + 2b3b9ca commit 652bb56
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
13 changes: 9 additions & 4 deletions lib/rubocop/rails/schema_loader/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -118,20 +118,25 @@ def analyze_keywords!(node)

# Reprecent an index
class Index
attr_reader :name, :columns, :unique
attr_reader :name, :columns, :expression, :unique

def initialize(node)
node.first_argument
@columns = build_columns(node)
@columns, @expression = build_columns_or_expr(node)
@unique = nil

analyze_keywords!(node)
end

private

def build_columns(node)
node.first_argument.values.map(&:value)
def build_columns_or_expr(node)
arg = node.first_argument
if arg.array_type?
[arg.values.map(&:value), nil]
else
[[], arg.value]
end
end

def analyze_keywords!(node)
Expand Down
8 changes: 8 additions & 0 deletions spec/rubocop/rails/schema_loader_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
create_table "articles", force: :cascade do |t|
t.string "title", null: false
t.bigint "user_id"
t.index 'lower(title)', name: 'index_title_lower_unique', unique: true
end
end
RUBY
Expand Down Expand Up @@ -77,6 +78,13 @@
expect(table.columns.size).to eq 2
expect(table.columns.last.type).to eq :bigint
end

it 'has an index in articles table' do
table = loaded_schema.table_by(name: 'articles')
expect(table.indices.size).to eq 1
expect(table.indices.first.name).to eq 'index_title_lower_unique'
expect(table.indices.first.unique).to be true
end
end

context 'when the current directory is Rails.root' do
Expand Down

0 comments on commit 652bb56

Please sign in to comment.