activerecord 4.2.0 → 6.1.7.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of activerecord might be problematic. Click here for more details.
- checksums.yaml +5 -5
 - data/CHANGELOG.md +1221 -796
 - data/MIT-LICENSE +4 -2
 - data/README.rdoc +15 -14
 - data/examples/performance.rb +33 -32
 - data/examples/simple.rb +5 -4
 - data/lib/active_record/aggregations.rb +267 -249
 - data/lib/active_record/association_relation.rb +45 -7
 - data/lib/active_record/associations/alias_tracker.rb +40 -43
 - data/lib/active_record/associations/association.rb +172 -67
 - data/lib/active_record/associations/association_scope.rb +105 -129
 - data/lib/active_record/associations/belongs_to_association.rb +85 -59
 - data/lib/active_record/associations/belongs_to_polymorphic_association.rb +13 -12
 - data/lib/active_record/associations/builder/association.rb +57 -43
 - data/lib/active_record/associations/builder/belongs_to.rb +74 -57
 - data/lib/active_record/associations/builder/collection_association.rb +15 -33
 - data/lib/active_record/associations/builder/has_and_belongs_to_many.rb +57 -70
 - data/lib/active_record/associations/builder/has_many.rb +13 -5
 - data/lib/active_record/associations/builder/has_one.rb +44 -6
 - data/lib/active_record/associations/builder/singular_association.rb +16 -10
 - data/lib/active_record/associations/collection_association.rb +168 -279
 - data/lib/active_record/associations/collection_proxy.rb +263 -155
 - data/lib/active_record/associations/foreign_association.rb +33 -0
 - data/lib/active_record/associations/has_many_association.rb +57 -84
 - data/lib/active_record/associations/has_many_through_association.rb +70 -82
 - data/lib/active_record/associations/has_one_association.rb +74 -47
 - data/lib/active_record/associations/has_one_through_association.rb +20 -11
 - data/lib/active_record/associations/join_dependency/join_association.rb +54 -73
 - 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/join_dependency.rb +175 -164
 - data/lib/active_record/associations/preloader/association.rb +107 -112
 - data/lib/active_record/associations/preloader/through_association.rb +85 -65
 - data/lib/active_record/associations/preloader.rb +99 -96
 - data/lib/active_record/associations/singular_association.rb +18 -45
 - data/lib/active_record/associations/through_association.rb +49 -24
 - data/lib/active_record/associations.rb +1845 -1597
 - data/lib/active_record/attribute_assignment.rb +59 -185
 - data/lib/active_record/attribute_methods/before_type_cast.rb +20 -7
 - data/lib/active_record/attribute_methods/dirty.rb +168 -138
 - data/lib/active_record/attribute_methods/primary_key.rb +93 -83
 - data/lib/active_record/attribute_methods/query.rb +8 -10
 - data/lib/active_record/attribute_methods/read.rb +19 -79
 - data/lib/active_record/attribute_methods/serialization.rb +49 -24
 - data/lib/active_record/attribute_methods/time_zone_conversion.rb +59 -36
 - data/lib/active_record/attribute_methods/write.rb +25 -56
 - data/lib/active_record/attribute_methods.rb +153 -162
 - data/lib/active_record/attributes.rb +234 -70
 - data/lib/active_record/autosave_association.rb +157 -69
 - data/lib/active_record/base.rb +49 -50
 - data/lib/active_record/callbacks.rb +234 -79
 - data/lib/active_record/coders/json.rb +3 -1
 - data/lib/active_record/coders/yaml_column.rb +46 -13
 - data/lib/active_record/connection_adapters/abstract/connection_pool.rb +887 -317
 - data/lib/active_record/connection_adapters/abstract/database_limits.rb +17 -41
 - data/lib/active_record/connection_adapters/abstract/database_statements.rb +301 -113
 - data/lib/active_record/connection_adapters/abstract/query_cache.rb +78 -24
 - data/lib/active_record/connection_adapters/abstract/quoting.rb +187 -60
 - data/lib/active_record/connection_adapters/abstract/savepoints.rb +9 -7
 - data/lib/active_record/connection_adapters/abstract/schema_creation.rb +157 -93
 - data/lib/active_record/connection_adapters/abstract/schema_definitions.rb +485 -253
 - data/lib/active_record/connection_adapters/abstract/schema_dumper.rb +79 -36
 - data/lib/active_record/connection_adapters/abstract/schema_statements.rb +909 -263
 - data/lib/active_record/connection_adapters/abstract/transaction.rb +254 -92
 - data/lib/active_record/connection_adapters/abstract_adapter.rb +492 -221
 - data/lib/active_record/connection_adapters/abstract_mysql_adapter.rb +580 -608
 - data/lib/active_record/connection_adapters/column.rb +67 -40
 - data/lib/active_record/connection_adapters/deduplicable.rb +29 -0
 - data/lib/active_record/connection_adapters/legacy_pool_manager.rb +35 -0
 - data/lib/active_record/connection_adapters/mysql/column.rb +27 -0
 - data/lib/active_record/connection_adapters/mysql/database_statements.rb +196 -0
 - data/lib/active_record/connection_adapters/mysql/explain_pretty_printer.rb +71 -0
 - data/lib/active_record/connection_adapters/mysql/quoting.rb +96 -0
 - data/lib/active_record/connection_adapters/mysql/schema_creation.rb +97 -0
 - data/lib/active_record/connection_adapters/mysql/schema_definitions.rb +103 -0
 - data/lib/active_record/connection_adapters/mysql/schema_dumper.rb +91 -0
 - data/lib/active_record/connection_adapters/mysql/schema_statements.rb +271 -0
 - data/lib/active_record/connection_adapters/mysql/type_metadata.rb +40 -0
 - data/lib/active_record/connection_adapters/mysql2_adapter.rb +81 -199
 - data/lib/active_record/connection_adapters/pool_config.rb +73 -0
 - data/lib/active_record/connection_adapters/pool_manager.rb +47 -0
 - data/lib/active_record/connection_adapters/postgresql/column.rb +44 -11
 - data/lib/active_record/connection_adapters/postgresql/database_statements.rb +78 -161
 - data/lib/active_record/connection_adapters/postgresql/explain_pretty_printer.rb +44 -0
 - data/lib/active_record/connection_adapters/postgresql/oid/array.rb +49 -57
 - data/lib/active_record/connection_adapters/postgresql/oid/bit.rb +9 -8
 - data/lib/active_record/connection_adapters/postgresql/oid/bit_varying.rb +2 -0
 - data/lib/active_record/connection_adapters/postgresql/oid/bytea.rb +5 -2
 - data/lib/active_record/connection_adapters/postgresql/oid/cidr.rb +8 -6
 - data/lib/active_record/connection_adapters/postgresql/oid/date.rb +13 -1
 - data/lib/active_record/connection_adapters/postgresql/oid/date_time.rb +17 -13
 - data/lib/active_record/connection_adapters/postgresql/oid/decimal.rb +3 -1
 - data/lib/active_record/connection_adapters/postgresql/oid/enum.rb +6 -3
 - data/lib/active_record/connection_adapters/postgresql/oid/hstore.rb +31 -20
 - 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 -9
 - data/lib/active_record/connection_adapters/postgresql/oid/{infinity.rb → oid.rb} +5 -3
 - data/lib/active_record/connection_adapters/postgresql/oid/point.rb +32 -11
 - data/lib/active_record/connection_adapters/postgresql/oid/range.rb +70 -34
 - 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 +67 -51
 - data/lib/active_record/connection_adapters/postgresql/oid/uuid.rb +18 -4
 - data/lib/active_record/connection_adapters/postgresql/oid/vector.rb +3 -1
 - data/lib/active_record/connection_adapters/postgresql/oid/xml.rb +3 -1
 - data/lib/active_record/connection_adapters/postgresql/oid.rb +25 -25
 - data/lib/active_record/connection_adapters/postgresql/quoting.rb +171 -48
 - data/lib/active_record/connection_adapters/postgresql/referential_integrity.rb +27 -14
 - data/lib/active_record/connection_adapters/postgresql/schema_creation.rb +80 -0
 - data/lib/active_record/connection_adapters/postgresql/schema_definitions.rb +178 -108
 - data/lib/active_record/connection_adapters/postgresql/schema_dumper.rb +49 -0
 - data/lib/active_record/connection_adapters/postgresql/schema_statements.rb +499 -293
 - data/lib/active_record/connection_adapters/postgresql/type_metadata.rb +44 -0
 - data/lib/active_record/connection_adapters/postgresql/utils.rb +11 -8
 - data/lib/active_record/connection_adapters/postgresql_adapter.rb +595 -382
 - data/lib/active_record/connection_adapters/schema_cache.rb +191 -29
 - data/lib/active_record/connection_adapters/sql_type_metadata.rb +45 -0
 - data/lib/active_record/connection_adapters/sqlite3/database_statements.rb +146 -0
 - data/lib/active_record/connection_adapters/sqlite3/explain_pretty_printer.rb +21 -0
 - data/lib/active_record/connection_adapters/sqlite3/quoting.rb +102 -0
 - data/lib/active_record/connection_adapters/sqlite3/schema_creation.rb +21 -0
 - 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 +322 -389
 - data/lib/active_record/connection_adapters/statement_pool.rb +33 -13
 - data/lib/active_record/connection_adapters.rb +52 -0
 - data/lib/active_record/connection_handling.rb +314 -41
 - data/lib/active_record/core.rb +488 -243
 - data/lib/active_record/counter_cache.rb +71 -50
 - data/lib/active_record/database_configurations/connection_url_resolver.rb +99 -0
 - data/lib/active_record/database_configurations/database_config.rb +80 -0
 - data/lib/active_record/database_configurations/hash_config.rb +96 -0
 - data/lib/active_record/database_configurations/url_config.rb +53 -0
 - data/lib/active_record/database_configurations.rb +273 -0
 - data/lib/active_record/delegated_type.rb +209 -0
 - data/lib/active_record/destroy_association_async_job.rb +36 -0
 - data/lib/active_record/dynamic_matchers.rb +87 -106
 - data/lib/active_record/enum.rb +212 -94
 - data/lib/active_record/errors.rb +225 -54
 - data/lib/active_record/explain.rb +27 -11
 - data/lib/active_record/explain_registry.rb +4 -2
 - data/lib/active_record/explain_subscriber.rb +11 -6
 - data/lib/active_record/fixture_set/file.rb +33 -14
 - 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 +273 -496
 - data/lib/active_record/gem_version.rb +6 -4
 - data/lib/active_record/inheritance.rb +175 -110
 - data/lib/active_record/insert_all.rb +212 -0
 - data/lib/active_record/integration.rb +121 -29
 - data/lib/active_record/internal_metadata.rb +64 -0
 - data/lib/active_record/legacy_yaml_adapter.rb +52 -0
 - data/lib/active_record/locale/en.yml +3 -2
 - data/lib/active_record/locking/optimistic.rb +103 -95
 - data/lib/active_record/locking/pessimistic.rb +22 -6
 - data/lib/active_record/log_subscriber.rb +93 -31
 - data/lib/active_record/middleware/database_selector/resolver/session.rb +48 -0
 - data/lib/active_record/middleware/database_selector/resolver.rb +92 -0
 - data/lib/active_record/middleware/database_selector.rb +77 -0
 - data/lib/active_record/migration/command_recorder.rb +185 -90
 - data/lib/active_record/migration/compatibility.rb +298 -0
 - data/lib/active_record/migration/join_table.rb +8 -7
 - data/lib/active_record/migration.rb +685 -309
 - data/lib/active_record/model_schema.rb +420 -113
 - data/lib/active_record/nested_attributes.rb +265 -216
 - data/lib/active_record/no_touching.rb +15 -2
 - data/lib/active_record/null_relation.rb +24 -38
 - data/lib/active_record/persistence.rb +574 -135
 - data/lib/active_record/query_cache.rb +29 -23
 - data/lib/active_record/querying.rb +50 -31
 - data/lib/active_record/railtie.rb +175 -54
 - data/lib/active_record/railties/console_sandbox.rb +3 -3
 - data/lib/active_record/railties/controller_runtime.rb +34 -33
 - data/lib/active_record/railties/databases.rake +533 -216
 - data/lib/active_record/readonly_attributes.rb +9 -4
 - data/lib/active_record/reflection.rb +485 -310
 - data/lib/active_record/relation/batches/batch_enumerator.rb +85 -0
 - data/lib/active_record/relation/batches.rb +217 -59
 - data/lib/active_record/relation/calculations.rb +326 -244
 - data/lib/active_record/relation/delegation.rb +76 -84
 - data/lib/active_record/relation/finder_methods.rb +318 -256
 - data/lib/active_record/relation/from_clause.rb +30 -0
 - data/lib/active_record/relation/merger.rb +99 -84
 - data/lib/active_record/relation/predicate_builder/array_handler.rb +26 -25
 - data/lib/active_record/relation/predicate_builder/association_query_value.rb +42 -0
 - data/lib/active_record/relation/predicate_builder/basic_object_handler.rb +19 -0
 - data/lib/active_record/relation/predicate_builder/polymorphic_array_value.rb +57 -0
 - data/lib/active_record/relation/predicate_builder/range_handler.rb +22 -0
 - data/lib/active_record/relation/predicate_builder/relation_handler.rb +7 -1
 - data/lib/active_record/relation/predicate_builder.rb +139 -96
 - data/lib/active_record/relation/query_attribute.rb +50 -0
 - data/lib/active_record/relation/query_methods.rb +757 -409
 - data/lib/active_record/relation/record_fetch_warning.rb +51 -0
 - data/lib/active_record/relation/spawn_methods.rb +23 -21
 - data/lib/active_record/relation/where_clause.rb +239 -0
 - data/lib/active_record/relation.rb +554 -342
 - data/lib/active_record/result.rb +91 -47
 - data/lib/active_record/runtime_registry.rb +6 -4
 - data/lib/active_record/sanitization.rb +134 -122
 - data/lib/active_record/schema.rb +21 -24
 - data/lib/active_record/schema_dumper.rb +141 -92
 - data/lib/active_record/schema_migration.rb +24 -26
 - data/lib/active_record/scoping/default.rb +96 -82
 - data/lib/active_record/scoping/named.rb +78 -36
 - data/lib/active_record/scoping.rb +45 -27
 - data/lib/active_record/secure_token.rb +48 -0
 - data/lib/active_record/serialization.rb +8 -6
 - data/lib/active_record/signed_id.rb +116 -0
 - data/lib/active_record/statement_cache.rb +89 -36
 - data/lib/active_record/store.rb +133 -43
 - data/lib/active_record/suppressor.rb +61 -0
 - data/lib/active_record/table_metadata.rb +81 -0
 - data/lib/active_record/tasks/database_tasks.rb +366 -129
 - data/lib/active_record/tasks/mysql_database_tasks.rb +68 -100
 - data/lib/active_record/tasks/postgresql_database_tasks.rb +87 -39
 - data/lib/active_record/tasks/sqlite_database_tasks.rb +44 -19
 - data/lib/active_record/test_databases.rb +24 -0
 - data/lib/active_record/test_fixtures.rb +291 -0
 - data/lib/active_record/timestamp.rb +86 -43
 - data/lib/active_record/touch_later.rb +65 -0
 - data/lib/active_record/transactions.rb +181 -152
 - data/lib/active_record/translation.rb +3 -1
 - data/lib/active_record/type/adapter_specific_registry.rb +126 -0
 - data/lib/active_record/type/date.rb +4 -41
 - data/lib/active_record/type/date_time.rb +4 -38
 - data/lib/active_record/type/decimal_without_scale.rb +6 -2
 - data/lib/active_record/type/hash_lookup_type_map.rb +12 -5
 - data/lib/active_record/type/internal/timezone.rb +17 -0
 - data/lib/active_record/type/json.rb +30 -0
 - data/lib/active_record/type/serialized.rb +33 -15
 - data/lib/active_record/type/text.rb +2 -2
 - data/lib/active_record/type/time.rb +21 -16
 - data/lib/active_record/type/type_map.rb +16 -19
 - data/lib/active_record/type/unsigned_integer.rb +9 -8
 - data/lib/active_record/type.rb +84 -23
 - data/lib/active_record/type_caster/connection.rb +33 -0
 - data/lib/active_record/type_caster/map.rb +23 -0
 - data/lib/active_record/type_caster.rb +9 -0
 - data/lib/active_record/validations/absence.rb +25 -0
 - data/lib/active_record/validations/associated.rb +12 -4
 - data/lib/active_record/validations/length.rb +26 -0
 - data/lib/active_record/validations/numericality.rb +35 -0
 - data/lib/active_record/validations/presence.rb +14 -13
 - data/lib/active_record/validations/uniqueness.rb +65 -48
 - data/lib/active_record/validations.rb +39 -35
 - data/lib/active_record/version.rb +3 -1
 - data/lib/active_record.rb +44 -28
 - data/lib/arel/alias_predication.rb +9 -0
 - data/lib/arel/attributes/attribute.rb +41 -0
 - data/lib/arel/collectors/bind.rb +29 -0
 - data/lib/arel/collectors/composite.rb +39 -0
 - data/lib/arel/collectors/plain_string.rb +20 -0
 - data/lib/arel/collectors/sql_string.rb +27 -0
 - data/lib/arel/collectors/substitute_binds.rb +35 -0
 - data/lib/arel/crud.rb +42 -0
 - data/lib/arel/delete_manager.rb +18 -0
 - data/lib/arel/errors.rb +9 -0
 - data/lib/arel/expressions.rb +29 -0
 - data/lib/arel/factory_methods.rb +49 -0
 - data/lib/arel/insert_manager.rb +49 -0
 - data/lib/arel/math.rb +45 -0
 - data/lib/arel/nodes/and.rb +32 -0
 - data/lib/arel/nodes/ascending.rb +23 -0
 - data/lib/arel/nodes/binary.rb +126 -0
 - data/lib/arel/nodes/bind_param.rb +44 -0
 - data/lib/arel/nodes/case.rb +55 -0
 - data/lib/arel/nodes/casted.rb +62 -0
 - data/lib/arel/nodes/comment.rb +29 -0
 - data/lib/arel/nodes/count.rb +12 -0
 - data/lib/arel/nodes/delete_statement.rb +45 -0
 - data/lib/arel/nodes/descending.rb +23 -0
 - data/lib/arel/nodes/equality.rb +15 -0
 - data/lib/arel/nodes/extract.rb +24 -0
 - data/lib/arel/nodes/false.rb +16 -0
 - data/lib/arel/nodes/full_outer_join.rb +8 -0
 - data/lib/arel/nodes/function.rb +44 -0
 - data/lib/arel/nodes/grouping.rb +11 -0
 - data/lib/arel/nodes/homogeneous_in.rb +76 -0
 - data/lib/arel/nodes/in.rb +15 -0
 - data/lib/arel/nodes/infix_operation.rb +92 -0
 - data/lib/arel/nodes/inner_join.rb +8 -0
 - data/lib/arel/nodes/insert_statement.rb +37 -0
 - data/lib/arel/nodes/join_source.rb +20 -0
 - data/lib/arel/nodes/matches.rb +18 -0
 - data/lib/arel/nodes/named_function.rb +23 -0
 - data/lib/arel/nodes/node.rb +51 -0
 - data/lib/arel/nodes/node_expression.rb +13 -0
 - data/lib/arel/nodes/ordering.rb +27 -0
 - data/lib/arel/nodes/outer_join.rb +8 -0
 - data/lib/arel/nodes/over.rb +15 -0
 - data/lib/arel/nodes/regexp.rb +16 -0
 - data/lib/arel/nodes/right_outer_join.rb +8 -0
 - data/lib/arel/nodes/select_core.rb +67 -0
 - data/lib/arel/nodes/select_statement.rb +41 -0
 - data/lib/arel/nodes/sql_literal.rb +19 -0
 - data/lib/arel/nodes/string_join.rb +11 -0
 - data/lib/arel/nodes/table_alias.rb +31 -0
 - data/lib/arel/nodes/terminal.rb +16 -0
 - data/lib/arel/nodes/true.rb +16 -0
 - data/lib/arel/nodes/unary.rb +44 -0
 - data/lib/arel/nodes/unary_operation.rb +20 -0
 - data/lib/arel/nodes/unqualified_column.rb +22 -0
 - data/lib/arel/nodes/update_statement.rb +41 -0
 - data/lib/arel/nodes/values_list.rb +9 -0
 - data/lib/arel/nodes/window.rb +126 -0
 - data/lib/arel/nodes/with.rb +11 -0
 - data/lib/arel/nodes.rb +70 -0
 - data/lib/arel/order_predications.rb +13 -0
 - data/lib/arel/predications.rb +250 -0
 - data/lib/arel/select_manager.rb +270 -0
 - data/lib/arel/table.rb +118 -0
 - data/lib/arel/tree_manager.rb +72 -0
 - data/lib/arel/update_manager.rb +34 -0
 - data/lib/arel/visitors/dot.rb +308 -0
 - data/lib/arel/visitors/mysql.rb +93 -0
 - data/lib/arel/visitors/postgresql.rb +120 -0
 - data/lib/arel/visitors/sqlite.rb +38 -0
 - data/lib/arel/visitors/to_sql.rb +899 -0
 - data/lib/arel/visitors/visitor.rb +45 -0
 - data/lib/arel/visitors.rb +13 -0
 - data/lib/arel/window_predications.rb +9 -0
 - data/lib/arel.rb +54 -0
 - data/lib/rails/generators/active_record/application_record/application_record_generator.rb +26 -0
 - data/lib/rails/generators/active_record/application_record/templates/application_record.rb.tt +5 -0
 - data/lib/rails/generators/active_record/migration/migration_generator.rb +43 -37
 - data/lib/rails/generators/active_record/migration/templates/create_table_migration.rb.tt +26 -0
 - data/lib/rails/generators/active_record/migration/templates/{migration.rb → migration.rb.tt} +13 -10
 - data/lib/rails/generators/active_record/migration.rb +35 -1
 - data/lib/rails/generators/active_record/model/model_generator.rb +55 -22
 - data/lib/rails/generators/active_record/model/templates/abstract_base_class.rb.tt +7 -0
 - data/lib/rails/generators/active_record/model/templates/model.rb.tt +22 -0
 - data/lib/rails/generators/active_record.rb +7 -5
 - metadata +175 -65
 - data/lib/active_record/associations/preloader/belongs_to.rb +0 -17
 - data/lib/active_record/associations/preloader/collection_association.rb +0 -24
 - 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 -23
 - data/lib/active_record/associations/preloader/has_one_through.rb +0 -9
 - data/lib/active_record/associations/preloader/singular_association.rb +0 -21
 - data/lib/active_record/attribute.rb +0 -149
 - data/lib/active_record/attribute_decorators.rb +0 -66
 - data/lib/active_record/attribute_set/builder.rb +0 -86
 - data/lib/active_record/attribute_set.rb +0 -77
 - data/lib/active_record/connection_adapters/connection_specification.rb +0 -275
 - data/lib/active_record/connection_adapters/mysql_adapter.rb +0 -491
 - data/lib/active_record/connection_adapters/postgresql/array_parser.rb +0 -93
 - data/lib/active_record/connection_adapters/postgresql/oid/float.rb +0 -21
 - data/lib/active_record/connection_adapters/postgresql/oid/integer.rb +0 -11
 - data/lib/active_record/connection_adapters/postgresql/oid/json.rb +0 -35
 - data/lib/active_record/connection_adapters/postgresql/oid/time.rb +0 -11
 - data/lib/active_record/railties/jdbcmysql_error.rb +0 -16
 - data/lib/active_record/serializers/xml_serializer.rb +0 -193
 - data/lib/active_record/type/big_integer.rb +0 -13
 - data/lib/active_record/type/binary.rb +0 -50
 - data/lib/active_record/type/boolean.rb +0 -30
 - data/lib/active_record/type/decimal.rb +0 -40
 - data/lib/active_record/type/decorator.rb +0 -14
 - data/lib/active_record/type/float.rb +0 -19
 - data/lib/active_record/type/integer.rb +0 -55
 - data/lib/active_record/type/mutable.rb +0 -16
 - data/lib/active_record/type/numeric.rb +0 -36
 - data/lib/active_record/type/string.rb +0 -36
 - data/lib/active_record/type/time_value.rb +0 -38
 - data/lib/active_record/type/value.rb +0 -101
 - data/lib/rails/generators/active_record/migration/templates/create_table_migration.rb +0 -22
 - data/lib/rails/generators/active_record/model/templates/model.rb +0 -10
 - /data/lib/rails/generators/active_record/model/templates/{module.rb → module.rb.tt} +0 -0
 
| 
         @@ -1,4 +1,6 @@ 
     | 
|
| 
       1 
     | 
    
         
            -
             
     | 
| 
      
 1 
     | 
    
         
            +
            # frozen_string_literal: true
         
     | 
| 
      
 2 
     | 
    
         
            +
             
     | 
| 
      
 3 
     | 
    
         
            +
            require "active_support/core_ext/string/filters"
         
     | 
| 
       2 
4 
     | 
    
         | 
| 
       3 
5 
     | 
    
         
             
            module ActiveRecord
         
     | 
| 
       4 
6 
     | 
    
         
             
              module Integration
         
     | 
| 
         @@ -7,17 +9,32 @@ module ActiveRecord 
     | 
|
| 
       7 
9 
     | 
    
         
             
                included do
         
     | 
| 
       8 
10 
     | 
    
         
             
                  ##
         
     | 
| 
       9 
11 
     | 
    
         
             
                  # :singleton-method:
         
     | 
| 
       10 
     | 
    
         
            -
                  # Indicates the format used to generate the timestamp in the cache key 
     | 
| 
       11 
     | 
    
         
            -
                  # Accepts any of the symbols in <tt>Time::DATE_FORMATS</tt>.
         
     | 
| 
      
 12 
     | 
    
         
            +
                  # Indicates the format used to generate the timestamp in the cache key, if
         
     | 
| 
      
 13 
     | 
    
         
            +
                  # versioning is off. Accepts any of the symbols in <tt>Time::DATE_FORMATS</tt>.
         
     | 
| 
      
 14 
     | 
    
         
            +
                  #
         
     | 
| 
      
 15 
     | 
    
         
            +
                  # This is +:usec+, by default.
         
     | 
| 
      
 16 
     | 
    
         
            +
                  class_attribute :cache_timestamp_format, instance_writer: false, default: :usec
         
     | 
| 
      
 17 
     | 
    
         
            +
             
     | 
| 
      
 18 
     | 
    
         
            +
                  ##
         
     | 
| 
      
 19 
     | 
    
         
            +
                  # :singleton-method:
         
     | 
| 
      
 20 
     | 
    
         
            +
                  # Indicates whether to use a stable #cache_key method that is accompanied
         
     | 
| 
      
 21 
     | 
    
         
            +
                  # by a changing version in the #cache_version method.
         
     | 
| 
      
 22 
     | 
    
         
            +
                  #
         
     | 
| 
      
 23 
     | 
    
         
            +
                  # This is +true+, by default on Rails 5.2 and above.
         
     | 
| 
      
 24 
     | 
    
         
            +
                  class_attribute :cache_versioning, instance_writer: false, default: false
         
     | 
| 
      
 25 
     | 
    
         
            +
             
     | 
| 
      
 26 
     | 
    
         
            +
                  ##
         
     | 
| 
      
 27 
     | 
    
         
            +
                  # :singleton-method:
         
     | 
| 
      
 28 
     | 
    
         
            +
                  # Indicates whether to use a stable #cache_key method that is accompanied
         
     | 
| 
      
 29 
     | 
    
         
            +
                  # by a changing version in the #cache_version method on collections.
         
     | 
| 
       12 
30 
     | 
    
         
             
                  #
         
     | 
| 
       13 
     | 
    
         
            -
                  # This is  
     | 
| 
       14 
     | 
    
         
            -
                  class_attribute : 
     | 
| 
       15 
     | 
    
         
            -
                  self.cache_timestamp_format = :nsec
         
     | 
| 
      
 31 
     | 
    
         
            +
                  # This is +false+, by default until Rails 6.1.
         
     | 
| 
      
 32 
     | 
    
         
            +
                  class_attribute :collection_cache_versioning, instance_writer: false, default: false
         
     | 
| 
       16 
33 
     | 
    
         
             
                end
         
     | 
| 
       17 
34 
     | 
    
         | 
| 
       18 
     | 
    
         
            -
                # Returns a String 
     | 
| 
       19 
     | 
    
         
            -
                # object. The default implementation returns this record's id as a String 
     | 
| 
       20 
     | 
    
         
            -
                # or nil if this record's unsaved.
         
     | 
| 
      
 35 
     | 
    
         
            +
                # Returns a +String+, which Action Pack uses for constructing a URL to this
         
     | 
| 
      
 36 
     | 
    
         
            +
                # object. The default implementation returns this record's id as a +String+,
         
     | 
| 
      
 37 
     | 
    
         
            +
                # or +nil+ if this record's unsaved.
         
     | 
| 
       21 
38 
     | 
    
         
             
                #
         
     | 
| 
       22 
39 
     | 
    
         
             
                # For example, suppose that you have a User model, and that you have a
         
     | 
| 
       23 
40 
     | 
    
         
             
                # <tt>resources :users</tt> route. Normally, +user_path+ will
         
     | 
| 
         @@ -42,29 +59,62 @@ module ActiveRecord 
     | 
|
| 
       42 
59 
     | 
    
         
             
                  id && id.to_s # Be sure to stringify the id for routes
         
     | 
| 
       43 
60 
     | 
    
         
             
                end
         
     | 
| 
       44 
61 
     | 
    
         | 
| 
       45 
     | 
    
         
            -
                # Returns a cache key that can be used to identify this record.
         
     | 
| 
      
 62 
     | 
    
         
            +
                # Returns a stable cache key that can be used to identify this record.
         
     | 
| 
       46 
63 
     | 
    
         
             
                #
         
     | 
| 
       47 
64 
     | 
    
         
             
                #   Product.new.cache_key     # => "products/new"
         
     | 
| 
       48 
     | 
    
         
            -
                #   Product.find(5).cache_key # => "products/5" 
     | 
| 
       49 
     | 
    
         
            -
                #   Person.find(5).cache_key  # => "people/5-20071224150000" (updated_at available)
         
     | 
| 
      
 65 
     | 
    
         
            +
                #   Product.find(5).cache_key # => "products/5"
         
     | 
| 
       50 
66 
     | 
    
         
             
                #
         
     | 
| 
       51 
     | 
    
         
            -
                #  
     | 
| 
       52 
     | 
    
         
            -
                #  
     | 
| 
      
 67 
     | 
    
         
            +
                # If ActiveRecord::Base.cache_versioning is turned off, as it was in Rails 5.1 and earlier,
         
     | 
| 
      
 68 
     | 
    
         
            +
                # the cache key will also include a version.
         
     | 
| 
       53 
69 
     | 
    
         
             
                #
         
     | 
| 
       54 
     | 
    
         
            -
                #    
     | 
| 
       55 
     | 
    
         
            -
                 
     | 
| 
       56 
     | 
    
         
            -
             
     | 
| 
       57 
     | 
    
         
            -
                   
     | 
| 
      
 70 
     | 
    
         
            +
                #   Product.cache_versioning = false
         
     | 
| 
      
 71 
     | 
    
         
            +
                #   Product.find(5).cache_key  # => "products/5-20071224150000" (updated_at available)
         
     | 
| 
      
 72 
     | 
    
         
            +
                def cache_key
         
     | 
| 
      
 73 
     | 
    
         
            +
                  if new_record?
         
     | 
| 
       58 
74 
     | 
    
         
             
                    "#{model_name.cache_key}/new"
         
     | 
| 
       59 
     | 
    
         
            -
                  when timestamp_names.any?
         
     | 
| 
       60 
     | 
    
         
            -
                    timestamp = max_updated_column_timestamp(timestamp_names)
         
     | 
| 
       61 
     | 
    
         
            -
                    timestamp = timestamp.utc.to_s(cache_timestamp_format)
         
     | 
| 
       62 
     | 
    
         
            -
                    "#{model_name.cache_key}/#{id}-#{timestamp}"
         
     | 
| 
       63 
     | 
    
         
            -
                  when timestamp = max_updated_column_timestamp
         
     | 
| 
       64 
     | 
    
         
            -
                    timestamp = timestamp.utc.to_s(cache_timestamp_format)
         
     | 
| 
       65 
     | 
    
         
            -
                    "#{model_name.cache_key}/#{id}-#{timestamp}"
         
     | 
| 
       66 
75 
     | 
    
         
             
                  else
         
     | 
| 
       67 
     | 
    
         
            -
                     
     | 
| 
      
 76 
     | 
    
         
            +
                    if cache_version
         
     | 
| 
      
 77 
     | 
    
         
            +
                      "#{model_name.cache_key}/#{id}"
         
     | 
| 
      
 78 
     | 
    
         
            +
                    else
         
     | 
| 
      
 79 
     | 
    
         
            +
                      timestamp = max_updated_column_timestamp
         
     | 
| 
      
 80 
     | 
    
         
            +
             
     | 
| 
      
 81 
     | 
    
         
            +
                      if timestamp
         
     | 
| 
      
 82 
     | 
    
         
            +
                        timestamp = timestamp.utc.to_s(cache_timestamp_format)
         
     | 
| 
      
 83 
     | 
    
         
            +
                        "#{model_name.cache_key}/#{id}-#{timestamp}"
         
     | 
| 
      
 84 
     | 
    
         
            +
                      else
         
     | 
| 
      
 85 
     | 
    
         
            +
                        "#{model_name.cache_key}/#{id}"
         
     | 
| 
      
 86 
     | 
    
         
            +
                      end
         
     | 
| 
      
 87 
     | 
    
         
            +
                    end
         
     | 
| 
      
 88 
     | 
    
         
            +
                  end
         
     | 
| 
      
 89 
     | 
    
         
            +
                end
         
     | 
| 
      
 90 
     | 
    
         
            +
             
     | 
| 
      
 91 
     | 
    
         
            +
                # Returns a cache version that can be used together with the cache key to form
         
     | 
| 
      
 92 
     | 
    
         
            +
                # a recyclable caching scheme. By default, the #updated_at column is used for the
         
     | 
| 
      
 93 
     | 
    
         
            +
                # cache_version, but this method can be overwritten to return something else.
         
     | 
| 
      
 94 
     | 
    
         
            +
                #
         
     | 
| 
      
 95 
     | 
    
         
            +
                # Note, this method will return nil if ActiveRecord::Base.cache_versioning is set to
         
     | 
| 
      
 96 
     | 
    
         
            +
                # +false+.
         
     | 
| 
      
 97 
     | 
    
         
            +
                def cache_version
         
     | 
| 
      
 98 
     | 
    
         
            +
                  return unless cache_versioning
         
     | 
| 
      
 99 
     | 
    
         
            +
             
     | 
| 
      
 100 
     | 
    
         
            +
                  if has_attribute?("updated_at")
         
     | 
| 
      
 101 
     | 
    
         
            +
                    timestamp = updated_at_before_type_cast
         
     | 
| 
      
 102 
     | 
    
         
            +
                    if can_use_fast_cache_version?(timestamp)
         
     | 
| 
      
 103 
     | 
    
         
            +
                      raw_timestamp_to_cache_version(timestamp)
         
     | 
| 
      
 104 
     | 
    
         
            +
                    elsif timestamp = updated_at
         
     | 
| 
      
 105 
     | 
    
         
            +
                      timestamp.utc.to_s(cache_timestamp_format)
         
     | 
| 
      
 106 
     | 
    
         
            +
                    end
         
     | 
| 
      
 107 
     | 
    
         
            +
                  elsif self.class.has_attribute?("updated_at")
         
     | 
| 
      
 108 
     | 
    
         
            +
                    raise ActiveModel::MissingAttributeError, "missing attribute: updated_at"
         
     | 
| 
      
 109 
     | 
    
         
            +
                  end
         
     | 
| 
      
 110 
     | 
    
         
            +
                end
         
     | 
| 
      
 111 
     | 
    
         
            +
             
     | 
| 
      
 112 
     | 
    
         
            +
                # Returns a cache key along with the version.
         
     | 
| 
      
 113 
     | 
    
         
            +
                def cache_key_with_version
         
     | 
| 
      
 114 
     | 
    
         
            +
                  if version = cache_version
         
     | 
| 
      
 115 
     | 
    
         
            +
                    "#{cache_key}-#{version}"
         
     | 
| 
      
 116 
     | 
    
         
            +
                  else
         
     | 
| 
      
 117 
     | 
    
         
            +
                    cache_key
         
     | 
| 
       68 
118 
     | 
    
         
             
                  end
         
     | 
| 
       69 
119 
     | 
    
         
             
                end
         
     | 
| 
       70 
120 
     | 
    
         | 
| 
         @@ -84,9 +134,9 @@ module ActiveRecord 
     | 
|
| 
       84 
134 
     | 
    
         
             
                  # Values longer than 20 characters will be truncated. The value
         
     | 
| 
       85 
135 
     | 
    
         
             
                  # is truncated word by word.
         
     | 
| 
       86 
136 
     | 
    
         
             
                  #
         
     | 
| 
       87 
     | 
    
         
            -
                  #   user = User.find_by(name: 'David  
     | 
| 
      
 137 
     | 
    
         
            +
                  #   user = User.find_by(name: 'David Heinemeier Hansson')
         
     | 
| 
       88 
138 
     | 
    
         
             
                  #   user.id         # => 125
         
     | 
| 
       89 
     | 
    
         
            -
                  #   user_path(user) # => "/users/125-david"
         
     | 
| 
      
 139 
     | 
    
         
            +
                  #   user_path(user) # => "/users/125-david-heinemeier"
         
     | 
| 
       90 
140 
     | 
    
         
             
                  #
         
     | 
| 
       91 
141 
     | 
    
         
             
                  # Because the generated param begins with the record's +id+, it is
         
     | 
| 
       92 
142 
     | 
    
         
             
                  # suitable for passing to +find+. In a controller, for example:
         
     | 
| 
         @@ -100,7 +150,7 @@ module ActiveRecord 
     | 
|
| 
       100 
150 
     | 
    
         
             
                      define_method :to_param do
         
     | 
| 
       101 
151 
     | 
    
         
             
                        if (default = super()) &&
         
     | 
| 
       102 
152 
     | 
    
         
             
                             (result = send(method_name).to_s).present? &&
         
     | 
| 
       103 
     | 
    
         
            -
                               (param = result.squish.truncate(20, separator:  
     | 
| 
      
 153 
     | 
    
         
            +
                               (param = result.squish.parameterize.truncate(20, separator: /-/, omission: "")).present?
         
     | 
| 
       104 
154 
     | 
    
         
             
                          "#{default}-#{param}"
         
     | 
| 
       105 
155 
     | 
    
         
             
                        else
         
     | 
| 
       106 
156 
     | 
    
         
             
                          default
         
     | 
| 
         @@ -108,6 +158,48 @@ module ActiveRecord 
     | 
|
| 
       108 
158 
     | 
    
         
             
                      end
         
     | 
| 
       109 
159 
     | 
    
         
             
                    end
         
     | 
| 
       110 
160 
     | 
    
         
             
                  end
         
     | 
| 
      
 161 
     | 
    
         
            +
             
     | 
| 
      
 162 
     | 
    
         
            +
                  def collection_cache_key(collection = all, timestamp_column = :updated_at) # :nodoc:
         
     | 
| 
      
 163 
     | 
    
         
            +
                    collection.send(:compute_cache_key, timestamp_column)
         
     | 
| 
      
 164 
     | 
    
         
            +
                  end
         
     | 
| 
       111 
165 
     | 
    
         
             
                end
         
     | 
| 
      
 166 
     | 
    
         
            +
             
     | 
| 
      
 167 
     | 
    
         
            +
                private
         
     | 
| 
      
 168 
     | 
    
         
            +
                  # Detects if the value before type cast
         
     | 
| 
      
 169 
     | 
    
         
            +
                  # can be used to generate a cache_version.
         
     | 
| 
      
 170 
     | 
    
         
            +
                  #
         
     | 
| 
      
 171 
     | 
    
         
            +
                  # The fast cache version only works with a
         
     | 
| 
      
 172 
     | 
    
         
            +
                  # string value directly from the database.
         
     | 
| 
      
 173 
     | 
    
         
            +
                  #
         
     | 
| 
      
 174 
     | 
    
         
            +
                  # We also must check if the timestamp format has been changed
         
     | 
| 
      
 175 
     | 
    
         
            +
                  # or if the timezone is not set to UTC then
         
     | 
| 
      
 176 
     | 
    
         
            +
                  # we cannot apply our transformations correctly.
         
     | 
| 
      
 177 
     | 
    
         
            +
                  def can_use_fast_cache_version?(timestamp)
         
     | 
| 
      
 178 
     | 
    
         
            +
                    timestamp.is_a?(String) &&
         
     | 
| 
      
 179 
     | 
    
         
            +
                      cache_timestamp_format == :usec &&
         
     | 
| 
      
 180 
     | 
    
         
            +
                      default_timezone == :utc &&
         
     | 
| 
      
 181 
     | 
    
         
            +
                      !updated_at_came_from_user?
         
     | 
| 
      
 182 
     | 
    
         
            +
                  end
         
     | 
| 
      
 183 
     | 
    
         
            +
             
     | 
| 
      
 184 
     | 
    
         
            +
                  # Converts a raw database string to `:usec`
         
     | 
| 
      
 185 
     | 
    
         
            +
                  # format.
         
     | 
| 
      
 186 
     | 
    
         
            +
                  #
         
     | 
| 
      
 187 
     | 
    
         
            +
                  # Example:
         
     | 
| 
      
 188 
     | 
    
         
            +
                  #
         
     | 
| 
      
 189 
     | 
    
         
            +
                  #   timestamp = "2018-10-15 20:02:15.266505"
         
     | 
| 
      
 190 
     | 
    
         
            +
                  #   raw_timestamp_to_cache_version(timestamp)
         
     | 
| 
      
 191 
     | 
    
         
            +
                  #   # => "20181015200215266505"
         
     | 
| 
      
 192 
     | 
    
         
            +
                  #
         
     | 
| 
      
 193 
     | 
    
         
            +
                  # PostgreSQL truncates trailing zeros,
         
     | 
| 
      
 194 
     | 
    
         
            +
                  # https://github.com/postgres/postgres/commit/3e1beda2cde3495f41290e1ece5d544525810214
         
     | 
| 
      
 195 
     | 
    
         
            +
                  # to account for this we pad the output with zeros
         
     | 
| 
      
 196 
     | 
    
         
            +
                  def raw_timestamp_to_cache_version(timestamp)
         
     | 
| 
      
 197 
     | 
    
         
            +
                    key = timestamp.delete("- :.")
         
     | 
| 
      
 198 
     | 
    
         
            +
                    if key.length < 20
         
     | 
| 
      
 199 
     | 
    
         
            +
                      key.ljust(20, "0")
         
     | 
| 
      
 200 
     | 
    
         
            +
                    else
         
     | 
| 
      
 201 
     | 
    
         
            +
                      key
         
     | 
| 
      
 202 
     | 
    
         
            +
                    end
         
     | 
| 
      
 203 
     | 
    
         
            +
                  end
         
     | 
| 
       112 
204 
     | 
    
         
             
              end
         
     | 
| 
       113 
205 
     | 
    
         
             
            end
         
     | 
| 
         @@ -0,0 +1,64 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            # frozen_string_literal: true
         
     | 
| 
      
 2 
     | 
    
         
            +
             
     | 
| 
      
 3 
     | 
    
         
            +
            require "active_record/scoping/default"
         
     | 
| 
      
 4 
     | 
    
         
            +
            require "active_record/scoping/named"
         
     | 
| 
      
 5 
     | 
    
         
            +
             
     | 
| 
      
 6 
     | 
    
         
            +
            module ActiveRecord
         
     | 
| 
      
 7 
     | 
    
         
            +
              # This class is used to create a table that keeps track of values and keys such
         
     | 
| 
      
 8 
     | 
    
         
            +
              # as which environment migrations were run in.
         
     | 
| 
      
 9 
     | 
    
         
            +
              #
         
     | 
| 
      
 10 
     | 
    
         
            +
              # This is enabled by default. To disable this functionality set
         
     | 
| 
      
 11 
     | 
    
         
            +
              # `use_metadata_table` to false in your database configuration.
         
     | 
| 
      
 12 
     | 
    
         
            +
              class InternalMetadata < ActiveRecord::Base # :nodoc:
         
     | 
| 
      
 13 
     | 
    
         
            +
                self.record_timestamps = true
         
     | 
| 
      
 14 
     | 
    
         
            +
             
     | 
| 
      
 15 
     | 
    
         
            +
                class << self
         
     | 
| 
      
 16 
     | 
    
         
            +
                  def enabled?
         
     | 
| 
      
 17 
     | 
    
         
            +
                    ActiveRecord::Base.connection.use_metadata_table?
         
     | 
| 
      
 18 
     | 
    
         
            +
                  end
         
     | 
| 
      
 19 
     | 
    
         
            +
             
     | 
| 
      
 20 
     | 
    
         
            +
                  def _internal?
         
     | 
| 
      
 21 
     | 
    
         
            +
                    true
         
     | 
| 
      
 22 
     | 
    
         
            +
                  end
         
     | 
| 
      
 23 
     | 
    
         
            +
             
     | 
| 
      
 24 
     | 
    
         
            +
                  def primary_key
         
     | 
| 
      
 25 
     | 
    
         
            +
                    "key"
         
     | 
| 
      
 26 
     | 
    
         
            +
                  end
         
     | 
| 
      
 27 
     | 
    
         
            +
             
     | 
| 
      
 28 
     | 
    
         
            +
                  def table_name
         
     | 
| 
      
 29 
     | 
    
         
            +
                    "#{table_name_prefix}#{internal_metadata_table_name}#{table_name_suffix}"
         
     | 
| 
      
 30 
     | 
    
         
            +
                  end
         
     | 
| 
      
 31 
     | 
    
         
            +
             
     | 
| 
      
 32 
     | 
    
         
            +
                  def []=(key, value)
         
     | 
| 
      
 33 
     | 
    
         
            +
                    return unless enabled?
         
     | 
| 
      
 34 
     | 
    
         
            +
             
     | 
| 
      
 35 
     | 
    
         
            +
                    find_or_initialize_by(key: key).update!(value: value)
         
     | 
| 
      
 36 
     | 
    
         
            +
                  end
         
     | 
| 
      
 37 
     | 
    
         
            +
             
     | 
| 
      
 38 
     | 
    
         
            +
                  def [](key)
         
     | 
| 
      
 39 
     | 
    
         
            +
                    return unless enabled?
         
     | 
| 
      
 40 
     | 
    
         
            +
             
     | 
| 
      
 41 
     | 
    
         
            +
                    where(key: key).pluck(:value).first
         
     | 
| 
      
 42 
     | 
    
         
            +
                  end
         
     | 
| 
      
 43 
     | 
    
         
            +
             
     | 
| 
      
 44 
     | 
    
         
            +
                  # Creates an internal metadata table with columns +key+ and +value+
         
     | 
| 
      
 45 
     | 
    
         
            +
                  def create_table
         
     | 
| 
      
 46 
     | 
    
         
            +
                    return unless enabled?
         
     | 
| 
      
 47 
     | 
    
         
            +
             
     | 
| 
      
 48 
     | 
    
         
            +
                    unless connection.table_exists?(table_name)
         
     | 
| 
      
 49 
     | 
    
         
            +
                      connection.create_table(table_name, id: false) do |t|
         
     | 
| 
      
 50 
     | 
    
         
            +
                        t.string :key, **connection.internal_string_options_for_primary_key
         
     | 
| 
      
 51 
     | 
    
         
            +
                        t.string :value
         
     | 
| 
      
 52 
     | 
    
         
            +
                        t.timestamps
         
     | 
| 
      
 53 
     | 
    
         
            +
                      end
         
     | 
| 
      
 54 
     | 
    
         
            +
                    end
         
     | 
| 
      
 55 
     | 
    
         
            +
                  end
         
     | 
| 
      
 56 
     | 
    
         
            +
             
     | 
| 
      
 57 
     | 
    
         
            +
                  def drop_table
         
     | 
| 
      
 58 
     | 
    
         
            +
                    return unless enabled?
         
     | 
| 
      
 59 
     | 
    
         
            +
             
     | 
| 
      
 60 
     | 
    
         
            +
                    connection.drop_table table_name, if_exists: true
         
     | 
| 
      
 61 
     | 
    
         
            +
                  end
         
     | 
| 
      
 62 
     | 
    
         
            +
                end
         
     | 
| 
      
 63 
     | 
    
         
            +
              end
         
     | 
| 
      
 64 
     | 
    
         
            +
            end
         
     | 
| 
         @@ -0,0 +1,52 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            # frozen_string_literal: true
         
     | 
| 
      
 2 
     | 
    
         
            +
             
     | 
| 
      
 3 
     | 
    
         
            +
            module ActiveRecord
         
     | 
| 
      
 4 
     | 
    
         
            +
              module LegacyYamlAdapter # :nodoc:
         
     | 
| 
      
 5 
     | 
    
         
            +
                def self.convert(klass, coder)
         
     | 
| 
      
 6 
     | 
    
         
            +
                  return coder unless coder.is_a?(Psych::Coder)
         
     | 
| 
      
 7 
     | 
    
         
            +
             
     | 
| 
      
 8 
     | 
    
         
            +
                  case coder["active_record_yaml_version"]
         
     | 
| 
      
 9 
     | 
    
         
            +
                  when 1, 2 then coder
         
     | 
| 
      
 10 
     | 
    
         
            +
                  else
         
     | 
| 
      
 11 
     | 
    
         
            +
                    ActiveSupport::Deprecation.warn(<<-MSG.squish)
         
     | 
| 
      
 12 
     | 
    
         
            +
                      YAML loading from legacy format older than Rails 5.0 is deprecated
         
     | 
| 
      
 13 
     | 
    
         
            +
                      and will be removed in Rails 7.0.
         
     | 
| 
      
 14 
     | 
    
         
            +
                    MSG
         
     | 
| 
      
 15 
     | 
    
         
            +
                    if coder["attributes"].is_a?(ActiveModel::AttributeSet)
         
     | 
| 
      
 16 
     | 
    
         
            +
                      Rails420.convert(klass, coder)
         
     | 
| 
      
 17 
     | 
    
         
            +
                    else
         
     | 
| 
      
 18 
     | 
    
         
            +
                      Rails41.convert(klass, coder)
         
     | 
| 
      
 19 
     | 
    
         
            +
                    end
         
     | 
| 
      
 20 
     | 
    
         
            +
                  end
         
     | 
| 
      
 21 
     | 
    
         
            +
                end
         
     | 
| 
      
 22 
     | 
    
         
            +
             
     | 
| 
      
 23 
     | 
    
         
            +
                module Rails420 # :nodoc:
         
     | 
| 
      
 24 
     | 
    
         
            +
                  def self.convert(klass, coder)
         
     | 
| 
      
 25 
     | 
    
         
            +
                    attribute_set = coder["attributes"]
         
     | 
| 
      
 26 
     | 
    
         
            +
             
     | 
| 
      
 27 
     | 
    
         
            +
                    klass.attribute_names.each do |attr_name|
         
     | 
| 
      
 28 
     | 
    
         
            +
                      attribute = attribute_set[attr_name]
         
     | 
| 
      
 29 
     | 
    
         
            +
                      if attribute.type.is_a?(Delegator)
         
     | 
| 
      
 30 
     | 
    
         
            +
                        type_from_klass = klass.type_for_attribute(attr_name)
         
     | 
| 
      
 31 
     | 
    
         
            +
                        attribute_set[attr_name] = attribute.with_type(type_from_klass)
         
     | 
| 
      
 32 
     | 
    
         
            +
                      end
         
     | 
| 
      
 33 
     | 
    
         
            +
                    end
         
     | 
| 
      
 34 
     | 
    
         
            +
             
     | 
| 
      
 35 
     | 
    
         
            +
                    coder
         
     | 
| 
      
 36 
     | 
    
         
            +
                  end
         
     | 
| 
      
 37 
     | 
    
         
            +
                end
         
     | 
| 
      
 38 
     | 
    
         
            +
             
     | 
| 
      
 39 
     | 
    
         
            +
                module Rails41 # :nodoc:
         
     | 
| 
      
 40 
     | 
    
         
            +
                  def self.convert(klass, coder)
         
     | 
| 
      
 41 
     | 
    
         
            +
                    attributes = klass.attributes_builder
         
     | 
| 
      
 42 
     | 
    
         
            +
                      .build_from_database(coder["attributes"])
         
     | 
| 
      
 43 
     | 
    
         
            +
                    new_record = coder["attributes"][klass.primary_key].blank?
         
     | 
| 
      
 44 
     | 
    
         
            +
             
     | 
| 
      
 45 
     | 
    
         
            +
                    {
         
     | 
| 
      
 46 
     | 
    
         
            +
                      "attributes" => attributes,
         
     | 
| 
      
 47 
     | 
    
         
            +
                      "new_record" => new_record,
         
     | 
| 
      
 48 
     | 
    
         
            +
                    }
         
     | 
| 
      
 49 
     | 
    
         
            +
                  end
         
     | 
| 
      
 50 
     | 
    
         
            +
                end
         
     | 
| 
      
 51 
     | 
    
         
            +
              end
         
     | 
| 
      
 52 
     | 
    
         
            +
            end
         
     | 
| 
         @@ -7,6 +7,7 @@ en: 
     | 
|
| 
       7 
7 
     | 
    
         
             
              # Default error messages
         
     | 
| 
       8 
8 
     | 
    
         
             
              errors:
         
     | 
| 
       9 
9 
     | 
    
         
             
                messages:
         
     | 
| 
      
 10 
     | 
    
         
            +
                  required: "must exist"
         
     | 
| 
       10 
11 
     | 
    
         
             
                  taken: "has already been taken"
         
     | 
| 
       11 
12 
     | 
    
         | 
| 
       12 
13 
     | 
    
         
             
              # Active Record models configuration
         
     | 
| 
         @@ -15,8 +16,8 @@ en: 
     | 
|
| 
       15 
16 
     | 
    
         
             
                  messages:
         
     | 
| 
       16 
17 
     | 
    
         
             
                    record_invalid: "Validation failed: %{errors}"
         
     | 
| 
       17 
18 
     | 
    
         
             
                    restrict_dependent_destroy:
         
     | 
| 
       18 
     | 
    
         
            -
                       
     | 
| 
       19 
     | 
    
         
            -
                       
     | 
| 
      
 19 
     | 
    
         
            +
                      has_one: "Cannot delete record because a dependent %{record} exists"
         
     | 
| 
      
 20 
     | 
    
         
            +
                      has_many: "Cannot delete record because dependent %{record} exist"
         
     | 
| 
       20 
21 
     | 
    
         
             
                    # Append your own errors here or at the model/attributes scope.
         
     | 
| 
       21 
22 
     | 
    
         | 
| 
       22 
23 
     | 
    
         
             
                  # You can define own errors for models or model attributes.
         
     | 
| 
         @@ -1,3 +1,5 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            # frozen_string_literal: true
         
     | 
| 
      
 2 
     | 
    
         
            +
             
     | 
| 
       1 
3 
     | 
    
         
             
            module ActiveRecord
         
     | 
| 
       2 
4 
     | 
    
         
             
              module Locking
         
     | 
| 
       3 
5 
     | 
    
         
             
                # == What is Optimistic Locking
         
     | 
| 
         @@ -11,7 +13,7 @@ module ActiveRecord 
     | 
|
| 
       11 
13 
     | 
    
         
             
                #
         
     | 
| 
       12 
14 
     | 
    
         
             
                # == Usage
         
     | 
| 
       13 
15 
     | 
    
         
             
                #
         
     | 
| 
       14 
     | 
    
         
            -
                # Active  
     | 
| 
      
 16 
     | 
    
         
            +
                # Active Record supports optimistic locking if the +lock_version+ field is present. Each update to the
         
     | 
| 
       15 
17 
     | 
    
         
             
                # record increments the +lock_version+ column and the locking facilities ensure that records instantiated twice
         
     | 
| 
       16 
18 
     | 
    
         
             
                # will let the last one saved raise a +StaleObjectError+ if the first was also updated. Example:
         
     | 
| 
       17 
19 
     | 
    
         
             
                #
         
     | 
| 
         @@ -22,7 +24,7 @@ module ActiveRecord 
     | 
|
| 
       22 
24 
     | 
    
         
             
                #   p1.save
         
     | 
| 
       23 
25 
     | 
    
         
             
                #
         
     | 
| 
       24 
26 
     | 
    
         
             
                #   p2.first_name = "should fail"
         
     | 
| 
       25 
     | 
    
         
            -
                #   p2.save # Raises  
     | 
| 
      
 27 
     | 
    
         
            +
                #   p2.save # Raises an ActiveRecord::StaleObjectError
         
     | 
| 
       26 
28 
     | 
    
         
             
                #
         
     | 
| 
       27 
29 
     | 
    
         
             
                # Optimistic locking will also check for stale data when objects are destroyed. Example:
         
     | 
| 
       28 
30 
     | 
    
         
             
                #
         
     | 
| 
         @@ -32,7 +34,7 @@ module ActiveRecord 
     | 
|
| 
       32 
34 
     | 
    
         
             
                #   p1.first_name = "Michael"
         
     | 
| 
       33 
35 
     | 
    
         
             
                #   p1.save
         
     | 
| 
       34 
36 
     | 
    
         
             
                #
         
     | 
| 
       35 
     | 
    
         
            -
                #   p2.destroy # Raises  
     | 
| 
      
 37 
     | 
    
         
            +
                #   p2.destroy # Raises an ActiveRecord::StaleObjectError
         
     | 
| 
       36 
38 
     | 
    
         
             
                #
         
     | 
| 
       37 
39 
     | 
    
         
             
                # You're then responsible for dealing with the conflict by rescuing the exception and either rolling back, merging,
         
     | 
| 
       38 
40 
     | 
    
         
             
                # or otherwise apply the business logic needed to resolve the conflict.
         
     | 
| 
         @@ -51,153 +53,159 @@ module ActiveRecord 
     | 
|
| 
       51 
53 
     | 
    
         
             
                  extend ActiveSupport::Concern
         
     | 
| 
       52 
54 
     | 
    
         | 
| 
       53 
55 
     | 
    
         
             
                  included do
         
     | 
| 
       54 
     | 
    
         
            -
                    class_attribute :lock_optimistically, instance_writer: false
         
     | 
| 
       55 
     | 
    
         
            -
                    self.lock_optimistically = true
         
     | 
| 
      
 56 
     | 
    
         
            +
                    class_attribute :lock_optimistically, instance_writer: false, default: true
         
     | 
| 
       56 
57 
     | 
    
         
             
                  end
         
     | 
| 
       57 
58 
     | 
    
         | 
| 
       58 
59 
     | 
    
         
             
                  def locking_enabled? #:nodoc:
         
     | 
| 
       59 
60 
     | 
    
         
             
                    self.class.locking_enabled?
         
     | 
| 
       60 
61 
     | 
    
         
             
                  end
         
     | 
| 
       61 
62 
     | 
    
         | 
| 
      
 63 
     | 
    
         
            +
                  def increment!(*, **) #:nodoc:
         
     | 
| 
      
 64 
     | 
    
         
            +
                    super.tap do
         
     | 
| 
      
 65 
     | 
    
         
            +
                      if locking_enabled?
         
     | 
| 
      
 66 
     | 
    
         
            +
                        self[self.class.locking_column] += 1
         
     | 
| 
      
 67 
     | 
    
         
            +
                        clear_attribute_change(self.class.locking_column)
         
     | 
| 
      
 68 
     | 
    
         
            +
                      end
         
     | 
| 
      
 69 
     | 
    
         
            +
                    end
         
     | 
| 
      
 70 
     | 
    
         
            +
                  end
         
     | 
| 
      
 71 
     | 
    
         
            +
             
     | 
| 
       62 
72 
     | 
    
         
             
                  private
         
     | 
| 
       63 
     | 
    
         
            -
                    def  
     | 
| 
       64 
     | 
    
         
            -
                       
     | 
| 
       65 
     | 
    
         
            -
             
     | 
| 
       66 
     | 
    
         
            -
             
     | 
| 
      
 73 
     | 
    
         
            +
                    def _create_record(attribute_names = self.attribute_names)
         
     | 
| 
      
 74 
     | 
    
         
            +
                      if locking_enabled?
         
     | 
| 
      
 75 
     | 
    
         
            +
                        # We always want to persist the locking version, even if we don't detect
         
     | 
| 
      
 76 
     | 
    
         
            +
                        # a change from the default, since the database might have no default
         
     | 
| 
      
 77 
     | 
    
         
            +
                        attribute_names |= [self.class.locking_column]
         
     | 
| 
      
 78 
     | 
    
         
            +
                      end
         
     | 
| 
      
 79 
     | 
    
         
            +
                      super
         
     | 
| 
      
 80 
     | 
    
         
            +
                    end
         
     | 
| 
      
 81 
     | 
    
         
            +
             
     | 
| 
      
 82 
     | 
    
         
            +
                    def _touch_row(attribute_names, time)
         
     | 
| 
      
 83 
     | 
    
         
            +
                      @_touch_attr_names << self.class.locking_column if locking_enabled?
         
     | 
| 
      
 84 
     | 
    
         
            +
                      super
         
     | 
| 
       67 
85 
     | 
    
         
             
                    end
         
     | 
| 
       68 
86 
     | 
    
         | 
| 
       69 
     | 
    
         
            -
                    def  
     | 
| 
      
 87 
     | 
    
         
            +
                    def _update_row(attribute_names, attempted_action = "update")
         
     | 
| 
       70 
88 
     | 
    
         
             
                      return super unless locking_enabled?
         
     | 
| 
       71 
     | 
    
         
            -
                      return 0 if attribute_names.empty?
         
     | 
| 
       72 
89 
     | 
    
         | 
| 
       73 
     | 
    
         
            -
                       
     | 
| 
       74 
     | 
    
         
            -
             
     | 
| 
       75 
     | 
    
         
            -
             
     | 
| 
      
 90 
     | 
    
         
            +
                      begin
         
     | 
| 
      
 91 
     | 
    
         
            +
                        locking_column = self.class.locking_column
         
     | 
| 
      
 92 
     | 
    
         
            +
                        lock_attribute_was = @attributes[locking_column]
         
     | 
| 
      
 93 
     | 
    
         
            +
                        lock_value_for_database = _lock_value_for_database(locking_column)
         
     | 
| 
       76 
94 
     | 
    
         | 
| 
       77 
     | 
    
         
            -
             
     | 
| 
       78 
     | 
    
         
            -
             
     | 
| 
      
 95 
     | 
    
         
            +
                        attribute_names = attribute_names.dup if attribute_names.frozen?
         
     | 
| 
      
 96 
     | 
    
         
            +
                        attribute_names << locking_column
         
     | 
| 
       79 
97 
     | 
    
         | 
| 
       80 
     | 
    
         
            -
             
     | 
| 
       81 
     | 
    
         
            -
                        relation = self.class.unscoped
         
     | 
| 
       82 
     | 
    
         
            -
             
     | 
| 
       83 
     | 
    
         
            -
                        stmt = relation.where(
         
     | 
| 
       84 
     | 
    
         
            -
                          relation.table[self.class.primary_key].eq(id).and(
         
     | 
| 
       85 
     | 
    
         
            -
                            relation.table[lock_col].eq(self.class.quote_value(previous_lock_value, column_for_attribute(lock_col)))
         
     | 
| 
       86 
     | 
    
         
            -
                          )
         
     | 
| 
       87 
     | 
    
         
            -
                        ).arel.compile_update(
         
     | 
| 
       88 
     | 
    
         
            -
                          arel_attributes_with_values_for_update(attribute_names),
         
     | 
| 
       89 
     | 
    
         
            -
                          self.class.primary_key
         
     | 
| 
       90 
     | 
    
         
            -
                        )
         
     | 
| 
      
 98 
     | 
    
         
            +
                        self[locking_column] += 1
         
     | 
| 
       91 
99 
     | 
    
         | 
| 
       92 
     | 
    
         
            -
                        affected_rows = self.class. 
     | 
| 
      
 100 
     | 
    
         
            +
                        affected_rows = self.class._update_record(
         
     | 
| 
      
 101 
     | 
    
         
            +
                          attributes_with_values(attribute_names),
         
     | 
| 
      
 102 
     | 
    
         
            +
                          @primary_key => id_in_database,
         
     | 
| 
      
 103 
     | 
    
         
            +
                          locking_column => lock_value_for_database
         
     | 
| 
      
 104 
     | 
    
         
            +
                        )
         
     | 
| 
       93 
105 
     | 
    
         | 
| 
       94 
     | 
    
         
            -
                         
     | 
| 
       95 
     | 
    
         
            -
                          raise ActiveRecord::StaleObjectError.new(self,  
     | 
| 
      
 106 
     | 
    
         
            +
                        if affected_rows != 1
         
     | 
| 
      
 107 
     | 
    
         
            +
                          raise ActiveRecord::StaleObjectError.new(self, attempted_action)
         
     | 
| 
       96 
108 
     | 
    
         
             
                        end
         
     | 
| 
       97 
109 
     | 
    
         | 
| 
       98 
110 
     | 
    
         
             
                        affected_rows
         
     | 
| 
       99 
111 
     | 
    
         | 
| 
       100 
     | 
    
         
            -
                      # If something went wrong, revert the  
     | 
| 
      
 112 
     | 
    
         
            +
                      # If something went wrong, revert the locking_column value.
         
     | 
| 
       101 
113 
     | 
    
         
             
                      rescue Exception
         
     | 
| 
       102 
     | 
    
         
            -
                         
     | 
| 
      
 114 
     | 
    
         
            +
                        @attributes[locking_column] = lock_attribute_was
         
     | 
| 
       103 
115 
     | 
    
         
             
                        raise
         
     | 
| 
       104 
116 
     | 
    
         
             
                      end
         
     | 
| 
       105 
117 
     | 
    
         
             
                    end
         
     | 
| 
       106 
118 
     | 
    
         | 
| 
       107 
119 
     | 
    
         
             
                    def destroy_row
         
     | 
| 
       108 
     | 
    
         
            -
                       
     | 
| 
      
 120 
     | 
    
         
            +
                      return super unless locking_enabled?
         
     | 
| 
      
 121 
     | 
    
         
            +
             
     | 
| 
      
 122 
     | 
    
         
            +
                      locking_column = self.class.locking_column
         
     | 
| 
       109 
123 
     | 
    
         | 
| 
       110 
     | 
    
         
            -
                       
     | 
| 
      
 124 
     | 
    
         
            +
                      affected_rows = self.class._delete_record(
         
     | 
| 
      
 125 
     | 
    
         
            +
                        @primary_key => id_in_database,
         
     | 
| 
      
 126 
     | 
    
         
            +
                        locking_column => _lock_value_for_database(locking_column)
         
     | 
| 
      
 127 
     | 
    
         
            +
                      )
         
     | 
| 
      
 128 
     | 
    
         
            +
             
     | 
| 
      
 129 
     | 
    
         
            +
                      if affected_rows != 1
         
     | 
| 
       111 
130 
     | 
    
         
             
                        raise ActiveRecord::StaleObjectError.new(self, "destroy")
         
     | 
| 
       112 
131 
     | 
    
         
             
                      end
         
     | 
| 
       113 
132 
     | 
    
         | 
| 
       114 
133 
     | 
    
         
             
                      affected_rows
         
     | 
| 
       115 
134 
     | 
    
         
             
                    end
         
     | 
| 
       116 
135 
     | 
    
         | 
| 
       117 
     | 
    
         
            -
                    def  
     | 
| 
       118 
     | 
    
         
            -
                       
     | 
| 
       119 
     | 
    
         
            -
             
     | 
| 
       120 
     | 
    
         
            -
                       
     | 
| 
       121 
     | 
    
         
            -
                         
     | 
| 
       122 
     | 
    
         
            -
                        column      = self.class.columns_hash[column_name]
         
     | 
| 
       123 
     | 
    
         
            -
                        substitute  = self.class.connection.substitute_at(column)
         
     | 
| 
       124 
     | 
    
         
            -
             
     | 
| 
       125 
     | 
    
         
            -
                        relation = relation.where(self.class.arel_table[column_name].eq(substitute))
         
     | 
| 
       126 
     | 
    
         
            -
                        relation.bind_values << [column, self[column_name].to_i]
         
     | 
| 
      
 136 
     | 
    
         
            +
                    def _lock_value_for_database(locking_column)
         
     | 
| 
      
 137 
     | 
    
         
            +
                      if will_save_change_to_attribute?(locking_column)
         
     | 
| 
      
 138 
     | 
    
         
            +
                        @attributes[locking_column].value_for_database
         
     | 
| 
      
 139 
     | 
    
         
            +
                      else
         
     | 
| 
      
 140 
     | 
    
         
            +
                        @attributes[locking_column].original_value_for_database
         
     | 
| 
       127 
141 
     | 
    
         
             
                      end
         
     | 
| 
       128 
     | 
    
         
            -
             
     | 
| 
       129 
     | 
    
         
            -
                      relation
         
     | 
| 
       130 
142 
     | 
    
         
             
                    end
         
     | 
| 
       131 
143 
     | 
    
         | 
| 
       132 
     | 
    
         
            -
             
     | 
| 
       133 
     | 
    
         
            -
             
     | 
| 
      
 144 
     | 
    
         
            +
                    module ClassMethods
         
     | 
| 
      
 145 
     | 
    
         
            +
                      DEFAULT_LOCKING_COLUMN = "lock_version"
         
     | 
| 
       134 
146 
     | 
    
         | 
| 
       135 
     | 
    
         
            -
             
     | 
| 
       136 
     | 
    
         
            -
             
     | 
| 
       137 
     | 
    
         
            -
             
     | 
| 
       138 
     | 
    
         
            -
             
     | 
| 
       139 
     | 
    
         
            -
             
     | 
| 
       140 
     | 
    
         
            -
             
     | 
| 
      
 147 
     | 
    
         
            +
                      # Returns true if the +lock_optimistically+ flag is set to true
         
     | 
| 
      
 148 
     | 
    
         
            +
                      # (which it is, by default) and the table includes the
         
     | 
| 
      
 149 
     | 
    
         
            +
                      # +locking_column+ column (defaults to +lock_version+).
         
     | 
| 
      
 150 
     | 
    
         
            +
                      def locking_enabled?
         
     | 
| 
      
 151 
     | 
    
         
            +
                        lock_optimistically && columns_hash[locking_column]
         
     | 
| 
      
 152 
     | 
    
         
            +
                      end
         
     | 
| 
       141 
153 
     | 
    
         | 
| 
       142 
     | 
    
         
            -
             
     | 
| 
       143 
     | 
    
         
            -
             
     | 
| 
       144 
     | 
    
         
            -
             
     | 
| 
       145 
     | 
    
         
            -
             
     | 
| 
       146 
     | 
    
         
            -
             
     | 
| 
      
 154 
     | 
    
         
            +
                      # Set the column to use for optimistic locking. Defaults to +lock_version+.
         
     | 
| 
      
 155 
     | 
    
         
            +
                      def locking_column=(value)
         
     | 
| 
      
 156 
     | 
    
         
            +
                        reload_schema_from_cache
         
     | 
| 
      
 157 
     | 
    
         
            +
                        @locking_column = value.to_s
         
     | 
| 
      
 158 
     | 
    
         
            +
                      end
         
     | 
| 
       147 
159 
     | 
    
         | 
| 
       148 
     | 
    
         
            -
             
     | 
| 
       149 
     | 
    
         
            -
             
     | 
| 
       150 
     | 
    
         
            -
             
     | 
| 
       151 
     | 
    
         
            -
             
     | 
| 
       152 
     | 
    
         
            -
             
     | 
| 
      
 160 
     | 
    
         
            +
                      # The version column used for optimistic locking. Defaults to +lock_version+.
         
     | 
| 
      
 161 
     | 
    
         
            +
                      def locking_column
         
     | 
| 
      
 162 
     | 
    
         
            +
                        @locking_column = DEFAULT_LOCKING_COLUMN unless defined?(@locking_column)
         
     | 
| 
      
 163 
     | 
    
         
            +
                        @locking_column
         
     | 
| 
      
 164 
     | 
    
         
            +
                      end
         
     | 
| 
       153 
165 
     | 
    
         | 
| 
       154 
     | 
    
         
            -
             
     | 
| 
       155 
     | 
    
         
            -
             
     | 
| 
       156 
     | 
    
         
            -
             
     | 
| 
       157 
     | 
    
         
            -
             
     | 
| 
      
 166 
     | 
    
         
            +
                      # Reset the column used for optimistic locking back to the +lock_version+ default.
         
     | 
| 
      
 167 
     | 
    
         
            +
                      def reset_locking_column
         
     | 
| 
      
 168 
     | 
    
         
            +
                        self.locking_column = DEFAULT_LOCKING_COLUMN
         
     | 
| 
      
 169 
     | 
    
         
            +
                      end
         
     | 
| 
       158 
170 
     | 
    
         | 
| 
       159 
     | 
    
         
            -
             
     | 
| 
       160 
     | 
    
         
            -
             
     | 
| 
       161 
     | 
    
         
            -
             
     | 
| 
       162 
     | 
    
         
            -
             
     | 
| 
       163 
     | 
    
         
            -
             
     | 
| 
       164 
     | 
    
         
            -
             
     | 
| 
      
 171 
     | 
    
         
            +
                      # Make sure the lock version column gets updated when counters are
         
     | 
| 
      
 172 
     | 
    
         
            +
                      # updated.
         
     | 
| 
      
 173 
     | 
    
         
            +
                      def update_counters(id, counters)
         
     | 
| 
      
 174 
     | 
    
         
            +
                        counters = counters.merge(locking_column => 1) if locking_enabled?
         
     | 
| 
      
 175 
     | 
    
         
            +
                        super
         
     | 
| 
      
 176 
     | 
    
         
            +
                      end
         
     | 
| 
       165 
177 
     | 
    
         | 
| 
       166 
     | 
    
         
            -
             
     | 
| 
       167 
     | 
    
         
            -
             
     | 
| 
       168 
     | 
    
         
            -
             
     | 
| 
       169 
     | 
    
         
            -
                    # created by the matcher would otherwise evaluate for `ActiveRecord::Base`, not the
         
     | 
| 
       170 
     | 
    
         
            -
                    # sub class being decorated. As such, changes to `lock_optimistically`, or
         
     | 
| 
       171 
     | 
    
         
            -
                    # `locking_column` would not be picked up.
         
     | 
| 
       172 
     | 
    
         
            -
                    def inherited(subclass)
         
     | 
| 
       173 
     | 
    
         
            -
                      subclass.class_eval do
         
     | 
| 
       174 
     | 
    
         
            -
                        is_lock_column = ->(name, _) { lock_optimistically && name == locking_column }
         
     | 
| 
       175 
     | 
    
         
            -
                        decorate_matching_attribute_types(is_lock_column, :_optimistic_locking) do |type|
         
     | 
| 
       176 
     | 
    
         
            -
                          LockingType.new(type)
         
     | 
| 
      
 178 
     | 
    
         
            +
                      def define_attribute(name, cast_type, **) # :nodoc:
         
     | 
| 
      
 179 
     | 
    
         
            +
                        if lock_optimistically && name == locking_column
         
     | 
| 
      
 180 
     | 
    
         
            +
                          cast_type = LockingType.new(cast_type)
         
     | 
| 
       177 
181 
     | 
    
         
             
                        end
         
     | 
| 
      
 182 
     | 
    
         
            +
                        super
         
     | 
| 
       178 
183 
     | 
    
         
             
                      end
         
     | 
| 
       179 
     | 
    
         
            -
                      super
         
     | 
| 
       180 
184 
     | 
    
         
             
                    end
         
     | 
| 
       181 
     | 
    
         
            -
                  end
         
     | 
| 
       182 
185 
     | 
    
         
             
                end
         
     | 
| 
       183 
186 
     | 
    
         | 
| 
       184 
     | 
    
         
            -
                 
     | 
| 
       185 
     | 
    
         
            -
             
     | 
| 
       186 
     | 
    
         
            -
             
     | 
| 
      
 187 
     | 
    
         
            +
                # In de/serialize we change `nil` to 0, so that we can allow passing
         
     | 
| 
      
 188 
     | 
    
         
            +
                # `nil` values to `lock_version`, and not result in `ActiveRecord::StaleObjectError`
         
     | 
| 
      
 189 
     | 
    
         
            +
                # during update record.
         
     | 
| 
      
 190 
     | 
    
         
            +
                class LockingType < DelegateClass(Type::Value) # :nodoc:
         
     | 
| 
      
 191 
     | 
    
         
            +
                  def self.new(subtype)
         
     | 
| 
      
 192 
     | 
    
         
            +
                    self === subtype ? subtype : super
         
     | 
| 
      
 193 
     | 
    
         
            +
                  end
         
     | 
| 
      
 194 
     | 
    
         
            +
             
     | 
| 
      
 195 
     | 
    
         
            +
                  def deserialize(value)
         
     | 
| 
       187 
196 
     | 
    
         
             
                    super.to_i
         
     | 
| 
       188 
197 
     | 
    
         
             
                  end
         
     | 
| 
       189 
198 
     | 
    
         | 
| 
       190 
     | 
    
         
            -
                  def  
     | 
| 
       191 
     | 
    
         
            -
                     
     | 
| 
       192 
     | 
    
         
            -
                    super || old_value == 0
         
     | 
| 
      
 199 
     | 
    
         
            +
                  def serialize(value)
         
     | 
| 
      
 200 
     | 
    
         
            +
                    super.to_i
         
     | 
| 
       193 
201 
     | 
    
         
             
                  end
         
     | 
| 
       194 
202 
     | 
    
         | 
| 
       195 
203 
     | 
    
         
             
                  def init_with(coder)
         
     | 
| 
       196 
     | 
    
         
            -
                    __setobj__(coder[ 
     | 
| 
      
 204 
     | 
    
         
            +
                    __setobj__(coder["subtype"])
         
     | 
| 
       197 
205 
     | 
    
         
             
                  end
         
     | 
| 
       198 
206 
     | 
    
         | 
| 
       199 
207 
     | 
    
         
             
                  def encode_with(coder)
         
     | 
| 
       200 
     | 
    
         
            -
                    coder[ 
     | 
| 
      
 208 
     | 
    
         
            +
                    coder["subtype"] = __getobj__
         
     | 
| 
       201 
209 
     | 
    
         
             
                  end
         
     | 
| 
       202 
210 
     | 
    
         
             
                end
         
     | 
| 
       203 
211 
     | 
    
         
             
              end
         
     | 
| 
         @@ -1,3 +1,5 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            # frozen_string_literal: true
         
     | 
| 
      
 2 
     | 
    
         
            +
             
     | 
| 
       1 
3 
     | 
    
         
             
            module ActiveRecord
         
     | 
| 
       2 
4 
     | 
    
         
             
              module Locking
         
     | 
| 
       3 
5 
     | 
    
         
             
                # Locking::Pessimistic provides support for row-level locking using
         
     | 
| 
         @@ -12,9 +14,9 @@ module ActiveRecord 
     | 
|
| 
       12 
14 
     | 
    
         
             
                # of your own such as 'LOCK IN SHARE MODE' or 'FOR UPDATE NOWAIT'. Example:
         
     | 
| 
       13 
15 
     | 
    
         
             
                #
         
     | 
| 
       14 
16 
     | 
    
         
             
                #   Account.transaction do
         
     | 
| 
       15 
     | 
    
         
            -
                #     # select * from accounts where name = 'shugo' limit 1 for update
         
     | 
| 
       16 
     | 
    
         
            -
                #     shugo = Account. 
     | 
| 
       17 
     | 
    
         
            -
                #     yuko = Account. 
     | 
| 
      
 17 
     | 
    
         
            +
                #     # select * from accounts where name = 'shugo' limit 1 for update nowait
         
     | 
| 
      
 18 
     | 
    
         
            +
                #     shugo = Account.lock("FOR UPDATE NOWAIT").find_by(name: "shugo")
         
     | 
| 
      
 19 
     | 
    
         
            +
                #     yuko = Account.lock("FOR UPDATE NOWAIT").find_by(name: "yuko")
         
     | 
| 
       18 
20 
     | 
    
         
             
                #     shugo.balance -= 100
         
     | 
| 
       19 
21 
     | 
    
         
             
                #     shugo.save!
         
     | 
| 
       20 
22 
     | 
    
         
             
                #     yuko.balance += 100
         
     | 
| 
         @@ -51,15 +53,29 @@ module ActiveRecord 
     | 
|
| 
       51 
53 
     | 
    
         
             
                #   end
         
     | 
| 
       52 
54 
     | 
    
         
             
                #
         
     | 
| 
       53 
55 
     | 
    
         
             
                # Database-specific information on row locking:
         
     | 
| 
       54 
     | 
    
         
            -
                # 
     | 
| 
       55 
     | 
    
         
            -
                # 
     | 
| 
      
 56 
     | 
    
         
            +
                #
         
     | 
| 
      
 57 
     | 
    
         
            +
                # [MySQL]
         
     | 
| 
      
 58 
     | 
    
         
            +
                #   https://dev.mysql.com/doc/refman/en/innodb-locking-reads.html
         
     | 
| 
      
 59 
     | 
    
         
            +
                #
         
     | 
| 
      
 60 
     | 
    
         
            +
                # [PostgreSQL]
         
     | 
| 
      
 61 
     | 
    
         
            +
                #   https://www.postgresql.org/docs/current/interactive/sql-select.html#SQL-FOR-UPDATE-SHARE
         
     | 
| 
       56 
62 
     | 
    
         
             
                module Pessimistic
         
     | 
| 
       57 
63 
     | 
    
         
             
                  # Obtain a row lock on this record. Reloads the record to obtain the requested
         
     | 
| 
       58 
64 
     | 
    
         
             
                  # lock. Pass an SQL locking clause to append the end of the SELECT statement
         
     | 
| 
       59 
65 
     | 
    
         
             
                  # or pass true for "FOR UPDATE" (the default, an exclusive row lock). Returns
         
     | 
| 
       60 
66 
     | 
    
         
             
                  # the locked record.
         
     | 
| 
       61 
67 
     | 
    
         
             
                  def lock!(lock = true)
         
     | 
| 
       62 
     | 
    
         
            -
                     
     | 
| 
      
 68 
     | 
    
         
            +
                    if persisted?
         
     | 
| 
      
 69 
     | 
    
         
            +
                      if has_changes_to_save?
         
     | 
| 
      
 70 
     | 
    
         
            +
                        raise(<<-MSG.squish)
         
     | 
| 
      
 71 
     | 
    
         
            +
                          Locking a record with unpersisted changes is not supported. Use
         
     | 
| 
      
 72 
     | 
    
         
            +
                          `save` to persist the changes, or `reload` to discard them
         
     | 
| 
      
 73 
     | 
    
         
            +
                          explicitly.
         
     | 
| 
      
 74 
     | 
    
         
            +
                        MSG
         
     | 
| 
      
 75 
     | 
    
         
            +
                      end
         
     | 
| 
      
 76 
     | 
    
         
            +
             
     | 
| 
      
 77 
     | 
    
         
            +
                      reload(lock: lock)
         
     | 
| 
      
 78 
     | 
    
         
            +
                    end
         
     | 
| 
       63 
79 
     | 
    
         
             
                    self
         
     | 
| 
       64 
80 
     | 
    
         
             
                  end
         
     | 
| 
       65 
81 
     | 
    
         |