aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorEileen M. Uchitelle <eileencodes@users.noreply.github.com>2017-12-07 07:58:21 -0500
committerGitHub <noreply@github.com>2017-12-07 07:58:21 -0500
commitbbacd60048a8efa1777a01292a9392e146a7d885 (patch)
tree8b2b0cdefaf456cf6b9461d5117bf66be3ef6552
parente2c1d5e1d73322945c6d0477c5d8a004479e6b7a (diff)
parent5ac4f4d2563e7f9c5ffaecce4be4b9e2c5b0c081 (diff)
downloadrails-bbacd60048a8efa1777a01292a9392e146a7d885.tar.gz
rails-bbacd60048a8efa1777a01292a9392e146a7d885.tar.bz2
rails-bbacd60048a8efa1777a01292a9392e146a7d885.zip
Merge pull request #31327 from aellispierce/custom-id-change-table-sqlite
Fix sqlite migrations with custom primary keys
-rw-r--r--activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb6
-rw-r--r--activerecord/test/cases/adapters/sqlite3/sqlite3_adapter_test.rb18
2 files changed, 23 insertions, 1 deletions
diff --git a/activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb b/activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb
index 574a7b4572..65aba20052 100644
--- a/activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb
+++ b/activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb
@@ -404,10 +404,11 @@ 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) if from_primary_key.present?
+ @definition.primary_key(from_primary_key, from_primary_key_column.type) if from_primary_key.present?
columns(from).each do |column|
column_name = options[:rename] ?
(options[:rename][column.name] ||
@@ -431,6 +432,9 @@ module ActiveRecord
def copy_table_indexes(from, to, rename = {})
indexes(from).each do |index|
name = index.name
+ # indexes sqlite creates for internal use start with `sqlite_` and
+ # don't need to be copied
+ next if name.starts_with?("sqlite_")
if to == "a#{from}"
name = "t#{name}"
elsif from == "a#{to}"
diff --git a/activerecord/test/cases/adapters/sqlite3/sqlite3_adapter_test.rb b/activerecord/test/cases/adapters/sqlite3/sqlite3_adapter_test.rb
index 1f057fe5c6..14f4997d5b 100644
--- a/activerecord/test/cases/adapters/sqlite3/sqlite3_adapter_test.rb
+++ b/activerecord/test/cases/adapters/sqlite3/sqlite3_adapter_test.rb
@@ -360,6 +360,24 @@ module ActiveRecord
end
end
+ class Barcode < ActiveRecord::Base
+ end
+
+ def test_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"
+
+ connection.change_table "barcodes" do |t|
+ connection.remove_column("barcodes", "other_attr")
+ end
+
+ assert_equal code, Barcode.first.id
+ end
+
def test_supports_extensions
assert_not @conn.supports_extensions?, "does not support extensions"
end