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