diff --git a/lib/rubocop/rails/schema_loader/schema.rb b/lib/rubocop/rails/schema_loader/schema.rb index cbb1882926..36d18b76ff 100644 --- a/lib/rubocop/rails/schema_loader/schema.rb +++ b/lib/rubocop/rails/schema_loader/schema.rb @@ -118,11 +118,11 @@ 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) @@ -130,8 +130,13 @@ def initialize(node) 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) diff --git a/spec/rubocop/rails/schema_loader_spec.rb b/spec/rubocop/rails/schema_loader_spec.rb index 81248508ff..29c0f55105 100644 --- a/spec/rubocop/rails/schema_loader_spec.rb +++ b/spec/rubocop/rails/schema_loader_spec.rb @@ -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 @@ -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