Skip to content

Commit

Permalink
Merge pull request #307 from abicky/fix-unexpected-warning
Browse files Browse the repository at this point in the history
Fix unexpected warning when a foreign key is added on the primary key
  • Loading branch information
abicky committed Aug 9, 2020
2 parents ff2fed8 + bbb0f25 commit 7266635
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 6 deletions.
3 changes: 2 additions & 1 deletion lib/ridgepole/dsl_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,11 @@ 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.
Although an index will be added automatically by InnoDB, please add an index explicitly for your future operations.
Although an index will be added automatically by InnoDB, please add an index explicitly before the next operation.
MSG
end
end
Expand Down
30 changes: 25 additions & 5 deletions spec/mysql/fk/migrate_create_fk_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -188,13 +188,11 @@
it {
expect(Ridgepole::Logger.instance).to receive(:warn).with(<<-MSG).twice
[WARNING] Table `child` has a foreign key on `parent_id` column, but doesn't have any indexes on the column.
Although an index will be added automatically by InnoDB, please add an index explicitly for your future operations.
Although an index will be added automatically by InnoDB, please add an index explicitly before the next operation.
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 7266635

Please sign in to comment.