activerecord 5.2.8.1 → 6.1.7.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +1347 -624
- data/MIT-LICENSE +3 -1
- data/README.rdoc +7 -5
- data/examples/performance.rb +1 -1
- data/lib/active_record/aggregations.rb +9 -8
- data/lib/active_record/association_relation.rb +30 -10
- data/lib/active_record/associations/alias_tracker.rb +19 -16
- data/lib/active_record/associations/association.rb +100 -41
- data/lib/active_record/associations/association_scope.rb +23 -21
- data/lib/active_record/associations/belongs_to_association.rb +55 -48
- data/lib/active_record/associations/belongs_to_polymorphic_association.rb +7 -6
- data/lib/active_record/associations/builder/association.rb +45 -22
- data/lib/active_record/associations/builder/belongs_to.rb +29 -59
- data/lib/active_record/associations/builder/collection_association.rb +8 -17
- data/lib/active_record/associations/builder/has_and_belongs_to_many.rb +17 -41
- data/lib/active_record/associations/builder/has_many.rb +8 -2
- data/lib/active_record/associations/builder/has_one.rb +33 -2
- data/lib/active_record/associations/builder/singular_association.rb +3 -1
- data/lib/active_record/associations/collection_association.rb +44 -34
- data/lib/active_record/associations/collection_proxy.rb +25 -21
- data/lib/active_record/associations/foreign_association.rb +20 -0
- data/lib/active_record/associations/has_many_association.rb +26 -13
- data/lib/active_record/associations/has_many_through_association.rb +24 -18
- data/lib/active_record/associations/has_one_association.rb +43 -31
- data/lib/active_record/associations/has_one_through_association.rb +5 -5
- data/lib/active_record/associations/join_dependency/join_association.rb +44 -22
- data/lib/active_record/associations/join_dependency/join_part.rb +5 -5
- data/lib/active_record/associations/join_dependency.rb +91 -60
- data/lib/active_record/associations/preloader/association.rb +69 -43
- data/lib/active_record/associations/preloader/through_association.rb +49 -40
- data/lib/active_record/associations/preloader.rb +47 -34
- data/lib/active_record/associations/singular_association.rb +3 -17
- data/lib/active_record/associations/through_association.rb +1 -1
- data/lib/active_record/associations.rb +137 -25
- data/lib/active_record/attribute_assignment.rb +17 -19
- data/lib/active_record/attribute_methods/before_type_cast.rb +13 -7
- data/lib/active_record/attribute_methods/dirty.rb +101 -40
- data/lib/active_record/attribute_methods/primary_key.rb +20 -25
- data/lib/active_record/attribute_methods/query.rb +4 -8
- data/lib/active_record/attribute_methods/read.rb +14 -56
- data/lib/active_record/attribute_methods/serialization.rb +12 -7
- data/lib/active_record/attribute_methods/time_zone_conversion.rb +12 -15
- data/lib/active_record/attribute_methods/write.rb +18 -34
- data/lib/active_record/attribute_methods.rb +81 -143
- data/lib/active_record/attributes.rb +46 -9
- data/lib/active_record/autosave_association.rb +57 -42
- data/lib/active_record/base.rb +4 -17
- data/lib/active_record/callbacks.rb +158 -43
- data/lib/active_record/coders/yaml_column.rb +16 -7
- data/lib/active_record/connection_adapters/abstract/connection_pool.rb +272 -130
- data/lib/active_record/connection_adapters/abstract/database_limits.rb +7 -36
- data/lib/active_record/connection_adapters/abstract/database_statements.rb +167 -146
- data/lib/active_record/connection_adapters/abstract/query_cache.rb +18 -14
- data/lib/active_record/connection_adapters/abstract/quoting.rb +107 -47
- data/lib/active_record/connection_adapters/abstract/savepoints.rb +3 -3
- data/lib/active_record/connection_adapters/abstract/schema_creation.rb +153 -110
- data/lib/active_record/connection_adapters/abstract/schema_definitions.rb +211 -90
- data/lib/active_record/connection_adapters/abstract/schema_dumper.rb +2 -4
- data/lib/active_record/connection_adapters/abstract/schema_statements.rb +385 -144
- data/lib/active_record/connection_adapters/abstract/transaction.rb +167 -69
- data/lib/active_record/connection_adapters/abstract_adapter.rb +229 -99
- data/lib/active_record/connection_adapters/abstract_mysql_adapter.rb +243 -275
- data/lib/active_record/connection_adapters/column.rb +30 -12
- data/lib/active_record/connection_adapters/deduplicable.rb +29 -0
- data/lib/active_record/connection_adapters/legacy_pool_manager.rb +35 -0
- data/lib/active_record/connection_adapters/mysql/column.rb +1 -1
- data/lib/active_record/connection_adapters/mysql/database_statements.rb +88 -32
- data/lib/active_record/connection_adapters/mysql/explain_pretty_printer.rb +1 -2
- data/lib/active_record/connection_adapters/mysql/quoting.rb +59 -7
- data/lib/active_record/connection_adapters/mysql/schema_creation.rb +34 -10
- data/lib/active_record/connection_adapters/mysql/schema_definitions.rb +48 -32
- data/lib/active_record/connection_adapters/mysql/schema_dumper.rb +18 -7
- data/lib/active_record/connection_adapters/mysql/schema_statements.rb +142 -19
- data/lib/active_record/connection_adapters/mysql/type_metadata.rb +14 -9
- data/lib/active_record/connection_adapters/mysql2_adapter.rb +53 -18
- data/lib/active_record/connection_adapters/pool_config.rb +73 -0
- data/lib/active_record/connection_adapters/pool_manager.rb +47 -0
- data/lib/active_record/connection_adapters/postgresql/column.rb +37 -28
- data/lib/active_record/connection_adapters/postgresql/database_statements.rb +40 -54
- data/lib/active_record/connection_adapters/postgresql/oid/array.rb +1 -2
- data/lib/active_record/connection_adapters/postgresql/oid/bit.rb +1 -4
- data/lib/active_record/connection_adapters/postgresql/oid/cidr.rb +3 -5
- data/lib/active_record/connection_adapters/postgresql/oid/date.rb +2 -2
- data/lib/active_record/connection_adapters/postgresql/oid/date_time.rb +10 -2
- data/lib/active_record/connection_adapters/postgresql/oid/enum.rb +0 -1
- data/lib/active_record/connection_adapters/postgresql/oid/hstore.rb +1 -2
- data/lib/active_record/connection_adapters/postgresql/oid/interval.rb +49 -0
- data/lib/active_record/connection_adapters/postgresql/oid/legacy_point.rb +3 -4
- data/lib/active_record/connection_adapters/postgresql/oid/macaddr.rb +25 -0
- data/lib/active_record/connection_adapters/postgresql/oid/oid.rb +1 -1
- data/lib/active_record/connection_adapters/postgresql/oid/point.rb +3 -4
- data/lib/active_record/connection_adapters/postgresql/oid/range.rb +25 -7
- data/lib/active_record/connection_adapters/postgresql/oid/specialized_string.rb +1 -1
- data/lib/active_record/connection_adapters/postgresql/oid/type_map_initializer.rb +9 -7
- data/lib/active_record/connection_adapters/postgresql/oid/uuid.rb +15 -3
- data/lib/active_record/connection_adapters/postgresql/oid.rb +2 -0
- data/lib/active_record/connection_adapters/postgresql/quoting.rb +73 -10
- data/lib/active_record/connection_adapters/postgresql/referential_integrity.rb +2 -2
- data/lib/active_record/connection_adapters/postgresql/schema_creation.rb +19 -4
- data/lib/active_record/connection_adapters/postgresql/schema_definitions.rb +107 -91
- data/lib/active_record/connection_adapters/postgresql/schema_dumper.rb +0 -1
- data/lib/active_record/connection_adapters/postgresql/schema_statements.rb +120 -100
- data/lib/active_record/connection_adapters/postgresql/type_metadata.rb +31 -26
- data/lib/active_record/connection_adapters/postgresql/utils.rb +0 -1
- data/lib/active_record/connection_adapters/postgresql_adapter.rb +225 -121
- data/lib/active_record/connection_adapters/schema_cache.rb +159 -21
- data/lib/active_record/connection_adapters/sql_type_metadata.rb +17 -6
- data/lib/active_record/connection_adapters/sqlite3/database_statements.rb +146 -0
- data/lib/active_record/connection_adapters/sqlite3/quoting.rb +42 -7
- data/lib/active_record/connection_adapters/sqlite3/schema_creation.rb +5 -1
- data/lib/active_record/connection_adapters/sqlite3/schema_statements.rb +77 -13
- data/lib/active_record/connection_adapters/sqlite3_adapter.rb +174 -186
- data/lib/active_record/connection_adapters/statement_pool.rb +0 -1
- data/lib/active_record/connection_adapters.rb +52 -0
- data/lib/active_record/connection_handling.rb +293 -33
- data/lib/active_record/core.rb +341 -99
- data/lib/active_record/counter_cache.rb +8 -30
- data/lib/active_record/database_configurations/connection_url_resolver.rb +99 -0
- data/lib/active_record/database_configurations/database_config.rb +80 -0
- data/lib/active_record/database_configurations/hash_config.rb +96 -0
- data/lib/active_record/database_configurations/url_config.rb +53 -0
- data/lib/active_record/database_configurations.rb +273 -0
- data/lib/active_record/delegated_type.rb +209 -0
- data/lib/active_record/destroy_association_async_job.rb +36 -0
- data/lib/active_record/dynamic_matchers.rb +3 -4
- data/lib/active_record/enum.rb +108 -36
- data/lib/active_record/errors.rb +62 -19
- data/lib/active_record/explain.rb +10 -6
- data/lib/active_record/explain_subscriber.rb +1 -1
- data/lib/active_record/fixture_set/file.rb +10 -17
- data/lib/active_record/fixture_set/model_metadata.rb +32 -0
- data/lib/active_record/fixture_set/render_context.rb +17 -0
- data/lib/active_record/fixture_set/table_row.rb +152 -0
- data/lib/active_record/fixture_set/table_rows.rb +46 -0
- data/lib/active_record/fixtures.rb +200 -481
- data/lib/active_record/gem_version.rb +4 -4
- data/lib/active_record/inheritance.rb +53 -24
- data/lib/active_record/insert_all.rb +212 -0
- data/lib/active_record/integration.rb +67 -17
- data/lib/active_record/internal_metadata.rb +28 -9
- data/lib/active_record/legacy_yaml_adapter.rb +7 -3
- data/lib/active_record/locking/optimistic.rb +37 -23
- data/lib/active_record/locking/pessimistic.rb +9 -5
- data/lib/active_record/log_subscriber.rb +35 -35
- data/lib/active_record/middleware/database_selector/resolver/session.rb +48 -0
- data/lib/active_record/middleware/database_selector/resolver.rb +92 -0
- data/lib/active_record/middleware/database_selector.rb +77 -0
- data/lib/active_record/migration/command_recorder.rb +96 -44
- data/lib/active_record/migration/compatibility.rb +145 -64
- data/lib/active_record/migration/join_table.rb +0 -1
- data/lib/active_record/migration.rb +206 -157
- data/lib/active_record/model_schema.rb +148 -22
- data/lib/active_record/nested_attributes.rb +4 -7
- data/lib/active_record/no_touching.rb +8 -1
- data/lib/active_record/null_relation.rb +0 -1
- data/lib/active_record/persistence.rb +267 -59
- data/lib/active_record/query_cache.rb +21 -4
- data/lib/active_record/querying.rb +40 -23
- data/lib/active_record/railtie.rb +113 -74
- data/lib/active_record/railties/console_sandbox.rb +2 -4
- data/lib/active_record/railties/controller_runtime.rb +30 -35
- data/lib/active_record/railties/databases.rake +411 -80
- data/lib/active_record/readonly_attributes.rb +4 -0
- data/lib/active_record/reflection.rb +109 -93
- data/lib/active_record/relation/batches/batch_enumerator.rb +25 -9
- data/lib/active_record/relation/batches.rb +44 -35
- data/lib/active_record/relation/calculations.rb +157 -90
- data/lib/active_record/relation/delegation.rb +35 -50
- data/lib/active_record/relation/finder_methods.rb +64 -39
- data/lib/active_record/relation/from_clause.rb +5 -1
- data/lib/active_record/relation/merger.rb +32 -40
- data/lib/active_record/relation/predicate_builder/array_handler.rb +13 -13
- data/lib/active_record/relation/predicate_builder/association_query_value.rb +5 -9
- data/lib/active_record/relation/predicate_builder/basic_object_handler.rb +1 -2
- data/lib/active_record/relation/predicate_builder/polymorphic_array_value.rb +11 -10
- data/lib/active_record/relation/predicate_builder/range_handler.rb +3 -23
- data/lib/active_record/relation/predicate_builder/relation_handler.rb +1 -1
- data/lib/active_record/relation/predicate_builder.rb +62 -45
- data/lib/active_record/relation/query_attribute.rb +13 -8
- data/lib/active_record/relation/query_methods.rb +478 -187
- data/lib/active_record/relation/record_fetch_warning.rb +3 -3
- data/lib/active_record/relation/spawn_methods.rb +9 -9
- data/lib/active_record/relation/where_clause.rb +115 -62
- data/lib/active_record/relation.rb +379 -115
- data/lib/active_record/result.rb +64 -38
- data/lib/active_record/runtime_registry.rb +2 -2
- data/lib/active_record/sanitization.rb +22 -41
- data/lib/active_record/schema.rb +2 -11
- data/lib/active_record/schema_dumper.rb +54 -9
- data/lib/active_record/schema_migration.rb +7 -9
- data/lib/active_record/scoping/default.rb +4 -8
- data/lib/active_record/scoping/named.rb +17 -24
- data/lib/active_record/scoping.rb +8 -9
- data/lib/active_record/secure_token.rb +16 -8
- data/lib/active_record/serialization.rb +5 -3
- data/lib/active_record/signed_id.rb +116 -0
- data/lib/active_record/statement_cache.rb +49 -6
- data/lib/active_record/store.rb +94 -10
- data/lib/active_record/suppressor.rb +2 -2
- data/lib/active_record/table_metadata.rb +42 -43
- data/lib/active_record/tasks/database_tasks.rb +277 -81
- data/lib/active_record/tasks/mysql_database_tasks.rb +37 -39
- data/lib/active_record/tasks/postgresql_database_tasks.rb +27 -32
- data/lib/active_record/tasks/sqlite_database_tasks.rb +14 -17
- data/lib/active_record/test_databases.rb +24 -0
- data/lib/active_record/test_fixtures.rb +291 -0
- data/lib/active_record/timestamp.rb +43 -32
- data/lib/active_record/touch_later.rb +23 -22
- data/lib/active_record/transactions.rb +62 -118
- data/lib/active_record/translation.rb +1 -1
- data/lib/active_record/type/adapter_specific_registry.rb +3 -13
- data/lib/active_record/type/hash_lookup_type_map.rb +0 -1
- data/lib/active_record/type/serialized.rb +6 -3
- data/lib/active_record/type/time.rb +10 -0
- data/lib/active_record/type/type_map.rb +0 -1
- data/lib/active_record/type/unsigned_integer.rb +0 -1
- data/lib/active_record/type.rb +10 -5
- data/lib/active_record/type_caster/connection.rb +15 -15
- data/lib/active_record/type_caster/map.rb +8 -8
- data/lib/active_record/validations/associated.rb +1 -2
- data/lib/active_record/validations/numericality.rb +35 -0
- data/lib/active_record/validations/uniqueness.rb +38 -30
- data/lib/active_record/validations.rb +4 -3
- data/lib/active_record.rb +13 -12
- data/lib/arel/alias_predication.rb +9 -0
- data/lib/arel/attributes/attribute.rb +41 -0
- data/lib/arel/collectors/bind.rb +29 -0
- data/lib/arel/collectors/composite.rb +39 -0
- data/lib/arel/collectors/plain_string.rb +20 -0
- data/lib/arel/collectors/sql_string.rb +27 -0
- data/lib/arel/collectors/substitute_binds.rb +35 -0
- data/lib/arel/crud.rb +42 -0
- data/lib/arel/delete_manager.rb +18 -0
- data/lib/arel/errors.rb +9 -0
- data/lib/arel/expressions.rb +29 -0
- data/lib/arel/factory_methods.rb +49 -0
- data/lib/arel/insert_manager.rb +49 -0
- data/lib/arel/math.rb +45 -0
- data/lib/arel/nodes/and.rb +32 -0
- data/lib/arel/nodes/ascending.rb +23 -0
- data/lib/arel/nodes/binary.rb +126 -0
- data/lib/arel/nodes/bind_param.rb +44 -0
- data/lib/arel/nodes/case.rb +55 -0
- data/lib/arel/nodes/casted.rb +62 -0
- data/lib/arel/nodes/comment.rb +29 -0
- data/lib/arel/nodes/count.rb +12 -0
- data/lib/arel/nodes/delete_statement.rb +45 -0
- data/lib/arel/nodes/descending.rb +23 -0
- data/lib/arel/nodes/equality.rb +15 -0
- data/lib/arel/nodes/extract.rb +24 -0
- data/lib/arel/nodes/false.rb +16 -0
- data/lib/arel/nodes/full_outer_join.rb +8 -0
- data/lib/arel/nodes/function.rb +44 -0
- data/lib/arel/nodes/grouping.rb +11 -0
- data/lib/arel/nodes/homogeneous_in.rb +76 -0
- data/lib/arel/nodes/in.rb +15 -0
- data/lib/arel/nodes/infix_operation.rb +92 -0
- data/lib/arel/nodes/inner_join.rb +8 -0
- data/lib/arel/nodes/insert_statement.rb +37 -0
- data/lib/arel/nodes/join_source.rb +20 -0
- data/lib/arel/nodes/matches.rb +18 -0
- data/lib/arel/nodes/named_function.rb +23 -0
- data/lib/arel/nodes/node.rb +51 -0
- data/lib/arel/nodes/node_expression.rb +13 -0
- data/lib/arel/nodes/ordering.rb +27 -0
- data/lib/arel/nodes/outer_join.rb +8 -0
- data/lib/arel/nodes/over.rb +15 -0
- data/lib/arel/nodes/regexp.rb +16 -0
- data/lib/arel/nodes/right_outer_join.rb +8 -0
- data/lib/arel/nodes/select_core.rb +67 -0
- data/lib/arel/nodes/select_statement.rb +41 -0
- data/lib/arel/nodes/sql_literal.rb +19 -0
- data/lib/arel/nodes/string_join.rb +11 -0
- data/lib/arel/nodes/table_alias.rb +31 -0
- data/lib/arel/nodes/terminal.rb +16 -0
- data/lib/arel/nodes/true.rb +16 -0
- data/lib/arel/nodes/unary.rb +44 -0
- data/lib/arel/nodes/unary_operation.rb +20 -0
- data/lib/arel/nodes/unqualified_column.rb +22 -0
- data/lib/arel/nodes/update_statement.rb +41 -0
- data/lib/arel/nodes/values_list.rb +9 -0
- data/lib/arel/nodes/window.rb +126 -0
- data/lib/arel/nodes/with.rb +11 -0
- data/lib/arel/nodes.rb +70 -0
- data/lib/arel/order_predications.rb +13 -0
- data/lib/arel/predications.rb +250 -0
- data/lib/arel/select_manager.rb +270 -0
- data/lib/arel/table.rb +118 -0
- data/lib/arel/tree_manager.rb +72 -0
- data/lib/arel/update_manager.rb +34 -0
- data/lib/arel/visitors/dot.rb +308 -0
- data/lib/arel/visitors/mysql.rb +93 -0
- data/lib/arel/visitors/postgresql.rb +120 -0
- data/lib/arel/visitors/sqlite.rb +38 -0
- data/lib/arel/visitors/to_sql.rb +899 -0
- data/lib/arel/visitors/visitor.rb +45 -0
- data/lib/arel/visitors.rb +13 -0
- data/lib/arel/window_predications.rb +9 -0
- data/lib/arel.rb +54 -0
- data/lib/rails/generators/active_record/application_record/application_record_generator.rb +0 -1
- data/lib/rails/generators/active_record/migration/migration_generator.rb +3 -5
- data/lib/rails/generators/active_record/migration/templates/create_table_migration.rb.tt +3 -1
- data/lib/rails/generators/active_record/migration/templates/migration.rb.tt +7 -5
- data/lib/rails/generators/active_record/migration.rb +19 -2
- data/lib/rails/generators/active_record/model/model_generator.rb +39 -2
- data/lib/rails/generators/active_record/model/templates/abstract_base_class.rb.tt +7 -0
- data/lib/rails/generators/active_record/model/templates/model.rb.tt +10 -1
- metadata +118 -32
- data/lib/active_record/attribute_decorators.rb +0 -90
- data/lib/active_record/collection_cache_key.rb +0 -53
- data/lib/active_record/connection_adapters/connection_specification.rb +0 -287
- data/lib/active_record/connection_adapters/determine_if_preparable_visitor.rb +0 -33
- data/lib/active_record/define_callbacks.rb +0 -22
- data/lib/active_record/relation/predicate_builder/base_handler.rb +0 -19
- data/lib/active_record/relation/where_clause_factory.rb +0 -34
@@ -38,6 +38,8 @@ module ActiveRecord
|
|
38
38
|
extend ActiveSupport::Concern
|
39
39
|
|
40
40
|
included do
|
41
|
+
class_attribute :store_full_class_name, instance_writer: false, default: true
|
42
|
+
|
41
43
|
# Determines whether to store the full constant name including namespace when using STI.
|
42
44
|
# This is true, by default.
|
43
45
|
class_attribute :store_full_sti_class, instance_writer: false, default: true
|
@@ -52,10 +54,14 @@ module ActiveRecord
|
|
52
54
|
raise NotImplementedError, "#{self} is an abstract class and cannot be instantiated."
|
53
55
|
end
|
54
56
|
|
55
|
-
if
|
57
|
+
if _has_attribute?(inheritance_column)
|
56
58
|
subclass = subclass_from_attributes(attributes)
|
57
59
|
|
58
|
-
if subclass.nil? &&
|
60
|
+
if subclass.nil? && scope_attributes = current_scope&.scope_for_create
|
61
|
+
subclass = subclass_from_attributes(scope_attributes)
|
62
|
+
end
|
63
|
+
|
64
|
+
if subclass.nil? && base_class?
|
59
65
|
subclass = subclass_from_attributes(column_defaults)
|
60
66
|
end
|
61
67
|
end
|
@@ -104,6 +110,12 @@ module ActiveRecord
|
|
104
110
|
end
|
105
111
|
end
|
106
112
|
|
113
|
+
# Returns whether the class is a base class.
|
114
|
+
# See #base_class for more information.
|
115
|
+
def base_class?
|
116
|
+
base_class == self
|
117
|
+
end
|
118
|
+
|
107
119
|
# Set this to +true+ if this is an abstract class (see
|
108
120
|
# <tt>abstract_class?</tt>).
|
109
121
|
# If you are using inheritance with Active Record and don't want a class
|
@@ -152,12 +164,42 @@ module ActiveRecord
|
|
152
164
|
defined?(@abstract_class) && @abstract_class == true
|
153
165
|
end
|
154
166
|
|
167
|
+
# Returns the value to be stored in the inheritance column for STI.
|
155
168
|
def sti_name
|
156
|
-
store_full_sti_class ? name : name.demodulize
|
169
|
+
store_full_sti_class && store_full_class_name ? name : name.demodulize
|
157
170
|
end
|
158
171
|
|
172
|
+
# Returns the class for the provided +type_name+.
|
173
|
+
#
|
174
|
+
# It is used to find the class correspondent to the value stored in the inheritance column.
|
175
|
+
def sti_class_for(type_name)
|
176
|
+
if store_full_sti_class && store_full_class_name
|
177
|
+
ActiveSupport::Dependencies.constantize(type_name)
|
178
|
+
else
|
179
|
+
compute_type(type_name)
|
180
|
+
end
|
181
|
+
rescue NameError
|
182
|
+
raise SubclassNotFound,
|
183
|
+
"The single-table inheritance mechanism failed to locate the subclass: '#{type_name}'. " \
|
184
|
+
"This error is raised because the column '#{inheritance_column}' is reserved for storing the class in case of inheritance. " \
|
185
|
+
"Please rename this column if you didn't intend it to be used for storing the inheritance class " \
|
186
|
+
"or overwrite #{name}.inheritance_column to use another column for that information."
|
187
|
+
end
|
188
|
+
|
189
|
+
# Returns the value to be stored in the polymorphic type column for Polymorphic Associations.
|
159
190
|
def polymorphic_name
|
160
|
-
base_class.name
|
191
|
+
store_full_class_name ? base_class.name : base_class.name.demodulize
|
192
|
+
end
|
193
|
+
|
194
|
+
# Returns the class for the provided +name+.
|
195
|
+
#
|
196
|
+
# It is used to find the class correspondent to the value stored in the polymorphic type column.
|
197
|
+
def polymorphic_class_for(name)
|
198
|
+
if store_full_class_name
|
199
|
+
ActiveSupport::Dependencies.constantize(name)
|
200
|
+
else
|
201
|
+
compute_type(name)
|
202
|
+
end
|
161
203
|
end
|
162
204
|
|
163
205
|
def inherited(subclass)
|
@@ -166,11 +208,10 @@ module ActiveRecord
|
|
166
208
|
end
|
167
209
|
|
168
210
|
protected
|
169
|
-
|
170
211
|
# Returns the class type of the record using the current module as a prefix. So descendants of
|
171
212
|
# MyApp::Business::Account would appear as MyApp::Business::AccountSubclass.
|
172
213
|
def compute_type(type_name)
|
173
|
-
if type_name.start_with?("::"
|
214
|
+
if type_name.start_with?("::")
|
174
215
|
# If the type is prefixed with a scope operator then we assume that
|
175
216
|
# the type_name is an absolute reference.
|
176
217
|
ActiveSupport::Dependencies.constantize(type_name)
|
@@ -198,7 +239,6 @@ module ActiveRecord
|
|
198
239
|
end
|
199
240
|
|
200
241
|
private
|
201
|
-
|
202
242
|
# Called by +instantiate+ to decide which class to use for a new
|
203
243
|
# record instance. For single-table inheritance, we check the record
|
204
244
|
# for a +type+ column and return the corresponding class.
|
@@ -211,35 +251,25 @@ module ActiveRecord
|
|
211
251
|
end
|
212
252
|
|
213
253
|
def using_single_table_inheritance?(record)
|
214
|
-
record[inheritance_column].present? &&
|
254
|
+
record[inheritance_column].present? && _has_attribute?(inheritance_column)
|
215
255
|
end
|
216
256
|
|
217
257
|
def find_sti_class(type_name)
|
218
258
|
type_name = base_class.type_for_attribute(inheritance_column).cast(type_name)
|
219
|
-
subclass =
|
220
|
-
|
221
|
-
ActiveSupport::Dependencies.constantize(type_name)
|
222
|
-
else
|
223
|
-
compute_type(type_name)
|
224
|
-
end
|
225
|
-
rescue NameError
|
226
|
-
raise SubclassNotFound,
|
227
|
-
"The single-table inheritance mechanism failed to locate the subclass: '#{type_name}'. " \
|
228
|
-
"This error is raised because the column '#{inheritance_column}' is reserved for storing the class in case of inheritance. " \
|
229
|
-
"Please rename this column if you didn't intend it to be used for storing the inheritance class " \
|
230
|
-
"or overwrite #{name}.inheritance_column to use another column for that information."
|
231
|
-
end
|
259
|
+
subclass = sti_class_for(type_name)
|
260
|
+
|
232
261
|
unless subclass == self || descendants.include?(subclass)
|
233
262
|
raise SubclassNotFound, "Invalid single-table inheritance type: #{subclass.name} is not a subclass of #{name}"
|
234
263
|
end
|
264
|
+
|
235
265
|
subclass
|
236
266
|
end
|
237
267
|
|
238
268
|
def type_condition(table = arel_table)
|
239
|
-
sti_column =
|
269
|
+
sti_column = table[inheritance_column]
|
240
270
|
sti_names = ([self] + descendants).map(&:sti_name)
|
241
271
|
|
242
|
-
|
272
|
+
predicate_builder.build(sti_column, sti_names)
|
243
273
|
end
|
244
274
|
|
245
275
|
# Detect the subclass from the inheritance column of attrs. If the inheritance column value
|
@@ -262,7 +292,6 @@ module ActiveRecord
|
|
262
292
|
end
|
263
293
|
|
264
294
|
private
|
265
|
-
|
266
295
|
def initialize_internals_callback
|
267
296
|
super
|
268
297
|
ensure_proper_type
|
@@ -0,0 +1,212 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "active_support/core_ext/enumerable"
|
4
|
+
|
5
|
+
module ActiveRecord
|
6
|
+
class InsertAll # :nodoc:
|
7
|
+
attr_reader :model, :connection, :inserts, :keys
|
8
|
+
attr_reader :on_duplicate, :returning, :unique_by
|
9
|
+
|
10
|
+
def initialize(model, inserts, on_duplicate:, returning: nil, unique_by: nil)
|
11
|
+
raise ArgumentError, "Empty list of attributes passed" if inserts.blank?
|
12
|
+
|
13
|
+
@model, @connection, @inserts, @keys = model, model.connection, inserts, inserts.first.keys.map(&:to_s)
|
14
|
+
@on_duplicate, @returning, @unique_by = on_duplicate, returning, unique_by
|
15
|
+
|
16
|
+
if model.scope_attributes?
|
17
|
+
@scope_attributes = model.scope_attributes
|
18
|
+
@keys |= @scope_attributes.keys
|
19
|
+
end
|
20
|
+
@keys = @keys.to_set
|
21
|
+
|
22
|
+
@returning = (connection.supports_insert_returning? ? primary_keys : false) if @returning.nil?
|
23
|
+
@returning = false if @returning == []
|
24
|
+
|
25
|
+
@unique_by = find_unique_index_for(unique_by)
|
26
|
+
@on_duplicate = :skip if @on_duplicate == :update && updatable_columns.empty?
|
27
|
+
|
28
|
+
ensure_valid_options_for_connection!
|
29
|
+
end
|
30
|
+
|
31
|
+
def execute
|
32
|
+
message = +"#{model} "
|
33
|
+
message << "Bulk " if inserts.many?
|
34
|
+
message << (on_duplicate == :update ? "Upsert" : "Insert")
|
35
|
+
connection.exec_insert_all to_sql, message
|
36
|
+
end
|
37
|
+
|
38
|
+
def updatable_columns
|
39
|
+
keys - readonly_columns - unique_by_columns
|
40
|
+
end
|
41
|
+
|
42
|
+
def primary_keys
|
43
|
+
Array(connection.schema_cache.primary_keys(model.table_name))
|
44
|
+
end
|
45
|
+
|
46
|
+
|
47
|
+
def skip_duplicates?
|
48
|
+
on_duplicate == :skip
|
49
|
+
end
|
50
|
+
|
51
|
+
def update_duplicates?
|
52
|
+
on_duplicate == :update
|
53
|
+
end
|
54
|
+
|
55
|
+
def map_key_with_value
|
56
|
+
inserts.map do |attributes|
|
57
|
+
attributes = attributes.stringify_keys
|
58
|
+
attributes.merge!(scope_attributes) if scope_attributes
|
59
|
+
|
60
|
+
verify_attributes(attributes)
|
61
|
+
|
62
|
+
keys.map do |key|
|
63
|
+
yield key, attributes[key]
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
private
|
69
|
+
attr_reader :scope_attributes
|
70
|
+
|
71
|
+
def find_unique_index_for(unique_by)
|
72
|
+
if !connection.supports_insert_conflict_target?
|
73
|
+
return if unique_by.nil?
|
74
|
+
|
75
|
+
raise ArgumentError, "#{connection.class} does not support :unique_by"
|
76
|
+
end
|
77
|
+
|
78
|
+
name_or_columns = unique_by || model.primary_key
|
79
|
+
match = Array(name_or_columns).map(&:to_s)
|
80
|
+
|
81
|
+
if index = unique_indexes.find { |i| match.include?(i.name) || i.columns == match }
|
82
|
+
index
|
83
|
+
elsif match == primary_keys
|
84
|
+
unique_by.nil? ? nil : ActiveRecord::ConnectionAdapters::IndexDefinition.new(model.table_name, "#{model.table_name}_primary_key", true, match)
|
85
|
+
else
|
86
|
+
raise ArgumentError, "No unique index found for #{name_or_columns}"
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
90
|
+
def unique_indexes
|
91
|
+
connection.schema_cache.indexes(model.table_name).select(&:unique)
|
92
|
+
end
|
93
|
+
|
94
|
+
|
95
|
+
def ensure_valid_options_for_connection!
|
96
|
+
if returning && !connection.supports_insert_returning?
|
97
|
+
raise ArgumentError, "#{connection.class} does not support :returning"
|
98
|
+
end
|
99
|
+
|
100
|
+
if skip_duplicates? && !connection.supports_insert_on_duplicate_skip?
|
101
|
+
raise ArgumentError, "#{connection.class} does not support skipping duplicates"
|
102
|
+
end
|
103
|
+
|
104
|
+
if update_duplicates? && !connection.supports_insert_on_duplicate_update?
|
105
|
+
raise ArgumentError, "#{connection.class} does not support upsert"
|
106
|
+
end
|
107
|
+
|
108
|
+
if unique_by && !connection.supports_insert_conflict_target?
|
109
|
+
raise ArgumentError, "#{connection.class} does not support :unique_by"
|
110
|
+
end
|
111
|
+
end
|
112
|
+
|
113
|
+
|
114
|
+
def to_sql
|
115
|
+
connection.build_insert_sql(ActiveRecord::InsertAll::Builder.new(self))
|
116
|
+
end
|
117
|
+
|
118
|
+
|
119
|
+
def readonly_columns
|
120
|
+
primary_keys + model.readonly_attributes.to_a
|
121
|
+
end
|
122
|
+
|
123
|
+
def unique_by_columns
|
124
|
+
Array(unique_by&.columns)
|
125
|
+
end
|
126
|
+
|
127
|
+
|
128
|
+
def verify_attributes(attributes)
|
129
|
+
if keys != attributes.keys.to_set
|
130
|
+
raise ArgumentError, "All objects being inserted must have the same keys"
|
131
|
+
end
|
132
|
+
end
|
133
|
+
|
134
|
+
class Builder # :nodoc:
|
135
|
+
attr_reader :model
|
136
|
+
|
137
|
+
delegate :skip_duplicates?, :update_duplicates?, :keys, to: :insert_all
|
138
|
+
|
139
|
+
def initialize(insert_all)
|
140
|
+
@insert_all, @model, @connection = insert_all, insert_all.model, insert_all.connection
|
141
|
+
end
|
142
|
+
|
143
|
+
def into
|
144
|
+
"INTO #{model.quoted_table_name} (#{columns_list})"
|
145
|
+
end
|
146
|
+
|
147
|
+
def values_list
|
148
|
+
types = extract_types_from_columns_on(model.table_name, keys: keys)
|
149
|
+
|
150
|
+
values_list = insert_all.map_key_with_value do |key, value|
|
151
|
+
connection.with_yaml_fallback(types[key].serialize(value))
|
152
|
+
end
|
153
|
+
|
154
|
+
connection.visitor.compile(Arel::Nodes::ValuesList.new(values_list))
|
155
|
+
end
|
156
|
+
|
157
|
+
def returning
|
158
|
+
format_columns(insert_all.returning) if insert_all.returning
|
159
|
+
end
|
160
|
+
|
161
|
+
def conflict_target
|
162
|
+
if index = insert_all.unique_by
|
163
|
+
sql = +"(#{format_columns(index.columns)})"
|
164
|
+
sql << " WHERE #{index.where}" if index.where
|
165
|
+
sql
|
166
|
+
elsif update_duplicates?
|
167
|
+
"(#{format_columns(insert_all.primary_keys)})"
|
168
|
+
end
|
169
|
+
end
|
170
|
+
|
171
|
+
def updatable_columns
|
172
|
+
quote_columns(insert_all.updatable_columns)
|
173
|
+
end
|
174
|
+
|
175
|
+
def touch_model_timestamps_unless(&block)
|
176
|
+
model.send(:timestamp_attributes_for_update_in_model).map do |column_name|
|
177
|
+
if touch_timestamp_attribute?(column_name)
|
178
|
+
"#{column_name}=(CASE WHEN (#{updatable_columns.map(&block).join(" AND ")}) THEN #{model.quoted_table_name}.#{column_name} ELSE CURRENT_TIMESTAMP END),"
|
179
|
+
end
|
180
|
+
end.compact.join
|
181
|
+
end
|
182
|
+
|
183
|
+
private
|
184
|
+
attr_reader :connection, :insert_all
|
185
|
+
|
186
|
+
def touch_timestamp_attribute?(column_name)
|
187
|
+
update_duplicates? && !insert_all.updatable_columns.include?(column_name)
|
188
|
+
end
|
189
|
+
|
190
|
+
def columns_list
|
191
|
+
format_columns(insert_all.keys)
|
192
|
+
end
|
193
|
+
|
194
|
+
def extract_types_from_columns_on(table_name, keys:)
|
195
|
+
columns = connection.schema_cache.columns_hash(table_name)
|
196
|
+
|
197
|
+
unknown_column = (keys - columns.keys).first
|
198
|
+
raise UnknownAttributeError.new(model.new, unknown_column) if unknown_column
|
199
|
+
|
200
|
+
keys.index_with { |key| model.type_for_attribute(key) }
|
201
|
+
end
|
202
|
+
|
203
|
+
def format_columns(columns)
|
204
|
+
columns.respond_to?(:map) ? quote_columns(columns).join(",") : columns
|
205
|
+
end
|
206
|
+
|
207
|
+
def quote_columns(columns)
|
208
|
+
columns.map(&connection.method(:quote_column_name))
|
209
|
+
end
|
210
|
+
end
|
211
|
+
end
|
212
|
+
end
|
@@ -20,8 +20,16 @@ module ActiveRecord
|
|
20
20
|
# Indicates whether to use a stable #cache_key method that is accompanied
|
21
21
|
# by a changing version in the #cache_version method.
|
22
22
|
#
|
23
|
-
# This is +
|
23
|
+
# This is +true+, by default on Rails 5.2 and above.
|
24
24
|
class_attribute :cache_versioning, instance_writer: false, default: false
|
25
|
+
|
26
|
+
##
|
27
|
+
# :singleton-method:
|
28
|
+
# Indicates whether to use a stable #cache_key method that is accompanied
|
29
|
+
# by a changing version in the #cache_version method on collections.
|
30
|
+
#
|
31
|
+
# This is +false+, by default until Rails 6.1.
|
32
|
+
class_attribute :collection_cache_versioning, instance_writer: false, default: false
|
25
33
|
end
|
26
34
|
|
27
35
|
# Returns a +String+, which Action Pack uses for constructing a URL to this
|
@@ -60,24 +68,15 @@ module ActiveRecord
|
|
60
68
|
# the cache key will also include a version.
|
61
69
|
#
|
62
70
|
# Product.cache_versioning = false
|
63
|
-
#
|
64
|
-
def cache_key
|
71
|
+
# Product.find(5).cache_key # => "products/5-20071224150000" (updated_at available)
|
72
|
+
def cache_key
|
65
73
|
if new_record?
|
66
74
|
"#{model_name.cache_key}/new"
|
67
75
|
else
|
68
|
-
if cache_version
|
76
|
+
if cache_version
|
69
77
|
"#{model_name.cache_key}/#{id}"
|
70
78
|
else
|
71
|
-
timestamp =
|
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
|
76
|
-
|
77
|
-
max_updated_column_timestamp(timestamp_names)
|
78
|
-
else
|
79
|
-
max_updated_column_timestamp
|
80
|
-
end
|
79
|
+
timestamp = max_updated_column_timestamp
|
81
80
|
|
82
81
|
if timestamp
|
83
82
|
timestamp = timestamp.utc.to_s(cache_timestamp_format)
|
@@ -94,10 +93,19 @@ module ActiveRecord
|
|
94
93
|
# cache_version, but this method can be overwritten to return something else.
|
95
94
|
#
|
96
95
|
# Note, this method will return nil if ActiveRecord::Base.cache_versioning is set to
|
97
|
-
# +false
|
96
|
+
# +false+.
|
98
97
|
def cache_version
|
99
|
-
|
100
|
-
|
98
|
+
return unless cache_versioning
|
99
|
+
|
100
|
+
if has_attribute?("updated_at")
|
101
|
+
timestamp = updated_at_before_type_cast
|
102
|
+
if can_use_fast_cache_version?(timestamp)
|
103
|
+
raw_timestamp_to_cache_version(timestamp)
|
104
|
+
elsif timestamp = updated_at
|
105
|
+
timestamp.utc.to_s(cache_timestamp_format)
|
106
|
+
end
|
107
|
+
elsif self.class.has_attribute?("updated_at")
|
108
|
+
raise ActiveModel::MissingAttributeError, "missing attribute: updated_at"
|
101
109
|
end
|
102
110
|
end
|
103
111
|
|
@@ -150,6 +158,48 @@ module ActiveRecord
|
|
150
158
|
end
|
151
159
|
end
|
152
160
|
end
|
161
|
+
|
162
|
+
def collection_cache_key(collection = all, timestamp_column = :updated_at) # :nodoc:
|
163
|
+
collection.send(:compute_cache_key, timestamp_column)
|
164
|
+
end
|
153
165
|
end
|
166
|
+
|
167
|
+
private
|
168
|
+
# Detects if the value before type cast
|
169
|
+
# can be used to generate a cache_version.
|
170
|
+
#
|
171
|
+
# The fast cache version only works with a
|
172
|
+
# string value directly from the database.
|
173
|
+
#
|
174
|
+
# We also must check if the timestamp format has been changed
|
175
|
+
# or if the timezone is not set to UTC then
|
176
|
+
# we cannot apply our transformations correctly.
|
177
|
+
def can_use_fast_cache_version?(timestamp)
|
178
|
+
timestamp.is_a?(String) &&
|
179
|
+
cache_timestamp_format == :usec &&
|
180
|
+
default_timezone == :utc &&
|
181
|
+
!updated_at_came_from_user?
|
182
|
+
end
|
183
|
+
|
184
|
+
# Converts a raw database string to `:usec`
|
185
|
+
# format.
|
186
|
+
#
|
187
|
+
# Example:
|
188
|
+
#
|
189
|
+
# timestamp = "2018-10-15 20:02:15.266505"
|
190
|
+
# raw_timestamp_to_cache_version(timestamp)
|
191
|
+
# # => "20181015200215266505"
|
192
|
+
#
|
193
|
+
# PostgreSQL truncates trailing zeros,
|
194
|
+
# https://github.com/postgres/postgres/commit/3e1beda2cde3495f41290e1ece5d544525810214
|
195
|
+
# to account for this we pad the output with zeros
|
196
|
+
def raw_timestamp_to_cache_version(timestamp)
|
197
|
+
key = timestamp.delete("- :.")
|
198
|
+
if key.length < 20
|
199
|
+
key.ljust(20, "0")
|
200
|
+
else
|
201
|
+
key
|
202
|
+
end
|
203
|
+
end
|
154
204
|
end
|
155
205
|
end
|
@@ -6,40 +6,59 @@ require "active_record/scoping/named"
|
|
6
6
|
module ActiveRecord
|
7
7
|
# This class is used to create a table that keeps track of values and keys such
|
8
8
|
# as which environment migrations were run in.
|
9
|
+
#
|
10
|
+
# This is enabled by default. To disable this functionality set
|
11
|
+
# `use_metadata_table` to false in your database configuration.
|
9
12
|
class InternalMetadata < ActiveRecord::Base # :nodoc:
|
13
|
+
self.record_timestamps = true
|
14
|
+
|
10
15
|
class << self
|
16
|
+
def enabled?
|
17
|
+
ActiveRecord::Base.connection.use_metadata_table?
|
18
|
+
end
|
19
|
+
|
20
|
+
def _internal?
|
21
|
+
true
|
22
|
+
end
|
23
|
+
|
11
24
|
def primary_key
|
12
25
|
"key"
|
13
26
|
end
|
14
27
|
|
15
28
|
def table_name
|
16
|
-
"#{table_name_prefix}#{
|
29
|
+
"#{table_name_prefix}#{internal_metadata_table_name}#{table_name_suffix}"
|
17
30
|
end
|
18
31
|
|
19
32
|
def []=(key, value)
|
20
|
-
|
33
|
+
return unless enabled?
|
34
|
+
|
35
|
+
find_or_initialize_by(key: key).update!(value: value)
|
21
36
|
end
|
22
37
|
|
23
38
|
def [](key)
|
24
|
-
|
25
|
-
end
|
39
|
+
return unless enabled?
|
26
40
|
|
27
|
-
|
28
|
-
connection.table_exists?(table_name)
|
41
|
+
where(key: key).pluck(:value).first
|
29
42
|
end
|
30
43
|
|
31
44
|
# Creates an internal metadata table with columns +key+ and +value+
|
32
45
|
def create_table
|
33
|
-
unless
|
34
|
-
key_options = connection.internal_string_options_for_primary_key
|
46
|
+
return unless enabled?
|
35
47
|
|
48
|
+
unless connection.table_exists?(table_name)
|
36
49
|
connection.create_table(table_name, id: false) do |t|
|
37
|
-
t.string :key,
|
50
|
+
t.string :key, **connection.internal_string_options_for_primary_key
|
38
51
|
t.string :value
|
39
52
|
t.timestamps
|
40
53
|
end
|
41
54
|
end
|
42
55
|
end
|
56
|
+
|
57
|
+
def drop_table
|
58
|
+
return unless enabled?
|
59
|
+
|
60
|
+
connection.drop_table table_name, if_exists: true
|
61
|
+
end
|
43
62
|
end
|
44
63
|
end
|
45
64
|
end
|
@@ -1,13 +1,17 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
module ActiveRecord
|
4
|
-
module LegacyYamlAdapter
|
4
|
+
module LegacyYamlAdapter # :nodoc:
|
5
5
|
def self.convert(klass, coder)
|
6
6
|
return coder unless coder.is_a?(Psych::Coder)
|
7
7
|
|
8
8
|
case coder["active_record_yaml_version"]
|
9
9
|
when 1, 2 then coder
|
10
10
|
else
|
11
|
+
ActiveSupport::Deprecation.warn(<<-MSG.squish)
|
12
|
+
YAML loading from legacy format older than Rails 5.0 is deprecated
|
13
|
+
and will be removed in Rails 7.0.
|
14
|
+
MSG
|
11
15
|
if coder["attributes"].is_a?(ActiveModel::AttributeSet)
|
12
16
|
Rails420.convert(klass, coder)
|
13
17
|
else
|
@@ -16,7 +20,7 @@ module ActiveRecord
|
|
16
20
|
end
|
17
21
|
end
|
18
22
|
|
19
|
-
module Rails420
|
23
|
+
module Rails420 # :nodoc:
|
20
24
|
def self.convert(klass, coder)
|
21
25
|
attribute_set = coder["attributes"]
|
22
26
|
|
@@ -32,7 +36,7 @@ module ActiveRecord
|
|
32
36
|
end
|
33
37
|
end
|
34
38
|
|
35
|
-
module Rails41
|
39
|
+
module Rails41 # :nodoc:
|
36
40
|
def self.convert(klass, coder)
|
37
41
|
attributes = klass.attributes_builder
|
38
42
|
.build_from_database(coder["attributes"])
|