diff options
author | Ryuta Kamizono <kamipo@gmail.com> | 2017-12-08 10:49:25 +0900 |
---|---|---|
committer | Ryuta Kamizono <kamipo@gmail.com> | 2017-12-08 11:05:33 +0900 |
commit | 131cc6eab64eeae6c7f508dca4176183144cf3a6 (patch) | |
tree | 5513316e752360c6d49fcf566ce1d3a8828ffde4 /activerecord/test/cases/adapters/sqlite3 | |
parent | e88e6cea2113ce3e54410cbd8c2da92b86f83d2b (diff) | |
download | rails-131cc6eab64eeae6c7f508dca4176183144cf3a6.tar.gz rails-131cc6eab64eeae6c7f508dca4176183144cf3a6.tar.bz2 rails-131cc6eab64eeae6c7f508dca4176183144cf3a6.zip |
SQLite: Fix `copy_table` with composite primary keys
`connection.primary_key` also return composite primary keys, so
`from_primary_key_column` may not be found even if `from_primary_key` is
presented.
```
% ARCONN=sqlite3 be ruby -w -Itest
test/cases/adapters/sqlite3/sqlite3_adapter_test.rb -n
test_copy_table_with_composite_primary_keys
Using sqlite3
Run options: -n test_copy_table_with_composite_primary_keys --seed 19041
# Running:
E
Error:
ActiveRecord::ConnectionAdapters::SQLite3AdapterTest#test_copy_table_with_composite_primary_keys:
NoMethodError: undefined method `type' for nil:NilClass
/path/to/rails/activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb:411:in
`block in copy_table'
```
This change fixes `copy_table` to do not lose composite primary keys.
Diffstat (limited to 'activerecord/test/cases/adapters/sqlite3')
-rw-r--r-- | activerecord/test/cases/adapters/sqlite3/sqlite3_adapter_test.rb | 31 |
1 files changed, 29 insertions, 2 deletions
diff --git a/activerecord/test/cases/adapters/sqlite3/sqlite3_adapter_test.rb b/activerecord/test/cases/adapters/sqlite3/sqlite3_adapter_test.rb index 14f4997d5b..1357719422 100644 --- a/activerecord/test/cases/adapters/sqlite3/sqlite3_adapter_test.rb +++ b/activerecord/test/cases/adapters/sqlite3/sqlite3_adapter_test.rb @@ -361,21 +361,48 @@ module ActiveRecord end class Barcode < ActiveRecord::Base + self.primary_key = "code" end - def test_existing_records_have_custom_primary_key + def test_copy_table_with_existing_records_have_custom_primary_key connection = Barcode.connection connection.create_table(:barcodes, primary_key: "code", id: :string, limit: 42, force: true) do |t| t.text :other_attr end code = "214fe0c2-dd47-46df-b53b-66090b3c1d40" - Barcode.create! code: code, other_attr: "xxx" + Barcode.create!(code: code, other_attr: "xxx") connection.change_table "barcodes" do |t| connection.remove_column("barcodes", "other_attr") end assert_equal code, Barcode.first.id + ensure + Barcode.reset_column_information + end + + def test_copy_table_with_composite_primary_keys + connection = Barcode.connection + connection.create_table(:barcodes, primary_key: ["region", "code"], force: true) do |t| + t.string :region + t.string :code + t.text :other_attr + end + region = "US" + code = "214fe0c2-dd47-46df-b53b-66090b3c1d40" + Barcode.create!(region: region, code: code, other_attr: "xxx") + + connection.change_table "barcodes" do |t| + connection.remove_column("barcodes", "other_attr") + end + + assert_equal ["region", "code"], connection.primary_keys("barcodes") + + barcode = Barcode.first + assert_equal region, barcode.region + assert_equal code, barcode.code + ensure + Barcode.reset_column_information end def test_supports_extensions |