From d6466beb9fff9f2ba4f73673e65f087dd6bba488 Mon Sep 17 00:00:00 2001 From: eileencodes Date: Mon, 20 Feb 2017 13:35:19 -0500 Subject: Ensure test threads share a DB connection This ensures multiple threads inside a transactional test to see consistent database state. When a system test starts Puma spins up one thread and Capybara spins up another thread. Because of this when tests are run the database cannot see what was inserted into the database on teardown. This is because there are two threads using two different connections. This change uses the statement cache to lock the threads to using a single connection ID instead of each not being able to see each other. This code only runs in the fixture setup and teardown so it does not affect real production databases. When a transaction is opened we set `lock_thread` to `Thread.current` so we can keep track of which connection the thread is using. When we rollback the transaction we unlock the thread and then there will be no left-over data in the database because the transaction will roll back the correct connections. [ Eileen M. Uchitelle, Matthew Draper ] --- .../lib/active_record/connection_adapters/abstract_adapter.rb | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) (limited to 'activerecord/lib/active_record/connection_adapters/abstract_adapter.rb') diff --git a/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb b/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb index 6b14a498df..b31ce0a181 100644 --- a/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb @@ -107,6 +107,7 @@ module ActiveRecord @schema_cache = SchemaCache.new self @quoted_column_names, @quoted_table_names = {}, {} @visitor = arel_visitor + @lock = Monitor.new if self.class.type_cast_config_to_boolean(config.fetch(:prepared_statements) { true }) @prepared_statements = true @@ -605,7 +606,11 @@ module ActiveRecord binds: binds, type_casted_binds: type_casted_binds, statement_name: statement_name, - connection_id: object_id) { yield } + connection_id: object_id) do + @lock.synchronize do + yield + end + end rescue => e raise translate_exception_class(e, sql) end -- cgit v1.2.3 From 08e781569aea7f0e084c169c70b452e337aa1b5f Mon Sep 17 00:00:00 2001 From: Kir Shatrov Date: Sat, 25 Feb 2017 16:33:06 -0600 Subject: Deprecate AbstractAdapter#verify! with arguments --- activerecord/lib/active_record/connection_adapters/abstract_adapter.rb | 3 +++ 1 file changed, 3 insertions(+) (limited to 'activerecord/lib/active_record/connection_adapters/abstract_adapter.rb') diff --git a/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb b/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb index fd11cab5c0..b66d3a2e14 100644 --- a/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb @@ -439,6 +439,9 @@ module ActiveRecord # This is done under the hood by calling #active?. If the connection # is no longer active, then this method will reconnect to the database. def verify!(*ignored) + if ignored.size > 0 + ActiveSupport::Deprecation.warn("Passing arguments to #verify method of the connection has no affect and has been deprecated. Please remove all arguments from the #verify method call.") + end reconnect! unless active? end -- cgit v1.2.3 From d15b29177be06c14c8319d4726bb0e39c558faf0 Mon Sep 17 00:00:00 2001 From: Rebecca Skinner Date: Sun, 26 Feb 2017 15:47:15 +0800 Subject: Fix typo 'affect' -> 'effect' [ci skip] --- activerecord/lib/active_record/connection_adapters/abstract_adapter.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'activerecord/lib/active_record/connection_adapters/abstract_adapter.rb') diff --git a/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb b/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb index 26eb70d7cd..24007db8f0 100644 --- a/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb @@ -440,7 +440,7 @@ module ActiveRecord # is no longer active, then this method will reconnect to the database. def verify!(*ignored) if ignored.size > 0 - ActiveSupport::Deprecation.warn("Passing arguments to #verify method of the connection has no affect and has been deprecated. Please remove all arguments from the #verify method call.") + ActiveSupport::Deprecation.warn("Passing arguments to #verify method of the connection has no effect and has been deprecated. Please remove all arguments from the #verify method call.") end reconnect! unless active? end -- cgit v1.2.3 From b2548869bc6a30a19a992b7a29f07868e1359985 Mon Sep 17 00:00:00 2001 From: Ryuta Kamizono Date: Sun, 26 Feb 2017 17:14:03 +0900 Subject: Push `valid_type?` up to abstract adapter `valid_type?` should return true if a type exists in `native_database_types` at least. https://github.com/rails/rails/blob/v5.1.0.beta1/activerecord/lib/active_record/schema_dumper.rb#L136 --- .../lib/active_record/connection_adapters/abstract_adapter.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'activerecord/lib/active_record/connection_adapters/abstract_adapter.rb') diff --git a/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb b/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb index 26eb70d7cd..4595f56e57 100644 --- a/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb @@ -154,8 +154,8 @@ module ActiveRecord Arel::Visitors::ToSql.new(self) end - def valid_type?(type) - false + def valid_type?(type) # :nodoc: + !native_database_types[type].nil? end def schema_creation -- cgit v1.2.3 From 322a4a13107b66814d3d5f802c7f63bbc6dd8f93 Mon Sep 17 00:00:00 2001 From: Ryuta Kamizono Date: Sun, 26 Feb 2017 01:45:04 +0900 Subject: Deprecate `supports_migrations?` on connection adapters `supports_migrations?` was added at 4160b518 to determine if schema statements (`create_table`, `drop_table`, etc) are implemented in the adapter. But all tested databases has been supported migrations since a4fc93c3 at least. --- .../lib/active_record/connection_adapters/abstract_adapter.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'activerecord/lib/active_record/connection_adapters/abstract_adapter.rb') diff --git a/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb b/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb index 5e3b8fd393..ef1d9f81a9 100644 --- a/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb @@ -232,10 +232,10 @@ module ActiveRecord self.class::ADAPTER_NAME end - # Does this adapter support migrations? - def supports_migrations? - false + def supports_migrations? # :nodoc: + true end + deprecate :supports_migrations? def supports_primary_key? # :nodoc: true -- cgit v1.2.3 From fdc3e789b5dada526f530969308e24874b9b1986 Mon Sep 17 00:00:00 2001 From: Ryuta Kamizono Date: Mon, 20 Mar 2017 20:27:02 +0900 Subject: Remove duplicated `columns` definition --- .../lib/active_record/connection_adapters/abstract_adapter.rb | 8 -------- 1 file changed, 8 deletions(-) (limited to 'activerecord/lib/active_record/connection_adapters/abstract_adapter.rb') diff --git a/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb b/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb index ef1d9f81a9..b9fbcd05c7 100644 --- a/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb @@ -162,14 +162,6 @@ module ActiveRecord SchemaCreation.new self end - # Returns an array of +Column+ objects for the table specified by +table_name+. - def columns(table_name) # :nodoc: - table_name = table_name.to_s - column_definitions(table_name).map do |field| - new_column_from_field(table_name, field) - end - end - # this method must only be called while holding connection pool's mutex def lease if in_use? -- cgit v1.2.3 From f28a331023fab241f694c53802b45fb2d66c2c4f Mon Sep 17 00:00:00 2001 From: Lars Kanis Date: Wed, 22 Mar 2017 11:10:03 +0100 Subject: [PostgreSQL]: Replace deprecated PG constants. The old top level classes PGconn, PGresult and PGError were deprecated since pg-0.13.0: https://github.com/ged/ruby-pg/blob/master/History.rdoc#v0130-2012-02-09-michael-granger-gedfaeriemudorg --- activerecord/lib/active_record/connection_adapters/abstract_adapter.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'activerecord/lib/active_record/connection_adapters/abstract_adapter.rb') diff --git a/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb b/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb index b9fbcd05c7..550b4cc86b 100644 --- a/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb @@ -439,7 +439,7 @@ module ActiveRecord # Provides access to the underlying database driver for this adapter. For # example, this method returns a Mysql2::Client object in case of Mysql2Adapter, - # and a PGconn object in case of PostgreSQLAdapter. + # and a PG::Connection object in case of PostgreSQLAdapter. # # This is useful for when you need to call a proprietary method such as # PostgreSQL's lo_* methods. -- cgit v1.2.3 From e4108fc619e0f1c28cdec6049d31f2db01d56dfd Mon Sep 17 00:00:00 2001 From: Ryuta Kamizono Date: Fri, 17 Feb 2017 22:58:52 +0900 Subject: Make internal methods to private --- .../connection_adapters/abstract_adapter.rb | 40 +++++++--------------- 1 file changed, 13 insertions(+), 27 deletions(-) (limited to 'activerecord/lib/active_record/connection_adapters/abstract_adapter.rb') diff --git a/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb b/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb index 550b4cc86b..96083e6519 100644 --- a/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb @@ -74,7 +74,7 @@ module ActiveRecord SIMPLE_INT = /\A\d+\z/ attr_accessor :visitor, :pool - attr_reader :schema_cache, :owner, :logger + attr_reader :schema_cache, :owner, :logger, :prepared_statements alias :in_use? :owner def self.type_cast_config_to_integer(config) @@ -93,8 +93,6 @@ module ActiveRecord end end - attr_reader :prepared_statements - def initialize(connection, logger = nil, config = {}) # :nodoc: super() @@ -142,26 +140,10 @@ module ActiveRecord end end - def collector - if prepared_statements - SQLString.new - else - BindCollector.new - end - end - - def arel_visitor # :nodoc: - Arel::Visitors::ToSql.new(self) - end - def valid_type?(type) # :nodoc: !native_database_types[type].nil? end - def schema_creation - SchemaCreation.new self - end - # this method must only be called while holding connection pool's mutex def lease if in_use? @@ -475,14 +457,6 @@ module ActiveRecord end end - def new_column(name, default, sql_type_metadata, null, table_name, default_function = nil, collation = nil) # :nodoc: - Column.new(name, default, sql_type_metadata, null, table_name, default_function, collation) - end - - def lookup_cast_type(sql_type) # :nodoc: - type_map.lookup(sql_type) - end - def column_name_for_operation(operation, node) # :nodoc: visitor.accept(node, collector).value end @@ -629,6 +603,18 @@ module ActiveRecord columns(table_name).detect { |c| c.name == column_name } || raise(ActiveRecordError, "No such column: #{table_name}.#{column_name}") end + + def collector + if prepared_statements + SQLString.new + else + BindCollector.new + end + end + + def arel_visitor + Arel::Visitors::ToSql.new(self) + end end end end -- cgit v1.2.3