activerecord 5.1.5 → 5.2.8.1
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of activerecord might be problematic. Click here for more details.
- checksums.yaml +4 -4
- data/CHANGELOG.md +655 -608
- data/MIT-LICENSE +1 -1
- data/README.rdoc +5 -5
- data/examples/performance.rb +2 -0
- data/examples/simple.rb +2 -0
- data/lib/active_record/aggregations.rb +6 -5
- data/lib/active_record/association_relation.rb +7 -5
- data/lib/active_record/associations/alias_tracker.rb +19 -27
- data/lib/active_record/associations/association.rb +41 -37
- data/lib/active_record/associations/association_scope.rb +38 -50
- data/lib/active_record/associations/belongs_to_association.rb +28 -9
- data/lib/active_record/associations/belongs_to_polymorphic_association.rb +8 -8
- data/lib/active_record/associations/builder/association.rb +4 -7
- data/lib/active_record/associations/builder/belongs_to.rb +14 -5
- data/lib/active_record/associations/builder/collection_association.rb +3 -3
- data/lib/active_record/associations/builder/has_and_belongs_to_many.rb +3 -1
- data/lib/active_record/associations/builder/has_many.rb +2 -0
- data/lib/active_record/associations/builder/has_one.rb +2 -0
- data/lib/active_record/associations/builder/singular_association.rb +2 -0
- data/lib/active_record/associations/collection_association.rb +59 -47
- data/lib/active_record/associations/collection_proxy.rb +20 -49
- data/lib/active_record/associations/foreign_association.rb +2 -0
- data/lib/active_record/associations/has_many_association.rb +12 -1
- data/lib/active_record/associations/has_many_through_association.rb +36 -30
- data/lib/active_record/associations/has_one_association.rb +12 -1
- data/lib/active_record/associations/has_one_through_association.rb +13 -8
- data/lib/active_record/associations/join_dependency/join_association.rb +39 -63
- data/lib/active_record/associations/join_dependency/join_base.rb +9 -8
- data/lib/active_record/associations/join_dependency/join_part.rb +9 -9
- data/lib/active_record/associations/join_dependency.rb +48 -93
- data/lib/active_record/associations/preloader/association.rb +45 -61
- data/lib/active_record/associations/preloader/through_association.rb +71 -79
- data/lib/active_record/associations/preloader.rb +18 -38
- data/lib/active_record/associations/singular_association.rb +14 -16
- data/lib/active_record/associations/through_association.rb +26 -11
- data/lib/active_record/associations.rb +40 -63
- data/lib/active_record/attribute_assignment.rb +2 -5
- data/lib/active_record/attribute_decorators.rb +3 -2
- data/lib/active_record/attribute_methods/before_type_cast.rb +2 -0
- data/lib/active_record/attribute_methods/dirty.rb +32 -216
- data/lib/active_record/attribute_methods/primary_key.rb +7 -6
- data/lib/active_record/attribute_methods/query.rb +2 -0
- data/lib/active_record/attribute_methods/read.rb +9 -3
- data/lib/active_record/attribute_methods/serialization.rb +23 -0
- data/lib/active_record/attribute_methods/time_zone_conversion.rb +6 -8
- data/lib/active_record/attribute_methods/write.rb +21 -9
- data/lib/active_record/attribute_methods.rb +65 -24
- data/lib/active_record/attributes.rb +7 -6
- data/lib/active_record/autosave_association.rb +35 -19
- data/lib/active_record/base.rb +2 -0
- data/lib/active_record/callbacks.rb +12 -6
- data/lib/active_record/coders/json.rb +2 -0
- data/lib/active_record/coders/yaml_column.rb +15 -1
- data/lib/active_record/collection_cache_key.rb +12 -8
- data/lib/active_record/connection_adapters/abstract/connection_pool.rb +142 -42
- data/lib/active_record/connection_adapters/abstract/database_limits.rb +7 -0
- data/lib/active_record/connection_adapters/abstract/database_statements.rb +174 -33
- data/lib/active_record/connection_adapters/abstract/query_cache.rb +15 -5
- data/lib/active_record/connection_adapters/abstract/quoting.rb +15 -32
- data/lib/active_record/connection_adapters/abstract/savepoints.rb +2 -0
- data/lib/active_record/connection_adapters/abstract/schema_creation.rb +14 -5
- data/lib/active_record/connection_adapters/abstract/schema_definitions.rb +64 -6
- data/lib/active_record/connection_adapters/abstract/schema_dumper.rb +31 -53
- data/lib/active_record/connection_adapters/abstract/schema_statements.rb +152 -81
- data/lib/active_record/connection_adapters/abstract/transaction.rb +66 -21
- data/lib/active_record/connection_adapters/abstract_adapter.rb +84 -97
- data/lib/active_record/connection_adapters/abstract_mysql_adapter.rb +110 -173
- data/lib/active_record/connection_adapters/column.rb +3 -1
- data/lib/active_record/connection_adapters/connection_specification.rb +17 -3
- data/lib/active_record/connection_adapters/determine_if_preparable_visitor.rb +13 -2
- data/lib/active_record/connection_adapters/mysql/column.rb +2 -0
- data/lib/active_record/connection_adapters/mysql/database_statements.rb +47 -2
- data/lib/active_record/connection_adapters/mysql/explain_pretty_printer.rb +2 -0
- data/lib/active_record/connection_adapters/mysql/quoting.rb +9 -10
- data/lib/active_record/connection_adapters/mysql/schema_creation.rb +5 -3
- data/lib/active_record/connection_adapters/mysql/schema_definitions.rb +7 -10
- data/lib/active_record/connection_adapters/mysql/schema_dumper.rb +30 -30
- data/lib/active_record/connection_adapters/mysql/schema_statements.rb +106 -1
- data/lib/active_record/connection_adapters/mysql/type_metadata.rb +2 -0
- data/lib/active_record/connection_adapters/mysql2_adapter.rb +8 -2
- data/lib/active_record/connection_adapters/postgresql/column.rb +2 -0
- data/lib/active_record/connection_adapters/postgresql/database_statements.rb +6 -0
- data/lib/active_record/connection_adapters/postgresql/explain_pretty_printer.rb +2 -0
- data/lib/active_record/connection_adapters/postgresql/oid/array.rb +13 -1
- data/lib/active_record/connection_adapters/postgresql/oid/bit.rb +2 -0
- data/lib/active_record/connection_adapters/postgresql/oid/bit_varying.rb +2 -0
- data/lib/active_record/connection_adapters/postgresql/oid/bytea.rb +2 -0
- data/lib/active_record/connection_adapters/postgresql/oid/cidr.rb +2 -0
- data/lib/active_record/connection_adapters/postgresql/oid/date.rb +23 -0
- data/lib/active_record/connection_adapters/postgresql/oid/date_time.rb +2 -0
- data/lib/active_record/connection_adapters/postgresql/oid/decimal.rb +3 -1
- data/lib/active_record/connection_adapters/postgresql/oid/enum.rb +2 -0
- data/lib/active_record/connection_adapters/postgresql/oid/hstore.rb +2 -0
- data/lib/active_record/connection_adapters/postgresql/oid/inet.rb +2 -0
- data/lib/active_record/connection_adapters/postgresql/oid/jsonb.rb +3 -1
- data/lib/active_record/connection_adapters/postgresql/oid/legacy_point.rb +2 -0
- data/lib/active_record/connection_adapters/postgresql/oid/money.rb +5 -3
- data/lib/active_record/connection_adapters/postgresql/oid/oid.rb +2 -0
- data/lib/active_record/connection_adapters/postgresql/oid/point.rb +2 -0
- data/lib/active_record/connection_adapters/postgresql/oid/range.rb +8 -2
- data/lib/active_record/connection_adapters/postgresql/oid/specialized_string.rb +2 -0
- data/lib/active_record/connection_adapters/postgresql/oid/type_map_initializer.rb +4 -2
- data/lib/active_record/connection_adapters/postgresql/oid/uuid.rb +3 -1
- data/lib/active_record/connection_adapters/postgresql/oid/vector.rb +2 -0
- data/lib/active_record/connection_adapters/postgresql/oid/xml.rb +2 -0
- data/lib/active_record/connection_adapters/postgresql/oid.rb +3 -1
- data/lib/active_record/connection_adapters/postgresql/quoting.rb +18 -0
- data/lib/active_record/connection_adapters/postgresql/referential_integrity.rb +19 -25
- data/lib/active_record/connection_adapters/postgresql/schema_creation.rb +50 -0
- data/lib/active_record/connection_adapters/postgresql/schema_definitions.rb +24 -11
- data/lib/active_record/connection_adapters/postgresql/schema_dumper.rb +20 -13
- data/lib/active_record/connection_adapters/postgresql/schema_statements.rb +234 -112
- data/lib/active_record/connection_adapters/postgresql/type_metadata.rb +2 -0
- data/lib/active_record/connection_adapters/postgresql/utils.rb +3 -1
- data/lib/active_record/connection_adapters/postgresql_adapter.rb +66 -74
- data/lib/active_record/connection_adapters/schema_cache.rb +4 -2
- data/lib/active_record/connection_adapters/sql_type_metadata.rb +2 -0
- data/lib/active_record/connection_adapters/sqlite3/explain_pretty_printer.rb +2 -0
- data/lib/active_record/connection_adapters/sqlite3/quoting.rb +24 -1
- data/lib/active_record/connection_adapters/sqlite3/schema_creation.rb +2 -0
- data/lib/active_record/connection_adapters/sqlite3/schema_definitions.rb +6 -15
- data/lib/active_record/connection_adapters/sqlite3/schema_dumper.rb +3 -2
- data/lib/active_record/connection_adapters/sqlite3/schema_statements.rb +75 -1
- data/lib/active_record/connection_adapters/sqlite3_adapter.rb +82 -95
- data/lib/active_record/connection_adapters/statement_pool.rb +2 -0
- data/lib/active_record/connection_handling.rb +4 -2
- data/lib/active_record/core.rb +51 -61
- data/lib/active_record/counter_cache.rb +20 -15
- data/lib/active_record/define_callbacks.rb +5 -3
- data/lib/active_record/dynamic_matchers.rb +9 -9
- data/lib/active_record/enum.rb +18 -13
- data/lib/active_record/errors.rb +60 -15
- data/lib/active_record/explain.rb +3 -1
- data/lib/active_record/explain_registry.rb +2 -0
- data/lib/active_record/explain_subscriber.rb +2 -0
- data/lib/active_record/fixture_set/file.rb +2 -0
- data/lib/active_record/fixtures.rb +67 -60
- data/lib/active_record/gem_version.rb +5 -3
- data/lib/active_record/inheritance.rb +49 -19
- data/lib/active_record/integration.rb +58 -19
- data/lib/active_record/internal_metadata.rb +2 -0
- data/lib/active_record/legacy_yaml_adapter.rb +3 -1
- data/lib/active_record/locking/optimistic.rb +30 -42
- data/lib/active_record/locking/pessimistic.rb +9 -6
- data/lib/active_record/log_subscriber.rb +43 -0
- data/lib/active_record/migration/command_recorder.rb +11 -9
- data/lib/active_record/migration/compatibility.rb +47 -9
- data/lib/active_record/migration/join_table.rb +2 -0
- data/lib/active_record/migration.rb +189 -139
- data/lib/active_record/model_schema.rb +19 -24
- data/lib/active_record/nested_attributes.rb +18 -6
- data/lib/active_record/no_touching.rb +3 -1
- data/lib/active_record/null_relation.rb +2 -0
- data/lib/active_record/persistence.rb +198 -49
- data/lib/active_record/query_cache.rb +12 -14
- data/lib/active_record/querying.rb +4 -2
- data/lib/active_record/railtie.rb +80 -6
- data/lib/active_record/railties/console_sandbox.rb +2 -0
- data/lib/active_record/railties/controller_runtime.rb +2 -0
- data/lib/active_record/railties/databases.rake +46 -36
- data/lib/active_record/readonly_attributes.rb +3 -2
- data/lib/active_record/reflection.rb +108 -194
- data/lib/active_record/relation/batches/batch_enumerator.rb +2 -0
- data/lib/active_record/relation/batches.rb +20 -5
- data/lib/active_record/relation/calculations.rb +46 -20
- data/lib/active_record/relation/delegation.rb +45 -27
- data/lib/active_record/relation/finder_methods.rb +77 -78
- data/lib/active_record/relation/from_clause.rb +2 -8
- data/lib/active_record/relation/merger.rb +53 -23
- data/lib/active_record/relation/predicate_builder/array_handler.rb +10 -7
- data/lib/active_record/relation/predicate_builder/association_query_value.rb +46 -0
- data/lib/active_record/relation/predicate_builder/base_handler.rb +2 -2
- data/lib/active_record/relation/predicate_builder/basic_object_handler.rb +12 -1
- data/lib/active_record/relation/predicate_builder/polymorphic_array_value.rb +56 -0
- data/lib/active_record/relation/predicate_builder/range_handler.rb +26 -9
- data/lib/active_record/relation/predicate_builder/relation_handler.rb +6 -0
- data/lib/active_record/relation/predicate_builder.rb +60 -79
- data/lib/active_record/relation/query_attribute.rb +28 -2
- data/lib/active_record/relation/query_methods.rb +129 -100
- data/lib/active_record/relation/record_fetch_warning.rb +2 -0
- data/lib/active_record/relation/spawn_methods.rb +4 -2
- data/lib/active_record/relation/where_clause.rb +65 -68
- data/lib/active_record/relation/where_clause_factory.rb +5 -48
- data/lib/active_record/relation.rb +120 -214
- data/lib/active_record/result.rb +2 -0
- data/lib/active_record/runtime_registry.rb +2 -0
- data/lib/active_record/sanitization.rb +129 -121
- data/lib/active_record/schema.rb +4 -2
- data/lib/active_record/schema_dumper.rb +36 -26
- data/lib/active_record/schema_migration.rb +2 -0
- data/lib/active_record/scoping/default.rb +8 -9
- data/lib/active_record/scoping/named.rb +23 -7
- data/lib/active_record/scoping.rb +9 -8
- data/lib/active_record/secure_token.rb +2 -0
- data/lib/active_record/serialization.rb +2 -0
- data/lib/active_record/statement_cache.rb +23 -13
- data/lib/active_record/store.rb +3 -1
- data/lib/active_record/suppressor.rb +2 -0
- data/lib/active_record/table_metadata.rb +12 -3
- data/lib/active_record/tasks/database_tasks.rb +26 -15
- data/lib/active_record/tasks/mysql_database_tasks.rb +9 -48
- data/lib/active_record/tasks/postgresql_database_tasks.rb +10 -2
- data/lib/active_record/tasks/sqlite_database_tasks.rb +25 -3
- data/lib/active_record/timestamp.rb +13 -6
- data/lib/active_record/touch_later.rb +2 -0
- data/lib/active_record/transactions.rb +33 -28
- data/lib/active_record/translation.rb +2 -0
- data/lib/active_record/type/adapter_specific_registry.rb +2 -0
- data/lib/active_record/type/date.rb +2 -0
- data/lib/active_record/type/date_time.rb +2 -0
- data/lib/active_record/type/decimal_without_scale.rb +2 -0
- data/lib/active_record/type/hash_lookup_type_map.rb +2 -0
- data/lib/active_record/type/internal/timezone.rb +2 -0
- data/lib/active_record/type/json.rb +30 -0
- data/lib/active_record/type/serialized.rb +6 -0
- data/lib/active_record/type/text.rb +2 -0
- data/lib/active_record/type/time.rb +2 -0
- data/lib/active_record/type/type_map.rb +2 -0
- data/lib/active_record/type/unsigned_integer.rb +2 -0
- data/lib/active_record/type.rb +4 -1
- data/lib/active_record/type_caster/connection.rb +2 -0
- data/lib/active_record/type_caster/map.rb +3 -1
- data/lib/active_record/type_caster.rb +2 -0
- data/lib/active_record/validations/absence.rb +2 -0
- data/lib/active_record/validations/associated.rb +2 -0
- data/lib/active_record/validations/length.rb +2 -0
- data/lib/active_record/validations/presence.rb +2 -0
- data/lib/active_record/validations/uniqueness.rb +36 -6
- data/lib/active_record/validations.rb +2 -0
- data/lib/active_record/version.rb +2 -0
- data/lib/active_record.rb +11 -4
- data/lib/rails/generators/active_record/application_record/application_record_generator.rb +27 -0
- data/lib/rails/generators/active_record/{model/templates/application_record.rb → application_record/templates/application_record.rb.tt} +0 -0
- data/lib/rails/generators/active_record/migration/migration_generator.rb +3 -1
- data/lib/rails/generators/active_record/migration/templates/{create_table_migration.rb → create_table_migration.rb.tt} +0 -0
- data/lib/rails/generators/active_record/migration/templates/{migration.rb → migration.rb.tt} +0 -0
- data/lib/rails/generators/active_record/migration.rb +2 -0
- data/lib/rails/generators/active_record/model/model_generator.rb +2 -23
- data/lib/rails/generators/active_record/model/templates/{model.rb → model.rb.tt} +0 -0
- data/lib/rails/generators/active_record/model/templates/{module.rb → module.rb.tt} +0 -0
- data/lib/rails/generators/active_record.rb +3 -1
- metadata +26 -40
- data/lib/active_record/associations/preloader/belongs_to.rb +0 -15
- data/lib/active_record/associations/preloader/collection_association.rb +0 -17
- data/lib/active_record/associations/preloader/has_many.rb +0 -15
- data/lib/active_record/associations/preloader/has_many_through.rb +0 -19
- data/lib/active_record/associations/preloader/has_one.rb +0 -15
- data/lib/active_record/associations/preloader/has_one_through.rb +0 -9
- data/lib/active_record/associations/preloader/singular_association.rb +0 -18
- data/lib/active_record/attribute/user_provided_default.rb +0 -30
- data/lib/active_record/attribute.rb +0 -240
- data/lib/active_record/attribute_mutation_tracker.rb +0 -114
- data/lib/active_record/attribute_set/builder.rb +0 -124
- data/lib/active_record/attribute_set/yaml_encoder.rb +0 -41
- data/lib/active_record/attribute_set.rb +0 -113
- data/lib/active_record/connection_adapters/postgresql/oid/json.rb +0 -10
- data/lib/active_record/railties/jdbcmysql_error.rb +0 -16
- data/lib/active_record/relation/predicate_builder/association_query_handler.rb +0 -88
- data/lib/active_record/relation/predicate_builder/polymorphic_array_handler.rb +0 -59
- data/lib/active_record/type/internal/abstract_json.rb +0 -37
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require "active_support/core_ext/string/filters"
|
2
4
|
|
3
5
|
module ActiveRecord
|
@@ -7,12 +9,19 @@ module ActiveRecord
|
|
7
9
|
included do
|
8
10
|
##
|
9
11
|
# :singleton-method:
|
10
|
-
# Indicates the format used to generate the timestamp in the cache key
|
11
|
-
# Accepts any of the symbols in <tt>Time::DATE_FORMATS</tt>.
|
12
|
+
# Indicates the format used to generate the timestamp in the cache key, if
|
13
|
+
# versioning is off. Accepts any of the symbols in <tt>Time::DATE_FORMATS</tt>.
|
12
14
|
#
|
13
15
|
# This is +:usec+, by default.
|
14
|
-
class_attribute :cache_timestamp_format, instance_writer: false
|
15
|
-
|
16
|
+
class_attribute :cache_timestamp_format, instance_writer: false, default: :usec
|
17
|
+
|
18
|
+
##
|
19
|
+
# :singleton-method:
|
20
|
+
# Indicates whether to use a stable #cache_key method that is accompanied
|
21
|
+
# by a changing version in the #cache_version method.
|
22
|
+
#
|
23
|
+
# This is +false+, by default until Rails 6.0.
|
24
|
+
class_attribute :cache_versioning, instance_writer: false, default: false
|
16
25
|
end
|
17
26
|
|
18
27
|
# Returns a +String+, which Action Pack uses for constructing a URL to this
|
@@ -42,35 +51,65 @@ module ActiveRecord
|
|
42
51
|
id && id.to_s # Be sure to stringify the id for routes
|
43
52
|
end
|
44
53
|
|
45
|
-
# Returns a cache key that can be used to identify this record.
|
54
|
+
# Returns a stable cache key that can be used to identify this record.
|
46
55
|
#
|
47
56
|
# Product.new.cache_key # => "products/new"
|
48
|
-
# Product.find(5).cache_key # => "products/5"
|
49
|
-
# Person.find(5).cache_key # => "people/5-20071224150000" (updated_at available)
|
57
|
+
# Product.find(5).cache_key # => "products/5"
|
50
58
|
#
|
51
|
-
#
|
52
|
-
#
|
59
|
+
# If ActiveRecord::Base.cache_versioning is turned off, as it was in Rails 5.1 and earlier,
|
60
|
+
# the cache key will also include a version.
|
53
61
|
#
|
54
|
-
#
|
62
|
+
# Product.cache_versioning = false
|
63
|
+
# Person.find(5).cache_key # => "people/5-20071224150000" (updated_at available)
|
55
64
|
def cache_key(*timestamp_names)
|
56
65
|
if new_record?
|
57
66
|
"#{model_name.cache_key}/new"
|
58
67
|
else
|
59
|
-
|
60
|
-
|
68
|
+
if cache_version && timestamp_names.none?
|
69
|
+
"#{model_name.cache_key}/#{id}"
|
61
70
|
else
|
62
|
-
|
63
|
-
|
71
|
+
timestamp = if timestamp_names.any?
|
72
|
+
ActiveSupport::Deprecation.warn(<<-MSG.squish)
|
73
|
+
Specifying a timestamp name for #cache_key has been deprecated in favor of
|
74
|
+
the explicit #cache_version method that can be overwritten.
|
75
|
+
MSG
|
64
76
|
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
77
|
+
max_updated_column_timestamp(timestamp_names)
|
78
|
+
else
|
79
|
+
max_updated_column_timestamp
|
80
|
+
end
|
81
|
+
|
82
|
+
if timestamp
|
83
|
+
timestamp = timestamp.utc.to_s(cache_timestamp_format)
|
84
|
+
"#{model_name.cache_key}/#{id}-#{timestamp}"
|
85
|
+
else
|
86
|
+
"#{model_name.cache_key}/#{id}"
|
87
|
+
end
|
70
88
|
end
|
71
89
|
end
|
72
90
|
end
|
73
91
|
|
92
|
+
# Returns a cache version that can be used together with the cache key to form
|
93
|
+
# a recyclable caching scheme. By default, the #updated_at column is used for the
|
94
|
+
# cache_version, but this method can be overwritten to return something else.
|
95
|
+
#
|
96
|
+
# Note, this method will return nil if ActiveRecord::Base.cache_versioning is set to
|
97
|
+
# +false+ (which it is by default until Rails 6.0).
|
98
|
+
def cache_version
|
99
|
+
if cache_versioning && timestamp = try(:updated_at)
|
100
|
+
timestamp.utc.to_s(:usec)
|
101
|
+
end
|
102
|
+
end
|
103
|
+
|
104
|
+
# Returns a cache key along with the version.
|
105
|
+
def cache_key_with_version
|
106
|
+
if version = cache_version
|
107
|
+
"#{cache_key}-#{version}"
|
108
|
+
else
|
109
|
+
cache_key
|
110
|
+
end
|
111
|
+
end
|
112
|
+
|
74
113
|
module ClassMethods
|
75
114
|
# Defines your model's +to_param+ method to generate "pretty" URLs
|
76
115
|
# using +method_name+, which can be any attribute or method that
|
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module ActiveRecord
|
2
4
|
module LegacyYamlAdapter
|
3
5
|
def self.convert(klass, coder)
|
@@ -6,7 +8,7 @@ module ActiveRecord
|
|
6
8
|
case coder["active_record_yaml_version"]
|
7
9
|
when 1, 2 then coder
|
8
10
|
else
|
9
|
-
if coder["attributes"].is_a?(AttributeSet)
|
11
|
+
if coder["attributes"].is_a?(ActiveModel::AttributeSet)
|
10
12
|
Rails420.convert(klass, coder)
|
11
13
|
else
|
12
14
|
Rails41.convert(klass, coder)
|
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module ActiveRecord
|
2
4
|
module Locking
|
3
5
|
# == What is Optimistic Locking
|
@@ -51,8 +53,7 @@ module ActiveRecord
|
|
51
53
|
extend ActiveSupport::Concern
|
52
54
|
|
53
55
|
included do
|
54
|
-
class_attribute :lock_optimistically, instance_writer: false
|
55
|
-
self.lock_optimistically = true
|
56
|
+
class_attribute :lock_optimistically, instance_writer: false, default: true
|
56
57
|
end
|
57
58
|
|
58
59
|
def locking_enabled? #:nodoc:
|
@@ -60,13 +61,6 @@ module ActiveRecord
|
|
60
61
|
end
|
61
62
|
|
62
63
|
private
|
63
|
-
|
64
|
-
def increment_lock
|
65
|
-
lock_col = self.class.locking_column
|
66
|
-
previous_lock_value = send(lock_col).to_i
|
67
|
-
send(lock_col + "=", previous_lock_value + 1)
|
68
|
-
end
|
69
|
-
|
70
64
|
def _create_record(attribute_names = self.attribute_names, *)
|
71
65
|
if locking_enabled?
|
72
66
|
# We always want to persist the locking version, even if we don't detect
|
@@ -76,62 +70,56 @@ module ActiveRecord
|
|
76
70
|
super
|
77
71
|
end
|
78
72
|
|
79
|
-
def
|
73
|
+
def _touch_row(attribute_names, time)
|
74
|
+
super
|
75
|
+
ensure
|
76
|
+
clear_attribute_change(self.class.locking_column) if locking_enabled?
|
77
|
+
end
|
78
|
+
|
79
|
+
def _update_row(attribute_names, attempted_action = "update")
|
80
80
|
return super unless locking_enabled?
|
81
|
-
return 0 if attribute_names.empty?
|
82
81
|
|
83
82
|
begin
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
increment_lock
|
89
|
-
|
90
|
-
attribute_names.push(lock_col)
|
83
|
+
locking_column = self.class.locking_column
|
84
|
+
previous_lock_value = read_attribute_before_type_cast(locking_column)
|
85
|
+
attribute_names << locking_column
|
91
86
|
|
92
|
-
|
87
|
+
self[locking_column] += 1
|
93
88
|
|
94
|
-
affected_rows =
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
attributes_for_update(attribute_names).map do |name|
|
99
|
-
[name, _read_attribute(name)]
|
100
|
-
end.to_h
|
89
|
+
affected_rows = self.class._update_record(
|
90
|
+
attributes_with_values(attribute_names),
|
91
|
+
self.class.primary_key => id_in_database,
|
92
|
+
locking_column => previous_lock_value
|
101
93
|
)
|
102
94
|
|
103
|
-
|
104
|
-
raise ActiveRecord::StaleObjectError.new(self,
|
95
|
+
if affected_rows != 1
|
96
|
+
raise ActiveRecord::StaleObjectError.new(self, attempted_action)
|
105
97
|
end
|
106
98
|
|
107
99
|
affected_rows
|
108
100
|
|
109
101
|
# If something went wrong, revert the locking_column value.
|
110
102
|
rescue Exception
|
111
|
-
|
103
|
+
self[locking_column] = previous_lock_value.to_i
|
112
104
|
raise
|
113
105
|
end
|
114
106
|
end
|
115
107
|
|
116
108
|
def destroy_row
|
117
|
-
|
118
|
-
|
119
|
-
if locking_enabled? && affected_rows != 1
|
120
|
-
raise ActiveRecord::StaleObjectError.new(self, "destroy")
|
121
|
-
end
|
109
|
+
return super unless locking_enabled?
|
122
110
|
|
123
|
-
|
124
|
-
end
|
111
|
+
locking_column = self.class.locking_column
|
125
112
|
|
126
|
-
|
127
|
-
|
113
|
+
affected_rows = self.class._delete_record(
|
114
|
+
self.class.primary_key => id_in_database,
|
115
|
+
locking_column => read_attribute_before_type_cast(locking_column)
|
116
|
+
)
|
128
117
|
|
129
|
-
if
|
130
|
-
|
131
|
-
relation = relation.where(locking_column => _read_attribute(locking_column))
|
118
|
+
if affected_rows != 1
|
119
|
+
raise ActiveRecord::StaleObjectError.new(self, "destroy")
|
132
120
|
end
|
133
121
|
|
134
|
-
|
122
|
+
affected_rows
|
135
123
|
end
|
136
124
|
|
137
125
|
module ClassMethods
|
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module ActiveRecord
|
2
4
|
module Locking
|
3
5
|
# Locking::Pessimistic provides support for row-level locking using
|
@@ -51,8 +53,8 @@ module ActiveRecord
|
|
51
53
|
# end
|
52
54
|
#
|
53
55
|
# Database-specific information on row locking:
|
54
|
-
# MySQL:
|
55
|
-
# PostgreSQL:
|
56
|
+
# MySQL: https://dev.mysql.com/doc/refman/5.7/en/innodb-locking-reads.html
|
57
|
+
# PostgreSQL: https://www.postgresql.org/docs/current/interactive/sql-select.html#SQL-FOR-UPDATE-SHARE
|
56
58
|
module Pessimistic
|
57
59
|
# Obtain a row lock on this record. Reloads the record to obtain the requested
|
58
60
|
# lock. Pass an SQL locking clause to append the end of the SELECT statement
|
@@ -61,12 +63,13 @@ module ActiveRecord
|
|
61
63
|
def lock!(lock = true)
|
62
64
|
if persisted?
|
63
65
|
if has_changes_to_save?
|
64
|
-
|
65
|
-
Locking a record with unpersisted changes is
|
66
|
-
|
67
|
-
|
66
|
+
raise(<<-MSG.squish)
|
67
|
+
Locking a record with unpersisted changes is not supported. Use
|
68
|
+
`save` to persist the changes, or `reload` to discard them
|
69
|
+
explicitly.
|
68
70
|
MSG
|
69
71
|
end
|
72
|
+
|
70
73
|
reload(lock: lock)
|
71
74
|
end
|
72
75
|
self
|
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module ActiveRecord
|
2
4
|
class LogSubscriber < ActiveSupport::LogSubscriber
|
3
5
|
IGNORE_PAYLOAD_NAMES = ["SCHEMA", "EXPLAIN"]
|
@@ -88,6 +90,47 @@ module ActiveRecord
|
|
88
90
|
def logger
|
89
91
|
ActiveRecord::Base.logger
|
90
92
|
end
|
93
|
+
|
94
|
+
def debug(progname = nil, &block)
|
95
|
+
return unless super
|
96
|
+
|
97
|
+
if ActiveRecord::Base.verbose_query_logs
|
98
|
+
log_query_source
|
99
|
+
end
|
100
|
+
end
|
101
|
+
|
102
|
+
def log_query_source
|
103
|
+
source_line, line_number = extract_callstack(caller_locations)
|
104
|
+
|
105
|
+
if source_line
|
106
|
+
if defined?(::Rails.root)
|
107
|
+
app_root = "#{::Rails.root.to_s}/".freeze
|
108
|
+
source_line = source_line.sub(app_root, "")
|
109
|
+
end
|
110
|
+
|
111
|
+
logger.debug(" ↳ #{ source_line }:#{ line_number }")
|
112
|
+
end
|
113
|
+
end
|
114
|
+
|
115
|
+
def extract_callstack(callstack)
|
116
|
+
line = callstack.find do |frame|
|
117
|
+
frame.absolute_path && !ignored_callstack(frame.absolute_path)
|
118
|
+
end
|
119
|
+
|
120
|
+
offending_line = line || callstack.first
|
121
|
+
|
122
|
+
[
|
123
|
+
offending_line.path,
|
124
|
+
offending_line.lineno
|
125
|
+
]
|
126
|
+
end
|
127
|
+
|
128
|
+
RAILS_GEM_ROOT = File.expand_path("../../..", __dir__) + "/"
|
129
|
+
|
130
|
+
def ignored_callstack(path)
|
131
|
+
path.start_with?(RAILS_GEM_ROOT) ||
|
132
|
+
path.start_with?(RbConfig::CONFIG["rubylibdir"])
|
133
|
+
end
|
91
134
|
end
|
92
135
|
end
|
93
136
|
|
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module ActiveRecord
|
2
4
|
class Migration
|
3
5
|
# <tt>ActiveRecord::Migration::CommandRecorder</tt> records commands done during
|
@@ -92,10 +94,6 @@ module ActiveRecord
|
|
92
94
|
send(method, args, &block)
|
93
95
|
end
|
94
96
|
|
95
|
-
def respond_to_missing?(*args) # :nodoc:
|
96
|
-
super || delegate.respond_to?(*args)
|
97
|
-
end
|
98
|
-
|
99
97
|
ReversibleAndIrreversibleMethods.each do |method|
|
100
98
|
class_eval <<-EOV, __FILE__, __LINE__ + 1
|
101
99
|
def #{method}(*args, &block) # def create_table(*args, &block)
|
@@ -112,7 +110,7 @@ module ActiveRecord
|
|
112
110
|
|
113
111
|
private
|
114
112
|
|
115
|
-
module StraightReversions
|
113
|
+
module StraightReversions # :nodoc:
|
116
114
|
private
|
117
115
|
{ transaction: :transaction,
|
118
116
|
execute_block: :execute_block,
|
@@ -163,8 +161,8 @@ module ActiveRecord
|
|
163
161
|
table, columns, options = *args
|
164
162
|
options ||= {}
|
165
163
|
|
166
|
-
|
167
|
-
options_hash =
|
164
|
+
options_hash = options.slice(:name, :algorithm)
|
165
|
+
options_hash[:column] = columns if !options_hash[:name]
|
168
166
|
|
169
167
|
[:remove_index, [table, options_hash]]
|
170
168
|
end
|
@@ -225,10 +223,14 @@ module ActiveRecord
|
|
225
223
|
[:add_foreign_key, reversed_args]
|
226
224
|
end
|
227
225
|
|
226
|
+
def respond_to_missing?(method, _)
|
227
|
+
super || delegate.respond_to?(method)
|
228
|
+
end
|
229
|
+
|
228
230
|
# Forwards any missing method call to the \target.
|
229
231
|
def method_missing(method, *args, &block)
|
230
|
-
if
|
231
|
-
|
232
|
+
if delegate.respond_to?(method)
|
233
|
+
delegate.public_send(method, *args, &block)
|
232
234
|
else
|
233
235
|
super
|
234
236
|
end
|
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module ActiveRecord
|
2
4
|
class Migration
|
3
5
|
module Compatibility # :nodoc: all
|
@@ -11,10 +13,36 @@ module ActiveRecord
|
|
11
13
|
const_get(name)
|
12
14
|
end
|
13
15
|
|
14
|
-
|
16
|
+
V5_2 = Current
|
17
|
+
|
18
|
+
class V5_1 < V5_2
|
19
|
+
def change_column(table_name, column_name, type, options = {})
|
20
|
+
if connection.adapter_name == "PostgreSQL"
|
21
|
+
super(table_name, column_name, type, options.except(:default, :null, :comment))
|
22
|
+
connection.change_column_default(table_name, column_name, options[:default]) if options.key?(:default)
|
23
|
+
connection.change_column_null(table_name, column_name, options[:null], options[:default]) if options.key?(:null)
|
24
|
+
connection.change_column_comment(table_name, column_name, options[:comment]) if options.key?(:comment)
|
25
|
+
else
|
26
|
+
super
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
def create_table(table_name, options = {})
|
31
|
+
if connection.adapter_name == "Mysql2"
|
32
|
+
super(table_name, options: "ENGINE=InnoDB", **options)
|
33
|
+
else
|
34
|
+
super
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
15
38
|
|
16
39
|
class V5_0 < V5_1
|
17
40
|
module TableDefinition
|
41
|
+
def primary_key(name, type = :primary_key, **options)
|
42
|
+
type = :integer if type == :primary_key
|
43
|
+
super
|
44
|
+
end
|
45
|
+
|
18
46
|
def references(*args, **options)
|
19
47
|
super(*args, type: :integer, **options)
|
20
48
|
end
|
@@ -22,19 +50,19 @@ module ActiveRecord
|
|
22
50
|
end
|
23
51
|
|
24
52
|
def create_table(table_name, options = {})
|
25
|
-
if adapter_name == "PostgreSQL"
|
53
|
+
if connection.adapter_name == "PostgreSQL"
|
26
54
|
if options[:id] == :uuid && !options.key?(:default)
|
27
55
|
options[:default] = "uuid_generate_v4()"
|
28
56
|
end
|
29
57
|
end
|
30
58
|
|
31
|
-
unless adapter_name == "Mysql2" && options[:id] == :bigint
|
59
|
+
unless connection.adapter_name == "Mysql2" && options[:id] == :bigint
|
32
60
|
if [:integer, :bigint].include?(options[:id]) && !options.key?(:default)
|
33
61
|
options[:default] = nil
|
34
62
|
end
|
35
63
|
end
|
36
64
|
|
37
|
-
# Since 5.1
|
65
|
+
# Since 5.1 PostgreSQL adapter uses bigserial type for primary
|
38
66
|
# keys by default and MySQL uses bigint. This compat layer makes old migrations utilize
|
39
67
|
# serial/int type instead -- the way it used to work before 5.1.
|
40
68
|
unless options.key?(:id)
|
@@ -72,6 +100,14 @@ module ActiveRecord
|
|
72
100
|
end
|
73
101
|
end
|
74
102
|
|
103
|
+
def add_column(table_name, column_name, type, options = {})
|
104
|
+
if type == :primary_key
|
105
|
+
type = :integer
|
106
|
+
options[:primary_key] = true
|
107
|
+
end
|
108
|
+
super
|
109
|
+
end
|
110
|
+
|
75
111
|
def add_reference(table_name, ref_name, **options)
|
76
112
|
super(table_name, ref_name, type: :integer, **options)
|
77
113
|
end
|
@@ -137,7 +173,7 @@ module ActiveRecord
|
|
137
173
|
if options[:name].present?
|
138
174
|
options[:name].to_s
|
139
175
|
else
|
140
|
-
index_name(table_name, column: column_names)
|
176
|
+
connection.index_name(table_name, column: column_names)
|
141
177
|
end
|
142
178
|
super
|
143
179
|
end
|
@@ -157,15 +193,17 @@ module ActiveRecord
|
|
157
193
|
end
|
158
194
|
|
159
195
|
def index_name_for_remove(table_name, options = {})
|
160
|
-
index_name = index_name(table_name, options)
|
196
|
+
index_name = connection.index_name(table_name, options)
|
161
197
|
|
162
|
-
unless index_name_exists?(table_name, index_name)
|
198
|
+
unless connection.index_name_exists?(table_name, index_name)
|
163
199
|
if options.is_a?(Hash) && options.has_key?(:name)
|
164
200
|
options_without_column = options.dup
|
165
201
|
options_without_column.delete :column
|
166
|
-
index_name_without_column = index_name(table_name, options_without_column)
|
202
|
+
index_name_without_column = connection.index_name(table_name, options_without_column)
|
167
203
|
|
168
|
-
|
204
|
+
if connection.index_name_exists?(table_name, index_name_without_column)
|
205
|
+
return index_name_without_column
|
206
|
+
end
|
169
207
|
end
|
170
208
|
|
171
209
|
raise ArgumentError, "Index name '#{index_name}' on table '#{table_name}' does not exist"
|