activerecord 5.0.6 → 6.0.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 +5 -5
- data/CHANGELOG.md +638 -2023
- data/MIT-LICENSE +3 -1
- data/README.rdoc +8 -6
- data/examples/performance.rb +31 -29
- data/examples/simple.rb +5 -3
- data/lib/active_record/aggregations.rb +249 -246
- data/lib/active_record/association_relation.rb +24 -13
- data/lib/active_record/associations/alias_tracker.rb +24 -33
- data/lib/active_record/associations/association.rb +119 -56
- data/lib/active_record/associations/association_scope.rb +94 -94
- data/lib/active_record/associations/belongs_to_association.rb +58 -42
- data/lib/active_record/associations/belongs_to_polymorphic_association.rb +8 -12
- data/lib/active_record/associations/builder/association.rb +18 -25
- data/lib/active_record/associations/builder/belongs_to.rb +43 -54
- data/lib/active_record/associations/builder/collection_association.rb +7 -18
- data/lib/active_record/associations/builder/has_and_belongs_to_many.rb +42 -61
- data/lib/active_record/associations/builder/has_many.rb +4 -0
- data/lib/active_record/associations/builder/has_one.rb +37 -1
- data/lib/active_record/associations/builder/singular_association.rb +4 -0
- data/lib/active_record/associations/collection_association.rb +80 -252
- data/lib/active_record/associations/collection_proxy.rb +158 -121
- data/lib/active_record/associations/foreign_association.rb +9 -0
- data/lib/active_record/associations/has_many_association.rb +23 -29
- data/lib/active_record/associations/has_many_through_association.rb +58 -44
- data/lib/active_record/associations/has_one_association.rb +59 -54
- data/lib/active_record/associations/has_one_through_association.rb +20 -11
- data/lib/active_record/associations/join_dependency/join_association.rb +38 -90
- data/lib/active_record/associations/join_dependency/join_base.rb +10 -9
- data/lib/active_record/associations/join_dependency/join_part.rb +12 -12
- data/lib/active_record/associations/join_dependency.rb +134 -176
- data/lib/active_record/associations/preloader/association.rb +84 -125
- data/lib/active_record/associations/preloader/through_association.rb +82 -75
- data/lib/active_record/associations/preloader.rb +90 -102
- data/lib/active_record/associations/singular_association.rb +12 -45
- data/lib/active_record/associations/through_association.rb +26 -14
- data/lib/active_record/associations.rb +1603 -1592
- data/lib/active_record/attribute_assignment.rb +54 -60
- data/lib/active_record/attribute_decorators.rb +38 -15
- data/lib/active_record/attribute_methods/before_type_cast.rb +12 -7
- data/lib/active_record/attribute_methods/dirty.rb +179 -109
- data/lib/active_record/attribute_methods/primary_key.rb +86 -91
- data/lib/active_record/attribute_methods/query.rb +4 -3
- data/lib/active_record/attribute_methods/read.rb +21 -49
- data/lib/active_record/attribute_methods/serialization.rb +30 -7
- data/lib/active_record/attribute_methods/time_zone_conversion.rb +39 -64
- data/lib/active_record/attribute_methods/write.rb +35 -33
- data/lib/active_record/attribute_methods.rb +66 -106
- data/lib/active_record/attributes.rb +38 -24
- data/lib/active_record/autosave_association.rb +53 -32
- data/lib/active_record/base.rb +27 -24
- data/lib/active_record/callbacks.rb +63 -33
- data/lib/active_record/coders/json.rb +2 -0
- data/lib/active_record/coders/yaml_column.rb +11 -11
- data/lib/active_record/connection_adapters/abstract/connection_pool.rb +553 -321
- data/lib/active_record/connection_adapters/abstract/database_limits.rb +23 -5
- data/lib/active_record/connection_adapters/abstract/database_statements.rb +213 -94
- data/lib/active_record/connection_adapters/abstract/query_cache.rb +59 -28
- data/lib/active_record/connection_adapters/abstract/quoting.rb +119 -75
- data/lib/active_record/connection_adapters/abstract/savepoints.rb +2 -0
- data/lib/active_record/connection_adapters/abstract/schema_creation.rb +33 -27
- data/lib/active_record/connection_adapters/abstract/schema_definitions.rb +207 -126
- data/lib/active_record/connection_adapters/abstract/schema_dumper.rb +68 -80
- data/lib/active_record/connection_adapters/abstract/schema_statements.rb +369 -199
- data/lib/active_record/connection_adapters/abstract/transaction.rb +169 -78
- data/lib/active_record/connection_adapters/abstract_adapter.rb +363 -202
- data/lib/active_record/connection_adapters/abstract_mysql_adapter.rb +405 -551
- data/lib/active_record/connection_adapters/column.rb +41 -13
- data/lib/active_record/connection_adapters/connection_specification.rb +172 -138
- data/lib/active_record/connection_adapters/determine_if_preparable_visitor.rb +11 -4
- data/lib/active_record/connection_adapters/mysql/column.rb +8 -31
- data/lib/active_record/connection_adapters/mysql/database_statements.rb +143 -49
- data/lib/active_record/connection_adapters/mysql/explain_pretty_printer.rb +24 -22
- data/lib/active_record/connection_adapters/mysql/quoting.rb +50 -20
- data/lib/active_record/connection_adapters/mysql/schema_creation.rb +50 -45
- data/lib/active_record/connection_adapters/mysql/schema_definitions.rb +58 -56
- data/lib/active_record/connection_adapters/mysql/schema_dumper.rb +70 -36
- data/lib/active_record/connection_adapters/mysql/schema_statements.rb +264 -0
- data/lib/active_record/connection_adapters/mysql/type_metadata.rb +12 -13
- data/lib/active_record/connection_adapters/mysql2_adapter.rb +49 -30
- data/lib/active_record/connection_adapters/postgresql/column.rb +22 -7
- data/lib/active_record/connection_adapters/postgresql/database_statements.rb +60 -54
- data/lib/active_record/connection_adapters/postgresql/explain_pretty_printer.rb +5 -3
- data/lib/active_record/connection_adapters/postgresql/oid/array.rb +22 -10
- data/lib/active_record/connection_adapters/postgresql/oid/bit.rb +6 -5
- 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 +3 -1
- data/lib/active_record/connection_adapters/postgresql/oid/date.rb +23 -0
- data/lib/active_record/connection_adapters/postgresql/oid/date_time.rb +4 -2
- data/lib/active_record/connection_adapters/postgresql/oid/decimal.rb +3 -1
- data/lib/active_record/connection_adapters/postgresql/oid/enum.rb +5 -3
- data/lib/active_record/connection_adapters/postgresql/oid/hstore.rb +19 -17
- data/lib/active_record/connection_adapters/postgresql/oid/inet.rb +2 -0
- data/lib/active_record/connection_adapters/postgresql/oid/jsonb.rb +3 -11
- data/lib/active_record/connection_adapters/postgresql/oid/legacy_point.rb +45 -0
- data/lib/active_record/connection_adapters/postgresql/oid/money.rb +7 -5
- data/lib/active_record/connection_adapters/postgresql/oid/{json.rb → oid.rb} +6 -1
- data/lib/active_record/connection_adapters/postgresql/oid/point.rb +31 -9
- data/lib/active_record/connection_adapters/postgresql/oid/range.rb +34 -30
- data/lib/active_record/connection_adapters/postgresql/oid/specialized_string.rb +4 -1
- data/lib/active_record/connection_adapters/postgresql/oid/type_map_initializer.rb +58 -54
- data/lib/active_record/connection_adapters/postgresql/oid/uuid.rb +9 -4
- 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 +24 -21
- data/lib/active_record/connection_adapters/postgresql/quoting.rb +95 -35
- data/lib/active_record/connection_adapters/postgresql/referential_integrity.rb +19 -25
- data/lib/active_record/connection_adapters/postgresql/schema_creation.rb +76 -0
- data/lib/active_record/connection_adapters/postgresql/schema_definitions.rb +147 -105
- data/lib/active_record/connection_adapters/postgresql/schema_dumper.rb +35 -32
- data/lib/active_record/connection_adapters/postgresql/schema_statements.rb +380 -300
- data/lib/active_record/connection_adapters/postgresql/type_metadata.rb +26 -25
- data/lib/active_record/connection_adapters/postgresql/utils.rb +10 -6
- data/lib/active_record/connection_adapters/postgresql_adapter.rb +382 -275
- data/lib/active_record/connection_adapters/schema_cache.rb +46 -12
- data/lib/active_record/connection_adapters/sql_type_metadata.rb +13 -8
- data/lib/active_record/connection_adapters/sqlite3/database_statements.rb +120 -0
- data/lib/active_record/connection_adapters/sqlite3/explain_pretty_printer.rb +3 -1
- data/lib/active_record/connection_adapters/sqlite3/quoting.rb +74 -19
- data/lib/active_record/connection_adapters/sqlite3/schema_creation.rb +3 -8
- data/lib/active_record/connection_adapters/sqlite3/schema_definitions.rb +19 -0
- data/lib/active_record/connection_adapters/sqlite3/schema_dumper.rb +18 -0
- data/lib/active_record/connection_adapters/sqlite3/schema_statements.rb +137 -0
- data/lib/active_record/connection_adapters/sqlite3_adapter.rb +254 -262
- data/lib/active_record/connection_adapters/statement_pool.rb +9 -7
- data/lib/active_record/connection_handling.rb +159 -40
- data/lib/active_record/core.rb +202 -162
- data/lib/active_record/counter_cache.rb +57 -28
- data/lib/active_record/database_configurations/database_config.rb +37 -0
- data/lib/active_record/database_configurations/hash_config.rb +50 -0
- data/lib/active_record/database_configurations/url_config.rb +79 -0
- data/lib/active_record/database_configurations.rb +233 -0
- data/lib/active_record/define_callbacks.rb +22 -0
- data/lib/active_record/dynamic_matchers.rb +87 -86
- data/lib/active_record/enum.rb +60 -23
- data/lib/active_record/errors.rb +114 -18
- data/lib/active_record/explain.rb +4 -3
- data/lib/active_record/explain_registry.rb +3 -1
- data/lib/active_record/explain_subscriber.rb +9 -4
- data/lib/active_record/fixture_set/file.rb +13 -8
- data/lib/active_record/fixture_set/model_metadata.rb +33 -0
- data/lib/active_record/fixture_set/render_context.rb +17 -0
- data/lib/active_record/fixture_set/table_row.rb +153 -0
- data/lib/active_record/fixture_set/table_rows.rb +47 -0
- data/lib/active_record/fixtures.rb +195 -502
- data/lib/active_record/gem_version.rb +4 -2
- data/lib/active_record/inheritance.rb +151 -97
- data/lib/active_record/insert_all.rb +179 -0
- data/lib/active_record/integration.rb +116 -25
- data/lib/active_record/internal_metadata.rb +15 -18
- data/lib/active_record/legacy_yaml_adapter.rb +4 -2
- data/lib/active_record/locking/optimistic.rb +78 -87
- data/lib/active_record/locking/pessimistic.rb +18 -6
- data/lib/active_record/log_subscriber.rb +48 -29
- data/lib/active_record/middleware/database_selector/resolver/session.rb +45 -0
- data/lib/active_record/middleware/database_selector/resolver.rb +88 -0
- data/lib/active_record/middleware/database_selector.rb +75 -0
- data/lib/active_record/migration/command_recorder.rb +143 -97
- data/lib/active_record/migration/compatibility.rb +174 -56
- data/lib/active_record/migration/join_table.rb +8 -6
- data/lib/active_record/migration.rb +367 -300
- data/lib/active_record/model_schema.rb +145 -139
- data/lib/active_record/nested_attributes.rb +214 -201
- data/lib/active_record/no_touching.rb +10 -1
- data/lib/active_record/null_relation.rb +13 -34
- data/lib/active_record/persistence.rb +442 -72
- data/lib/active_record/query_cache.rb +15 -14
- data/lib/active_record/querying.rb +36 -23
- data/lib/active_record/railtie.rb +128 -36
- data/lib/active_record/railties/collection_cache_association_loading.rb +34 -0
- data/lib/active_record/railties/console_sandbox.rb +2 -0
- data/lib/active_record/railties/controller_runtime.rb +34 -33
- data/lib/active_record/railties/databases.rake +309 -177
- data/lib/active_record/readonly_attributes.rb +5 -4
- data/lib/active_record/reflection.rb +211 -249
- data/lib/active_record/relation/batches/batch_enumerator.rb +3 -1
- data/lib/active_record/relation/batches.rb +99 -52
- data/lib/active_record/relation/calculations.rb +211 -172
- data/lib/active_record/relation/delegation.rb +67 -65
- data/lib/active_record/relation/finder_methods.rb +208 -247
- data/lib/active_record/relation/from_clause.rb +2 -8
- data/lib/active_record/relation/merger.rb +78 -61
- data/lib/active_record/relation/predicate_builder/array_handler.rb +20 -14
- data/lib/active_record/relation/predicate_builder/association_query_value.rb +43 -0
- data/lib/active_record/relation/predicate_builder/base_handler.rb +4 -3
- data/lib/active_record/relation/predicate_builder/basic_object_handler.rb +6 -4
- data/lib/active_record/relation/predicate_builder/polymorphic_array_value.rb +53 -0
- data/lib/active_record/relation/predicate_builder/range_handler.rb +7 -18
- data/lib/active_record/relation/predicate_builder/relation_handler.rb +6 -0
- data/lib/active_record/relation/predicate_builder.rb +86 -104
- data/lib/active_record/relation/query_attribute.rb +33 -2
- data/lib/active_record/relation/query_methods.rb +458 -329
- data/lib/active_record/relation/record_fetch_warning.rb +5 -3
- data/lib/active_record/relation/spawn_methods.rb +8 -7
- data/lib/active_record/relation/where_clause.rb +111 -95
- data/lib/active_record/relation/where_clause_factory.rb +6 -11
- data/lib/active_record/relation.rb +429 -318
- data/lib/active_record/result.rb +69 -39
- data/lib/active_record/runtime_registry.rb +5 -3
- data/lib/active_record/sanitization.rb +83 -99
- data/lib/active_record/schema.rb +7 -14
- data/lib/active_record/schema_dumper.rb +71 -69
- data/lib/active_record/schema_migration.rb +15 -5
- data/lib/active_record/scoping/default.rb +93 -95
- data/lib/active_record/scoping/named.rb +45 -25
- data/lib/active_record/scoping.rb +20 -19
- data/lib/active_record/secure_token.rb +4 -2
- data/lib/active_record/serialization.rb +2 -0
- data/lib/active_record/statement_cache.rb +63 -28
- data/lib/active_record/store.rb +121 -41
- data/lib/active_record/suppressor.rb +4 -1
- data/lib/active_record/table_metadata.rb +26 -20
- data/lib/active_record/tasks/database_tasks.rb +276 -85
- data/lib/active_record/tasks/mysql_database_tasks.rb +54 -90
- data/lib/active_record/tasks/postgresql_database_tasks.rb +78 -47
- data/lib/active_record/tasks/sqlite_database_tasks.rb +34 -16
- data/lib/active_record/test_databases.rb +23 -0
- data/lib/active_record/test_fixtures.rb +224 -0
- data/lib/active_record/timestamp.rb +70 -35
- data/lib/active_record/touch_later.rb +7 -4
- data/lib/active_record/transactions.rb +133 -149
- data/lib/active_record/translation.rb +3 -1
- data/lib/active_record/type/adapter_specific_registry.rb +44 -45
- 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 +15 -0
- data/lib/active_record/type/hash_lookup_type_map.rb +5 -3
- 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 +16 -8
- data/lib/active_record/type/text.rb +11 -0
- data/lib/active_record/type/time.rb +2 -1
- data/lib/active_record/type/type_map.rb +13 -15
- data/lib/active_record/type/unsigned_integer.rb +17 -0
- data/lib/active_record/type.rb +23 -17
- data/lib/active_record/type_caster/connection.rb +17 -12
- data/lib/active_record/type_caster/map.rb +5 -4
- data/lib/active_record/type_caster.rb +4 -2
- data/lib/active_record/validations/absence.rb +2 -0
- data/lib/active_record/validations/associated.rb +3 -1
- data/lib/active_record/validations/length.rb +2 -0
- data/lib/active_record/validations/presence.rb +4 -2
- data/lib/active_record/validations/uniqueness.rb +29 -42
- data/lib/active_record/validations.rb +7 -4
- data/lib/active_record/version.rb +3 -1
- data/lib/active_record.rb +36 -22
- data/lib/arel/alias_predication.rb +9 -0
- data/lib/arel/attributes/attribute.rb +37 -0
- data/lib/arel/attributes.rb +22 -0
- data/lib/arel/collectors/bind.rb +24 -0
- data/lib/arel/collectors/composite.rb +31 -0
- data/lib/arel/collectors/plain_string.rb +20 -0
- data/lib/arel/collectors/sql_string.rb +20 -0
- data/lib/arel/collectors/substitute_binds.rb +28 -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 +52 -0
- data/lib/arel/nodes/bind_param.rb +36 -0
- data/lib/arel/nodes/case.rb +55 -0
- data/lib/arel/nodes/casted.rb +50 -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 +18 -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 +8 -0
- data/lib/arel/nodes/in.rb +8 -0
- data/lib/arel/nodes/infix_operation.rb +80 -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 +50 -0
- data/lib/arel/nodes/node_expression.rb +13 -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 +16 -0
- data/lib/arel/nodes/string_join.rb +11 -0
- data/lib/arel/nodes/table_alias.rb +27 -0
- data/lib/arel/nodes/terminal.rb +16 -0
- data/lib/arel/nodes/true.rb +16 -0
- data/lib/arel/nodes/unary.rb +45 -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 +68 -0
- data/lib/arel/order_predications.rb +13 -0
- data/lib/arel/predications.rb +257 -0
- data/lib/arel/select_manager.rb +271 -0
- data/lib/arel/table.rb +110 -0
- data/lib/arel/tree_manager.rb +72 -0
- data/lib/arel/update_manager.rb +34 -0
- data/lib/arel/visitors/depth_first.rb +204 -0
- data/lib/arel/visitors/dot.rb +297 -0
- data/lib/arel/visitors/ibm_db.rb +34 -0
- data/lib/arel/visitors/informix.rb +62 -0
- data/lib/arel/visitors/mssql.rb +157 -0
- data/lib/arel/visitors/mysql.rb +83 -0
- data/lib/arel/visitors/oracle.rb +159 -0
- data/lib/arel/visitors/oracle12.rb +66 -0
- data/lib/arel/visitors/postgresql.rb +110 -0
- data/lib/arel/visitors/sqlite.rb +39 -0
- data/lib/arel/visitors/to_sql.rb +889 -0
- data/lib/arel/visitors/visitor.rb +46 -0
- data/lib/arel/visitors/where_sql.rb +23 -0
- data/lib/arel/visitors.rb +20 -0
- data/lib/arel/window_predications.rb +9 -0
- data/lib/arel.rb +58 -0
- 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 +37 -35
- data/lib/rails/generators/active_record/migration/templates/{create_table_migration.rb → create_table_migration.rb.tt} +1 -1
- data/lib/rails/generators/active_record/migration/templates/{migration.rb → migration.rb.tt} +4 -2
- data/lib/rails/generators/active_record/migration.rb +17 -2
- data/lib/rails/generators/active_record/model/model_generator.rb +9 -29
- data/lib/rails/generators/active_record/model/templates/{model.rb → model.rb.tt} +10 -1
- data/lib/rails/generators/active_record/model/templates/{module.rb → module.rb.tt} +0 -0
- data/lib/rails/generators/active_record.rb +7 -5
- metadata +133 -50
- data/lib/active_record/associations/preloader/belongs_to.rb +0 -17
- data/lib/active_record/associations/preloader/collection_association.rb +0 -17
- data/lib/active_record/associations/preloader/has_many.rb +0 -17
- 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 -20
- data/lib/active_record/attribute/user_provided_default.rb +0 -28
- data/lib/active_record/attribute.rb +0 -213
- data/lib/active_record/attribute_mutation_tracker.rb +0 -70
- data/lib/active_record/attribute_set/builder.rb +0 -130
- data/lib/active_record/attribute_set.rb +0 -110
- data/lib/active_record/collection_cache_key.rb +0 -50
- data/lib/active_record/connection_adapters/postgresql/oid/rails_5_1_point.rb +0 -50
- 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/class_handler.rb +0 -27
- data/lib/active_record/relation/predicate_builder/polymorphic_array_handler.rb +0 -57
- data/lib/active_record/type/internal/abstract_json.rb +0 -33
@@ -1,16 +1,17 @@
|
|
1
|
-
|
1
|
+
# frozen_string_literal: true
|
2
2
|
|
3
3
|
module ActiveRecord
|
4
4
|
# = Active Record \Relation
|
5
5
|
class Relation
|
6
6
|
MULTI_VALUE_METHODS = [:includes, :eager_load, :preload, :select, :group,
|
7
|
-
:order, :joins, :
|
8
|
-
:extending, :unscope]
|
7
|
+
:order, :joins, :left_outer_joins, :references,
|
8
|
+
:extending, :unscope, :optimizer_hints, :annotate]
|
9
9
|
|
10
10
|
SINGLE_VALUE_METHODS = [:limit, :offset, :lock, :readonly, :reordering,
|
11
|
-
:reverse_order, :distinct, :create_with]
|
11
|
+
:reverse_order, :distinct, :create_with, :skip_query_cache]
|
12
|
+
|
12
13
|
CLAUSE_METHODS = [:where, :having, :from]
|
13
|
-
INVALID_METHODS_FOR_DELETE_ALL = [:
|
14
|
+
INVALID_METHODS_FOR_DELETE_ALL = [:distinct, :group, :having]
|
14
15
|
|
15
16
|
VALUE_METHODS = MULTI_VALUE_METHODS + SINGLE_VALUE_METHODS + CLAUSE_METHODS
|
16
17
|
|
@@ -18,95 +19,39 @@ module ActiveRecord
|
|
18
19
|
include FinderMethods, Calculations, SpawnMethods, QueryMethods, Batches, Explain, Delegation
|
19
20
|
|
20
21
|
attr_reader :table, :klass, :loaded, :predicate_builder
|
22
|
+
attr_accessor :skip_preloading_value
|
21
23
|
alias :model :klass
|
22
24
|
alias :loaded? :loaded
|
25
|
+
alias :locked? :lock_value
|
23
26
|
|
24
|
-
def initialize(klass, table, predicate_builder, values
|
27
|
+
def initialize(klass, table: klass.arel_table, predicate_builder: klass.predicate_builder, values: {})
|
25
28
|
@klass = klass
|
26
29
|
@table = table
|
27
30
|
@values = values
|
28
31
|
@offsets = {}
|
29
32
|
@loaded = false
|
30
33
|
@predicate_builder = predicate_builder
|
34
|
+
@delegate_to_klass = false
|
31
35
|
end
|
32
36
|
|
33
37
|
def initialize_copy(other)
|
34
|
-
|
35
|
-
# https://bugs.ruby-lang.org/issues/7166
|
36
|
-
@values = Hash[@values]
|
38
|
+
@values = @values.dup
|
37
39
|
reset
|
38
40
|
end
|
39
41
|
|
40
|
-
def
|
41
|
-
|
42
|
-
|
43
|
-
if primary_key && Hash === values
|
44
|
-
primary_key_value = values[values.keys.find { |k|
|
45
|
-
k.name == primary_key
|
46
|
-
}]
|
47
|
-
|
48
|
-
if !primary_key_value && klass.prefetch_primary_key?
|
49
|
-
primary_key_value = klass.next_sequence_value
|
50
|
-
values[arel_attribute(klass.primary_key)] = primary_key_value
|
51
|
-
end
|
52
|
-
end
|
53
|
-
|
54
|
-
im = arel.create_insert
|
55
|
-
im.into @table
|
56
|
-
|
57
|
-
substitutes, binds = substitute_values values
|
58
|
-
|
59
|
-
if values.empty? # empty insert
|
60
|
-
im.values = Arel.sql(connection.empty_insert_statement_value)
|
61
|
-
else
|
62
|
-
im.insert substitutes
|
63
|
-
end
|
64
|
-
|
65
|
-
@klass.connection.insert(
|
66
|
-
im,
|
67
|
-
'SQL',
|
68
|
-
primary_key,
|
69
|
-
primary_key_value,
|
70
|
-
nil,
|
71
|
-
binds)
|
72
|
-
end
|
73
|
-
|
74
|
-
def _update_record(values, id, id_was) # :nodoc:
|
75
|
-
substitutes, binds = substitute_values values
|
76
|
-
|
77
|
-
scope = @klass.unscoped
|
78
|
-
|
79
|
-
if @klass.finder_needs_type_condition?
|
80
|
-
scope.unscope!(where: @klass.inheritance_column)
|
81
|
-
end
|
82
|
-
|
83
|
-
relation = scope.where(@klass.primary_key => (id_was || id))
|
84
|
-
bvs = binds + relation.bound_attributes
|
85
|
-
um = relation
|
86
|
-
.arel
|
87
|
-
.compile_update(substitutes, @klass.primary_key)
|
88
|
-
|
89
|
-
@klass.connection.update(
|
90
|
-
um,
|
91
|
-
'SQL',
|
92
|
-
bvs,
|
93
|
-
)
|
42
|
+
def arel_attribute(name) # :nodoc:
|
43
|
+
klass.arel_attribute(name, table)
|
94
44
|
end
|
95
45
|
|
96
|
-
def
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
values.each do |arel_attr, value|
|
101
|
-
binds.push QueryAttribute.new(arel_attr.name, value, klass.type_for_attribute(arel_attr.name))
|
102
|
-
substitutes.push [arel_attr, Arel::Nodes::BindParam.new]
|
46
|
+
def bind_attribute(name, value) # :nodoc:
|
47
|
+
if reflection = klass._reflect_on_association(name)
|
48
|
+
name = reflection.foreign_key
|
49
|
+
value = value.read_attribute(reflection.klass.primary_key) unless value.nil?
|
103
50
|
end
|
104
51
|
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
def arel_attribute(name) # :nodoc:
|
109
|
-
klass.arel_attribute(name, table)
|
52
|
+
attr = arel_attribute(name)
|
53
|
+
bind = predicate_builder.build_bind_attribute(attr.name, value)
|
54
|
+
yield attr, bind
|
110
55
|
end
|
111
56
|
|
112
57
|
# Initializes new record from relation while maintaining the current
|
@@ -121,8 +66,9 @@ module ActiveRecord
|
|
121
66
|
#
|
122
67
|
# user = users.new { |user| user.name = 'Oscar' }
|
123
68
|
# user.name # => Oscar
|
124
|
-
def new(
|
125
|
-
|
69
|
+
def new(attributes = nil, &block)
|
70
|
+
block = _deprecated_scope_block("new", &block)
|
71
|
+
scoping { klass.new(attributes, &block) }
|
126
72
|
end
|
127
73
|
|
128
74
|
alias build new
|
@@ -146,8 +92,13 @@ module ActiveRecord
|
|
146
92
|
#
|
147
93
|
# users.create(name: nil) # validation on name
|
148
94
|
# # => #<User id: nil, name: nil, ...>
|
149
|
-
def create(
|
150
|
-
|
95
|
+
def create(attributes = nil, &block)
|
96
|
+
if attributes.is_a?(Array)
|
97
|
+
attributes.collect { |attr| create(attr, &block) }
|
98
|
+
else
|
99
|
+
block = _deprecated_scope_block("create", &block)
|
100
|
+
scoping { klass.create(attributes, &block) }
|
101
|
+
end
|
151
102
|
end
|
152
103
|
|
153
104
|
# Similar to #create, but calls
|
@@ -156,8 +107,13 @@ module ActiveRecord
|
|
156
107
|
#
|
157
108
|
# Expects arguments in the same format as
|
158
109
|
# {ActiveRecord::Base.create!}[rdoc-ref:Persistence::ClassMethods#create!].
|
159
|
-
def create!(
|
160
|
-
|
110
|
+
def create!(attributes = nil, &block)
|
111
|
+
if attributes.is_a?(Array)
|
112
|
+
attributes.collect { |attr| create!(attr, &block) }
|
113
|
+
else
|
114
|
+
block = _deprecated_scope_block("create!", &block)
|
115
|
+
scoping { klass.create!(attributes, &block) }
|
116
|
+
end
|
161
117
|
end
|
162
118
|
|
163
119
|
def first_or_create(attributes = nil, &block) # :nodoc:
|
@@ -203,23 +159,12 @@ module ActiveRecord
|
|
203
159
|
# failed due to validation errors it won't be persisted, you get what
|
204
160
|
# #create returns in such situation.
|
205
161
|
#
|
206
|
-
# Please note
|
162
|
+
# Please note <b>this method is not atomic</b>, it runs first a SELECT, and if
|
207
163
|
# there are no results an INSERT is attempted. If there are other threads
|
208
164
|
# or processes there is a race condition between both calls and it could
|
209
165
|
# be the case that you end up with two similar records.
|
210
166
|
#
|
211
|
-
#
|
212
|
-
# application, but in the particular case in which rows have a UNIQUE
|
213
|
-
# constraint an exception may be raised, just retry:
|
214
|
-
#
|
215
|
-
# begin
|
216
|
-
# CreditAccount.transaction(requires_new: true) do
|
217
|
-
# CreditAccount.find_or_create_by(user_id: user.id)
|
218
|
-
# end
|
219
|
-
# rescue ActiveRecord::RecordNotUnique
|
220
|
-
# retry
|
221
|
-
# end
|
222
|
-
#
|
167
|
+
# If this might be a problem for your application, please see #create_or_find_by.
|
223
168
|
def find_or_create_by(attributes, &block)
|
224
169
|
find_by(attributes) || create(attributes, &block)
|
225
170
|
end
|
@@ -231,6 +176,51 @@ module ActiveRecord
|
|
231
176
|
find_by(attributes) || create!(attributes, &block)
|
232
177
|
end
|
233
178
|
|
179
|
+
# Attempts to create a record with the given attributes in a table that has a unique constraint
|
180
|
+
# on one or several of its columns. If a row already exists with one or several of these
|
181
|
+
# unique constraints, the exception such an insertion would normally raise is caught,
|
182
|
+
# and the existing record with those attributes is found using #find_by!.
|
183
|
+
#
|
184
|
+
# This is similar to #find_or_create_by, but avoids the problem of stale reads between the SELECT
|
185
|
+
# and the INSERT, as that method needs to first query the table, then attempt to insert a row
|
186
|
+
# if none is found.
|
187
|
+
#
|
188
|
+
# There are several drawbacks to #create_or_find_by, though:
|
189
|
+
#
|
190
|
+
# * The underlying table must have the relevant columns defined with unique constraints.
|
191
|
+
# * A unique constraint violation may be triggered by only one, or at least less than all,
|
192
|
+
# of the given attributes. This means that the subsequent #find_by! may fail to find a
|
193
|
+
# matching record, which will then raise an <tt>ActiveRecord::RecordNotFound</tt> exception,
|
194
|
+
# rather than a record with the given attributes.
|
195
|
+
# * While we avoid the race condition between SELECT -> INSERT from #find_or_create_by,
|
196
|
+
# we actually have another race condition between INSERT -> SELECT, which can be triggered
|
197
|
+
# if a DELETE between those two statements is run by another client. But for most applications,
|
198
|
+
# that's a significantly less likely condition to hit.
|
199
|
+
# * It relies on exception handling to handle control flow, which may be marginally slower.
|
200
|
+
# * The primary key may auto-increment on each create, even if it fails. This can accelerate
|
201
|
+
# the problem of running out of integers, if the underlying table is still stuck on a primary
|
202
|
+
# key of type int (note: All Rails apps since 5.1+ have defaulted to bigint, which is not liable
|
203
|
+
# to this problem).
|
204
|
+
#
|
205
|
+
# This method will return a record if all given attributes are covered by unique constraints
|
206
|
+
# (unless the INSERT -> DELETE -> SELECT race condition is triggered), but if creation was attempted
|
207
|
+
# and failed due to validation errors it won't be persisted, you get what #create returns in
|
208
|
+
# such situation.
|
209
|
+
def create_or_find_by(attributes, &block)
|
210
|
+
transaction(requires_new: true) { create(attributes, &block) }
|
211
|
+
rescue ActiveRecord::RecordNotUnique
|
212
|
+
find_by!(attributes)
|
213
|
+
end
|
214
|
+
|
215
|
+
# Like #create_or_find_by, but calls
|
216
|
+
# {create!}[rdoc-ref:Persistence::ClassMethods#create!] so an exception
|
217
|
+
# is raised if the created record is invalid.
|
218
|
+
def create_or_find_by!(attributes, &block)
|
219
|
+
transaction(requires_new: true) { create!(attributes, &block) }
|
220
|
+
rescue ActiveRecord::RecordNotUnique
|
221
|
+
find_by!(attributes)
|
222
|
+
end
|
223
|
+
|
234
224
|
# Like #find_or_create_by, but calls {new}[rdoc-ref:Core#new]
|
235
225
|
# instead of {create}[rdoc-ref:Persistence::ClassMethods#create].
|
236
226
|
def find_or_initialize_by(attributes, &block)
|
@@ -245,16 +235,16 @@ module ActiveRecord
|
|
245
235
|
# are needed by the next ones when eager loading is going on.
|
246
236
|
#
|
247
237
|
# Please see further details in the
|
248
|
-
# {Active Record Query Interface guide}[
|
238
|
+
# {Active Record Query Interface guide}[https://guides.rubyonrails.org/active_record_querying.html#running-explain].
|
249
239
|
def explain
|
250
|
-
#TODO: Fix for binds.
|
251
240
|
exec_explain(collecting_queries_for_explain { exec_queries })
|
252
241
|
end
|
253
242
|
|
254
243
|
# Converts relation objects to Array.
|
255
|
-
def
|
244
|
+
def to_ary
|
256
245
|
records.dup
|
257
246
|
end
|
247
|
+
alias to_a to_ary
|
258
248
|
|
259
249
|
def records # :nodoc:
|
260
250
|
load
|
@@ -266,10 +256,6 @@ module ActiveRecord
|
|
266
256
|
coder.represent_seq(nil, records)
|
267
257
|
end
|
268
258
|
|
269
|
-
def as_json(options = nil) #:nodoc:
|
270
|
-
records.as_json(options)
|
271
|
-
end
|
272
|
-
|
273
259
|
# Returns size of the records.
|
274
260
|
def size
|
275
261
|
loaded? ? @records.length : count(:all)
|
@@ -278,13 +264,7 @@ module ActiveRecord
|
|
278
264
|
# Returns true if there are no records.
|
279
265
|
def empty?
|
280
266
|
return @records.empty? if loaded?
|
281
|
-
|
282
|
-
if limit_value == 0
|
283
|
-
true
|
284
|
-
else
|
285
|
-
c = count(:all)
|
286
|
-
c.respond_to?(:zero?) ? c.zero? : c.empty?
|
287
|
-
end
|
267
|
+
!exists?
|
288
268
|
end
|
289
269
|
|
290
270
|
# Returns true if there are no records.
|
@@ -311,32 +291,100 @@ module ActiveRecord
|
|
311
291
|
limit_value ? records.many? : size > 1
|
312
292
|
end
|
313
293
|
|
314
|
-
# Returns a cache key that can be used to identify
|
315
|
-
#
|
316
|
-
# the number of records matched by the query and a timestamp of the last
|
317
|
-
# updated record. When a new record comes to match the query, or any of
|
318
|
-
# the existing records is updated or deleted, the cache key changes.
|
294
|
+
# Returns a stable cache key that can be used to identify this query.
|
295
|
+
# The cache key is built with a fingerprint of the SQL query.
|
319
296
|
#
|
320
|
-
#
|
321
|
-
#
|
297
|
+
# Product.where("name like ?", "%Cosmic Encounter%").cache_key
|
298
|
+
# # => "products/query-1850ab3d302391b85b8693e941286659"
|
322
299
|
#
|
323
|
-
# If
|
324
|
-
#
|
300
|
+
# If ActiveRecord::Base.collection_cache_versioning is turned off, as it was
|
301
|
+
# in Rails 6.0 and earlier, the cache key will also include a version.
|
325
302
|
#
|
326
|
-
#
|
303
|
+
# ActiveRecord::Base.collection_cache_versioning = false
|
304
|
+
# Product.where("name like ?", "%Cosmic Encounter%").cache_key
|
305
|
+
# # => "products/query-1850ab3d302391b85b8693e941286659-1-20150714212553907087000"
|
327
306
|
#
|
328
307
|
# You can also pass a custom timestamp column to fetch the timestamp of the
|
329
308
|
# last updated record.
|
330
309
|
#
|
331
310
|
# Product.where("name like ?", "%Game%").cache_key(:last_reviewed_at)
|
332
|
-
#
|
333
|
-
# You can customize the strategy to generate the key on a per model basis
|
334
|
-
# overriding ActiveRecord::Base#collection_cache_key.
|
335
311
|
def cache_key(timestamp_column = :updated_at)
|
336
312
|
@cache_keys ||= {}
|
337
|
-
@cache_keys[timestamp_column] ||=
|
313
|
+
@cache_keys[timestamp_column] ||= klass.collection_cache_key(self, timestamp_column)
|
338
314
|
end
|
339
315
|
|
316
|
+
def compute_cache_key(timestamp_column = :updated_at) # :nodoc:
|
317
|
+
query_signature = ActiveSupport::Digest.hexdigest(to_sql)
|
318
|
+
key = "#{klass.model_name.cache_key}/query-#{query_signature}"
|
319
|
+
|
320
|
+
if cache_version(timestamp_column)
|
321
|
+
key
|
322
|
+
else
|
323
|
+
"#{key}-#{compute_cache_version(timestamp_column)}"
|
324
|
+
end
|
325
|
+
end
|
326
|
+
private :compute_cache_key
|
327
|
+
|
328
|
+
# Returns a cache version that can be used together with the cache key to form
|
329
|
+
# a recyclable caching scheme. The cache version is built with the number of records
|
330
|
+
# matching the query, and the timestamp of the last updated record. When a new record
|
331
|
+
# comes to match the query, or any of the existing records is updated or deleted,
|
332
|
+
# the cache version changes.
|
333
|
+
#
|
334
|
+
# If the collection is loaded, the method will iterate through the records
|
335
|
+
# to generate the timestamp, otherwise it will trigger one SQL query like:
|
336
|
+
#
|
337
|
+
# SELECT COUNT(*), MAX("products"."updated_at") FROM "products" WHERE (name like '%Cosmic Encounter%')
|
338
|
+
def cache_version(timestamp_column = :updated_at)
|
339
|
+
if collection_cache_versioning
|
340
|
+
@cache_versions ||= {}
|
341
|
+
@cache_versions[timestamp_column] ||= compute_cache_version(timestamp_column)
|
342
|
+
end
|
343
|
+
end
|
344
|
+
|
345
|
+
def compute_cache_version(timestamp_column) # :nodoc:
|
346
|
+
if loaded? || distinct_value
|
347
|
+
size = records.size
|
348
|
+
if size > 0
|
349
|
+
timestamp = max_by(×tamp_column)._read_attribute(timestamp_column)
|
350
|
+
end
|
351
|
+
else
|
352
|
+
collection = eager_loading? ? apply_join_dependency : self
|
353
|
+
|
354
|
+
column = connection.visitor.compile(arel_attribute(timestamp_column))
|
355
|
+
select_values = "COUNT(*) AS #{connection.quote_column_name("size")}, MAX(%s) AS timestamp"
|
356
|
+
|
357
|
+
if collection.has_limit_or_offset?
|
358
|
+
query = collection.select("#{column} AS collection_cache_key_timestamp")
|
359
|
+
subquery_alias = "subquery_for_cache_key"
|
360
|
+
subquery_column = "#{subquery_alias}.collection_cache_key_timestamp"
|
361
|
+
arel = query.build_subquery(subquery_alias, select_values % subquery_column)
|
362
|
+
else
|
363
|
+
query = collection.unscope(:order)
|
364
|
+
query.select_values = [select_values % column]
|
365
|
+
arel = query.arel
|
366
|
+
end
|
367
|
+
|
368
|
+
result = connection.select_one(arel, nil)
|
369
|
+
|
370
|
+
if result
|
371
|
+
column_type = klass.type_for_attribute(timestamp_column)
|
372
|
+
timestamp = column_type.deserialize(result["timestamp"])
|
373
|
+
size = result["size"]
|
374
|
+
else
|
375
|
+
timestamp = nil
|
376
|
+
size = 0
|
377
|
+
end
|
378
|
+
end
|
379
|
+
|
380
|
+
if timestamp
|
381
|
+
"#{size}-#{timestamp.utc.to_s(cache_timestamp_format)}"
|
382
|
+
else
|
383
|
+
"#{size}"
|
384
|
+
end
|
385
|
+
end
|
386
|
+
private :compute_cache_version
|
387
|
+
|
340
388
|
# Scope all queries to the current scope.
|
341
389
|
#
|
342
390
|
# Comment.where(post_id: 1).scoping do
|
@@ -347,10 +395,14 @@ module ActiveRecord
|
|
347
395
|
# Please check unscoped if you want to remove all previous scopes (including
|
348
396
|
# the default_scope) during the execution of a block.
|
349
397
|
def scoping
|
350
|
-
|
351
|
-
|
398
|
+
already_in_scope? ? yield : _scoping(self) { yield }
|
399
|
+
end
|
400
|
+
|
401
|
+
def _exec_scope(name, *args, &block) # :nodoc:
|
402
|
+
@delegate_to_klass = true
|
403
|
+
_scoping(_deprecated_spawn(name)) { instance_exec(*args, &block) || self }
|
352
404
|
ensure
|
353
|
-
|
405
|
+
@delegate_to_klass = false
|
354
406
|
end
|
355
407
|
|
356
408
|
# Updates all records in the current relation with details given. This method constructs a single SQL UPDATE
|
@@ -358,6 +410,8 @@ module ActiveRecord
|
|
358
410
|
# trigger Active Record callbacks or validations. However, values passed to #update_all will still go through
|
359
411
|
# Active Record's normal type casting and serialization.
|
360
412
|
#
|
413
|
+
# Note: As Active Record callbacks are not triggered, this method will not automatically update +updated_at+/+updated_on+ columns.
|
414
|
+
#
|
361
415
|
# ==== Parameters
|
362
416
|
#
|
363
417
|
# * +updates+ - A string, array, or hash representing the SET part of an SQL statement.
|
@@ -378,66 +432,85 @@ module ActiveRecord
|
|
378
432
|
def update_all(updates)
|
379
433
|
raise ArgumentError, "Empty list of attributes to change" if updates.blank?
|
380
434
|
|
435
|
+
if eager_loading?
|
436
|
+
relation = apply_join_dependency
|
437
|
+
return relation.update_all(updates)
|
438
|
+
end
|
439
|
+
|
381
440
|
stmt = Arel::UpdateManager.new
|
441
|
+
stmt.table(arel.join_sources.empty? ? table : arel.source)
|
442
|
+
stmt.key = arel_attribute(primary_key)
|
443
|
+
stmt.take(arel.limit)
|
444
|
+
stmt.offset(arel.offset)
|
445
|
+
stmt.order(*arel.orders)
|
446
|
+
stmt.wheres = arel.constraints
|
447
|
+
|
448
|
+
if updates.is_a?(Hash)
|
449
|
+
if klass.locking_enabled? &&
|
450
|
+
!updates.key?(klass.locking_column) &&
|
451
|
+
!updates.key?(klass.locking_column.to_sym)
|
452
|
+
attr = arel_attribute(klass.locking_column)
|
453
|
+
updates[attr.name] = _increment_attribute(attr)
|
454
|
+
end
|
455
|
+
stmt.set _substitute_values(updates)
|
456
|
+
else
|
457
|
+
stmt.set Arel.sql(klass.sanitize_sql_for_assignment(updates, table.name))
|
458
|
+
end
|
382
459
|
|
383
|
-
|
384
|
-
|
460
|
+
@klass.connection.update stmt, "#{@klass} Update All"
|
461
|
+
end
|
385
462
|
|
386
|
-
|
387
|
-
|
463
|
+
def update(id = :all, attributes) # :nodoc:
|
464
|
+
if id == :all
|
465
|
+
each { |record| record.update(attributes) }
|
388
466
|
else
|
389
|
-
|
390
|
-
|
391
|
-
|
392
|
-
|
467
|
+
klass.update(id, attributes)
|
468
|
+
end
|
469
|
+
end
|
470
|
+
|
471
|
+
def update_counters(counters) # :nodoc:
|
472
|
+
touch = counters.delete(:touch)
|
473
|
+
|
474
|
+
updates = {}
|
475
|
+
counters.each do |counter_name, value|
|
476
|
+
attr = arel_attribute(counter_name)
|
477
|
+
updates[attr.name] = _increment_attribute(attr, value)
|
478
|
+
end
|
479
|
+
|
480
|
+
if touch
|
481
|
+
names = touch if touch != true
|
482
|
+
touch_updates = klass.touch_attributes_with_time(*names)
|
483
|
+
updates.merge!(touch_updates) unless touch_updates.empty?
|
393
484
|
end
|
394
485
|
|
395
|
-
|
486
|
+
update_all updates
|
396
487
|
end
|
397
488
|
|
398
|
-
#
|
399
|
-
#
|
489
|
+
# Touches all records in the current relation without instantiating records first with the +updated_at+/+updated_on+ attributes
|
490
|
+
# set to the current time or the time specified.
|
491
|
+
# This method can be passed attribute names and an optional time argument.
|
492
|
+
# If attribute names are passed, they are updated along with +updated_at+/+updated_on+ attributes.
|
493
|
+
# If no time argument is passed, the current time is used as default.
|
400
494
|
#
|
401
|
-
#
|
495
|
+
# === Examples
|
402
496
|
#
|
403
|
-
#
|
404
|
-
#
|
497
|
+
# # Touch all records
|
498
|
+
# Person.all.touch_all
|
499
|
+
# # => "UPDATE \"people\" SET \"updated_at\" = '2018-01-04 22:55:23.132670'"
|
405
500
|
#
|
406
|
-
#
|
501
|
+
# # Touch multiple records with a custom attribute
|
502
|
+
# Person.all.touch_all(:created_at)
|
503
|
+
# # => "UPDATE \"people\" SET \"updated_at\" = '2018-01-04 22:55:23.132670', \"created_at\" = '2018-01-04 22:55:23.132670'"
|
407
504
|
#
|
408
|
-
# #
|
409
|
-
# Person.
|
410
|
-
#
|
411
|
-
#
|
412
|
-
#
|
413
|
-
# Person.
|
414
|
-
#
|
415
|
-
|
416
|
-
|
417
|
-
# people.update(group: 'masters')
|
418
|
-
#
|
419
|
-
# Note: Updating a large number of records will run an
|
420
|
-
# UPDATE query for each record, which may cause a performance
|
421
|
-
# issue. So if it is not needed to run callbacks for each update, it is
|
422
|
-
# preferred to use #update_all for updating all records using
|
423
|
-
# a single query.
|
424
|
-
def update(id = :all, attributes)
|
425
|
-
if id.is_a?(Array)
|
426
|
-
id.map.with_index { |one_id, idx| update(one_id, attributes[idx]) }
|
427
|
-
elsif id == :all
|
428
|
-
records.each { |record| record.update(attributes) }
|
429
|
-
else
|
430
|
-
if ActiveRecord::Base === id
|
431
|
-
id = id.id
|
432
|
-
ActiveSupport::Deprecation.warn(<<-MSG.squish)
|
433
|
-
You are passing an instance of ActiveRecord::Base to `update`.
|
434
|
-
Please pass the id of the object by calling `.id`.
|
435
|
-
MSG
|
436
|
-
end
|
437
|
-
object = find(id)
|
438
|
-
object.update(attributes)
|
439
|
-
object
|
440
|
-
end
|
505
|
+
# # Touch multiple records with a specified time
|
506
|
+
# Person.all.touch_all(time: Time.new(2020, 5, 16, 0, 0, 0))
|
507
|
+
# # => "UPDATE \"people\" SET \"updated_at\" = '2020-05-16 00:00:00'"
|
508
|
+
#
|
509
|
+
# # Touch records with scope
|
510
|
+
# Person.where(name: 'David').touch_all
|
511
|
+
# # => "UPDATE \"people\" SET \"updated_at\" = '2018-01-04 22:55:23.132670' WHERE \"people\".\"name\" = 'David'"
|
512
|
+
def touch_all(*names, time: nil)
|
513
|
+
update_all klass.touch_attributes_with_time(*names, time: time)
|
441
514
|
end
|
442
515
|
|
443
516
|
# Destroys the records by instantiating each
|
@@ -456,43 +529,8 @@ module ActiveRecord
|
|
456
529
|
# ==== Examples
|
457
530
|
#
|
458
531
|
# Person.where(age: 0..18).destroy_all
|
459
|
-
def destroy_all
|
460
|
-
|
461
|
-
ActiveSupport::Deprecation.warn(<<-MESSAGE.squish)
|
462
|
-
Passing conditions to destroy_all is deprecated and will be removed in Rails 5.1.
|
463
|
-
To achieve the same use where(conditions).destroy_all.
|
464
|
-
MESSAGE
|
465
|
-
where(conditions).destroy_all
|
466
|
-
else
|
467
|
-
records.each(&:destroy).tap { reset }
|
468
|
-
end
|
469
|
-
end
|
470
|
-
|
471
|
-
# Destroy an object (or multiple objects) that has the given id. The object is instantiated first,
|
472
|
-
# therefore all callbacks and filters are fired off before the object is deleted. This method is
|
473
|
-
# less efficient than #delete but allows cleanup methods and other actions to be run.
|
474
|
-
#
|
475
|
-
# This essentially finds the object (or multiple objects) with the given id, creates a new object
|
476
|
-
# from the attributes, and then calls destroy on it.
|
477
|
-
#
|
478
|
-
# ==== Parameters
|
479
|
-
#
|
480
|
-
# * +id+ - Can be either an Integer or an Array of Integers.
|
481
|
-
#
|
482
|
-
# ==== Examples
|
483
|
-
#
|
484
|
-
# # Destroy a single object
|
485
|
-
# Todo.destroy(1)
|
486
|
-
#
|
487
|
-
# # Destroy multiple objects
|
488
|
-
# todos = [1,2,3]
|
489
|
-
# Todo.destroy(todos)
|
490
|
-
def destroy(id)
|
491
|
-
if id.is_a?(Array)
|
492
|
-
id.map { |one_id| destroy(one_id) }
|
493
|
-
else
|
494
|
-
find(id).destroy
|
495
|
-
end
|
532
|
+
def destroy_all
|
533
|
+
records.each(&:destroy).tap { reset }
|
496
534
|
end
|
497
535
|
|
498
536
|
# Deletes the records without instantiating the records
|
@@ -511,66 +549,60 @@ module ActiveRecord
|
|
511
549
|
#
|
512
550
|
# If an invalid method is supplied, #delete_all raises an ActiveRecordError:
|
513
551
|
#
|
514
|
-
# Post.
|
515
|
-
# # => ActiveRecord::ActiveRecordError: delete_all doesn't support
|
516
|
-
def delete_all
|
517
|
-
invalid_methods = INVALID_METHODS_FOR_DELETE_ALL.select
|
518
|
-
|
519
|
-
|
520
|
-
|
521
|
-
send("#{method}_value")
|
522
|
-
elsif CLAUSE_METHODS.include?(method)
|
523
|
-
send("#{method}_clause").any?
|
524
|
-
end
|
525
|
-
}
|
552
|
+
# Post.distinct.delete_all
|
553
|
+
# # => ActiveRecord::ActiveRecordError: delete_all doesn't support distinct
|
554
|
+
def delete_all
|
555
|
+
invalid_methods = INVALID_METHODS_FOR_DELETE_ALL.select do |method|
|
556
|
+
value = @values[method]
|
557
|
+
method == :distinct ? value : value&.any?
|
558
|
+
end
|
526
559
|
if invalid_methods.any?
|
527
560
|
raise ActiveRecordError.new("delete_all doesn't support #{invalid_methods.join(', ')}")
|
528
561
|
end
|
529
562
|
|
530
|
-
if
|
531
|
-
|
532
|
-
|
533
|
-
|
534
|
-
MESSAGE
|
535
|
-
where(conditions).delete_all
|
536
|
-
else
|
537
|
-
stmt = Arel::DeleteManager.new
|
538
|
-
stmt.from(table)
|
563
|
+
if eager_loading?
|
564
|
+
relation = apply_join_dependency
|
565
|
+
return relation.delete_all
|
566
|
+
end
|
539
567
|
|
540
|
-
|
541
|
-
|
542
|
-
|
543
|
-
|
544
|
-
|
568
|
+
stmt = Arel::DeleteManager.new
|
569
|
+
stmt.from(arel.join_sources.empty? ? table : arel.source)
|
570
|
+
stmt.key = arel_attribute(primary_key)
|
571
|
+
stmt.take(arel.limit)
|
572
|
+
stmt.offset(arel.offset)
|
573
|
+
stmt.order(*arel.orders)
|
574
|
+
stmt.wheres = arel.constraints
|
545
575
|
|
546
|
-
|
576
|
+
affected = @klass.connection.delete(stmt, "#{@klass} Destroy")
|
547
577
|
|
548
|
-
|
549
|
-
|
550
|
-
end
|
578
|
+
reset
|
579
|
+
affected
|
551
580
|
end
|
552
581
|
|
553
|
-
#
|
554
|
-
#
|
555
|
-
#
|
556
|
-
# executed, including any <tt>:dependent</tt> association options.
|
582
|
+
# Finds and destroys all records matching the specified conditions.
|
583
|
+
# This is short-hand for <tt>relation.where(condition).destroy_all</tt>.
|
584
|
+
# Returns the collection of objects that were destroyed.
|
557
585
|
#
|
558
|
-
#
|
586
|
+
# If no record is found, returns empty array.
|
559
587
|
#
|
560
|
-
#
|
561
|
-
#
|
562
|
-
#
|
563
|
-
|
564
|
-
|
565
|
-
|
588
|
+
# Person.destroy_by(id: 13)
|
589
|
+
# Person.destroy_by(name: 'Spartacus', rating: 4)
|
590
|
+
# Person.destroy_by("published_at < ?", 2.weeks.ago)
|
591
|
+
def destroy_by(*args)
|
592
|
+
where(*args).destroy_all
|
593
|
+
end
|
594
|
+
|
595
|
+
# Finds and deletes all records matching the specified conditions.
|
596
|
+
# This is short-hand for <tt>relation.where(condition).delete_all</tt>.
|
597
|
+
# Returns the number of rows affected.
|
566
598
|
#
|
567
|
-
#
|
568
|
-
# Todo.delete(1)
|
599
|
+
# If no record is found, returns <tt>0</tt> as zero rows were affected.
|
569
600
|
#
|
570
|
-
#
|
571
|
-
#
|
572
|
-
|
573
|
-
|
601
|
+
# Person.delete_by(id: 13)
|
602
|
+
# Person.delete_by(name: 'Spartacus', rating: 4)
|
603
|
+
# Person.delete_by("published_at < ?", 2.weeks.ago)
|
604
|
+
def delete_by(*args)
|
605
|
+
where(*args).delete_all
|
574
606
|
end
|
575
607
|
|
576
608
|
# Causes the records to be loaded from the database if they have not
|
@@ -592,10 +624,12 @@ module ActiveRecord
|
|
592
624
|
end
|
593
625
|
|
594
626
|
def reset
|
595
|
-
@
|
596
|
-
@
|
627
|
+
@delegate_to_klass = false
|
628
|
+
@_deprecated_scope_source = nil
|
629
|
+
@to_sql = @arel = @loaded = @should_eager_load = nil
|
597
630
|
@records = [].freeze
|
598
631
|
@offsets = {}
|
632
|
+
@take = nil
|
599
633
|
self
|
600
634
|
end
|
601
635
|
|
@@ -605,32 +639,28 @@ module ActiveRecord
|
|
605
639
|
# # => SELECT "users".* FROM "users" WHERE "users"."name" = 'Oscar'
|
606
640
|
def to_sql
|
607
641
|
@to_sql ||= begin
|
608
|
-
|
609
|
-
|
610
|
-
|
611
|
-
|
612
|
-
|
613
|
-
|
614
|
-
|
615
|
-
|
616
|
-
|
617
|
-
|
618
|
-
binds.map! { |value| connection.quote(value) }
|
619
|
-
collect = visitor.accept(relation.arel.ast, Arel::Collectors::Bind.new)
|
620
|
-
collect.substitute_binds(binds).join
|
621
|
-
end
|
642
|
+
if eager_loading?
|
643
|
+
apply_join_dependency do |relation, join_dependency|
|
644
|
+
relation = join_dependency.apply_column_aliases(relation)
|
645
|
+
relation.to_sql
|
646
|
+
end
|
647
|
+
else
|
648
|
+
conn = klass.connection
|
649
|
+
conn.unprepared_statement { conn.to_sql(arel) }
|
650
|
+
end
|
651
|
+
end
|
622
652
|
end
|
623
653
|
|
624
654
|
# Returns a hash of where conditions.
|
625
655
|
#
|
626
656
|
# User.where(name: 'Oscar').where_values_hash
|
627
657
|
# # => {name: "Oscar"}
|
628
|
-
def where_values_hash(relation_table_name = table_name)
|
658
|
+
def where_values_hash(relation_table_name = klass.table_name)
|
629
659
|
where_clause.to_h(relation_table_name)
|
630
660
|
end
|
631
661
|
|
632
662
|
def scope_for_create
|
633
|
-
|
663
|
+
where_values_hash.merge!(create_with_value.stringify_keys)
|
634
664
|
end
|
635
665
|
|
636
666
|
# Returns true if relation needs eager loading.
|
@@ -648,15 +678,6 @@ module ActiveRecord
|
|
648
678
|
includes_values & joins_values
|
649
679
|
end
|
650
680
|
|
651
|
-
# {#uniq}[rdoc-ref:QueryMethods#uniq] and
|
652
|
-
# {#uniq!}[rdoc-ref:QueryMethods#uniq!] are silently deprecated.
|
653
|
-
# #uniq_value delegates to #distinct_value to maintain backwards compatibility.
|
654
|
-
# Use #distinct_value instead.
|
655
|
-
def uniq_value
|
656
|
-
distinct_value
|
657
|
-
end
|
658
|
-
deprecate uniq_value: :distinct_value
|
659
|
-
|
660
681
|
# Compares two relations for equality.
|
661
682
|
def ==(other)
|
662
683
|
case other
|
@@ -670,7 +691,7 @@ module ActiveRecord
|
|
670
691
|
end
|
671
692
|
|
672
693
|
def pretty_print(q)
|
673
|
-
q.pp(
|
694
|
+
q.pp(records)
|
674
695
|
end
|
675
696
|
|
676
697
|
# Returns true if relation is blank.
|
@@ -679,12 +700,14 @@ module ActiveRecord
|
|
679
700
|
end
|
680
701
|
|
681
702
|
def values
|
682
|
-
|
703
|
+
@values.dup
|
683
704
|
end
|
684
705
|
|
685
706
|
def inspect
|
686
|
-
|
687
|
-
entries
|
707
|
+
subject = loaded? ? records : self
|
708
|
+
entries = subject.take([limit_value, 11].compact.min).map!(&:inspect)
|
709
|
+
|
710
|
+
entries[10] = "..." if entries.size == 11
|
688
711
|
|
689
712
|
"#<#{self.class.name} [#{entries.join(', ')}]>"
|
690
713
|
end
|
@@ -693,57 +716,145 @@ module ActiveRecord
|
|
693
716
|
@values == klass.unscoped.values
|
694
717
|
end
|
695
718
|
|
719
|
+
def has_limit_or_offset? # :nodoc:
|
720
|
+
limit_value || offset_value
|
721
|
+
end
|
722
|
+
|
723
|
+
def alias_tracker(joins = [], aliases = nil) # :nodoc:
|
724
|
+
joins += [aliases] if aliases
|
725
|
+
ActiveRecord::Associations::AliasTracker.create(connection, table.name, joins)
|
726
|
+
end
|
727
|
+
|
728
|
+
def preload_associations(records) # :nodoc:
|
729
|
+
preload = preload_values
|
730
|
+
preload += includes_values unless eager_loading?
|
731
|
+
preloader = nil
|
732
|
+
preload.each do |associations|
|
733
|
+
preloader ||= build_preloader
|
734
|
+
preloader.preload records, associations
|
735
|
+
end
|
736
|
+
end
|
737
|
+
|
738
|
+
attr_reader :_deprecated_scope_source # :nodoc:
|
739
|
+
|
696
740
|
protected
|
741
|
+
attr_writer :_deprecated_scope_source # :nodoc:
|
697
742
|
|
698
743
|
def load_records(records)
|
699
744
|
@records = records.freeze
|
700
745
|
@loaded = true
|
701
746
|
end
|
702
747
|
|
748
|
+
def null_relation? # :nodoc:
|
749
|
+
is_a?(NullRelation)
|
750
|
+
end
|
751
|
+
|
703
752
|
private
|
753
|
+
def already_in_scope?
|
754
|
+
@delegate_to_klass && begin
|
755
|
+
scope = klass.current_scope(true)
|
756
|
+
scope && !scope._deprecated_scope_source
|
757
|
+
end
|
758
|
+
end
|
704
759
|
|
705
|
-
|
706
|
-
|
760
|
+
def _deprecated_spawn(name)
|
761
|
+
spawn.tap { |scope| scope._deprecated_scope_source = name }
|
762
|
+
end
|
707
763
|
|
708
|
-
|
709
|
-
|
710
|
-
|
711
|
-
|
712
|
-
|
764
|
+
def _deprecated_scope_block(name, &block)
|
765
|
+
-> record do
|
766
|
+
klass.current_scope = _deprecated_spawn(name)
|
767
|
+
yield record if block_given?
|
768
|
+
end
|
713
769
|
end
|
714
770
|
|
715
|
-
|
771
|
+
def _scoping(scope)
|
772
|
+
previous, klass.current_scope = klass.current_scope(true), scope
|
773
|
+
yield
|
774
|
+
ensure
|
775
|
+
klass.current_scope = previous
|
776
|
+
end
|
716
777
|
|
717
|
-
|
718
|
-
|
719
|
-
|
778
|
+
def _substitute_values(values)
|
779
|
+
values.map do |name, value|
|
780
|
+
attr = arel_attribute(name)
|
781
|
+
unless Arel.arel_node?(value)
|
782
|
+
type = klass.type_for_attribute(attr.name)
|
783
|
+
value = predicate_builder.build_bind_attribute(attr.name, type.cast(value))
|
784
|
+
end
|
785
|
+
[attr, value]
|
786
|
+
end
|
787
|
+
end
|
720
788
|
|
721
|
-
|
722
|
-
|
723
|
-
|
789
|
+
def _increment_attribute(attribute, value = 1)
|
790
|
+
bind = predicate_builder.build_bind_attribute(attribute.name, value.abs)
|
791
|
+
expr = table.coalesce(Arel::Nodes::UnqualifiedColumn.new(attribute), 0)
|
792
|
+
expr = value < 0 ? expr - bind : expr + bind
|
793
|
+
expr.expr
|
794
|
+
end
|
795
|
+
|
796
|
+
def exec_queries(&block)
|
797
|
+
skip_query_cache_if_necessary do
|
798
|
+
@records =
|
799
|
+
if eager_loading?
|
800
|
+
apply_join_dependency do |relation, join_dependency|
|
801
|
+
if relation.null_relation?
|
802
|
+
[]
|
803
|
+
else
|
804
|
+
relation = join_dependency.apply_column_aliases(relation)
|
805
|
+
rows = connection.select_all(relation.arel, "SQL")
|
806
|
+
join_dependency.instantiate(rows, &block)
|
807
|
+
end.freeze
|
808
|
+
end
|
809
|
+
else
|
810
|
+
klass.find_by_sql(arel, &block).freeze
|
811
|
+
end
|
812
|
+
|
813
|
+
preload_associations(@records) unless skip_preloading_value
|
814
|
+
|
815
|
+
@records.each(&:readonly!) if readonly_value
|
816
|
+
|
817
|
+
@loaded = true
|
818
|
+
@records
|
819
|
+
end
|
820
|
+
end
|
724
821
|
|
725
|
-
|
726
|
-
|
727
|
-
|
728
|
-
|
822
|
+
def skip_query_cache_if_necessary
|
823
|
+
if skip_query_cache_value
|
824
|
+
uncached do
|
825
|
+
yield
|
826
|
+
end
|
729
827
|
else
|
730
|
-
|
828
|
+
yield
|
731
829
|
end
|
732
830
|
end
|
733
831
|
|
734
|
-
|
832
|
+
def build_preloader
|
833
|
+
ActiveRecord::Associations::Preloader.new
|
834
|
+
end
|
735
835
|
|
736
|
-
|
737
|
-
|
836
|
+
def references_eager_loaded_tables?
|
837
|
+
joined_tables = arel.join_sources.map do |join|
|
838
|
+
if join.is_a?(Arel::Nodes::StringJoin)
|
839
|
+
tables_in_string(join.left)
|
840
|
+
else
|
841
|
+
[join.left.table_name, join.left.table_alias]
|
842
|
+
end
|
843
|
+
end
|
738
844
|
|
739
|
-
|
740
|
-
end
|
845
|
+
joined_tables += [table.name, table.table_alias]
|
741
846
|
|
742
|
-
|
743
|
-
|
744
|
-
|
745
|
-
|
746
|
-
|
747
|
-
|
847
|
+
# always convert table names to downcase as in Oracle quoted table names are in uppercase
|
848
|
+
joined_tables = joined_tables.flatten.compact.map(&:downcase).uniq
|
849
|
+
|
850
|
+
(references_values - joined_tables).any?
|
851
|
+
end
|
852
|
+
|
853
|
+
def tables_in_string(string)
|
854
|
+
return [] if string.blank?
|
855
|
+
# always convert table names to downcase as in Oracle quoted table names are in uppercase
|
856
|
+
# ignore raw_sql_ that is used by Oracle adapter as alias for limit/offset subqueries
|
857
|
+
string.scan(/([a-zA-Z_][.\w]+).?\./).flatten.map(&:downcase).uniq - ["raw_sql_"]
|
858
|
+
end
|
748
859
|
end
|
749
860
|
end
|