djezzzl / database_consistency

The tool to avoid various issues due to inconsistencies and inefficiencies between a database schema and application models.
MIT License
1.02k stars 43 forks source link

False-positive when index columns are defined as a string and not array #198

Closed iatanas0v closed 1 year ago

iatanas0v commented 1 year ago

Defining index columns as a string instead of an array leads to false-positive output. You can see the schema details below. The column that's reported as missing index is part of a compound index and is the first column in that index, so it'd be used when querying.

The problem is that the database_consistency check does not work with index columns defined as string.

After some debugging, I pinpointed the problem to this line: https://github.com/djezzzl/database_consistency/blob/3bbdb228ec80b1b02d5ab5bea9415eaa367a5069/lib/database_consistency/checkers/association_checkers/missing_index_checker.rb#L89

Schema

create_table "lists", force: :cascade do |t|
  t.string "name"
end

create_table "items", force: :cascade do |t|
  t.string "name" 
  t.bigint "list_id", null: false
  t.index "list_id, lower((name)::text)", name: "index_list_id_and_name", unique: true
end

Models

class Item < ApplicationRecord
  db_belongs_to :list, inverse_of: :items
end

class List < ApplicationRecord 
  has_many :items, inverse_of: :list, dependent: :destroy
end

Migration for the index

def change
    add_index :items,
        'list_id, lower(name)',
        unique: true,
        algorithm: :concurrently,
        name: :index_list_id_and_name
end

Output

MissingIndexChecker fail **mList items associated model should have proper index in the database

Expected Output No errors to be reported

djezzzl commented 1 year ago

Hi @iatanas0v,

Thank you a lot for using the gem and reporting the issue!

I will fix it as soon as possible.

djezzzl commented 1 year ago

Hey,

I just released 1.7.10. Please try it out, and let me know if the issue was fixed.

Please feel free to reopen the issue if you need to.

Have a great day!

iatanas0v commented 1 year ago

@djezzzl works great :rocket: thanks for the quick fix

djezzzl commented 1 year ago

I'm happy to hear that!

Have a great rest of the week!