Skip to content

Commit f3b4cdb

Browse files
committed
Move AbstractAdapter#valid_type? to a class method
since this method only needs access to a constant, and does not actually need a live connection. Continue to support instance methods `#valid_type?` and `#native_database_types`, but both of these methods now forward to a class method of the same name. (All the concrete adapters now implement a class method `.native_database_types`.) This allows code without a connection to check the column type without leasing a connection.
1 parent 7645f01 commit f3b4cdb

File tree

7 files changed

+24
-14
lines changed

7 files changed

+24
-14
lines changed

activerecord/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
* Model generator no longer needs a database connection to validate column types.
2+
3+
*Mike Dalessio*
4+
15
* Allow signed ID verifiers to be configurable via `Rails.application.message_verifiers`
26

37
Prior to this change, the primary way to configure signed ID verifiers was

activerecord/lib/active_record/connection_adapters/abstract_adapter.rb

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,11 @@ def to_s
260260
end
261261

262262
def valid_type?(type) # :nodoc:
263-
!native_database_types[type].nil?
263+
self.class.valid_type?(type)
264+
end
265+
266+
def native_database_types # :nodoc:
267+
self.class.native_database_types
264268
end
265269

266270
# this method must only be called while holding connection pool's mutex
@@ -886,6 +890,10 @@ def extended_type_map(default_timezone:) # :nodoc:
886890
end
887891
end
888892

893+
def valid_type?(type) # :nodoc:
894+
!native_database_types[type].nil?
895+
end
896+
889897
private
890898
def initialize_type_map(m)
891899
register_class_with_limit m, %r(boolean)i, Type::Boolean

activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,10 @@ def dbconsole(config, options = {})
8181

8282
find_cmd_and_exec(ActiveRecord.database_cli[:mysql], *args)
8383
end
84+
85+
def native_database_types # :nodoc:
86+
NATIVE_DATABASE_TYPES
87+
end
8488
end
8589

8690
def get_database_version # :nodoc:
@@ -196,10 +200,6 @@ def release_advisory_lock(lock_name) # :nodoc:
196200
query_value("SELECT RELEASE_LOCK(#{quote(lock_name.to_s)})") == 1
197201
end
198202

199-
def native_database_types
200-
NATIVE_DATABASE_TYPES
201-
end
202-
203203
def index_algorithms
204204
{
205205
default: "ALGORITHM = DEFAULT",

activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -397,10 +397,6 @@ def discard! # :nodoc:
397397
@raw_connection = nil
398398
end
399399

400-
def native_database_types # :nodoc:
401-
self.class.native_database_types
402-
end
403-
404400
def self.native_database_types # :nodoc:
405401
@native_database_types ||= begin
406402
types = NATIVE_DATABASE_TYPES.dup

activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,10 @@ def dbconsole(config, options = {})
6666

6767
find_cmd_and_exec(ActiveRecord.database_cli[:sqlite], *args)
6868
end
69+
70+
def native_database_types # :nodoc:
71+
NATIVE_DATABASE_TYPES
72+
end
6973
end
7074

7175
include SQLite3::Quoting
@@ -258,10 +262,6 @@ def supports_index_sort_order?
258262
true
259263
end
260264

261-
def native_database_types # :nodoc:
262-
NATIVE_DATABASE_TYPES
263-
end
264-
265265
# Returns the current database encoding format as a string, e.g. 'UTF-8'
266266
def encoding
267267
any_raw_connection.encoding.to_s

activerecord/test/cases/adapter_test.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,13 @@ def test_create_record_with_pk_as_zero
4848
def test_valid_column
4949
@connection.native_database_types.each_key do |type|
5050
assert @connection.valid_type?(type)
51+
assert @connection.class.valid_type?(type)
5152
end
5253
end
5354

5455
def test_invalid_column
5556
assert_not @connection.valid_type?(:foobar)
57+
assert_not @connection.class.valid_type?(:foobar)
5658
end
5759

5860
def test_tables

railties/lib/rails/generators/generated_attribute.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ def dangerous_name?(name)
7373
def valid_type?(type)
7474
DEFAULT_TYPES.include?(type.to_s) ||
7575
!defined?(ActiveRecord::Base) ||
76-
ActiveRecord::Base.lease_connection.valid_type?(type)
76+
ActiveRecord::Base.connection_db_config.adapter_class.valid_type?(type)
7777
end
7878

7979
def valid_index_type?(index_type)

0 commit comments

Comments
 (0)