aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb10
-rw-r--r--activerecord/test/cases/adapters/sqlite3/sqlite3_adapter_test.rb31
2 files changed, 35 insertions, 6 deletions
diff --git a/activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb b/activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb
index 65aba20052..c72db15ce3 100644
--- a/activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb
+++ b/activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb
@@ -404,22 +404,24 @@ module ActiveRecord
def copy_table(from, to, options = {})
from_primary_key = primary_key(from)
- from_primary_key_column = columns(from).select { |column| column.name == from_primary_key }.first
options[:id] = false
create_table(to, options) do |definition|
@definition = definition
- @definition.primary_key(from_primary_key, from_primary_key_column.type) if from_primary_key.present?
+ if from_primary_key.is_a?(Array)
+ @definition.primary_keys from_primary_key
+ end
columns(from).each do |column|
column_name = options[:rename] ?
(options[:rename][column.name] ||
options[:rename][column.name.to_sym] ||
column.name) : column.name
- next if column_name == from_primary_key
@definition.column(column_name, column.type,
limit: column.limit, default: column.default,
precision: column.precision, scale: column.scale,
- null: column.null, collation: column.collation)
+ null: column.null, collation: column.collation,
+ primary_key: column_name == from_primary_key
+ )
end
yield @definition if block_given?
end
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