Skip partial indexes and conditional validators in uniqueness checkers#277
Skip partial indexes and conditional validators in uniqueness checkers#277
Conversation
…artial indexes Co-authored-by: djezzzl <9407725+djezzzl@users.noreply.github.com>
Co-authored-by: djezzzl <9407725+djezzzl@users.noreply.github.com>
|
@copilot fix PR so that CI is green. |
…est specs Co-authored-by: djezzzl <9407725+djezzzl@users.noreply.github.com>
| let(:attribute) { :account_id } | ||
| let(:klass) do | ||
| define_class do |klass| | ||
| klass.validates :account_id, uniqueness: { scope: :is_default, conditions: -> { where(is_default: true) } } |
There was a problem hiding this comment.
Remove passing scope. Keep only conditions.
There was a problem hiding this comment.
Done in dcb7ecd — removed scope:, kept only conditions:.
| context 'when partial unique index is provided' do | ||
| before do | ||
| define_database_with_entity do |table| | ||
| table.integer :account_id | ||
| table.boolean :is_default | ||
| table.index %i[account_id], unique: true, where: 'is_default = true' | ||
| end | ||
| end | ||
|
|
||
| specify do | ||
| expect(checker.report).to be_nil | ||
| end | ||
| end | ||
|
|
||
| context 'when no index is provided' do | ||
| before do | ||
| define_database_with_entity do |table| | ||
| table.integer :account_id | ||
| table.boolean :is_default | ||
| end | ||
| end | ||
|
|
||
| specify do | ||
| expect(checker.report).to be_nil | ||
| end | ||
| end |
There was a problem hiding this comment.
Keep only a single specify where expect(checker.report).to be_nil because it doesn't rely on the actual index presence anymore.
There was a problem hiding this comment.
Done in dcb7ecd — collapsed to a single specify with expect(checker.report).to be_nil.
| @@ -0,0 +1,41 @@ | |||
| # frozen_string_literal: true | |||
|
|
|||
| RSpec.describe DatabaseConsistency::Checkers::UniqueIndexChecker, :postgresql do | |||
There was a problem hiding this comment.
Remove this spec file. Instead, adjust unique_index_checker_spec.rb.
There was a problem hiding this comment.
Done in dcb7ecd — deleted the file and added a :postgresql-tagged context directly in unique_index_checker_spec.rb.
| end | ||
| end | ||
|
|
||
| context 'when uniqueness validation has conditions option' do |
There was a problem hiding this comment.
Move this test to checker/missing_unique_index_checker_spec.rb
There was a problem hiding this comment.
Done in dcb7ecd — moved to spec/checkers/missing_unique_index_checker_spec.rb.
Co-authored-by: djezzzl <9407725+djezzzl@users.noreply.github.com>
| context 'when unique partial index exists', :postgresql do | ||
| before do | ||
| define_database_with_entity do |table| | ||
| table.integer :account_id | ||
| table.boolean :is_default | ||
| table.index %i[account_id], unique: true, name: index_name, where: 'is_default = true' | ||
| end | ||
| end | ||
|
|
||
| let(:klass) { define_class } | ||
|
|
||
| specify do | ||
| expect(checker.report).to be_nil | ||
| end | ||
| end |
There was a problem hiding this comment.
Yes, you're right. This test is only supported by PostgreSQL, but maybe also SQLite?
Therefore, please extract it to a separate file.
There was a problem hiding this comment.
Done in a705f4b. SQLite does support partial indexes with WHERE clauses (confirmed by existing tests in redundant_index_checker/sqlite_spec.rb). Extracted the test to separate files: spec/checkers/unique_index_checker/sqlite_spec.rb (using is_default = 1 for SQLite boolean syntax) and spec/checkers/unique_index_checker/postgresql_spec.rb, following the same pattern as redundant_unique_index_checker/.
…spec.rb files Co-authored-by: djezzzl <9407725+djezzzl@users.noreply.github.com>
UniqueIndexCheckerto skip partial indexes (indexes with WHERE clause)MissingUniqueIndexCheckerto skip validators withconditions:optionmissing_unique_index_checker_spec.rbspec/checkers/unique_index_checker/sqlite_spec.rbandspec/checkers/unique_index_checker/postgresql_spec.rbUnreleasedsectionOriginal prompt
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.