2017-07-09 13:41:28 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2011-06-06 14:17:44 -04:00
|
|
|
require "cases/helper"
|
2016-08-06 12:26:20 -04:00
|
|
|
require "support/schema_dumping_helper"
|
|
|
|
require "models/topic"
|
|
|
|
require "models/reply"
|
|
|
|
require "models/subscriber"
|
|
|
|
require "models/movie"
|
|
|
|
require "models/keyboard"
|
|
|
|
require "models/mixed_case_monkey"
|
|
|
|
require "models/dashboard"
|
2017-02-07 10:41:56 -05:00
|
|
|
require "models/non_primary_key"
|
2004-11-23 20:04:44 -05:00
|
|
|
|
2008-01-21 12:20:51 -05:00
|
|
|
class PrimaryKeysTest < ActiveRecord::TestCase
|
2007-03-08 22:23:37 -05:00
|
|
|
fixtures :topics, :subscribers, :movies, :mixed_case_monkeys
|
2004-11-23 20:04:44 -05:00
|
|
|
|
2010-02-20 21:05:28 -05:00
|
|
|
def test_to_key_with_default_primary_key
|
Adds #key and #to_param to the AMo interface
This commit introduces two new methods that every
AMo compliant object must implement. Below are the
default implementations along with the implied
interface contract.
# Returns an Enumerable of all (primary) key
# attributes or nil if new_record? is true
def key
new_record? ? nil : [1]
end
# Returns a string representing the object's key
# suitable for use in URLs, or nil if new_record?
# is true
def to_param
key ? key.first.to_s : nil
end
1) The #key method
Previously rails' record_identifier code, which is
used in the #dom_id helper, relied on calling #id
on the record to provide a reasonable DOM id. Now
with rails3 being all ORM agnostic, it's not safe
anymore to assume that every record ever will have
an #id as its primary key attribute.
Having a #key method available on every AMo object
means that #dom_id can be implemented using
record.to_model.key # instead of
record.id
Using this we're able to take composite primary
keys into account (e.g. available in datamapper)
by implementing #dom_id using a newly added
record_key_for_dom_id(record)
method. The user can overwrite this method to
provide customized versions of the object's key
used in #dom_id.
Also, dealing with more complex keys that can
contain arbitrary strings, means that we need to
make sure that we only provide DOM ids that are
valid according to the spec. For this reason, this
patch sends the key provided through a newly added
sanitize_dom_id(candidate_id)
method, that makes sure we only produce valid HTML
The reason to not just add #dom_id to the AMo
interface was that it feels like providing a DOM
id should not be a model concern. Adding #dom_id
to the AMo interface would force these concern on
the model, while it's better left to be implemented
in a helper.
Now one could say the same is true for #to_param,
and actually I think that it doesn't really fit
into the model either, but it's used in AR and it's
a main part of integrating into the rails router.
This is different from #dom_id which is only used
in view helpers and can be implemented on top of a
semantically more meaningful method like #key.
2) The #to_param method
Since the rails router relies on #to_param to be
present, AR::Base implements it and returns the
id by default, allowing the user to overwrite the
method if desired.
Now with different ORMs integrating into rails,
every ORM railtie needs to implement it's own
#to_param implementation while already providing
code to be AMo compliant. Since the whole point of
AMo compliance seems to be to integrate any ORM
seamlessly into rails, it seems fair that all we
really need to do as another ORM, is to be AMo
compliant. By including #to_param into the official
interface, we can make sure that this code can be
centralized in the various AMo compliance layers,
and not be added separately by every ORM railtie.
3) All specs pass
2010-02-20 02:24:10 -05:00
|
|
|
topic = Topic.new
|
2010-08-16 21:29:57 -04:00
|
|
|
assert_nil topic.to_key
|
Adds #key and #to_param to the AMo interface
This commit introduces two new methods that every
AMo compliant object must implement. Below are the
default implementations along with the implied
interface contract.
# Returns an Enumerable of all (primary) key
# attributes or nil if new_record? is true
def key
new_record? ? nil : [1]
end
# Returns a string representing the object's key
# suitable for use in URLs, or nil if new_record?
# is true
def to_param
key ? key.first.to_s : nil
end
1) The #key method
Previously rails' record_identifier code, which is
used in the #dom_id helper, relied on calling #id
on the record to provide a reasonable DOM id. Now
with rails3 being all ORM agnostic, it's not safe
anymore to assume that every record ever will have
an #id as its primary key attribute.
Having a #key method available on every AMo object
means that #dom_id can be implemented using
record.to_model.key # instead of
record.id
Using this we're able to take composite primary
keys into account (e.g. available in datamapper)
by implementing #dom_id using a newly added
record_key_for_dom_id(record)
method. The user can overwrite this method to
provide customized versions of the object's key
used in #dom_id.
Also, dealing with more complex keys that can
contain arbitrary strings, means that we need to
make sure that we only provide DOM ids that are
valid according to the spec. For this reason, this
patch sends the key provided through a newly added
sanitize_dom_id(candidate_id)
method, that makes sure we only produce valid HTML
The reason to not just add #dom_id to the AMo
interface was that it feels like providing a DOM
id should not be a model concern. Adding #dom_id
to the AMo interface would force these concern on
the model, while it's better left to be implemented
in a helper.
Now one could say the same is true for #to_param,
and actually I think that it doesn't really fit
into the model either, but it's used in AR and it's
a main part of integrating into the rails router.
This is different from #dom_id which is only used
in view helpers and can be implemented on top of a
semantically more meaningful method like #key.
2) The #to_param method
Since the rails router relies on #to_param to be
present, AR::Base implements it and returns the
id by default, allowing the user to overwrite the
method if desired.
Now with different ORMs integrating into rails,
every ORM railtie needs to implement it's own
#to_param implementation while already providing
code to be AMo compliant. Since the whole point of
AMo compliance seems to be to integrate any ORM
seamlessly into rails, it seems fair that all we
really need to do as another ORM, is to be AMo
compliant. By including #to_param into the official
interface, we can make sure that this code can be
centralized in the various AMo compliance layers,
and not be added separately by every ORM railtie.
3) All specs pass
2010-02-20 02:24:10 -05:00
|
|
|
topic = Topic.find(1)
|
2010-07-16 22:24:18 -04:00
|
|
|
assert_equal [1], topic.to_key
|
2010-02-20 21:05:28 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_to_key_with_customized_primary_key
|
|
|
|
keyboard = Keyboard.new
|
2010-05-19 15:14:51 -04:00
|
|
|
assert_nil keyboard.to_key
|
2010-02-20 21:05:28 -05:00
|
|
|
keyboard.save
|
|
|
|
assert_equal keyboard.to_key, [keyboard.id]
|
Adds #key and #to_param to the AMo interface
This commit introduces two new methods that every
AMo compliant object must implement. Below are the
default implementations along with the implied
interface contract.
# Returns an Enumerable of all (primary) key
# attributes or nil if new_record? is true
def key
new_record? ? nil : [1]
end
# Returns a string representing the object's key
# suitable for use in URLs, or nil if new_record?
# is true
def to_param
key ? key.first.to_s : nil
end
1) The #key method
Previously rails' record_identifier code, which is
used in the #dom_id helper, relied on calling #id
on the record to provide a reasonable DOM id. Now
with rails3 being all ORM agnostic, it's not safe
anymore to assume that every record ever will have
an #id as its primary key attribute.
Having a #key method available on every AMo object
means that #dom_id can be implemented using
record.to_model.key # instead of
record.id
Using this we're able to take composite primary
keys into account (e.g. available in datamapper)
by implementing #dom_id using a newly added
record_key_for_dom_id(record)
method. The user can overwrite this method to
provide customized versions of the object's key
used in #dom_id.
Also, dealing with more complex keys that can
contain arbitrary strings, means that we need to
make sure that we only provide DOM ids that are
valid according to the spec. For this reason, this
patch sends the key provided through a newly added
sanitize_dom_id(candidate_id)
method, that makes sure we only produce valid HTML
The reason to not just add #dom_id to the AMo
interface was that it feels like providing a DOM
id should not be a model concern. Adding #dom_id
to the AMo interface would force these concern on
the model, while it's better left to be implemented
in a helper.
Now one could say the same is true for #to_param,
and actually I think that it doesn't really fit
into the model either, but it's used in AR and it's
a main part of integrating into the rails router.
This is different from #dom_id which is only used
in view helpers and can be implemented on top of a
semantically more meaningful method like #key.
2) The #to_param method
Since the rails router relies on #to_param to be
present, AR::Base implements it and returns the
id by default, allowing the user to overwrite the
method if desired.
Now with different ORMs integrating into rails,
every ORM railtie needs to implement it's own
#to_param implementation while already providing
code to be AMo compliant. Since the whole point of
AMo compliance seems to be to integrate any ORM
seamlessly into rails, it seems fair that all we
really need to do as another ORM, is to be AMo
compliant. By including #to_param into the official
interface, we can make sure that this code can be
centralized in the various AMo compliance layers,
and not be added separately by every ORM railtie.
3) All specs pass
2010-02-20 02:24:10 -05:00
|
|
|
end
|
|
|
|
|
2011-12-01 16:55:59 -05:00
|
|
|
def test_read_attribute_with_custom_primary_key
|
|
|
|
keyboard = Keyboard.create!
|
|
|
|
assert_equal keyboard.key_number, keyboard.read_attribute(:id)
|
|
|
|
end
|
|
|
|
|
2010-03-29 19:04:34 -04:00
|
|
|
def test_to_key_with_primary_key_after_destroy
|
|
|
|
topic = Topic.find(1)
|
|
|
|
topic.destroy
|
2011-08-05 17:42:41 -04:00
|
|
|
assert_equal [1], topic.to_key
|
2010-03-29 19:04:34 -04:00
|
|
|
end
|
|
|
|
|
2004-11-23 20:04:44 -05:00
|
|
|
def test_integer_key
|
|
|
|
topic = Topic.find(1)
|
2005-06-10 10:58:02 -04:00
|
|
|
assert_equal(topics(:first).author_name, topic.author_name)
|
2004-11-23 20:04:44 -05:00
|
|
|
topic = Topic.find(2)
|
2005-06-10 10:58:02 -04:00
|
|
|
assert_equal(topics(:second).author_name, topic.author_name)
|
2004-11-23 20:04:44 -05:00
|
|
|
|
|
|
|
topic = Topic.new
|
|
|
|
topic.title = "New Topic"
|
2010-05-19 15:14:51 -04:00
|
|
|
assert_nil topic.id
|
2017-06-05 22:56:12 -04:00
|
|
|
topic.save!
|
2004-11-23 20:04:44 -05:00
|
|
|
id = topic.id
|
|
|
|
|
|
|
|
topicReloaded = Topic.find(id)
|
|
|
|
assert_equal("New Topic", topicReloaded.title)
|
|
|
|
end
|
|
|
|
|
2005-10-12 15:55:46 -04:00
|
|
|
def test_customized_primary_key_auto_assigns_on_save
|
2005-10-25 14:14:09 -04:00
|
|
|
Keyboard.delete_all
|
2016-08-06 13:37:57 -04:00
|
|
|
keyboard = Keyboard.new(name: "HHKB")
|
2017-06-05 22:56:12 -04:00
|
|
|
keyboard.save!
|
2016-08-06 12:26:20 -04:00
|
|
|
assert_equal keyboard.id, Keyboard.find_by_name("HHKB").id
|
2005-10-12 15:55:46 -04:00
|
|
|
end
|
|
|
|
|
2005-10-13 16:44:39 -04:00
|
|
|
def test_customized_primary_key_can_be_get_before_saving
|
2005-10-12 15:55:46 -04:00
|
|
|
keyboard = Keyboard.new
|
2005-10-24 12:45:22 -04:00
|
|
|
assert_nil keyboard.id
|
2017-06-05 22:56:12 -04:00
|
|
|
assert_nil keyboard.key_number
|
2005-10-12 15:55:46 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_customized_string_primary_key_settable_before_save
|
|
|
|
subscriber = Subscriber.new
|
2017-06-05 22:56:12 -04:00
|
|
|
subscriber.id = "webster123"
|
2016-08-06 12:26:20 -04:00
|
|
|
assert_equal "webster123", subscriber.id
|
|
|
|
assert_equal "webster123", subscriber.nick
|
2005-10-12 15:55:46 -04:00
|
|
|
end
|
|
|
|
|
2017-06-05 22:56:12 -04:00
|
|
|
def test_update_with_non_primary_key_id_column
|
|
|
|
subscriber = Subscriber.first
|
|
|
|
subscriber.update(update_count: 1)
|
|
|
|
subscriber.reload
|
|
|
|
assert_equal 1, subscriber.update_count
|
|
|
|
end
|
|
|
|
|
2017-06-15 08:00:20 -04:00
|
|
|
def test_update_columns_with_non_primary_key_id_column
|
|
|
|
subscriber = Subscriber.first
|
|
|
|
subscriber.update_columns(id: 1)
|
|
|
|
assert_not_equal 1, subscriber.nick
|
|
|
|
end
|
|
|
|
|
2004-11-23 20:04:44 -05:00
|
|
|
def test_string_key
|
2005-06-10 10:58:02 -04:00
|
|
|
subscriber = Subscriber.find(subscribers(:first).nick)
|
|
|
|
assert_equal(subscribers(:first).name, subscriber.name)
|
|
|
|
subscriber = Subscriber.find(subscribers(:second).nick)
|
|
|
|
assert_equal(subscribers(:second).name, subscriber.name)
|
2004-11-23 20:04:44 -05:00
|
|
|
|
|
|
|
subscriber = Subscriber.new
|
|
|
|
subscriber.id = "jdoe"
|
|
|
|
assert_equal("jdoe", subscriber.id)
|
|
|
|
subscriber.name = "John Doe"
|
2017-06-05 22:56:12 -04:00
|
|
|
subscriber.save!
|
2005-11-14 17:24:55 -05:00
|
|
|
assert_equal("jdoe", subscriber.id)
|
2004-11-23 20:04:44 -05:00
|
|
|
|
|
|
|
subscriberReloaded = Subscriber.find("jdoe")
|
|
|
|
assert_equal("John Doe", subscriberReloaded.name)
|
|
|
|
end
|
|
|
|
|
2017-02-07 10:41:56 -05:00
|
|
|
def test_id_column_that_is_not_primary_key
|
|
|
|
NonPrimaryKey.create!(id: 100)
|
|
|
|
actual = NonPrimaryKey.find_by(id: 100)
|
|
|
|
assert_match %r{<NonPrimaryKey id: 100}, actual.inspect
|
|
|
|
end
|
|
|
|
|
2004-11-23 20:04:44 -05:00
|
|
|
def test_find_with_more_than_one_string_key
|
2005-06-10 10:58:02 -04:00
|
|
|
assert_equal 2, Subscriber.find(subscribers(:first).nick, subscribers(:second).nick).length
|
2004-11-23 20:04:44 -05:00
|
|
|
end
|
2008-01-18 02:30:42 -05:00
|
|
|
|
2004-11-23 20:04:44 -05:00
|
|
|
def test_primary_key_prefix
|
2014-06-06 09:38:14 -04:00
|
|
|
old_primary_key_prefix_type = ActiveRecord::Base.primary_key_prefix_type
|
2004-11-23 20:04:44 -05:00
|
|
|
ActiveRecord::Base.primary_key_prefix_type = :table_name
|
2005-10-06 19:19:55 -04:00
|
|
|
Topic.reset_primary_key
|
2004-11-23 20:04:44 -05:00
|
|
|
assert_equal "topicid", Topic.primary_key
|
|
|
|
|
|
|
|
ActiveRecord::Base.primary_key_prefix_type = :table_name_with_underscore
|
2005-10-06 19:19:55 -04:00
|
|
|
Topic.reset_primary_key
|
2004-11-23 20:04:44 -05:00
|
|
|
assert_equal "topic_id", Topic.primary_key
|
|
|
|
|
|
|
|
ActiveRecord::Base.primary_key_prefix_type = nil
|
2005-10-06 19:19:55 -04:00
|
|
|
Topic.reset_primary_key
|
2004-11-23 20:04:44 -05:00
|
|
|
assert_equal "id", Topic.primary_key
|
2014-06-06 09:38:14 -04:00
|
|
|
ensure
|
|
|
|
ActiveRecord::Base.primary_key_prefix_type = old_primary_key_prefix_type
|
2004-11-23 20:04:44 -05:00
|
|
|
end
|
2008-01-18 02:30:42 -05:00
|
|
|
|
2007-03-08 22:23:37 -05:00
|
|
|
def test_delete_should_quote_pkey
|
|
|
|
assert_nothing_raised { MixedCaseMonkey.delete(1) }
|
|
|
|
end
|
2017-02-10 02:31:18 -05:00
|
|
|
|
2007-03-08 22:23:37 -05:00
|
|
|
def test_update_counters_should_quote_pkey_and_quote_counter_columns
|
2016-08-06 13:37:57 -04:00
|
|
|
assert_nothing_raised { MixedCaseMonkey.update_counters(1, fleaCount: 99) }
|
2007-03-08 22:23:37 -05:00
|
|
|
end
|
2017-02-10 02:31:18 -05:00
|
|
|
|
2007-03-08 22:23:37 -05:00
|
|
|
def test_find_with_one_id_should_quote_pkey
|
|
|
|
assert_nothing_raised { MixedCaseMonkey.find(1) }
|
|
|
|
end
|
2017-02-10 02:31:18 -05:00
|
|
|
|
2007-03-08 22:23:37 -05:00
|
|
|
def test_find_with_multiple_ids_should_quote_pkey
|
2016-10-28 23:05:58 -04:00
|
|
|
assert_nothing_raised { MixedCaseMonkey.find([1, 2]) }
|
2007-03-08 22:23:37 -05:00
|
|
|
end
|
2017-02-10 02:31:18 -05:00
|
|
|
|
2007-03-08 22:23:37 -05:00
|
|
|
def test_instance_update_should_quote_pkey
|
|
|
|
assert_nothing_raised { MixedCaseMonkey.find(1).save }
|
|
|
|
end
|
2017-02-10 02:31:18 -05:00
|
|
|
|
2007-09-28 10:18:47 -04:00
|
|
|
def test_instance_destroy_should_quote_pkey
|
2007-03-08 22:23:37 -05:00
|
|
|
assert_nothing_raised { MixedCaseMonkey.find(1).destroy }
|
|
|
|
end
|
2009-02-26 10:48:58 -05:00
|
|
|
|
2017-02-01 06:11:40 -05:00
|
|
|
def test_primary_key_returns_value_if_it_exists
|
|
|
|
klass = Class.new(ActiveRecord::Base) do
|
|
|
|
self.table_name = "developers"
|
2009-02-26 10:48:58 -05:00
|
|
|
end
|
|
|
|
|
2017-02-01 06:11:40 -05:00
|
|
|
assert_equal "id", klass.primary_key
|
|
|
|
end
|
2014-06-26 11:36:40 -04:00
|
|
|
|
2017-02-01 06:11:40 -05:00
|
|
|
def test_primary_key_returns_nil_if_it_does_not_exist
|
|
|
|
klass = Class.new(ActiveRecord::Base) do
|
|
|
|
self.table_name = "developers_projects"
|
2009-02-26 10:48:58 -05:00
|
|
|
end
|
2017-02-01 06:11:40 -05:00
|
|
|
|
|
|
|
assert_nil klass.primary_key
|
2009-02-26 10:48:58 -05:00
|
|
|
end
|
2011-03-27 04:54:38 -04:00
|
|
|
|
|
|
|
def test_quoted_primary_key_after_set_primary_key
|
2016-10-28 23:05:58 -04:00
|
|
|
k = Class.new(ActiveRecord::Base)
|
2011-03-27 04:54:38 -04:00
|
|
|
assert_equal k.connection.quote_column_name("id"), k.quoted_primary_key
|
|
|
|
k.primary_key = "foo"
|
|
|
|
assert_equal k.connection.quote_column_name("foo"), k.quoted_primary_key
|
|
|
|
end
|
2011-12-15 08:23:18 -05:00
|
|
|
|
2014-02-05 02:52:59 -05:00
|
|
|
def test_auto_detect_primary_key_from_schema
|
|
|
|
MixedCaseMonkey.reset_primary_key
|
|
|
|
assert_equal "monkeyID", MixedCaseMonkey.primary_key
|
|
|
|
end
|
2014-08-06 22:44:00 -04:00
|
|
|
|
|
|
|
def test_primary_key_update_with_custom_key_name
|
2016-08-06 12:26:20 -04:00
|
|
|
dashboard = Dashboard.create!(dashboard_id: "1")
|
|
|
|
dashboard.id = "2"
|
2014-08-06 22:44:00 -04:00
|
|
|
dashboard.save!
|
|
|
|
|
|
|
|
dashboard = Dashboard.first
|
2016-08-06 12:26:20 -04:00
|
|
|
assert_equal "2", dashboard.id
|
2014-08-06 22:44:00 -04:00
|
|
|
end
|
2015-05-18 13:31:02 -04:00
|
|
|
|
2016-07-01 03:10:27 -04:00
|
|
|
def test_create_without_primary_key_no_extra_query
|
2017-02-16 03:34:24 -05:00
|
|
|
skip if current_adapter?(:OracleAdapter)
|
|
|
|
|
2016-07-01 03:10:27 -04:00
|
|
|
klass = Class.new(ActiveRecord::Base) do
|
2016-08-06 12:26:20 -04:00
|
|
|
self.table_name = "dashboards"
|
2016-07-01 03:10:27 -04:00
|
|
|
end
|
|
|
|
klass.create! # warmup schema cache
|
|
|
|
assert_queries(3, ignore_none: true) { klass.create! }
|
|
|
|
end
|
|
|
|
|
2019-04-22 01:41:00 -04:00
|
|
|
def test_assign_id_raises_error_if_primary_key_doesnt_exist
|
|
|
|
klass = Class.new(ActiveRecord::Base) do
|
|
|
|
self.table_name = "dashboards"
|
|
|
|
end
|
|
|
|
dashboard = klass.new
|
|
|
|
assert_raises(ActiveModel::MissingAttributeError) { dashboard.id = "1" }
|
|
|
|
end
|
|
|
|
|
2015-05-18 13:31:02 -04:00
|
|
|
if current_adapter?(:PostgreSQLAdapter)
|
|
|
|
def test_serial_with_quoted_sequence_name
|
|
|
|
column = MixedCaseMonkey.columns_hash[MixedCaseMonkey.primary_key]
|
|
|
|
assert_equal "nextval('\"mixed_case_monkeys_monkeyID_seq\"'::regclass)", column.default_function
|
2018-01-25 18:14:09 -05:00
|
|
|
assert_predicate column, :serial?
|
2015-05-18 13:31:02 -04:00
|
|
|
end
|
2015-05-22 05:25:23 -04:00
|
|
|
|
|
|
|
def test_serial_with_unquoted_sequence_name
|
|
|
|
column = Topic.columns_hash[Topic.primary_key]
|
|
|
|
assert_equal "nextval('topics_id_seq'::regclass)", column.default_function
|
2018-01-25 18:14:09 -05:00
|
|
|
assert_predicate column, :serial?
|
2015-05-22 05:25:23 -04:00
|
|
|
end
|
2015-05-18 13:31:02 -04:00
|
|
|
end
|
2011-10-05 20:21:43 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
class PrimaryKeyWithNoConnectionTest < ActiveRecord::TestCase
|
2015-03-10 22:21:19 -04:00
|
|
|
self.use_transactional_tests = false
|
2011-09-26 13:14:16 -04:00
|
|
|
|
2013-11-08 10:57:51 -05:00
|
|
|
unless in_memory_db?
|
|
|
|
def test_set_primary_key_with_no_connection
|
|
|
|
connection = ActiveRecord::Base.remove_connection
|
2011-09-26 13:14:16 -04:00
|
|
|
|
2013-11-08 10:57:51 -05:00
|
|
|
model = Class.new(ActiveRecord::Base)
|
2016-08-06 12:26:20 -04:00
|
|
|
model.primary_key = "foo"
|
2011-09-26 13:14:16 -04:00
|
|
|
|
2016-08-06 12:26:20 -04:00
|
|
|
assert_equal "foo", model.primary_key
|
2011-09-26 13:14:16 -04:00
|
|
|
|
2013-11-08 10:57:51 -05:00
|
|
|
ActiveRecord::Base.establish_connection(connection)
|
2011-09-26 13:14:16 -04:00
|
|
|
|
2016-08-06 12:26:20 -04:00
|
|
|
assert_equal "foo", model.primary_key
|
2013-11-08 10:57:51 -05:00
|
|
|
end
|
2011-09-26 13:14:16 -04:00
|
|
|
end
|
2004-11-23 20:04:44 -05:00
|
|
|
end
|
2012-01-30 11:32:49 -05:00
|
|
|
|
2016-12-05 13:35:30 -05:00
|
|
|
class PrimaryKeyWithAutoIncrementTest < ActiveRecord::TestCase
|
|
|
|
self.use_transactional_tests = false
|
|
|
|
|
|
|
|
class AutoIncrement < ActiveRecord::Base
|
|
|
|
end
|
|
|
|
|
|
|
|
def setup
|
|
|
|
@connection = ActiveRecord::Base.connection
|
|
|
|
end
|
|
|
|
|
|
|
|
def teardown
|
|
|
|
@connection.drop_table(:auto_increments, if_exists: true)
|
|
|
|
end
|
|
|
|
|
2017-02-16 18:52:52 -05:00
|
|
|
def test_primary_key_with_integer
|
|
|
|
@connection.create_table(:auto_increments, id: :integer, force: true)
|
2016-12-05 15:38:14 -05:00
|
|
|
assert_auto_incremented
|
|
|
|
end
|
2016-12-05 13:35:30 -05:00
|
|
|
|
2017-02-16 18:52:52 -05:00
|
|
|
def test_primary_key_with_bigint
|
|
|
|
@connection.create_table(:auto_increments, id: :bigint, force: true)
|
2016-12-05 15:38:14 -05:00
|
|
|
assert_auto_incremented
|
2016-12-05 13:35:30 -05:00
|
|
|
end
|
2016-12-05 15:38:14 -05:00
|
|
|
|
|
|
|
private
|
|
|
|
def assert_auto_incremented
|
|
|
|
record1 = AutoIncrement.create!
|
|
|
|
assert_not_nil record1.id
|
|
|
|
|
|
|
|
record1.destroy
|
|
|
|
|
|
|
|
record2 = AutoIncrement.create!
|
|
|
|
assert_not_nil record2.id
|
|
|
|
assert_operator record2.id, :>, record1.id
|
|
|
|
end
|
2016-12-05 13:35:30 -05:00
|
|
|
end
|
|
|
|
|
2014-11-15 13:08:46 -05:00
|
|
|
class PrimaryKeyAnyTypeTest < ActiveRecord::TestCase
|
2014-12-11 08:57:28 -05:00
|
|
|
include SchemaDumpingHelper
|
|
|
|
|
2015-03-10 22:21:19 -04:00
|
|
|
self.use_transactional_tests = false
|
2014-11-15 13:08:46 -05:00
|
|
|
|
|
|
|
class Barcode < ActiveRecord::Base
|
|
|
|
end
|
|
|
|
|
|
|
|
setup do
|
|
|
|
@connection = ActiveRecord::Base.connection
|
|
|
|
@connection.create_table(:barcodes, primary_key: "code", id: :string, limit: 42, force: true)
|
|
|
|
end
|
|
|
|
|
|
|
|
teardown do
|
2015-09-12 10:00:57 -04:00
|
|
|
@connection.drop_table(:barcodes, if_exists: true)
|
2014-11-15 13:08:46 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_any_type_primary_key
|
|
|
|
assert_equal "code", Barcode.primary_key
|
|
|
|
|
2016-02-29 07:31:18 -05:00
|
|
|
column = Barcode.column_for_attribute(Barcode.primary_key)
|
2016-02-11 17:01:04 -05:00
|
|
|
assert_not column.null
|
2016-02-29 07:31:18 -05:00
|
|
|
assert_equal :string, column.type
|
|
|
|
assert_equal 42, column.limit
|
2017-12-09 21:58:41 -05:00
|
|
|
ensure
|
|
|
|
Barcode.reset_column_information
|
2014-11-15 13:08:46 -05:00
|
|
|
end
|
2014-12-11 08:57:28 -05:00
|
|
|
|
|
|
|
test "schema dump primary key includes type and options" do
|
|
|
|
schema = dump_table_schema "barcodes"
|
2020-05-23 17:38:21 -04:00
|
|
|
assert_match %r/create_table "barcodes", primary_key: "code", id: { type: :string, limit: 42 }/, schema
|
2018-07-08 22:23:37 -04:00
|
|
|
assert_no_match %r{t\.index \["code"\]}, schema
|
2014-12-11 08:57:28 -05:00
|
|
|
end
|
2015-10-11 08:45:55 -04:00
|
|
|
|
2020-04-24 13:56:53 -04:00
|
|
|
if current_adapter?(:Mysql2Adapter) && supports_datetime_with_precision?
|
2015-10-11 08:45:55 -04:00
|
|
|
test "schema typed primary key column" do
|
|
|
|
@connection.create_table(:scheduled_logs, id: :timestamp, precision: 6, force: true)
|
|
|
|
schema = dump_table_schema("scheduled_logs")
|
2020-05-23 17:38:21 -04:00
|
|
|
assert_match %r/create_table "scheduled_logs", id: { type: :timestamp, precision: 6.* }/, schema
|
2015-10-11 08:45:55 -04:00
|
|
|
end
|
|
|
|
end
|
2014-11-15 13:08:46 -05:00
|
|
|
end
|
|
|
|
|
2015-06-25 03:52:33 -04:00
|
|
|
class CompositePrimaryKeyTest < ActiveRecord::TestCase
|
|
|
|
include SchemaDumpingHelper
|
|
|
|
|
|
|
|
self.use_transactional_tests = false
|
|
|
|
|
|
|
|
def setup
|
|
|
|
@connection = ActiveRecord::Base.connection
|
2016-06-29 13:39:36 -04:00
|
|
|
@connection.schema_cache.clear!
|
2016-08-21 10:25:32 -04:00
|
|
|
@connection.create_table(:uber_barcodes, primary_key: ["region", "code"], force: true) do |t|
|
2015-06-25 03:52:33 -04:00
|
|
|
t.string :region
|
|
|
|
t.integer :code
|
|
|
|
end
|
2017-02-08 17:46:54 -05:00
|
|
|
@connection.create_table(:barcodes_reverse, primary_key: ["code", "region"], force: true) do |t|
|
|
|
|
t.string :region
|
|
|
|
t.integer :code
|
|
|
|
end
|
2017-09-04 07:22:42 -04:00
|
|
|
@connection.create_table(:travels, primary_key: ["from", "to"], force: true) do |t|
|
|
|
|
t.string :from
|
|
|
|
t.string :to
|
|
|
|
end
|
2015-06-25 03:52:33 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def teardown
|
2017-09-04 07:22:42 -04:00
|
|
|
@connection.drop_table :uber_barcodes, if_exists: true
|
|
|
|
@connection.drop_table :barcodes_reverse, if_exists: true
|
|
|
|
@connection.drop_table :travels, if_exists: true
|
2015-06-25 03:52:33 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_composite_primary_key
|
2016-08-21 10:25:32 -04:00
|
|
|
assert_equal ["region", "code"], @connection.primary_keys("uber_barcodes")
|
2015-06-25 03:52:33 -04:00
|
|
|
end
|
|
|
|
|
2017-09-04 07:22:42 -04:00
|
|
|
def test_composite_primary_key_with_reserved_words
|
|
|
|
assert_equal ["from", "to"], @connection.primary_keys("travels")
|
|
|
|
end
|
|
|
|
|
2017-02-08 17:46:54 -05:00
|
|
|
def test_composite_primary_key_out_of_order
|
2017-02-09 20:36:36 -05:00
|
|
|
assert_equal ["code", "region"], @connection.primary_keys("barcodes_reverse")
|
2017-02-08 17:46:54 -05:00
|
|
|
end
|
|
|
|
|
2016-01-30 02:47:51 -05:00
|
|
|
def test_primary_key_issues_warning
|
2016-06-29 13:39:36 -04:00
|
|
|
model = Class.new(ActiveRecord::Base) do
|
|
|
|
def self.table_name
|
2016-08-21 10:25:32 -04:00
|
|
|
"uber_barcodes"
|
2016-06-29 13:39:36 -04:00
|
|
|
end
|
|
|
|
end
|
2016-01-30 02:47:51 -05:00
|
|
|
warning = capture(:stderr) do
|
2016-06-29 13:39:36 -04:00
|
|
|
assert_nil model.primary_key
|
2016-01-30 02:47:51 -05:00
|
|
|
end
|
2016-06-29 13:39:36 -04:00
|
|
|
assert_match(/WARNING: Active Record does not support composite primary key\./, warning)
|
2016-01-30 02:47:51 -05:00
|
|
|
end
|
|
|
|
|
2016-08-21 10:25:32 -04:00
|
|
|
def test_collectly_dump_composite_primary_key
|
|
|
|
schema = dump_table_schema "uber_barcodes"
|
|
|
|
assert_match %r{create_table "uber_barcodes", primary_key: \["region", "code"\]}, schema
|
2015-06-25 03:52:33 -04:00
|
|
|
end
|
2017-02-09 20:36:36 -05:00
|
|
|
|
|
|
|
def test_dumping_composite_primary_key_out_of_order
|
|
|
|
schema = dump_table_schema "barcodes_reverse"
|
|
|
|
assert_match %r{create_table "barcodes_reverse", primary_key: \["code", "region"\]}, schema
|
|
|
|
end
|
2015-06-25 03:52:33 -04:00
|
|
|
end
|
|
|
|
|
2017-01-21 19:54:14 -05:00
|
|
|
class PrimaryKeyIntegerNilDefaultTest < ActiveRecord::TestCase
|
|
|
|
include SchemaDumpingHelper
|
2015-10-27 10:07:19 -04:00
|
|
|
|
2017-01-21 19:54:14 -05:00
|
|
|
self.use_transactional_tests = false
|
2015-10-27 10:07:19 -04:00
|
|
|
|
2017-01-21 19:54:14 -05:00
|
|
|
def setup
|
|
|
|
@connection = ActiveRecord::Base.connection
|
|
|
|
end
|
2015-10-27 10:07:19 -04:00
|
|
|
|
2017-01-21 19:54:14 -05:00
|
|
|
def teardown
|
|
|
|
@connection.drop_table :int_defaults, if_exists: true
|
|
|
|
end
|
2015-10-27 10:07:19 -04:00
|
|
|
|
2017-01-21 19:54:14 -05:00
|
|
|
def test_schema_dump_primary_key_integer_with_default_nil
|
|
|
|
skip if current_adapter?(:SQLite3Adapter)
|
|
|
|
@connection.create_table(:int_defaults, id: :integer, default: nil, force: true)
|
|
|
|
schema = dump_table_schema "int_defaults"
|
|
|
|
assert_match %r{create_table "int_defaults", id: :integer, default: nil}, schema
|
|
|
|
end
|
2015-10-27 10:07:19 -04:00
|
|
|
|
2017-01-21 19:54:14 -05:00
|
|
|
def test_schema_dump_primary_key_bigint_with_default_nil
|
|
|
|
@connection.create_table(:int_defaults, id: :bigint, default: nil, force: true)
|
|
|
|
schema = dump_table_schema "int_defaults"
|
|
|
|
assert_match %r{create_table "int_defaults", id: :bigint, default: nil}, schema
|
2015-10-27 10:07:19 -04:00
|
|
|
end
|
2012-01-30 11:32:49 -05:00
|
|
|
end
|
2013-05-07 14:07:50 -04:00
|
|
|
|
2016-12-18 02:27:07 -05:00
|
|
|
if current_adapter?(:PostgreSQLAdapter, :Mysql2Adapter)
|
|
|
|
class PrimaryKeyIntegerTest < ActiveRecord::TestCase
|
|
|
|
include SchemaDumpingHelper
|
2013-05-07 14:07:50 -04:00
|
|
|
|
2016-12-18 02:27:07 -05:00
|
|
|
self.use_transactional_tests = false
|
2013-05-07 14:07:50 -04:00
|
|
|
|
2016-12-18 02:27:07 -05:00
|
|
|
class Widget < ActiveRecord::Base
|
|
|
|
end
|
2013-05-07 14:07:50 -04:00
|
|
|
|
2016-12-18 02:27:07 -05:00
|
|
|
setup do
|
|
|
|
@connection = ActiveRecord::Base.connection
|
2017-02-10 02:31:18 -05:00
|
|
|
@pk_type = current_adapter?(:PostgreSQLAdapter) ? :serial : :integer
|
2016-12-18 02:27:07 -05:00
|
|
|
end
|
2013-05-07 14:07:50 -04:00
|
|
|
|
2016-12-18 02:27:07 -05:00
|
|
|
teardown do
|
|
|
|
@connection.drop_table :widgets, if_exists: true
|
|
|
|
end
|
2013-05-07 14:07:50 -04:00
|
|
|
|
2016-12-18 02:27:07 -05:00
|
|
|
test "primary key column type with serial/integer" do
|
2017-02-10 02:31:18 -05:00
|
|
|
@connection.create_table(:widgets, id: @pk_type, force: true)
|
2016-12-18 02:27:07 -05:00
|
|
|
column = @connection.columns(:widgets).find { |c| c.name == "id" }
|
|
|
|
assert_equal :integer, column.type
|
2018-01-25 18:14:09 -05:00
|
|
|
assert_not_predicate column, :bigint?
|
2016-05-10 18:10:46 -04:00
|
|
|
end
|
|
|
|
|
2016-12-18 02:27:07 -05:00
|
|
|
test "primary key with serial/integer are automatically numbered" do
|
2017-02-10 02:31:18 -05:00
|
|
|
@connection.create_table(:widgets, id: @pk_type, force: true)
|
2016-12-18 02:27:07 -05:00
|
|
|
widget = Widget.create!
|
|
|
|
assert_not_nil widget.id
|
|
|
|
end
|
2014-12-11 08:57:28 -05:00
|
|
|
|
2016-12-18 02:27:07 -05:00
|
|
|
test "schema dump primary key with serial/integer" do
|
2017-02-10 02:31:18 -05:00
|
|
|
@connection.create_table(:widgets, id: @pk_type, force: true)
|
2016-12-18 02:27:07 -05:00
|
|
|
schema = dump_table_schema "widgets"
|
2017-08-27 09:16:05 -04:00
|
|
|
assert_match %r{create_table "widgets", id: :#{@pk_type}, }, schema
|
2014-12-11 08:57:28 -05:00
|
|
|
end
|
2014-11-15 13:08:46 -05:00
|
|
|
|
2015-12-15 17:01:30 -05:00
|
|
|
if current_adapter?(:Mysql2Adapter)
|
2016-12-18 02:27:07 -05:00
|
|
|
test "primary key column type with options" do
|
|
|
|
@connection.create_table(:widgets, id: :primary_key, limit: 4, unsigned: true, force: true)
|
|
|
|
column = @connection.columns(:widgets).find { |c| c.name == "id" }
|
2018-01-25 18:14:09 -05:00
|
|
|
assert_predicate column, :auto_increment?
|
2016-12-18 02:27:07 -05:00
|
|
|
assert_equal :integer, column.type
|
2018-01-25 18:14:09 -05:00
|
|
|
assert_not_predicate column, :bigint?
|
|
|
|
assert_predicate column, :unsigned?
|
2016-12-18 02:27:07 -05:00
|
|
|
|
|
|
|
schema = dump_table_schema "widgets"
|
2020-05-23 17:38:21 -04:00
|
|
|
assert_match %r/create_table "widgets", id: { type: :integer, unsigned: true }/, schema
|
2016-12-18 02:27:07 -05:00
|
|
|
end
|
2017-02-10 02:31:18 -05:00
|
|
|
|
|
|
|
test "bigint primary key with unsigned" do
|
|
|
|
@connection.create_table(:widgets, id: :bigint, unsigned: true, force: true)
|
|
|
|
column = @connection.columns(:widgets).find { |c| c.name == "id" }
|
2018-01-25 18:14:09 -05:00
|
|
|
assert_predicate column, :auto_increment?
|
2017-02-10 02:31:18 -05:00
|
|
|
assert_equal :integer, column.type
|
2018-01-25 18:14:09 -05:00
|
|
|
assert_predicate column, :bigint?
|
|
|
|
assert_predicate column, :unsigned?
|
2017-02-10 02:31:18 -05:00
|
|
|
|
|
|
|
schema = dump_table_schema "widgets"
|
2020-05-23 17:38:21 -04:00
|
|
|
assert_match %r/create_table "widgets", id: { type: :bigint, unsigned: true }/, schema
|
2017-02-10 02:31:18 -05:00
|
|
|
end
|
2014-11-15 13:08:46 -05:00
|
|
|
end
|
2013-05-07 14:07:50 -04:00
|
|
|
end
|
|
|
|
end
|