Skip to content

Commit

Permalink
Check option "primary_key" in check_foreign_key_without_index
Browse files Browse the repository at this point in the history
This commit resolves #293.
  • Loading branch information
abicky committed Aug 9, 2020
1 parent ff2fed8 commit 65e6097
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
1 change: 1 addition & 0 deletions lib/ridgepole/dsl_parser.rb
Expand Up @@ -37,6 +37,7 @@ def check_foreign_key_without_index(table_name, attrs)
attrs[:foreign_keys].each do |_, foreign_key_attrs|
fk_index = foreign_key_attrs[:options][:column] || "#{foreign_key_attrs[:to_table].singularize}_id"
next if attrs[:indices]&.any? { |_k, v| v[:column_name].first == fk_index }
next if attrs[:options][:primary_key] == fk_index

Ridgepole::Logger.instance.warn(<<-MSG)
[WARNING] Table `#{table_name}` has a foreign key on `#{fk_index}` column, but doesn't have any indexes on the column.
Expand Down
28 changes: 24 additions & 4 deletions spec/mysql/fk/migrate_create_fk_spec.rb
Expand Up @@ -192,9 +192,7 @@
MSG
subject.diff(dsl).migrate

expect do
subject.diff(dsl).migrate
end.to raise_error(/Mysql2::Error: Cannot drop index/)
expect(subject.diff(dsl).differ?).to be_truthy
}
end

Expand All @@ -219,7 +217,29 @@
expect(Ridgepole::Logger.instance).to_not receive(:warn)
subject.diff(dsl).migrate

expect { subject.diff(dsl).migrate }.to_not raise_error
expect(subject.diff(dsl).differ?).to be_falsey
}
end

context 'when create fk on the primary key' do
let(:dsl) do
erbh(<<-ERB)
create_table "users", force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8" do |t|
end
create_table "icons", primary_key: "user_id", force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8" do |t|
end
add_foreign_key "icons", "users", name: "fk_icons_users"
ERB
end

subject { client(dump_without_table_options: false) }

it {
expect(Ridgepole::Logger.instance).to_not receive(:warn)
subject.diff(dsl).migrate

expect(subject.diff(dsl).differ?).to be_falsey
}
end
end

0 comments on commit 65e6097

Please sign in to comment.