diff options
author | Yves Senn <yves.senn@gmail.com> | 2012-10-28 18:51:26 +0100 |
---|---|---|
committer | Yves Senn <yves.senn@gmail.com> | 2012-10-28 20:26:52 +0100 |
commit | b104157314e95115b74e7ddd968f9c7734309a6e (patch) | |
tree | c4c40aab31ac90427379b1b1d8e8d41bb4819d0e | |
parent | c82f0d76e4cfc9a80fe9988f5d1ca6f6a5594eed (diff) | |
download | rails-b104157314e95115b74e7ddd968f9c7734309a6e.tar.gz rails-b104157314e95115b74e7ddd968f9c7734309a6e.tar.bz2 rails-b104157314e95115b74e7ddd968f9c7734309a6e.zip |
refactor `SQLite3Adapter#copy_table` to prevent primary key redefinitions. #6378
-rw-r--r-- | activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb | 8 | ||||
-rw-r--r-- | activerecord/test/cases/adapters/sqlite3/copy_table_test.rb | 9 |
2 files changed, 11 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 4d5cb72c67..c5a859475f 100644 --- a/activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb @@ -519,24 +519,22 @@ module ActiveRecord def copy_table(from, to, options = {}) #:nodoc: from_primary_key = primary_key(from) - options[:primary_key] = from_primary_key if from_primary_key != 'id' - unless options[:primary_key] - options[:id] = columns(from).detect{|c| c.name == 'id'}.present? && from_primary_key == 'id' - end + options[:id] = false create_table(to, options) do |definition| @definition = definition + @definition.primary_key(from_primary_key) if from_primary_key.present? 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) end - @definition.primary_key(from_primary_key) if from_primary_key yield @definition if block_given? end diff --git a/activerecord/test/cases/adapters/sqlite3/copy_table_test.rb b/activerecord/test/cases/adapters/sqlite3/copy_table_test.rb index 74288a98d1..d03d1dd94c 100644 --- a/activerecord/test/cases/adapters/sqlite3/copy_table_test.rb +++ b/activerecord/test/cases/adapters/sqlite3/copy_table_test.rb @@ -32,6 +32,11 @@ class CopyTableTest < ActiveRecord::TestCase end end + def test_copy_table_allows_to_pass_options_to_create_table + @connection.create_table('blocker_table') + test_copy_table('customers', 'blocker_table', force: true) + end + def test_copy_table_with_index test_copy_table('comments', 'comments_with_index') do @connection.add_index('comments_with_index', ['post_id', 'type']) @@ -43,7 +48,9 @@ class CopyTableTest < ActiveRecord::TestCase end def test_copy_table_without_primary_key - test_copy_table('developers_projects', 'programmers_projects') + test_copy_table('developers_projects', 'programmers_projects') do + assert_nil @connection.primary_key('programmers_projects') + end end def test_copy_table_with_id_col_that_is_not_primary_key |