Permalink
Browse files

Merge pull request #31361 from aellispierce/backport-fix-custom-id-sq…

…lite

Backport fix sqlite migrations with custom primary keys to 5-1-stable
  • Loading branch information...
eileencodes committed Dec 7, 2017
2 parents e9b7795 + 832520c commit 5f4b57d3894de9a2d45eff55149e8b7ce6849c82
@@ -437,10 +437,11 @@ def move_table(from, to, options = {}, &block)
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] ||
@@ -464,6 +465,9 @@ def copy_table(from, to, options = {})
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}"
@@ -366,6 +366,24 @@ def test_no_primary_key
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

0 comments on commit 5f4b57d

Please sign in to comment.