aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record
diff options
context:
space:
mode:
Diffstat (limited to 'activerecord/lib/active_record')
-rw-r--r--activerecord/lib/active_record/associations/collection_proxy.rb3
-rw-r--r--activerecord/lib/active_record/associations/through_association.rb2
-rw-r--r--activerecord/lib/active_record/attributes.rb20
-rw-r--r--activerecord/lib/active_record/connection_adapters/abstract/schema_dumper.rb6
-rw-r--r--activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb4
-rw-r--r--activerecord/lib/active_record/connection_adapters/column.rb6
-rw-r--r--activerecord/lib/active_record/connection_adapters/postgresql/column.rb4
-rw-r--r--activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb23
-rw-r--r--activerecord/lib/active_record/type/value.rb6
9 files changed, 54 insertions, 20 deletions
diff --git a/activerecord/lib/active_record/associations/collection_proxy.rb b/activerecord/lib/active_record/associations/collection_proxy.rb
index e11c9490b7..685c3a5f17 100644
--- a/activerecord/lib/active_record/associations/collection_proxy.rb
+++ b/activerecord/lib/active_record/associations/collection_proxy.rb
@@ -976,6 +976,9 @@ module ActiveRecord
# Equivalent to +delete_all+. The difference is that returns +self+, instead
# of an array with the deleted objects, so methods can be chained. See
# +delete_all+ for more information.
+ # Note that because +delete_all+ removes records by directly
+ # running an SQL query into the database, the +updated_at+ column of
+ # the object is not changed.
def clear
delete_all
self
diff --git a/activerecord/lib/active_record/associations/through_association.rb b/activerecord/lib/active_record/associations/through_association.rb
index 3ce9cffdbc..af1bce523c 100644
--- a/activerecord/lib/active_record/associations/through_association.rb
+++ b/activerecord/lib/active_record/associations/through_association.rb
@@ -33,7 +33,7 @@ module ActiveRecord
# Construct attributes for :through pointing to owner and associate. This is used by the
# methods which create and delete records on the association.
#
- # We only support indirectly modifying through associations which has a belongs_to source.
+ # We only support indirectly modifying through associations which have a belongs_to source.
# This is the "has_many :tags, through: :taggings" situation, where the join model
# typically has a belongs_to on both side. In other words, associations which could also
# be represented as has_and_belongs_to_many associations.
diff --git a/activerecord/lib/active_record/attributes.rb b/activerecord/lib/active_record/attributes.rb
index c8979a60d7..50339b6f69 100644
--- a/activerecord/lib/active_record/attributes.rb
+++ b/activerecord/lib/active_record/attributes.rb
@@ -28,15 +28,18 @@ module ActiveRecord
# information about providing custom type objects.
#
# ==== Options
+ #
# The following options are accepted:
#
# +default+ The default value to use when no value is provided. If this option
# is not passed, the previous default value (if any) will be used.
# Otherwise, the default will be +nil+.
#
- # +array+ (PG only) specifies that the type should be an array (see the examples below).
+ # +array+ (PG only) specifies that the type should be an array (see the
+ # examples below).
#
- # +range+ (PG only) specifies that the type should be a range (see the examples below).
+ # +range+ (PG only) specifies that the type should be a range (see the
+ # examples below).
#
# ==== Examples
#
@@ -101,12 +104,11 @@ module ActiveRecord
# ==== Creating Custom Types
#
# Users may also define their own custom types, as long as they respond
- # to the methods defined on the value type. The method
- # +deserialize+ or +cast+ will be called on
- # your type object, with raw input from the database or from your
- # controllers. See ActiveRecord::Type::Value for the expected API. It is
- # recommended that your type objects inherit from an existing type, or
- # from ActiveRecord::Type::Value
+ # to the methods defined on the value type. The method +deserialize+ or
+ # +cast+ will be called on your type object, with raw input from the
+ # database or from your controllers. See ActiveRecord::Type::Value for the
+ # expected API. It is recommended that your type objects inherit from an
+ # existing type, or from ActiveRecord::Type::Value
#
# class MoneyType < ActiveRecord::Type::Integer
# def cast(value)
@@ -150,7 +152,7 @@ module ActiveRecord
# end
#
# # value will be the result of +deserialize+ or
- # # +cast+. Assumed to be in instance of +Money+ in
+ # # +cast+. Assumed to be an instance of +Money+ in
# # this case.
# def serialize(value)
# value_in_bitcoins = @currency_converter.convert_to_bitcoins(value)
diff --git a/activerecord/lib/active_record/connection_adapters/abstract/schema_dumper.rb b/activerecord/lib/active_record/connection_adapters/abstract/schema_dumper.rb
index af7ef7cbaa..999cb0ec5a 100644
--- a/activerecord/lib/active_record/connection_adapters/abstract/schema_dumper.rb
+++ b/activerecord/lib/active_record/connection_adapters/abstract/schema_dumper.rb
@@ -24,7 +24,7 @@ module ActiveRecord
def prepare_column_options(column)
spec = {}
spec[:name] = column.name.inspect
- spec[:type] = column.type.to_s
+ spec[:type] = schema_type(column)
spec[:null] = 'false' unless column.null
limit = column.limit || native_database_types[column.type][:limit]
@@ -45,6 +45,10 @@ module ActiveRecord
private
+ def schema_type(column)
+ column.type.to_s
+ end
+
def schema_default(column)
type = lookup_cast_type_from_column(column)
default = type.deserialize(column.default)
diff --git a/activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb b/activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb
index c084431588..9625fcf9b8 100644
--- a/activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb
+++ b/activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb
@@ -86,8 +86,8 @@ module ActiveRecord
def column_spec_for_primary_key(column)
spec = {}
if column.auto_increment?
- return unless column.limit == 8
- spec[:id] = ':bigint'
+ spec[:id] = ':bigint' if column.bigint?
+ return if spec.empty?
else
spec[:id] = column.type.inspect
spec.merge!(prepare_column_options(column).delete_if { |key, _| [:name, :type, :null].include?(key) })
diff --git a/activerecord/lib/active_record/connection_adapters/column.rb b/activerecord/lib/active_record/connection_adapters/column.rb
index fa5ed07b8a..a67127bd71 100644
--- a/activerecord/lib/active_record/connection_adapters/column.rb
+++ b/activerecord/lib/active_record/connection_adapters/column.rb
@@ -31,7 +31,11 @@ module ActiveRecord
end
def has_default?
- !default.nil?
+ !default.nil? || default_function
+ end
+
+ def bigint?
+ /bigint/ === sql_type
end
# Returns the human name of the column name.
diff --git a/activerecord/lib/active_record/connection_adapters/postgresql/column.rb b/activerecord/lib/active_record/connection_adapters/postgresql/column.rb
index 0eb4fb468c..be13ead120 100644
--- a/activerecord/lib/active_record/connection_adapters/postgresql/column.rb
+++ b/activerecord/lib/active_record/connection_adapters/postgresql/column.rb
@@ -6,7 +6,9 @@ module ActiveRecord
alias :array? :array
def serial?
- default_function && default_function =~ /\Anextval\(.*\)\z/
+ return unless default_function
+
+ %r{\Anextval\('(?<table_name>.+)_#{name}_seq'::regclass\)\z} === default_function
end
end
end
diff --git a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb
index 6d25b53b21..92f470ae70 100644
--- a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb
+++ b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb
@@ -128,7 +128,7 @@ module ActiveRecord
def column_spec_for_primary_key(column)
spec = {}
if column.serial?
- return unless column.sql_type == 'bigint'
+ return unless column.bigint?
spec[:id] = ':bigserial'
elsif column.type == :uuid
spec[:id] = ':uuid'
@@ -145,7 +145,6 @@ module ActiveRecord
def prepare_column_options(column) # :nodoc:
spec = super
spec[:array] = 'true' if column.array?
- spec[:default] = "\"#{column.default_function}\"" if column.default_function
spec
end
@@ -154,6 +153,26 @@ module ActiveRecord
super + [:array]
end
+ def schema_type(column)
+ return super unless column.serial?
+
+ if column.bigint?
+ 'bigserial'
+ else
+ 'serial'
+ end
+ end
+ private :schema_type
+
+ def schema_default(column)
+ if column.default_function
+ column.default_function.inspect unless column.serial?
+ else
+ super
+ end
+ end
+ private :schema_default
+
# Returns +true+, since this connection adapter supports prepared statement
# caching.
def supports_statement_cache?
diff --git a/activerecord/lib/active_record/type/value.rb b/activerecord/lib/active_record/type/value.rb
index fc3ef5e83b..6b9d147ecc 100644
--- a/activerecord/lib/active_record/type/value.rb
+++ b/activerecord/lib/active_record/type/value.rb
@@ -12,7 +12,7 @@ module ActiveRecord
def type # :nodoc:
end
- # Convert a value from database input to the appropriate ruby type. The
+ # Converts a value from database input to the appropriate ruby type. The
# return value of this method will be returned from
# ActiveRecord::AttributeMethods::Read#read_attribute. The default
# implementation just calls Value#cast.
@@ -36,7 +36,7 @@ module ActiveRecord
cast_value(value) unless value.nil?
end
- # Cast a value from the ruby type to a type that the database knows how
+ # Casts a value from the ruby type to a type that the database knows how
# to understand. The returned value from this method should be a
# +String+, +Numeric+, +Date+, +Time+, +Symbol+, +true+, +false+, or
# +nil+.
@@ -44,7 +44,7 @@ module ActiveRecord
value
end
- # Type cast a value for schema dumping. This method is private, as we are
+ # Type casts a value for schema dumping. This method is private, as we are
# hoping to remove it entirely.
def type_cast_for_schema(value) # :nodoc:
value.inspect