activerecord 5.2.8 → 7.0.2
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 +4 -4
 - data/CHANGELOG.md +1393 -587
 - data/MIT-LICENSE +3 -1
 - data/README.rdoc +7 -5
 - data/examples/performance.rb +1 -1
 - data/lib/active_record/aggregations.rb +10 -9
 - data/lib/active_record/association_relation.rb +22 -12
 - data/lib/active_record/associations/alias_tracker.rb +19 -16
 - data/lib/active_record/associations/association.rb +122 -47
 - data/lib/active_record/associations/association_scope.rb +24 -24
 - data/lib/active_record/associations/belongs_to_association.rb +67 -49
 - data/lib/active_record/associations/belongs_to_polymorphic_association.rb +16 -7
 - data/lib/active_record/associations/builder/association.rb +52 -23
 - data/lib/active_record/associations/builder/belongs_to.rb +44 -61
 - data/lib/active_record/associations/builder/collection_association.rb +17 -19
 - data/lib/active_record/associations/builder/has_and_belongs_to_many.rb +17 -41
 - data/lib/active_record/associations/builder/has_many.rb +10 -3
 - data/lib/active_record/associations/builder/has_one.rb +35 -3
 - data/lib/active_record/associations/builder/singular_association.rb +5 -3
 - data/lib/active_record/associations/collection_association.rb +59 -50
 - data/lib/active_record/associations/collection_proxy.rb +32 -23
 - data/lib/active_record/associations/disable_joins_association_scope.rb +59 -0
 - data/lib/active_record/associations/foreign_association.rb +20 -0
 - data/lib/active_record/associations/has_many_association.rb +27 -14
 - data/lib/active_record/associations/has_many_through_association.rb +26 -19
 - data/lib/active_record/associations/has_one_association.rb +52 -37
 - data/lib/active_record/associations/has_one_through_association.rb +6 -6
 - data/lib/active_record/associations/join_dependency/join_association.rb +44 -22
 - data/lib/active_record/associations/join_dependency/join_part.rb +5 -5
 - data/lib/active_record/associations/join_dependency.rb +97 -62
 - data/lib/active_record/associations/preloader/association.rb +220 -60
 - data/lib/active_record/associations/preloader/batch.rb +48 -0
 - data/lib/active_record/associations/preloader/branch.rb +147 -0
 - data/lib/active_record/associations/preloader/through_association.rb +85 -40
 - data/lib/active_record/associations/preloader.rb +44 -105
 - data/lib/active_record/associations/singular_association.rb +9 -17
 - data/lib/active_record/associations/through_association.rb +4 -4
 - data/lib/active_record/associations.rb +207 -66
 - data/lib/active_record/asynchronous_queries_tracker.rb +60 -0
 - data/lib/active_record/attribute_assignment.rb +17 -19
 - data/lib/active_record/attribute_methods/before_type_cast.rb +19 -8
 - data/lib/active_record/attribute_methods/dirty.rb +141 -47
 - data/lib/active_record/attribute_methods/primary_key.rb +22 -27
 - data/lib/active_record/attribute_methods/query.rb +6 -10
 - data/lib/active_record/attribute_methods/read.rb +15 -55
 - data/lib/active_record/attribute_methods/serialization.rb +77 -18
 - data/lib/active_record/attribute_methods/time_zone_conversion.rb +16 -18
 - data/lib/active_record/attribute_methods/write.rb +18 -37
 - data/lib/active_record/attribute_methods.rb +90 -153
 - data/lib/active_record/attributes.rb +38 -12
 - data/lib/active_record/autosave_association.rb +50 -50
 - data/lib/active_record/base.rb +23 -18
 - data/lib/active_record/callbacks.rb +159 -44
 - data/lib/active_record/coders/yaml_column.rb +12 -3
 - data/lib/active_record/connection_adapters/abstract/connection_handler.rb +292 -0
 - data/lib/active_record/connection_adapters/abstract/connection_pool/queue.rb +209 -0
 - data/lib/active_record/connection_adapters/abstract/connection_pool/reaper.rb +76 -0
 - data/lib/active_record/connection_adapters/abstract/connection_pool.rb +92 -464
 - data/lib/active_record/connection_adapters/abstract/database_limits.rb +5 -51
 - data/lib/active_record/connection_adapters/abstract/database_statements.rb +209 -164
 - data/lib/active_record/connection_adapters/abstract/query_cache.rb +38 -22
 - data/lib/active_record/connection_adapters/abstract/quoting.rb +103 -82
 - data/lib/active_record/connection_adapters/abstract/savepoints.rb +3 -3
 - data/lib/active_record/connection_adapters/abstract/schema_creation.rb +140 -110
 - data/lib/active_record/connection_adapters/abstract/schema_definitions.rb +236 -94
 - data/lib/active_record/connection_adapters/abstract/schema_dumper.rb +16 -5
 - data/lib/active_record/connection_adapters/abstract/schema_statements.rb +456 -159
 - data/lib/active_record/connection_adapters/abstract/transaction.rb +169 -78
 - data/lib/active_record/connection_adapters/abstract_adapter.rb +367 -162
 - data/lib/active_record/connection_adapters/abstract_mysql_adapter.rb +311 -327
 - data/lib/active_record/connection_adapters/column.rb +33 -11
 - data/lib/active_record/connection_adapters/deduplicable.rb +29 -0
 - data/lib/active_record/connection_adapters/legacy_pool_manager.rb +35 -0
 - data/lib/active_record/connection_adapters/mysql/column.rb +1 -1
 - data/lib/active_record/connection_adapters/mysql/database_statements.rb +113 -45
 - data/lib/active_record/connection_adapters/mysql/explain_pretty_printer.rb +1 -2
 - data/lib/active_record/connection_adapters/mysql/quoting.rb +71 -5
 - data/lib/active_record/connection_adapters/mysql/schema_creation.rb +34 -10
 - data/lib/active_record/connection_adapters/mysql/schema_definitions.rb +48 -32
 - data/lib/active_record/connection_adapters/mysql/schema_dumper.rb +25 -8
 - data/lib/active_record/connection_adapters/mysql/schema_statements.rb +143 -19
 - data/lib/active_record/connection_adapters/mysql/type_metadata.rb +14 -9
 - data/lib/active_record/connection_adapters/mysql2_adapter.rb +63 -22
 - 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 +53 -28
 - data/lib/active_record/connection_adapters/postgresql/database_statements.rb +56 -63
 - data/lib/active_record/connection_adapters/postgresql/oid/array.rb +1 -2
 - data/lib/active_record/connection_adapters/postgresql/oid/bit.rb +1 -4
 - data/lib/active_record/connection_adapters/postgresql/oid/cidr.rb +3 -5
 - data/lib/active_record/connection_adapters/postgresql/oid/date.rb +10 -2
 - data/lib/active_record/connection_adapters/postgresql/oid/date_time.rb +15 -2
 - data/lib/active_record/connection_adapters/postgresql/oid/enum.rb +0 -1
 - data/lib/active_record/connection_adapters/postgresql/oid/hstore.rb +54 -16
 - data/lib/active_record/connection_adapters/postgresql/oid/interval.rb +49 -0
 - data/lib/active_record/connection_adapters/postgresql/oid/legacy_point.rb +3 -4
 - data/lib/active_record/connection_adapters/postgresql/oid/macaddr.rb +25 -0
 - data/lib/active_record/connection_adapters/postgresql/oid/oid.rb +1 -1
 - data/lib/active_record/connection_adapters/postgresql/oid/point.rb +3 -4
 - data/lib/active_record/connection_adapters/postgresql/oid/range.rb +25 -7
 - data/lib/active_record/connection_adapters/postgresql/oid/specialized_string.rb +1 -1
 - data/lib/active_record/connection_adapters/postgresql/oid/timestamp.rb +15 -0
 - data/lib/active_record/connection_adapters/postgresql/oid/timestamp_with_time_zone.rb +30 -0
 - data/lib/active_record/connection_adapters/postgresql/oid/type_map_initializer.rb +26 -12
 - data/lib/active_record/connection_adapters/postgresql/oid/uuid.rb +15 -3
 - data/lib/active_record/connection_adapters/postgresql/oid.rb +4 -0
 - data/lib/active_record/connection_adapters/postgresql/quoting.rb +89 -52
 - data/lib/active_record/connection_adapters/postgresql/referential_integrity.rb +34 -2
 - data/lib/active_record/connection_adapters/postgresql/schema_creation.rb +39 -4
 - data/lib/active_record/connection_adapters/postgresql/schema_definitions.rb +128 -91
 - data/lib/active_record/connection_adapters/postgresql/schema_dumper.rb +25 -1
 - data/lib/active_record/connection_adapters/postgresql/schema_statements.rb +149 -113
 - data/lib/active_record/connection_adapters/postgresql/type_metadata.rb +31 -26
 - data/lib/active_record/connection_adapters/postgresql/utils.rb +0 -1
 - data/lib/active_record/connection_adapters/postgresql_adapter.rb +386 -182
 - data/lib/active_record/connection_adapters/schema_cache.rb +161 -22
 - data/lib/active_record/connection_adapters/sql_type_metadata.rb +17 -6
 - data/lib/active_record/connection_adapters/sqlite3/database_statements.rb +152 -0
 - data/lib/active_record/connection_adapters/sqlite3/quoting.rb +65 -18
 - data/lib/active_record/connection_adapters/sqlite3/schema_creation.rb +5 -1
 - data/lib/active_record/connection_adapters/sqlite3/schema_statements.rb +92 -26
 - data/lib/active_record/connection_adapters/sqlite3_adapter.rb +251 -204
 - data/lib/active_record/connection_adapters/statement_pool.rb +0 -1
 - data/lib/active_record/connection_adapters.rb +53 -0
 - data/lib/active_record/connection_handling.rb +292 -38
 - data/lib/active_record/core.rb +385 -158
 - data/lib/active_record/counter_cache.rb +8 -30
 - data/lib/active_record/database_configurations/connection_url_resolver.rb +100 -0
 - data/lib/active_record/database_configurations/database_config.rb +83 -0
 - data/lib/active_record/database_configurations/hash_config.rb +154 -0
 - data/lib/active_record/database_configurations/url_config.rb +53 -0
 - data/lib/active_record/database_configurations.rb +256 -0
 - data/lib/active_record/delegated_type.rb +250 -0
 - data/lib/active_record/destroy_association_async_job.rb +36 -0
 - data/lib/active_record/disable_joins_association_relation.rb +39 -0
 - data/lib/active_record/dynamic_matchers.rb +4 -5
 - data/lib/active_record/encryption/cipher/aes256_gcm.rb +98 -0
 - data/lib/active_record/encryption/cipher.rb +53 -0
 - data/lib/active_record/encryption/config.rb +44 -0
 - data/lib/active_record/encryption/configurable.rb +61 -0
 - data/lib/active_record/encryption/context.rb +35 -0
 - data/lib/active_record/encryption/contexts.rb +72 -0
 - data/lib/active_record/encryption/derived_secret_key_provider.rb +12 -0
 - data/lib/active_record/encryption/deterministic_key_provider.rb +14 -0
 - data/lib/active_record/encryption/encryptable_record.rb +208 -0
 - data/lib/active_record/encryption/encrypted_attribute_type.rb +140 -0
 - data/lib/active_record/encryption/encrypted_fixtures.rb +38 -0
 - data/lib/active_record/encryption/encrypting_only_encryptor.rb +12 -0
 - data/lib/active_record/encryption/encryptor.rb +155 -0
 - data/lib/active_record/encryption/envelope_encryption_key_provider.rb +55 -0
 - data/lib/active_record/encryption/errors.rb +15 -0
 - data/lib/active_record/encryption/extended_deterministic_queries.rb +160 -0
 - data/lib/active_record/encryption/extended_deterministic_uniqueness_validator.rb +28 -0
 - data/lib/active_record/encryption/key.rb +28 -0
 - data/lib/active_record/encryption/key_generator.rb +42 -0
 - data/lib/active_record/encryption/key_provider.rb +46 -0
 - data/lib/active_record/encryption/message.rb +33 -0
 - data/lib/active_record/encryption/message_serializer.rb +90 -0
 - data/lib/active_record/encryption/null_encryptor.rb +21 -0
 - data/lib/active_record/encryption/properties.rb +76 -0
 - data/lib/active_record/encryption/read_only_null_encryptor.rb +24 -0
 - data/lib/active_record/encryption/scheme.rb +99 -0
 - data/lib/active_record/encryption.rb +55 -0
 - data/lib/active_record/enum.rb +130 -51
 - data/lib/active_record/errors.rb +129 -23
 - data/lib/active_record/explain.rb +10 -6
 - data/lib/active_record/explain_registry.rb +11 -6
 - data/lib/active_record/explain_subscriber.rb +1 -1
 - data/lib/active_record/fixture_set/file.rb +22 -15
 - 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 +187 -0
 - data/lib/active_record/fixture_set/table_rows.rb +46 -0
 - data/lib/active_record/fixtures.rb +206 -490
 - data/lib/active_record/future_result.rb +139 -0
 - data/lib/active_record/gem_version.rb +3 -3
 - data/lib/active_record/inheritance.rb +104 -37
 - data/lib/active_record/insert_all.rb +278 -0
 - data/lib/active_record/integration.rb +69 -18
 - data/lib/active_record/internal_metadata.rb +24 -9
 - data/lib/active_record/legacy_yaml_adapter.rb +3 -36
 - data/lib/active_record/locking/optimistic.rb +41 -26
 - data/lib/active_record/locking/pessimistic.rb +18 -8
 - data/lib/active_record/log_subscriber.rb +46 -35
 - data/lib/active_record/middleware/database_selector/resolver/session.rb +48 -0
 - data/lib/active_record/middleware/database_selector/resolver.rb +88 -0
 - data/lib/active_record/middleware/database_selector.rb +82 -0
 - data/lib/active_record/middleware/shard_selector.rb +60 -0
 - data/lib/active_record/migration/command_recorder.rb +96 -44
 - data/lib/active_record/migration/compatibility.rb +246 -64
 - data/lib/active_record/migration/join_table.rb +1 -2
 - data/lib/active_record/migration.rb +266 -187
 - data/lib/active_record/model_schema.rb +165 -52
 - data/lib/active_record/nested_attributes.rb +17 -19
 - data/lib/active_record/no_touching.rb +11 -4
 - data/lib/active_record/null_relation.rb +2 -7
 - data/lib/active_record/persistence.rb +467 -92
 - data/lib/active_record/query_cache.rb +21 -4
 - data/lib/active_record/query_logs.rb +138 -0
 - data/lib/active_record/querying.rb +51 -24
 - data/lib/active_record/railtie.rb +224 -57
 - data/lib/active_record/railties/console_sandbox.rb +2 -4
 - data/lib/active_record/railties/controller_runtime.rb +31 -36
 - data/lib/active_record/railties/databases.rake +369 -101
 - data/lib/active_record/readonly_attributes.rb +15 -0
 - data/lib/active_record/reflection.rb +170 -137
 - data/lib/active_record/relation/batches/batch_enumerator.rb +44 -14
 - data/lib/active_record/relation/batches.rb +46 -37
 - data/lib/active_record/relation/calculations.rb +168 -96
 - data/lib/active_record/relation/delegation.rb +37 -52
 - data/lib/active_record/relation/finder_methods.rb +79 -58
 - data/lib/active_record/relation/from_clause.rb +5 -1
 - data/lib/active_record/relation/merger.rb +50 -51
 - data/lib/active_record/relation/predicate_builder/array_handler.rb +13 -13
 - data/lib/active_record/relation/predicate_builder/association_query_value.rb +5 -9
 - data/lib/active_record/relation/predicate_builder/basic_object_handler.rb +1 -2
 - data/lib/active_record/relation/predicate_builder/polymorphic_array_value.rb +11 -10
 - data/lib/active_record/relation/predicate_builder/range_handler.rb +3 -23
 - data/lib/active_record/relation/predicate_builder/relation_handler.rb +1 -1
 - data/lib/active_record/relation/predicate_builder.rb +58 -46
 - data/lib/active_record/relation/query_attribute.rb +9 -10
 - data/lib/active_record/relation/query_methods.rb +685 -208
 - data/lib/active_record/relation/record_fetch_warning.rb +9 -11
 - data/lib/active_record/relation/spawn_methods.rb +10 -10
 - data/lib/active_record/relation/where_clause.rb +108 -64
 - data/lib/active_record/relation.rb +515 -151
 - data/lib/active_record/result.rb +78 -42
 - data/lib/active_record/runtime_registry.rb +9 -13
 - data/lib/active_record/sanitization.rb +29 -44
 - data/lib/active_record/schema.rb +37 -31
 - data/lib/active_record/schema_dumper.rb +74 -23
 - data/lib/active_record/schema_migration.rb +7 -9
 - data/lib/active_record/scoping/default.rb +62 -17
 - data/lib/active_record/scoping/named.rb +17 -32
 - data/lib/active_record/scoping.rb +70 -41
 - data/lib/active_record/secure_token.rb +16 -8
 - data/lib/active_record/serialization.rb +6 -4
 - data/lib/active_record/signed_id.rb +116 -0
 - data/lib/active_record/statement_cache.rb +49 -6
 - data/lib/active_record/store.rb +88 -9
 - data/lib/active_record/suppressor.rb +13 -17
 - data/lib/active_record/table_metadata.rb +42 -43
 - data/lib/active_record/tasks/database_tasks.rb +352 -94
 - data/lib/active_record/tasks/mysql_database_tasks.rb +37 -39
 - data/lib/active_record/tasks/postgresql_database_tasks.rb +41 -39
 - data/lib/active_record/tasks/sqlite_database_tasks.rb +14 -17
 - data/lib/active_record/test_databases.rb +24 -0
 - data/lib/active_record/test_fixtures.rb +287 -0
 - data/lib/active_record/timestamp.rb +44 -34
 - data/lib/active_record/touch_later.rb +23 -22
 - data/lib/active_record/transactions.rb +67 -128
 - data/lib/active_record/translation.rb +3 -3
 - data/lib/active_record/type/adapter_specific_registry.rb +34 -19
 - data/lib/active_record/type/hash_lookup_type_map.rb +34 -2
 - data/lib/active_record/type/internal/timezone.rb +2 -2
 - data/lib/active_record/type/serialized.rb +7 -4
 - data/lib/active_record/type/time.rb +10 -0
 - data/lib/active_record/type/type_map.rb +17 -21
 - data/lib/active_record/type/unsigned_integer.rb +0 -1
 - data/lib/active_record/type.rb +9 -5
 - data/lib/active_record/type_caster/connection.rb +15 -15
 - data/lib/active_record/type_caster/map.rb +8 -8
 - data/lib/active_record/validations/associated.rb +2 -3
 - data/lib/active_record/validations/numericality.rb +35 -0
 - data/lib/active_record/validations/uniqueness.rb +39 -31
 - data/lib/active_record/validations.rb +4 -3
 - data/lib/active_record.rb +209 -32
 - data/lib/arel/alias_predication.rb +9 -0
 - data/lib/arel/attributes/attribute.rb +33 -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 +48 -0
 - data/lib/arel/delete_manager.rb +32 -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/filter_predications.rb +9 -0
 - data/lib/arel/insert_manager.rb +48 -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 +44 -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/filter.rb +10 -0
 - data/lib/arel/nodes/full_outer_join.rb +8 -0
 - data/lib/arel/nodes/function.rb +45 -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 +46 -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 +71 -0
 - data/lib/arel/order_predications.rb +13 -0
 - data/lib/arel/predications.rb +258 -0
 - data/lib/arel/select_manager.rb +276 -0
 - data/lib/arel/table.rb +117 -0
 - data/lib/arel/tree_manager.rb +60 -0
 - data/lib/arel/update_manager.rb +48 -0
 - data/lib/arel/visitors/dot.rb +298 -0
 - data/lib/arel/visitors/mysql.rb +99 -0
 - data/lib/arel/visitors/postgresql.rb +110 -0
 - data/lib/arel/visitors/sqlite.rb +38 -0
 - data/lib/arel/visitors/to_sql.rb +955 -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 +55 -0
 - data/lib/rails/generators/active_record/application_record/application_record_generator.rb +0 -1
 - data/lib/rails/generators/active_record/application_record/templates/application_record.rb.tt +1 -1
 - data/lib/rails/generators/active_record/migration/migration_generator.rb +3 -5
 - data/lib/rails/generators/active_record/migration/templates/create_table_migration.rb.tt +3 -1
 - data/lib/rails/generators/active_record/migration/templates/migration.rb.tt +7 -5
 - data/lib/rails/generators/active_record/migration.rb +19 -2
 - data/lib/rails/generators/active_record/model/model_generator.rb +39 -2
 - data/lib/rails/generators/active_record/model/templates/abstract_base_class.rb.tt +7 -0
 - data/lib/rails/generators/active_record/model/templates/model.rb.tt +10 -1
 - data/lib/rails/generators/active_record/model/templates/module.rb.tt +2 -2
 - data/lib/rails/generators/active_record/multi_db/multi_db_generator.rb +16 -0
 - data/lib/rails/generators/active_record/multi_db/templates/multi_db.rb.tt +44 -0
 - metadata +162 -32
 - data/lib/active_record/attribute_decorators.rb +0 -90
 - data/lib/active_record/collection_cache_key.rb +0 -53
 - data/lib/active_record/connection_adapters/connection_specification.rb +0 -287
 - data/lib/active_record/connection_adapters/determine_if_preparable_visitor.rb +0 -33
 - data/lib/active_record/define_callbacks.rb +0 -22
 - data/lib/active_record/relation/predicate_builder/base_handler.rb +0 -19
 - data/lib/active_record/relation/where_clause_factory.rb +0 -34
 
| 
         @@ -22,8 +22,8 @@ module ActiveRecord 
     | 
|
| 
       22 
22 
     | 
    
         | 
| 
       23 
23 
     | 
    
         
             
                  def load(yaml)
         
     | 
| 
       24 
24 
     | 
    
         
             
                    return object_class.new if object_class != Object && yaml.nil?
         
     | 
| 
       25 
     | 
    
         
            -
                    return yaml unless yaml.is_a?(String) &&  
     | 
| 
       26 
     | 
    
         
            -
                    obj =  
     | 
| 
      
 25 
     | 
    
         
            +
                    return yaml unless yaml.is_a?(String) && yaml.start_with?("---")
         
     | 
| 
      
 26 
     | 
    
         
            +
                    obj = yaml_load(yaml)
         
     | 
| 
       27 
27 
     | 
    
         | 
| 
       28 
28 
     | 
    
         
             
                    assert_valid_value(obj, action: "load")
         
     | 
| 
       29 
29 
     | 
    
         
             
                    obj ||= object_class.new if object_class != Object
         
     | 
| 
         @@ -39,12 +39,21 @@ module ActiveRecord 
     | 
|
| 
       39 
39 
     | 
    
         
             
                  end
         
     | 
| 
       40 
40 
     | 
    
         | 
| 
       41 
41 
     | 
    
         
             
                  private
         
     | 
| 
       42 
     | 
    
         
            -
             
     | 
| 
       43 
42 
     | 
    
         
             
                    def check_arity_of_constructor
         
     | 
| 
       44 
43 
     | 
    
         
             
                      load(nil)
         
     | 
| 
       45 
44 
     | 
    
         
             
                    rescue ArgumentError
         
     | 
| 
       46 
45 
     | 
    
         
             
                      raise ArgumentError, "Cannot serialize #{object_class}. Classes passed to `serialize` must have a 0 argument constructor."
         
     | 
| 
       47 
46 
     | 
    
         
             
                    end
         
     | 
| 
      
 47 
     | 
    
         
            +
             
     | 
| 
      
 48 
     | 
    
         
            +
                    if YAML.respond_to?(:unsafe_load)
         
     | 
| 
      
 49 
     | 
    
         
            +
                      def yaml_load(payload)
         
     | 
| 
      
 50 
     | 
    
         
            +
                        YAML.unsafe_load(payload)
         
     | 
| 
      
 51 
     | 
    
         
            +
                      end
         
     | 
| 
      
 52 
     | 
    
         
            +
                    else
         
     | 
| 
      
 53 
     | 
    
         
            +
                      def yaml_load(payload)
         
     | 
| 
      
 54 
     | 
    
         
            +
                        YAML.load(payload)
         
     | 
| 
      
 55 
     | 
    
         
            +
                      end
         
     | 
| 
      
 56 
     | 
    
         
            +
                    end
         
     | 
| 
       48 
57 
     | 
    
         
             
                end
         
     | 
| 
       49 
58 
     | 
    
         
             
              end
         
     | 
| 
       50 
59 
     | 
    
         
             
            end
         
     | 
| 
         @@ -0,0 +1,292 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            # frozen_string_literal: true
         
     | 
| 
      
 2 
     | 
    
         
            +
             
     | 
| 
      
 3 
     | 
    
         
            +
            require "thread"
         
     | 
| 
      
 4 
     | 
    
         
            +
            require "concurrent/map"
         
     | 
| 
      
 5 
     | 
    
         
            +
             
     | 
| 
      
 6 
     | 
    
         
            +
            module ActiveRecord
         
     | 
| 
      
 7 
     | 
    
         
            +
              module ConnectionAdapters
         
     | 
| 
      
 8 
     | 
    
         
            +
                # ConnectionHandler is a collection of ConnectionPool objects. It is used
         
     | 
| 
      
 9 
     | 
    
         
            +
                # for keeping separate connection pools that connect to different databases.
         
     | 
| 
      
 10 
     | 
    
         
            +
                #
         
     | 
| 
      
 11 
     | 
    
         
            +
                # For example, suppose that you have 5 models, with the following hierarchy:
         
     | 
| 
      
 12 
     | 
    
         
            +
                #
         
     | 
| 
      
 13 
     | 
    
         
            +
                #   class Author < ActiveRecord::Base
         
     | 
| 
      
 14 
     | 
    
         
            +
                #   end
         
     | 
| 
      
 15 
     | 
    
         
            +
                #
         
     | 
| 
      
 16 
     | 
    
         
            +
                #   class BankAccount < ActiveRecord::Base
         
     | 
| 
      
 17 
     | 
    
         
            +
                #   end
         
     | 
| 
      
 18 
     | 
    
         
            +
                #
         
     | 
| 
      
 19 
     | 
    
         
            +
                #   class Book < ActiveRecord::Base
         
     | 
| 
      
 20 
     | 
    
         
            +
                #     establish_connection :library_db
         
     | 
| 
      
 21 
     | 
    
         
            +
                #   end
         
     | 
| 
      
 22 
     | 
    
         
            +
                #
         
     | 
| 
      
 23 
     | 
    
         
            +
                #   class ScaryBook < Book
         
     | 
| 
      
 24 
     | 
    
         
            +
                #   end
         
     | 
| 
      
 25 
     | 
    
         
            +
                #
         
     | 
| 
      
 26 
     | 
    
         
            +
                #   class GoodBook < Book
         
     | 
| 
      
 27 
     | 
    
         
            +
                #   end
         
     | 
| 
      
 28 
     | 
    
         
            +
                #
         
     | 
| 
      
 29 
     | 
    
         
            +
                # And a database.yml that looked like this:
         
     | 
| 
      
 30 
     | 
    
         
            +
                #
         
     | 
| 
      
 31 
     | 
    
         
            +
                #   development:
         
     | 
| 
      
 32 
     | 
    
         
            +
                #     database: my_application
         
     | 
| 
      
 33 
     | 
    
         
            +
                #     host: localhost
         
     | 
| 
      
 34 
     | 
    
         
            +
                #
         
     | 
| 
      
 35 
     | 
    
         
            +
                #   library_db:
         
     | 
| 
      
 36 
     | 
    
         
            +
                #     database: library
         
     | 
| 
      
 37 
     | 
    
         
            +
                #     host: some.library.org
         
     | 
| 
      
 38 
     | 
    
         
            +
                #
         
     | 
| 
      
 39 
     | 
    
         
            +
                # Your primary database in the development environment is "my_application"
         
     | 
| 
      
 40 
     | 
    
         
            +
                # but the Book model connects to a separate database called "library_db"
         
     | 
| 
      
 41 
     | 
    
         
            +
                # (this can even be a database on a different machine).
         
     | 
| 
      
 42 
     | 
    
         
            +
                #
         
     | 
| 
      
 43 
     | 
    
         
            +
                # Book, ScaryBook and GoodBook will all use the same connection pool to
         
     | 
| 
      
 44 
     | 
    
         
            +
                # "library_db" while Author, BankAccount, and any other models you create
         
     | 
| 
      
 45 
     | 
    
         
            +
                # will use the default connection pool to "my_application".
         
     | 
| 
      
 46 
     | 
    
         
            +
                #
         
     | 
| 
      
 47 
     | 
    
         
            +
                # The various connection pools are managed by a single instance of
         
     | 
| 
      
 48 
     | 
    
         
            +
                # ConnectionHandler accessible via ActiveRecord::Base.connection_handler.
         
     | 
| 
      
 49 
     | 
    
         
            +
                # All Active Record models use this handler to determine the connection pool that they
         
     | 
| 
      
 50 
     | 
    
         
            +
                # should use.
         
     | 
| 
      
 51 
     | 
    
         
            +
                #
         
     | 
| 
      
 52 
     | 
    
         
            +
                # The ConnectionHandler class is not coupled with the Active models, as it has no knowledge
         
     | 
| 
      
 53 
     | 
    
         
            +
                # about the model. The model needs to pass a connection specification name to the handler,
         
     | 
| 
      
 54 
     | 
    
         
            +
                # in order to look up the correct connection pool.
         
     | 
| 
      
 55 
     | 
    
         
            +
                class ConnectionHandler
         
     | 
| 
      
 56 
     | 
    
         
            +
                  FINALIZER = lambda { |_| ActiveSupport::ForkTracker.check! }
         
     | 
| 
      
 57 
     | 
    
         
            +
                  private_constant :FINALIZER
         
     | 
| 
      
 58 
     | 
    
         
            +
             
     | 
| 
      
 59 
     | 
    
         
            +
                  class StringConnectionOwner # :nodoc:
         
     | 
| 
      
 60 
     | 
    
         
            +
                    attr_reader :name
         
     | 
| 
      
 61 
     | 
    
         
            +
             
     | 
| 
      
 62 
     | 
    
         
            +
                    def initialize(name)
         
     | 
| 
      
 63 
     | 
    
         
            +
                      @name = name
         
     | 
| 
      
 64 
     | 
    
         
            +
                    end
         
     | 
| 
      
 65 
     | 
    
         
            +
             
     | 
| 
      
 66 
     | 
    
         
            +
                    def primary_class?
         
     | 
| 
      
 67 
     | 
    
         
            +
                      false
         
     | 
| 
      
 68 
     | 
    
         
            +
                    end
         
     | 
| 
      
 69 
     | 
    
         
            +
             
     | 
| 
      
 70 
     | 
    
         
            +
                    def current_preventing_writes
         
     | 
| 
      
 71 
     | 
    
         
            +
                      false
         
     | 
| 
      
 72 
     | 
    
         
            +
                    end
         
     | 
| 
      
 73 
     | 
    
         
            +
                  end
         
     | 
| 
      
 74 
     | 
    
         
            +
             
     | 
| 
      
 75 
     | 
    
         
            +
                  def initialize
         
     | 
| 
      
 76 
     | 
    
         
            +
                    # These caches are keyed by pool_config.connection_specification_name (PoolConfig#connection_specification_name).
         
     | 
| 
      
 77 
     | 
    
         
            +
                    @owner_to_pool_manager = Concurrent::Map.new(initial_capacity: 2)
         
     | 
| 
      
 78 
     | 
    
         
            +
             
     | 
| 
      
 79 
     | 
    
         
            +
                    # Backup finalizer: if the forked child skipped Kernel#fork the early discard has not occurred
         
     | 
| 
      
 80 
     | 
    
         
            +
                    ObjectSpace.define_finalizer self, FINALIZER
         
     | 
| 
      
 81 
     | 
    
         
            +
                  end
         
     | 
| 
      
 82 
     | 
    
         
            +
             
     | 
| 
      
 83 
     | 
    
         
            +
                  def prevent_writes # :nodoc:
         
     | 
| 
      
 84 
     | 
    
         
            +
                    ActiveSupport::IsolatedExecutionState[:active_record_prevent_writes]
         
     | 
| 
      
 85 
     | 
    
         
            +
                  end
         
     | 
| 
      
 86 
     | 
    
         
            +
             
     | 
| 
      
 87 
     | 
    
         
            +
                  def prevent_writes=(prevent_writes) # :nodoc:
         
     | 
| 
      
 88 
     | 
    
         
            +
                    ActiveSupport::IsolatedExecutionState[:active_record_prevent_writes] = prevent_writes
         
     | 
| 
      
 89 
     | 
    
         
            +
                  end
         
     | 
| 
      
 90 
     | 
    
         
            +
             
     | 
| 
      
 91 
     | 
    
         
            +
                  # Prevent writing to the database regardless of role.
         
     | 
| 
      
 92 
     | 
    
         
            +
                  #
         
     | 
| 
      
 93 
     | 
    
         
            +
                  # In some cases you may want to prevent writes to the database
         
     | 
| 
      
 94 
     | 
    
         
            +
                  # even if you are on a database that can write. +while_preventing_writes+
         
     | 
| 
      
 95 
     | 
    
         
            +
                  # will prevent writes to the database for the duration of the block.
         
     | 
| 
      
 96 
     | 
    
         
            +
                  #
         
     | 
| 
      
 97 
     | 
    
         
            +
                  # This method does not provide the same protection as a readonly
         
     | 
| 
      
 98 
     | 
    
         
            +
                  # user and is meant to be a safeguard against accidental writes.
         
     | 
| 
      
 99 
     | 
    
         
            +
                  #
         
     | 
| 
      
 100 
     | 
    
         
            +
                  # See +READ_QUERY+ for the queries that are blocked by this
         
     | 
| 
      
 101 
     | 
    
         
            +
                  # method.
         
     | 
| 
      
 102 
     | 
    
         
            +
                  def while_preventing_writes(enabled = true)
         
     | 
| 
      
 103 
     | 
    
         
            +
                    unless ActiveRecord.legacy_connection_handling
         
     | 
| 
      
 104 
     | 
    
         
            +
                      raise NotImplementedError, "`while_preventing_writes` is only available on the connection_handler with legacy_connection_handling"
         
     | 
| 
      
 105 
     | 
    
         
            +
                    end
         
     | 
| 
      
 106 
     | 
    
         
            +
             
     | 
| 
      
 107 
     | 
    
         
            +
                    original, self.prevent_writes = self.prevent_writes, enabled
         
     | 
| 
      
 108 
     | 
    
         
            +
                    yield
         
     | 
| 
      
 109 
     | 
    
         
            +
                  ensure
         
     | 
| 
      
 110 
     | 
    
         
            +
                    self.prevent_writes = original
         
     | 
| 
      
 111 
     | 
    
         
            +
                  end
         
     | 
| 
      
 112 
     | 
    
         
            +
             
     | 
| 
      
 113 
     | 
    
         
            +
                  def connection_pool_names # :nodoc:
         
     | 
| 
      
 114 
     | 
    
         
            +
                    owner_to_pool_manager.keys
         
     | 
| 
      
 115 
     | 
    
         
            +
                  end
         
     | 
| 
      
 116 
     | 
    
         
            +
             
     | 
| 
      
 117 
     | 
    
         
            +
                  def all_connection_pools
         
     | 
| 
      
 118 
     | 
    
         
            +
                    owner_to_pool_manager.values.flat_map { |m| m.pool_configs.map(&:pool) }
         
     | 
| 
      
 119 
     | 
    
         
            +
                  end
         
     | 
| 
      
 120 
     | 
    
         
            +
             
     | 
| 
      
 121 
     | 
    
         
            +
                  def connection_pool_list(role = ActiveRecord::Base.current_role)
         
     | 
| 
      
 122 
     | 
    
         
            +
                    owner_to_pool_manager.values.flat_map { |m| m.pool_configs(role).map(&:pool) }
         
     | 
| 
      
 123 
     | 
    
         
            +
                  end
         
     | 
| 
      
 124 
     | 
    
         
            +
                  alias :connection_pools :connection_pool_list
         
     | 
| 
      
 125 
     | 
    
         
            +
             
     | 
| 
      
 126 
     | 
    
         
            +
                  def establish_connection(config, owner_name: Base, role: ActiveRecord::Base.current_role, shard: Base.current_shard)
         
     | 
| 
      
 127 
     | 
    
         
            +
                    owner_name = StringConnectionOwner.new(config.to_s) if config.is_a?(Symbol)
         
     | 
| 
      
 128 
     | 
    
         
            +
             
     | 
| 
      
 129 
     | 
    
         
            +
                    pool_config = resolve_pool_config(config, owner_name, role, shard)
         
     | 
| 
      
 130 
     | 
    
         
            +
                    db_config = pool_config.db_config
         
     | 
| 
      
 131 
     | 
    
         
            +
             
     | 
| 
      
 132 
     | 
    
         
            +
                    # Protects the connection named `ActiveRecord::Base` from being removed
         
     | 
| 
      
 133 
     | 
    
         
            +
                    # if the user calls `establish_connection :primary`.
         
     | 
| 
      
 134 
     | 
    
         
            +
                    if owner_to_pool_manager.key?(pool_config.connection_specification_name)
         
     | 
| 
      
 135 
     | 
    
         
            +
                      remove_connection_pool(pool_config.connection_specification_name, role: role, shard: shard)
         
     | 
| 
      
 136 
     | 
    
         
            +
                    end
         
     | 
| 
      
 137 
     | 
    
         
            +
             
     | 
| 
      
 138 
     | 
    
         
            +
                    message_bus = ActiveSupport::Notifications.instrumenter
         
     | 
| 
      
 139 
     | 
    
         
            +
                    payload = {}
         
     | 
| 
      
 140 
     | 
    
         
            +
                    if pool_config
         
     | 
| 
      
 141 
     | 
    
         
            +
                      payload[:spec_name] = pool_config.connection_specification_name
         
     | 
| 
      
 142 
     | 
    
         
            +
                      payload[:shard] = shard
         
     | 
| 
      
 143 
     | 
    
         
            +
                      payload[:config] = db_config.configuration_hash
         
     | 
| 
      
 144 
     | 
    
         
            +
                    end
         
     | 
| 
      
 145 
     | 
    
         
            +
             
     | 
| 
      
 146 
     | 
    
         
            +
                    if ActiveRecord.legacy_connection_handling
         
     | 
| 
      
 147 
     | 
    
         
            +
                      owner_to_pool_manager[pool_config.connection_specification_name] ||= LegacyPoolManager.new
         
     | 
| 
      
 148 
     | 
    
         
            +
                    else
         
     | 
| 
      
 149 
     | 
    
         
            +
                      owner_to_pool_manager[pool_config.connection_specification_name] ||= PoolManager.new
         
     | 
| 
      
 150 
     | 
    
         
            +
                    end
         
     | 
| 
      
 151 
     | 
    
         
            +
                    pool_manager = get_pool_manager(pool_config.connection_specification_name)
         
     | 
| 
      
 152 
     | 
    
         
            +
                    pool_manager.set_pool_config(role, shard, pool_config)
         
     | 
| 
      
 153 
     | 
    
         
            +
             
     | 
| 
      
 154 
     | 
    
         
            +
                    message_bus.instrument("!connection.active_record", payload) do
         
     | 
| 
      
 155 
     | 
    
         
            +
                      pool_config.pool
         
     | 
| 
      
 156 
     | 
    
         
            +
                    end
         
     | 
| 
      
 157 
     | 
    
         
            +
                  end
         
     | 
| 
      
 158 
     | 
    
         
            +
             
     | 
| 
      
 159 
     | 
    
         
            +
                  # Returns true if there are any active connections among the connection
         
     | 
| 
      
 160 
     | 
    
         
            +
                  # pools that the ConnectionHandler is managing.
         
     | 
| 
      
 161 
     | 
    
         
            +
                  def active_connections?(role = ActiveRecord::Base.current_role)
         
     | 
| 
      
 162 
     | 
    
         
            +
                    connection_pool_list(role).any?(&:active_connection?)
         
     | 
| 
      
 163 
     | 
    
         
            +
                  end
         
     | 
| 
      
 164 
     | 
    
         
            +
             
     | 
| 
      
 165 
     | 
    
         
            +
                  # Returns any connections in use by the current thread back to the pool,
         
     | 
| 
      
 166 
     | 
    
         
            +
                  # and also returns connections to the pool cached by threads that are no
         
     | 
| 
      
 167 
     | 
    
         
            +
                  # longer alive.
         
     | 
| 
      
 168 
     | 
    
         
            +
                  def clear_active_connections!(role = ActiveRecord::Base.current_role)
         
     | 
| 
      
 169 
     | 
    
         
            +
                    connection_pool_list(role).each(&:release_connection)
         
     | 
| 
      
 170 
     | 
    
         
            +
                  end
         
     | 
| 
      
 171 
     | 
    
         
            +
             
     | 
| 
      
 172 
     | 
    
         
            +
                  # Clears the cache which maps classes.
         
     | 
| 
      
 173 
     | 
    
         
            +
                  #
         
     | 
| 
      
 174 
     | 
    
         
            +
                  # See ConnectionPool#clear_reloadable_connections! for details.
         
     | 
| 
      
 175 
     | 
    
         
            +
                  def clear_reloadable_connections!(role = ActiveRecord::Base.current_role)
         
     | 
| 
      
 176 
     | 
    
         
            +
                    connection_pool_list(role).each(&:clear_reloadable_connections!)
         
     | 
| 
      
 177 
     | 
    
         
            +
                  end
         
     | 
| 
      
 178 
     | 
    
         
            +
             
     | 
| 
      
 179 
     | 
    
         
            +
                  def clear_all_connections!(role = ActiveRecord::Base.current_role)
         
     | 
| 
      
 180 
     | 
    
         
            +
                    connection_pool_list(role).each(&:disconnect!)
         
     | 
| 
      
 181 
     | 
    
         
            +
                  end
         
     | 
| 
      
 182 
     | 
    
         
            +
             
     | 
| 
      
 183 
     | 
    
         
            +
                  # Disconnects all currently idle connections.
         
     | 
| 
      
 184 
     | 
    
         
            +
                  #
         
     | 
| 
      
 185 
     | 
    
         
            +
                  # See ConnectionPool#flush! for details.
         
     | 
| 
      
 186 
     | 
    
         
            +
                  def flush_idle_connections!(role = ActiveRecord::Base.current_role)
         
     | 
| 
      
 187 
     | 
    
         
            +
                    connection_pool_list(role).each(&:flush!)
         
     | 
| 
      
 188 
     | 
    
         
            +
                  end
         
     | 
| 
      
 189 
     | 
    
         
            +
             
     | 
| 
      
 190 
     | 
    
         
            +
                  # Locate the connection of the nearest super class. This can be an
         
     | 
| 
      
 191 
     | 
    
         
            +
                  # active or defined connection: if it is the latter, it will be
         
     | 
| 
      
 192 
     | 
    
         
            +
                  # opened and set as the active connection for the class it was defined
         
     | 
| 
      
 193 
     | 
    
         
            +
                  # for (not necessarily the current class).
         
     | 
| 
      
 194 
     | 
    
         
            +
                  def retrieve_connection(spec_name, role: ActiveRecord::Base.current_role, shard: ActiveRecord::Base.current_shard) # :nodoc:
         
     | 
| 
      
 195 
     | 
    
         
            +
                    pool = retrieve_connection_pool(spec_name, role: role, shard: shard)
         
     | 
| 
      
 196 
     | 
    
         
            +
             
     | 
| 
      
 197 
     | 
    
         
            +
                    unless pool
         
     | 
| 
      
 198 
     | 
    
         
            +
                      if shard != ActiveRecord::Base.default_shard
         
     | 
| 
      
 199 
     | 
    
         
            +
                        message = "No connection pool for '#{spec_name}' found for the '#{shard}' shard."
         
     | 
| 
      
 200 
     | 
    
         
            +
                      elsif ActiveRecord::Base.connection_handler != ActiveRecord::Base.default_connection_handler
         
     | 
| 
      
 201 
     | 
    
         
            +
                        message = "No connection pool for '#{spec_name}' found for the '#{ActiveRecord::Base.current_role}' role."
         
     | 
| 
      
 202 
     | 
    
         
            +
                      elsif role != ActiveRecord::Base.default_role
         
     | 
| 
      
 203 
     | 
    
         
            +
                        message = "No connection pool for '#{spec_name}' found for the '#{role}' role."
         
     | 
| 
      
 204 
     | 
    
         
            +
                      else
         
     | 
| 
      
 205 
     | 
    
         
            +
                        message = "No connection pool for '#{spec_name}' found."
         
     | 
| 
      
 206 
     | 
    
         
            +
                      end
         
     | 
| 
      
 207 
     | 
    
         
            +
             
     | 
| 
      
 208 
     | 
    
         
            +
                      raise ConnectionNotEstablished, message
         
     | 
| 
      
 209 
     | 
    
         
            +
                    end
         
     | 
| 
      
 210 
     | 
    
         
            +
             
     | 
| 
      
 211 
     | 
    
         
            +
                    pool.connection
         
     | 
| 
      
 212 
     | 
    
         
            +
                  end
         
     | 
| 
      
 213 
     | 
    
         
            +
             
     | 
| 
      
 214 
     | 
    
         
            +
                  # Returns true if a connection that's accessible to this class has
         
     | 
| 
      
 215 
     | 
    
         
            +
                  # already been opened.
         
     | 
| 
      
 216 
     | 
    
         
            +
                  def connected?(spec_name, role: ActiveRecord::Base.current_role, shard: ActiveRecord::Base.current_shard)
         
     | 
| 
      
 217 
     | 
    
         
            +
                    pool = retrieve_connection_pool(spec_name, role: role, shard: shard)
         
     | 
| 
      
 218 
     | 
    
         
            +
                    pool && pool.connected?
         
     | 
| 
      
 219 
     | 
    
         
            +
                  end
         
     | 
| 
      
 220 
     | 
    
         
            +
             
     | 
| 
      
 221 
     | 
    
         
            +
                  def remove_connection_pool(owner, role: ActiveRecord::Base.current_role, shard: ActiveRecord::Base.current_shard)
         
     | 
| 
      
 222 
     | 
    
         
            +
                    if pool_manager = get_pool_manager(owner)
         
     | 
| 
      
 223 
     | 
    
         
            +
                      pool_config = pool_manager.remove_pool_config(role, shard)
         
     | 
| 
      
 224 
     | 
    
         
            +
             
     | 
| 
      
 225 
     | 
    
         
            +
                      if pool_config
         
     | 
| 
      
 226 
     | 
    
         
            +
                        pool_config.disconnect!
         
     | 
| 
      
 227 
     | 
    
         
            +
                        pool_config.db_config
         
     | 
| 
      
 228 
     | 
    
         
            +
                      end
         
     | 
| 
      
 229 
     | 
    
         
            +
                    end
         
     | 
| 
      
 230 
     | 
    
         
            +
                  end
         
     | 
| 
      
 231 
     | 
    
         
            +
             
     | 
| 
      
 232 
     | 
    
         
            +
                  # Retrieving the connection pool happens a lot, so we cache it in @owner_to_pool_manager.
         
     | 
| 
      
 233 
     | 
    
         
            +
                  # This makes retrieving the connection pool O(1) once the process is warm.
         
     | 
| 
      
 234 
     | 
    
         
            +
                  # When a connection is established or removed, we invalidate the cache.
         
     | 
| 
      
 235 
     | 
    
         
            +
                  def retrieve_connection_pool(owner, role: ActiveRecord::Base.current_role, shard: ActiveRecord::Base.current_shard)
         
     | 
| 
      
 236 
     | 
    
         
            +
                    pool_config = get_pool_manager(owner)&.get_pool_config(role, shard)
         
     | 
| 
      
 237 
     | 
    
         
            +
                    pool_config&.pool
         
     | 
| 
      
 238 
     | 
    
         
            +
                  end
         
     | 
| 
      
 239 
     | 
    
         
            +
             
     | 
| 
      
 240 
     | 
    
         
            +
                  private
         
     | 
| 
      
 241 
     | 
    
         
            +
                    attr_reader :owner_to_pool_manager
         
     | 
| 
      
 242 
     | 
    
         
            +
             
     | 
| 
      
 243 
     | 
    
         
            +
                    # Returns the pool manager for an owner.
         
     | 
| 
      
 244 
     | 
    
         
            +
                    def get_pool_manager(owner)
         
     | 
| 
      
 245 
     | 
    
         
            +
                      owner_to_pool_manager[owner]
         
     | 
| 
      
 246 
     | 
    
         
            +
                    end
         
     | 
| 
      
 247 
     | 
    
         
            +
             
     | 
| 
      
 248 
     | 
    
         
            +
                    # Returns an instance of PoolConfig for a given adapter.
         
     | 
| 
      
 249 
     | 
    
         
            +
                    # Accepts a hash one layer deep that contains all connection information.
         
     | 
| 
      
 250 
     | 
    
         
            +
                    #
         
     | 
| 
      
 251 
     | 
    
         
            +
                    # == Example
         
     | 
| 
      
 252 
     | 
    
         
            +
                    #
         
     | 
| 
      
 253 
     | 
    
         
            +
                    #   config = { "production" => { "host" => "localhost", "database" => "foo", "adapter" => "sqlite3" } }
         
     | 
| 
      
 254 
     | 
    
         
            +
                    #   pool_config = Base.configurations.resolve_pool_config(:production)
         
     | 
| 
      
 255 
     | 
    
         
            +
                    #   pool_config.db_config.configuration_hash
         
     | 
| 
      
 256 
     | 
    
         
            +
                    #   # => { host: "localhost", database: "foo", adapter: "sqlite3" }
         
     | 
| 
      
 257 
     | 
    
         
            +
                    #
         
     | 
| 
      
 258 
     | 
    
         
            +
                    def resolve_pool_config(config, owner_name, role, shard)
         
     | 
| 
      
 259 
     | 
    
         
            +
                      db_config = Base.configurations.resolve(config)
         
     | 
| 
      
 260 
     | 
    
         
            +
             
     | 
| 
      
 261 
     | 
    
         
            +
                      raise(AdapterNotSpecified, "database configuration does not specify adapter") unless db_config.adapter
         
     | 
| 
      
 262 
     | 
    
         
            +
             
     | 
| 
      
 263 
     | 
    
         
            +
                      # Require the adapter itself and give useful feedback about
         
     | 
| 
      
 264 
     | 
    
         
            +
                      #   1. Missing adapter gems and
         
     | 
| 
      
 265 
     | 
    
         
            +
                      #   2. Adapter gems' missing dependencies.
         
     | 
| 
      
 266 
     | 
    
         
            +
                      path_to_adapter = "active_record/connection_adapters/#{db_config.adapter}_adapter"
         
     | 
| 
      
 267 
     | 
    
         
            +
                      begin
         
     | 
| 
      
 268 
     | 
    
         
            +
                        require path_to_adapter
         
     | 
| 
      
 269 
     | 
    
         
            +
                      rescue LoadError => e
         
     | 
| 
      
 270 
     | 
    
         
            +
                        # We couldn't require the adapter itself. Raise an exception that
         
     | 
| 
      
 271 
     | 
    
         
            +
                        # points out config typos and missing gems.
         
     | 
| 
      
 272 
     | 
    
         
            +
                        if e.path == path_to_adapter
         
     | 
| 
      
 273 
     | 
    
         
            +
                          # We can assume that a non-builtin adapter was specified, so it's
         
     | 
| 
      
 274 
     | 
    
         
            +
                          # either misspelled or missing from Gemfile.
         
     | 
| 
      
 275 
     | 
    
         
            +
                          raise LoadError, "Could not load the '#{db_config.adapter}' Active Record adapter. Ensure that the adapter is spelled correctly in config/database.yml and that you've added the necessary adapter gem to your Gemfile.", e.backtrace
         
     | 
| 
      
 276 
     | 
    
         
            +
             
     | 
| 
      
 277 
     | 
    
         
            +
                          # Bubbled up from the adapter require. Prefix the exception message
         
     | 
| 
      
 278 
     | 
    
         
            +
                          # with some guidance about how to address it and reraise.
         
     | 
| 
      
 279 
     | 
    
         
            +
                        else
         
     | 
| 
      
 280 
     | 
    
         
            +
                          raise LoadError, "Error loading the '#{db_config.adapter}' Active Record adapter. Missing a gem it depends on? #{e.message}", e.backtrace
         
     | 
| 
      
 281 
     | 
    
         
            +
                        end
         
     | 
| 
      
 282 
     | 
    
         
            +
                      end
         
     | 
| 
      
 283 
     | 
    
         
            +
             
     | 
| 
      
 284 
     | 
    
         
            +
                      unless ActiveRecord::Base.respond_to?(db_config.adapter_method)
         
     | 
| 
      
 285 
     | 
    
         
            +
                        raise AdapterNotFound, "database configuration specifies nonexistent #{db_config.adapter} adapter"
         
     | 
| 
      
 286 
     | 
    
         
            +
                      end
         
     | 
| 
      
 287 
     | 
    
         
            +
             
     | 
| 
      
 288 
     | 
    
         
            +
                      ConnectionAdapters::PoolConfig.new(owner_name, db_config, role, shard)
         
     | 
| 
      
 289 
     | 
    
         
            +
                    end
         
     | 
| 
      
 290 
     | 
    
         
            +
                end
         
     | 
| 
      
 291 
     | 
    
         
            +
              end
         
     | 
| 
      
 292 
     | 
    
         
            +
            end
         
     | 
| 
         @@ -0,0 +1,209 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            # frozen_string_literal: true
         
     | 
| 
      
 2 
     | 
    
         
            +
             
     | 
| 
      
 3 
     | 
    
         
            +
            require "thread"
         
     | 
| 
      
 4 
     | 
    
         
            +
            require "monitor"
         
     | 
| 
      
 5 
     | 
    
         
            +
             
     | 
| 
      
 6 
     | 
    
         
            +
            module ActiveRecord
         
     | 
| 
      
 7 
     | 
    
         
            +
              module ConnectionAdapters
         
     | 
| 
      
 8 
     | 
    
         
            +
                class ConnectionPool
         
     | 
| 
      
 9 
     | 
    
         
            +
                  # Threadsafe, fair, LIFO queue.  Meant to be used by ConnectionPool
         
     | 
| 
      
 10 
     | 
    
         
            +
                  # with which it shares a Monitor.
         
     | 
| 
      
 11 
     | 
    
         
            +
                  class Queue
         
     | 
| 
      
 12 
     | 
    
         
            +
                    def initialize(lock = Monitor.new)
         
     | 
| 
      
 13 
     | 
    
         
            +
                      @lock = lock
         
     | 
| 
      
 14 
     | 
    
         
            +
                      @cond = @lock.new_cond
         
     | 
| 
      
 15 
     | 
    
         
            +
                      @num_waiting = 0
         
     | 
| 
      
 16 
     | 
    
         
            +
                      @queue = []
         
     | 
| 
      
 17 
     | 
    
         
            +
                    end
         
     | 
| 
      
 18 
     | 
    
         
            +
             
     | 
| 
      
 19 
     | 
    
         
            +
                    # Test if any threads are currently waiting on the queue.
         
     | 
| 
      
 20 
     | 
    
         
            +
                    def any_waiting?
         
     | 
| 
      
 21 
     | 
    
         
            +
                      synchronize do
         
     | 
| 
      
 22 
     | 
    
         
            +
                        @num_waiting > 0
         
     | 
| 
      
 23 
     | 
    
         
            +
                      end
         
     | 
| 
      
 24 
     | 
    
         
            +
                    end
         
     | 
| 
      
 25 
     | 
    
         
            +
             
     | 
| 
      
 26 
     | 
    
         
            +
                    # Returns the number of threads currently waiting on this
         
     | 
| 
      
 27 
     | 
    
         
            +
                    # queue.
         
     | 
| 
      
 28 
     | 
    
         
            +
                    def num_waiting
         
     | 
| 
      
 29 
     | 
    
         
            +
                      synchronize do
         
     | 
| 
      
 30 
     | 
    
         
            +
                        @num_waiting
         
     | 
| 
      
 31 
     | 
    
         
            +
                      end
         
     | 
| 
      
 32 
     | 
    
         
            +
                    end
         
     | 
| 
      
 33 
     | 
    
         
            +
             
     | 
| 
      
 34 
     | 
    
         
            +
                    # Add +element+ to the queue.  Never blocks.
         
     | 
| 
      
 35 
     | 
    
         
            +
                    def add(element)
         
     | 
| 
      
 36 
     | 
    
         
            +
                      synchronize do
         
     | 
| 
      
 37 
     | 
    
         
            +
                        @queue.push element
         
     | 
| 
      
 38 
     | 
    
         
            +
                        @cond.signal
         
     | 
| 
      
 39 
     | 
    
         
            +
                      end
         
     | 
| 
      
 40 
     | 
    
         
            +
                    end
         
     | 
| 
      
 41 
     | 
    
         
            +
             
     | 
| 
      
 42 
     | 
    
         
            +
                    # If +element+ is in the queue, remove and return it, or +nil+.
         
     | 
| 
      
 43 
     | 
    
         
            +
                    def delete(element)
         
     | 
| 
      
 44 
     | 
    
         
            +
                      synchronize do
         
     | 
| 
      
 45 
     | 
    
         
            +
                        @queue.delete(element)
         
     | 
| 
      
 46 
     | 
    
         
            +
                      end
         
     | 
| 
      
 47 
     | 
    
         
            +
                    end
         
     | 
| 
      
 48 
     | 
    
         
            +
             
     | 
| 
      
 49 
     | 
    
         
            +
                    # Remove all elements from the queue.
         
     | 
| 
      
 50 
     | 
    
         
            +
                    def clear
         
     | 
| 
      
 51 
     | 
    
         
            +
                      synchronize do
         
     | 
| 
      
 52 
     | 
    
         
            +
                        @queue.clear
         
     | 
| 
      
 53 
     | 
    
         
            +
                      end
         
     | 
| 
      
 54 
     | 
    
         
            +
                    end
         
     | 
| 
      
 55 
     | 
    
         
            +
             
     | 
| 
      
 56 
     | 
    
         
            +
                    # Remove the head of the queue.
         
     | 
| 
      
 57 
     | 
    
         
            +
                    #
         
     | 
| 
      
 58 
     | 
    
         
            +
                    # If +timeout+ is not given, remove and return the head of the
         
     | 
| 
      
 59 
     | 
    
         
            +
                    # queue if the number of available elements is strictly
         
     | 
| 
      
 60 
     | 
    
         
            +
                    # greater than the number of threads currently waiting (that
         
     | 
| 
      
 61 
     | 
    
         
            +
                    # is, don't jump ahead in line).  Otherwise, return +nil+.
         
     | 
| 
      
 62 
     | 
    
         
            +
                    #
         
     | 
| 
      
 63 
     | 
    
         
            +
                    # If +timeout+ is given, block if there is no element
         
     | 
| 
      
 64 
     | 
    
         
            +
                    # available, waiting up to +timeout+ seconds for an element to
         
     | 
| 
      
 65 
     | 
    
         
            +
                    # become available.
         
     | 
| 
      
 66 
     | 
    
         
            +
                    #
         
     | 
| 
      
 67 
     | 
    
         
            +
                    # Raises:
         
     | 
| 
      
 68 
     | 
    
         
            +
                    # - ActiveRecord::ConnectionTimeoutError if +timeout+ is given and no element
         
     | 
| 
      
 69 
     | 
    
         
            +
                    # becomes available within +timeout+ seconds,
         
     | 
| 
      
 70 
     | 
    
         
            +
                    def poll(timeout = nil)
         
     | 
| 
      
 71 
     | 
    
         
            +
                      synchronize { internal_poll(timeout) }
         
     | 
| 
      
 72 
     | 
    
         
            +
                    end
         
     | 
| 
      
 73 
     | 
    
         
            +
             
     | 
| 
      
 74 
     | 
    
         
            +
                    private
         
     | 
| 
      
 75 
     | 
    
         
            +
                      def internal_poll(timeout)
         
     | 
| 
      
 76 
     | 
    
         
            +
                        no_wait_poll || (timeout && wait_poll(timeout))
         
     | 
| 
      
 77 
     | 
    
         
            +
                      end
         
     | 
| 
      
 78 
     | 
    
         
            +
             
     | 
| 
      
 79 
     | 
    
         
            +
                      def synchronize(&block)
         
     | 
| 
      
 80 
     | 
    
         
            +
                        @lock.synchronize(&block)
         
     | 
| 
      
 81 
     | 
    
         
            +
                      end
         
     | 
| 
      
 82 
     | 
    
         
            +
             
     | 
| 
      
 83 
     | 
    
         
            +
                      # Test if the queue currently contains any elements.
         
     | 
| 
      
 84 
     | 
    
         
            +
                      def any?
         
     | 
| 
      
 85 
     | 
    
         
            +
                        !@queue.empty?
         
     | 
| 
      
 86 
     | 
    
         
            +
                      end
         
     | 
| 
      
 87 
     | 
    
         
            +
             
     | 
| 
      
 88 
     | 
    
         
            +
                      # A thread can remove an element from the queue without
         
     | 
| 
      
 89 
     | 
    
         
            +
                      # waiting if and only if the number of currently available
         
     | 
| 
      
 90 
     | 
    
         
            +
                      # connections is strictly greater than the number of waiting
         
     | 
| 
      
 91 
     | 
    
         
            +
                      # threads.
         
     | 
| 
      
 92 
     | 
    
         
            +
                      def can_remove_no_wait?
         
     | 
| 
      
 93 
     | 
    
         
            +
                        @queue.size > @num_waiting
         
     | 
| 
      
 94 
     | 
    
         
            +
                      end
         
     | 
| 
      
 95 
     | 
    
         
            +
             
     | 
| 
      
 96 
     | 
    
         
            +
                      # Removes and returns the head of the queue if possible, or +nil+.
         
     | 
| 
      
 97 
     | 
    
         
            +
                      def remove
         
     | 
| 
      
 98 
     | 
    
         
            +
                        @queue.pop
         
     | 
| 
      
 99 
     | 
    
         
            +
                      end
         
     | 
| 
      
 100 
     | 
    
         
            +
             
     | 
| 
      
 101 
     | 
    
         
            +
                      # Remove and return the head of the queue if the number of
         
     | 
| 
      
 102 
     | 
    
         
            +
                      # available elements is strictly greater than the number of
         
     | 
| 
      
 103 
     | 
    
         
            +
                      # threads currently waiting.  Otherwise, return +nil+.
         
     | 
| 
      
 104 
     | 
    
         
            +
                      def no_wait_poll
         
     | 
| 
      
 105 
     | 
    
         
            +
                        remove if can_remove_no_wait?
         
     | 
| 
      
 106 
     | 
    
         
            +
                      end
         
     | 
| 
      
 107 
     | 
    
         
            +
             
     | 
| 
      
 108 
     | 
    
         
            +
                      # Waits on the queue up to +timeout+ seconds, then removes and
         
     | 
| 
      
 109 
     | 
    
         
            +
                      # returns the head of the queue.
         
     | 
| 
      
 110 
     | 
    
         
            +
                      def wait_poll(timeout)
         
     | 
| 
      
 111 
     | 
    
         
            +
                        @num_waiting += 1
         
     | 
| 
      
 112 
     | 
    
         
            +
             
     | 
| 
      
 113 
     | 
    
         
            +
                        t0 = Process.clock_gettime(Process::CLOCK_MONOTONIC)
         
     | 
| 
      
 114 
     | 
    
         
            +
                        elapsed = 0
         
     | 
| 
      
 115 
     | 
    
         
            +
                        loop do
         
     | 
| 
      
 116 
     | 
    
         
            +
                          ActiveSupport::Dependencies.interlock.permit_concurrent_loads do
         
     | 
| 
      
 117 
     | 
    
         
            +
                            @cond.wait(timeout - elapsed)
         
     | 
| 
      
 118 
     | 
    
         
            +
                          end
         
     | 
| 
      
 119 
     | 
    
         
            +
             
     | 
| 
      
 120 
     | 
    
         
            +
                          return remove if any?
         
     | 
| 
      
 121 
     | 
    
         
            +
             
     | 
| 
      
 122 
     | 
    
         
            +
                          elapsed = Process.clock_gettime(Process::CLOCK_MONOTONIC) - t0
         
     | 
| 
      
 123 
     | 
    
         
            +
                          if elapsed >= timeout
         
     | 
| 
      
 124 
     | 
    
         
            +
                            msg = "could not obtain a connection from the pool within %0.3f seconds (waited %0.3f seconds); all pooled connections were in use" %
         
     | 
| 
      
 125 
     | 
    
         
            +
                              [timeout, elapsed]
         
     | 
| 
      
 126 
     | 
    
         
            +
                            raise ConnectionTimeoutError, msg
         
     | 
| 
      
 127 
     | 
    
         
            +
                          end
         
     | 
| 
      
 128 
     | 
    
         
            +
                        end
         
     | 
| 
      
 129 
     | 
    
         
            +
                      ensure
         
     | 
| 
      
 130 
     | 
    
         
            +
                        @num_waiting -= 1
         
     | 
| 
      
 131 
     | 
    
         
            +
                      end
         
     | 
| 
      
 132 
     | 
    
         
            +
                  end
         
     | 
| 
      
 133 
     | 
    
         
            +
             
     | 
| 
      
 134 
     | 
    
         
            +
                  # Adds the ability to turn a basic fair FIFO queue into one
         
     | 
| 
      
 135 
     | 
    
         
            +
                  # biased to some thread.
         
     | 
| 
      
 136 
     | 
    
         
            +
                  module BiasableQueue # :nodoc:
         
     | 
| 
      
 137 
     | 
    
         
            +
                    class BiasedConditionVariable # :nodoc:
         
     | 
| 
      
 138 
     | 
    
         
            +
                      # semantics of condition variables guarantee that +broadcast+, +broadcast_on_biased+,
         
     | 
| 
      
 139 
     | 
    
         
            +
                      # +signal+ and +wait+ methods are only called while holding a lock
         
     | 
| 
      
 140 
     | 
    
         
            +
                      def initialize(lock, other_cond, preferred_thread)
         
     | 
| 
      
 141 
     | 
    
         
            +
                        @real_cond = lock.new_cond
         
     | 
| 
      
 142 
     | 
    
         
            +
                        @other_cond = other_cond
         
     | 
| 
      
 143 
     | 
    
         
            +
                        @preferred_thread = preferred_thread
         
     | 
| 
      
 144 
     | 
    
         
            +
                        @num_waiting_on_real_cond = 0
         
     | 
| 
      
 145 
     | 
    
         
            +
                      end
         
     | 
| 
      
 146 
     | 
    
         
            +
             
     | 
| 
      
 147 
     | 
    
         
            +
                      def broadcast
         
     | 
| 
      
 148 
     | 
    
         
            +
                        broadcast_on_biased
         
     | 
| 
      
 149 
     | 
    
         
            +
                        @other_cond.broadcast
         
     | 
| 
      
 150 
     | 
    
         
            +
                      end
         
     | 
| 
      
 151 
     | 
    
         
            +
             
     | 
| 
      
 152 
     | 
    
         
            +
                      def broadcast_on_biased
         
     | 
| 
      
 153 
     | 
    
         
            +
                        @num_waiting_on_real_cond = 0
         
     | 
| 
      
 154 
     | 
    
         
            +
                        @real_cond.broadcast
         
     | 
| 
      
 155 
     | 
    
         
            +
                      end
         
     | 
| 
      
 156 
     | 
    
         
            +
             
     | 
| 
      
 157 
     | 
    
         
            +
                      def signal
         
     | 
| 
      
 158 
     | 
    
         
            +
                        if @num_waiting_on_real_cond > 0
         
     | 
| 
      
 159 
     | 
    
         
            +
                          @num_waiting_on_real_cond -= 1
         
     | 
| 
      
 160 
     | 
    
         
            +
                          @real_cond
         
     | 
| 
      
 161 
     | 
    
         
            +
                        else
         
     | 
| 
      
 162 
     | 
    
         
            +
                          @other_cond
         
     | 
| 
      
 163 
     | 
    
         
            +
                        end.signal
         
     | 
| 
      
 164 
     | 
    
         
            +
                      end
         
     | 
| 
      
 165 
     | 
    
         
            +
             
     | 
| 
      
 166 
     | 
    
         
            +
                      def wait(timeout)
         
     | 
| 
      
 167 
     | 
    
         
            +
                        if Thread.current == @preferred_thread
         
     | 
| 
      
 168 
     | 
    
         
            +
                          @num_waiting_on_real_cond += 1
         
     | 
| 
      
 169 
     | 
    
         
            +
                          @real_cond
         
     | 
| 
      
 170 
     | 
    
         
            +
                        else
         
     | 
| 
      
 171 
     | 
    
         
            +
                          @other_cond
         
     | 
| 
      
 172 
     | 
    
         
            +
                        end.wait(timeout)
         
     | 
| 
      
 173 
     | 
    
         
            +
                      end
         
     | 
| 
      
 174 
     | 
    
         
            +
                    end
         
     | 
| 
      
 175 
     | 
    
         
            +
             
     | 
| 
      
 176 
     | 
    
         
            +
                    def with_a_bias_for(thread)
         
     | 
| 
      
 177 
     | 
    
         
            +
                      previous_cond = nil
         
     | 
| 
      
 178 
     | 
    
         
            +
                      new_cond      = nil
         
     | 
| 
      
 179 
     | 
    
         
            +
                      synchronize do
         
     | 
| 
      
 180 
     | 
    
         
            +
                        previous_cond = @cond
         
     | 
| 
      
 181 
     | 
    
         
            +
                        @cond = new_cond = BiasedConditionVariable.new(@lock, @cond, thread)
         
     | 
| 
      
 182 
     | 
    
         
            +
                      end
         
     | 
| 
      
 183 
     | 
    
         
            +
                      yield
         
     | 
| 
      
 184 
     | 
    
         
            +
                    ensure
         
     | 
| 
      
 185 
     | 
    
         
            +
                      synchronize do
         
     | 
| 
      
 186 
     | 
    
         
            +
                        @cond = previous_cond if previous_cond
         
     | 
| 
      
 187 
     | 
    
         
            +
                        new_cond.broadcast_on_biased if new_cond # wake up any remaining sleepers
         
     | 
| 
      
 188 
     | 
    
         
            +
                      end
         
     | 
| 
      
 189 
     | 
    
         
            +
                    end
         
     | 
| 
      
 190 
     | 
    
         
            +
                  end
         
     | 
| 
      
 191 
     | 
    
         
            +
             
     | 
| 
      
 192 
     | 
    
         
            +
                  # Connections must be leased while holding the main pool mutex. This is
         
     | 
| 
      
 193 
     | 
    
         
            +
                  # an internal subclass that also +.leases+ returned connections while
         
     | 
| 
      
 194 
     | 
    
         
            +
                  # still in queue's critical section (queue synchronizes with the same
         
     | 
| 
      
 195 
     | 
    
         
            +
                  # <tt>@lock</tt> as the main pool) so that a returned connection is already
         
     | 
| 
      
 196 
     | 
    
         
            +
                  # leased and there is no need to re-enter synchronized block.
         
     | 
| 
      
 197 
     | 
    
         
            +
                  class ConnectionLeasingQueue < Queue # :nodoc:
         
     | 
| 
      
 198 
     | 
    
         
            +
                    include BiasableQueue
         
     | 
| 
      
 199 
     | 
    
         
            +
             
     | 
| 
      
 200 
     | 
    
         
            +
                    private
         
     | 
| 
      
 201 
     | 
    
         
            +
                      def internal_poll(timeout)
         
     | 
| 
      
 202 
     | 
    
         
            +
                        conn = super
         
     | 
| 
      
 203 
     | 
    
         
            +
                        conn.lease if conn
         
     | 
| 
      
 204 
     | 
    
         
            +
                        conn
         
     | 
| 
      
 205 
     | 
    
         
            +
                      end
         
     | 
| 
      
 206 
     | 
    
         
            +
                  end
         
     | 
| 
      
 207 
     | 
    
         
            +
                end
         
     | 
| 
      
 208 
     | 
    
         
            +
              end
         
     | 
| 
      
 209 
     | 
    
         
            +
            end
         
     | 
| 
         @@ -0,0 +1,76 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            # frozen_string_literal: true
         
     | 
| 
      
 2 
     | 
    
         
            +
             
     | 
| 
      
 3 
     | 
    
         
            +
            require "thread"
         
     | 
| 
      
 4 
     | 
    
         
            +
            require "weakref"
         
     | 
| 
      
 5 
     | 
    
         
            +
             
     | 
| 
      
 6 
     | 
    
         
            +
            module ActiveRecord
         
     | 
| 
      
 7 
     | 
    
         
            +
              module ConnectionAdapters
         
     | 
| 
      
 8 
     | 
    
         
            +
                class ConnectionPool
         
     | 
| 
      
 9 
     | 
    
         
            +
                  # Every +frequency+ seconds, the reaper will call +reap+ and +flush+ on
         
     | 
| 
      
 10 
     | 
    
         
            +
                  # +pool+. A reaper instantiated with a zero frequency will never reap
         
     | 
| 
      
 11 
     | 
    
         
            +
                  # the connection pool.
         
     | 
| 
      
 12 
     | 
    
         
            +
                  #
         
     | 
| 
      
 13 
     | 
    
         
            +
                  # Configure the frequency by setting +reaping_frequency+ in your database
         
     | 
| 
      
 14 
     | 
    
         
            +
                  # yaml file (default 60 seconds).
         
     | 
| 
      
 15 
     | 
    
         
            +
                  class Reaper
         
     | 
| 
      
 16 
     | 
    
         
            +
                    attr_reader :pool, :frequency
         
     | 
| 
      
 17 
     | 
    
         
            +
             
     | 
| 
      
 18 
     | 
    
         
            +
                    def initialize(pool, frequency)
         
     | 
| 
      
 19 
     | 
    
         
            +
                      @pool      = pool
         
     | 
| 
      
 20 
     | 
    
         
            +
                      @frequency = frequency
         
     | 
| 
      
 21 
     | 
    
         
            +
                    end
         
     | 
| 
      
 22 
     | 
    
         
            +
             
     | 
| 
      
 23 
     | 
    
         
            +
                    @mutex = Mutex.new
         
     | 
| 
      
 24 
     | 
    
         
            +
                    @pools = {}
         
     | 
| 
      
 25 
     | 
    
         
            +
                    @threads = {}
         
     | 
| 
      
 26 
     | 
    
         
            +
             
     | 
| 
      
 27 
     | 
    
         
            +
                    class << self
         
     | 
| 
      
 28 
     | 
    
         
            +
                      def register_pool(pool, frequency) # :nodoc:
         
     | 
| 
      
 29 
     | 
    
         
            +
                        @mutex.synchronize do
         
     | 
| 
      
 30 
     | 
    
         
            +
                          unless @threads[frequency]&.alive?
         
     | 
| 
      
 31 
     | 
    
         
            +
                            @threads[frequency] = spawn_thread(frequency)
         
     | 
| 
      
 32 
     | 
    
         
            +
                          end
         
     | 
| 
      
 33 
     | 
    
         
            +
                          @pools[frequency] ||= []
         
     | 
| 
      
 34 
     | 
    
         
            +
                          @pools[frequency] << WeakRef.new(pool)
         
     | 
| 
      
 35 
     | 
    
         
            +
                        end
         
     | 
| 
      
 36 
     | 
    
         
            +
                      end
         
     | 
| 
      
 37 
     | 
    
         
            +
             
     | 
| 
      
 38 
     | 
    
         
            +
                      private
         
     | 
| 
      
 39 
     | 
    
         
            +
                        def spawn_thread(frequency)
         
     | 
| 
      
 40 
     | 
    
         
            +
                          Thread.new(frequency) do |t|
         
     | 
| 
      
 41 
     | 
    
         
            +
                            # Advise multi-threaded app servers to ignore this thread for
         
     | 
| 
      
 42 
     | 
    
         
            +
                            # the purposes of fork safety warnings
         
     | 
| 
      
 43 
     | 
    
         
            +
                            Thread.current.thread_variable_set(:fork_safe, true)
         
     | 
| 
      
 44 
     | 
    
         
            +
                            running = true
         
     | 
| 
      
 45 
     | 
    
         
            +
                            while running
         
     | 
| 
      
 46 
     | 
    
         
            +
                              sleep t
         
     | 
| 
      
 47 
     | 
    
         
            +
                              @mutex.synchronize do
         
     | 
| 
      
 48 
     | 
    
         
            +
                                @pools[frequency].select! do |pool|
         
     | 
| 
      
 49 
     | 
    
         
            +
                                  pool.weakref_alive? && !pool.discarded?
         
     | 
| 
      
 50 
     | 
    
         
            +
                                end
         
     | 
| 
      
 51 
     | 
    
         
            +
             
     | 
| 
      
 52 
     | 
    
         
            +
                                @pools[frequency].each do |p|
         
     | 
| 
      
 53 
     | 
    
         
            +
                                  p.reap
         
     | 
| 
      
 54 
     | 
    
         
            +
                                  p.flush
         
     | 
| 
      
 55 
     | 
    
         
            +
                                rescue WeakRef::RefError
         
     | 
| 
      
 56 
     | 
    
         
            +
                                end
         
     | 
| 
      
 57 
     | 
    
         
            +
             
     | 
| 
      
 58 
     | 
    
         
            +
                                if @pools[frequency].empty?
         
     | 
| 
      
 59 
     | 
    
         
            +
                                  @pools.delete(frequency)
         
     | 
| 
      
 60 
     | 
    
         
            +
                                  @threads.delete(frequency)
         
     | 
| 
      
 61 
     | 
    
         
            +
                                  running = false
         
     | 
| 
      
 62 
     | 
    
         
            +
                                end
         
     | 
| 
      
 63 
     | 
    
         
            +
                              end
         
     | 
| 
      
 64 
     | 
    
         
            +
                            end
         
     | 
| 
      
 65 
     | 
    
         
            +
                          end
         
     | 
| 
      
 66 
     | 
    
         
            +
                        end
         
     | 
| 
      
 67 
     | 
    
         
            +
                    end
         
     | 
| 
      
 68 
     | 
    
         
            +
             
     | 
| 
      
 69 
     | 
    
         
            +
                    def run
         
     | 
| 
      
 70 
     | 
    
         
            +
                      return unless frequency && frequency > 0
         
     | 
| 
      
 71 
     | 
    
         
            +
                      self.class.register_pool(pool, frequency)
         
     | 
| 
      
 72 
     | 
    
         
            +
                    end
         
     | 
| 
      
 73 
     | 
    
         
            +
                  end
         
     | 
| 
      
 74 
     | 
    
         
            +
                end
         
     | 
| 
      
 75 
     | 
    
         
            +
              end
         
     | 
| 
      
 76 
     | 
    
         
            +
            end
         
     |