activerecord 1.0.0 → 3.0.0
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of activerecord might be problematic. Click here for more details.
- data/CHANGELOG +5518 -76
- data/README.rdoc +222 -0
- data/examples/performance.rb +162 -0
- data/examples/simple.rb +14 -0
- data/lib/active_record/aggregations.rb +192 -80
- data/lib/active_record/association_preload.rb +403 -0
- data/lib/active_record/associations/association_collection.rb +545 -53
- data/lib/active_record/associations/association_proxy.rb +295 -0
- data/lib/active_record/associations/belongs_to_association.rb +91 -0
- data/lib/active_record/associations/belongs_to_polymorphic_association.rb +78 -0
- data/lib/active_record/associations/has_and_belongs_to_many_association.rb +127 -36
- data/lib/active_record/associations/has_many_association.rb +108 -84
- data/lib/active_record/associations/has_many_through_association.rb +116 -0
- data/lib/active_record/associations/has_one_association.rb +143 -0
- data/lib/active_record/associations/has_one_through_association.rb +40 -0
- data/lib/active_record/associations/through_association_scope.rb +154 -0
- data/lib/active_record/associations.rb +2086 -368
- data/lib/active_record/attribute_methods/before_type_cast.rb +33 -0
- data/lib/active_record/attribute_methods/dirty.rb +95 -0
- data/lib/active_record/attribute_methods/primary_key.rb +50 -0
- data/lib/active_record/attribute_methods/query.rb +39 -0
- data/lib/active_record/attribute_methods/read.rb +116 -0
- data/lib/active_record/attribute_methods/time_zone_conversion.rb +61 -0
- data/lib/active_record/attribute_methods/write.rb +37 -0
- data/lib/active_record/attribute_methods.rb +60 -0
- data/lib/active_record/autosave_association.rb +369 -0
- data/lib/active_record/base.rb +1603 -721
- data/lib/active_record/callbacks.rb +176 -225
- data/lib/active_record/connection_adapters/abstract/connection_pool.rb +365 -0
- data/lib/active_record/connection_adapters/abstract/connection_specification.rb +113 -0
- data/lib/active_record/connection_adapters/abstract/database_limits.rb +57 -0
- data/lib/active_record/connection_adapters/abstract/database_statements.rb +329 -0
- data/lib/active_record/connection_adapters/abstract/query_cache.rb +81 -0
- data/lib/active_record/connection_adapters/abstract/quoting.rb +72 -0
- data/lib/active_record/connection_adapters/abstract/schema_definitions.rb +739 -0
- data/lib/active_record/connection_adapters/abstract/schema_statements.rb +543 -0
- data/lib/active_record/connection_adapters/abstract_adapter.rb +165 -279
- data/lib/active_record/connection_adapters/mysql_adapter.rb +594 -82
- data/lib/active_record/connection_adapters/postgresql_adapter.rb +988 -135
- data/lib/active_record/connection_adapters/sqlite3_adapter.rb +53 -0
- data/lib/active_record/connection_adapters/sqlite_adapter.rb +365 -71
- data/lib/active_record/counter_cache.rb +115 -0
- data/lib/active_record/dynamic_finder_match.rb +53 -0
- data/lib/active_record/dynamic_scope_match.rb +32 -0
- data/lib/active_record/errors.rb +172 -0
- data/lib/active_record/fixtures.rb +941 -105
- data/lib/active_record/locale/en.yml +40 -0
- data/lib/active_record/locking/optimistic.rb +172 -0
- data/lib/active_record/locking/pessimistic.rb +55 -0
- data/lib/active_record/log_subscriber.rb +48 -0
- data/lib/active_record/migration.rb +617 -0
- data/lib/active_record/named_scope.rb +138 -0
- data/lib/active_record/nested_attributes.rb +417 -0
- data/lib/active_record/observer.rb +105 -36
- data/lib/active_record/persistence.rb +291 -0
- data/lib/active_record/query_cache.rb +36 -0
- data/lib/active_record/railtie.rb +91 -0
- data/lib/active_record/railties/controller_runtime.rb +38 -0
- data/lib/active_record/railties/databases.rake +512 -0
- data/lib/active_record/reflection.rb +364 -87
- data/lib/active_record/relation/batches.rb +89 -0
- data/lib/active_record/relation/calculations.rb +286 -0
- data/lib/active_record/relation/finder_methods.rb +355 -0
- data/lib/active_record/relation/predicate_builder.rb +41 -0
- data/lib/active_record/relation/query_methods.rb +261 -0
- data/lib/active_record/relation/spawn_methods.rb +112 -0
- data/lib/active_record/relation.rb +393 -0
- data/lib/active_record/schema.rb +59 -0
- data/lib/active_record/schema_dumper.rb +195 -0
- data/lib/active_record/serialization.rb +60 -0
- data/lib/active_record/serializers/xml_serializer.rb +244 -0
- data/lib/active_record/session_store.rb +340 -0
- data/lib/active_record/test_case.rb +67 -0
- data/lib/active_record/timestamp.rb +88 -0
- data/lib/active_record/transactions.rb +329 -75
- data/lib/active_record/validations/associated.rb +48 -0
- data/lib/active_record/validations/uniqueness.rb +185 -0
- data/lib/active_record/validations.rb +58 -179
- data/lib/active_record/version.rb +9 -0
- data/lib/active_record.rb +100 -24
- data/lib/rails/generators/active_record/migration/migration_generator.rb +25 -0
- data/lib/rails/generators/active_record/migration/templates/migration.rb +17 -0
- data/lib/rails/generators/active_record/model/model_generator.rb +38 -0
- data/lib/rails/generators/active_record/model/templates/migration.rb +16 -0
- data/lib/rails/generators/active_record/model/templates/model.rb +5 -0
- data/lib/rails/generators/active_record/model/templates/module.rb +5 -0
- data/lib/rails/generators/active_record/observer/observer_generator.rb +15 -0
- data/lib/rails/generators/active_record/observer/templates/observer.rb +2 -0
- data/lib/rails/generators/active_record/session_migration/session_migration_generator.rb +24 -0
- data/lib/rails/generators/active_record/session_migration/templates/migration.rb +16 -0
- data/lib/rails/generators/active_record.rb +27 -0
- metadata +216 -158
- data/README +0 -361
- data/RUNNING_UNIT_TESTS +0 -36
- data/dev-utils/eval_debugger.rb +0 -9
- data/examples/associations.rb +0 -87
- data/examples/shared_setup.rb +0 -15
- data/examples/validation.rb +0 -88
- data/install.rb +0 -60
- data/lib/active_record/deprecated_associations.rb +0 -70
- data/lib/active_record/support/class_attribute_accessors.rb +0 -43
- data/lib/active_record/support/class_inheritable_attributes.rb +0 -37
- data/lib/active_record/support/clean_logger.rb +0 -10
- data/lib/active_record/support/inflector.rb +0 -70
- data/lib/active_record/vendor/mysql.rb +0 -1117
- data/lib/active_record/vendor/simple.rb +0 -702
- data/lib/active_record/wrappers/yaml_wrapper.rb +0 -15
- data/lib/active_record/wrappings.rb +0 -59
- data/rakefile +0 -122
- data/test/abstract_unit.rb +0 -16
- data/test/aggregations_test.rb +0 -34
- data/test/all.sh +0 -8
- data/test/associations_test.rb +0 -477
- data/test/base_test.rb +0 -513
- data/test/class_inheritable_attributes_test.rb +0 -33
- data/test/connections/native_mysql/connection.rb +0 -24
- data/test/connections/native_postgresql/connection.rb +0 -24
- data/test/connections/native_sqlite/connection.rb +0 -24
- data/test/deprecated_associations_test.rb +0 -336
- data/test/finder_test.rb +0 -67
- data/test/fixtures/accounts/signals37 +0 -3
- data/test/fixtures/accounts/unknown +0 -2
- data/test/fixtures/auto_id.rb +0 -4
- data/test/fixtures/column_name.rb +0 -3
- data/test/fixtures/companies/first_client +0 -6
- data/test/fixtures/companies/first_firm +0 -4
- data/test/fixtures/companies/second_client +0 -6
- data/test/fixtures/company.rb +0 -37
- data/test/fixtures/company_in_module.rb +0 -33
- data/test/fixtures/course.rb +0 -3
- data/test/fixtures/courses/java +0 -2
- data/test/fixtures/courses/ruby +0 -2
- data/test/fixtures/customer.rb +0 -30
- data/test/fixtures/customers/david +0 -6
- data/test/fixtures/db_definitions/mysql.sql +0 -96
- data/test/fixtures/db_definitions/mysql2.sql +0 -4
- data/test/fixtures/db_definitions/postgresql.sql +0 -113
- data/test/fixtures/db_definitions/postgresql2.sql +0 -4
- data/test/fixtures/db_definitions/sqlite.sql +0 -85
- data/test/fixtures/db_definitions/sqlite2.sql +0 -4
- data/test/fixtures/default.rb +0 -2
- data/test/fixtures/developer.rb +0 -8
- data/test/fixtures/developers/david +0 -2
- data/test/fixtures/developers/jamis +0 -2
- data/test/fixtures/developers_projects/david_action_controller +0 -2
- data/test/fixtures/developers_projects/david_active_record +0 -2
- data/test/fixtures/developers_projects/jamis_active_record +0 -2
- data/test/fixtures/entrant.rb +0 -3
- data/test/fixtures/entrants/first +0 -3
- data/test/fixtures/entrants/second +0 -3
- data/test/fixtures/entrants/third +0 -3
- data/test/fixtures/fixture_database.sqlite +0 -0
- data/test/fixtures/fixture_database_2.sqlite +0 -0
- data/test/fixtures/movie.rb +0 -5
- data/test/fixtures/movies/first +0 -2
- data/test/fixtures/movies/second +0 -2
- data/test/fixtures/project.rb +0 -3
- data/test/fixtures/projects/action_controller +0 -2
- data/test/fixtures/projects/active_record +0 -2
- data/test/fixtures/reply.rb +0 -21
- data/test/fixtures/subscriber.rb +0 -5
- data/test/fixtures/subscribers/first +0 -2
- data/test/fixtures/subscribers/second +0 -2
- data/test/fixtures/topic.rb +0 -20
- data/test/fixtures/topics/first +0 -9
- data/test/fixtures/topics/second +0 -8
- data/test/fixtures_test.rb +0 -20
- data/test/inflector_test.rb +0 -104
- data/test/inheritance_test.rb +0 -125
- data/test/lifecycle_test.rb +0 -110
- data/test/modules_test.rb +0 -21
- data/test/multiple_db_test.rb +0 -46
- data/test/pk_test.rb +0 -57
- data/test/reflection_test.rb +0 -78
- data/test/thread_safety_test.rb +0 -33
- data/test/transactions_test.rb +0 -83
- data/test/unconnected_test.rb +0 -24
- data/test/validations_test.rb +0 -126
@@ -0,0 +1,365 @@
|
|
1
|
+
require 'monitor'
|
2
|
+
require 'set'
|
3
|
+
require 'active_support/core_ext/module/synchronization'
|
4
|
+
|
5
|
+
module ActiveRecord
|
6
|
+
# Raised when a connection could not be obtained within the connection
|
7
|
+
# acquisition timeout period.
|
8
|
+
class ConnectionTimeoutError < ConnectionNotEstablished
|
9
|
+
end
|
10
|
+
|
11
|
+
module ConnectionAdapters
|
12
|
+
# Connection pool base class for managing Active Record database
|
13
|
+
# connections.
|
14
|
+
#
|
15
|
+
# == Introduction
|
16
|
+
#
|
17
|
+
# A connection pool synchronizes thread access to a limited number of
|
18
|
+
# database connections. The basic idea is that each thread checks out a
|
19
|
+
# database connection from the pool, uses that connection, and checks the
|
20
|
+
# connection back in. ConnectionPool is completely thread-safe, and will
|
21
|
+
# ensure that a connection cannot be used by two threads at the same time,
|
22
|
+
# as long as ConnectionPool's contract is correctly followed. It will also
|
23
|
+
# handle cases in which there are more threads than connections: if all
|
24
|
+
# connections have been checked out, and a thread tries to checkout a
|
25
|
+
# connection anyway, then ConnectionPool will wait until some other thread
|
26
|
+
# has checked in a connection.
|
27
|
+
#
|
28
|
+
# == Obtaining (checking out) a connection
|
29
|
+
#
|
30
|
+
# Connections can be obtained and used from a connection pool in several
|
31
|
+
# ways:
|
32
|
+
#
|
33
|
+
# 1. Simply use ActiveRecord::Base.connection as with Active Record 2.1 and
|
34
|
+
# earlier (pre-connection-pooling). Eventually, when you're done with
|
35
|
+
# the connection(s) and wish it to be returned to the pool, you call
|
36
|
+
# ActiveRecord::Base.clear_active_connections!. This will be the
|
37
|
+
# default behavior for Active Record when used in conjunction with
|
38
|
+
# Action Pack's request handling cycle.
|
39
|
+
# 2. Manually check out a connection from the pool with
|
40
|
+
# ActiveRecord::Base.connection_pool.checkout. You are responsible for
|
41
|
+
# returning this connection to the pool when finished by calling
|
42
|
+
# ActiveRecord::Base.connection_pool.checkin(connection).
|
43
|
+
# 3. Use ActiveRecord::Base.connection_pool.with_connection(&block), which
|
44
|
+
# obtains a connection, yields it as the sole argument to the block,
|
45
|
+
# and returns it to the pool after the block completes.
|
46
|
+
#
|
47
|
+
# Connections in the pool are actually AbstractAdapter objects (or objects
|
48
|
+
# compatible with AbstractAdapter's interface).
|
49
|
+
#
|
50
|
+
# == Options
|
51
|
+
#
|
52
|
+
# There are two connection-pooling-related options that you can add to
|
53
|
+
# your database connection configuration:
|
54
|
+
#
|
55
|
+
# * +pool+: number indicating size of connection pool (default 5)
|
56
|
+
# * +wait_timeout+: number of seconds to block and wait for a connection
|
57
|
+
# before giving up and raising a timeout error (default 5 seconds).
|
58
|
+
class ConnectionPool
|
59
|
+
attr_reader :spec, :connections
|
60
|
+
|
61
|
+
# Creates a new ConnectionPool object. +spec+ is a ConnectionSpecification
|
62
|
+
# object which describes database connection information (e.g. adapter,
|
63
|
+
# host name, username, password, etc), as well as the maximum size for
|
64
|
+
# this ConnectionPool.
|
65
|
+
#
|
66
|
+
# The default ConnectionPool maximum size is 5.
|
67
|
+
def initialize(spec)
|
68
|
+
@spec = spec
|
69
|
+
|
70
|
+
# The cache of reserved connections mapped to threads
|
71
|
+
@reserved_connections = {}
|
72
|
+
|
73
|
+
# The mutex used to synchronize pool access
|
74
|
+
@connection_mutex = Monitor.new
|
75
|
+
@queue = @connection_mutex.new_cond
|
76
|
+
|
77
|
+
# default 5 second timeout unless on ruby 1.9
|
78
|
+
@timeout =
|
79
|
+
if RUBY_VERSION < '1.9'
|
80
|
+
spec.config[:wait_timeout] || 5
|
81
|
+
end
|
82
|
+
|
83
|
+
# default max pool size to 5
|
84
|
+
@size = (spec.config[:pool] && spec.config[:pool].to_i) || 5
|
85
|
+
|
86
|
+
@connections = []
|
87
|
+
@checked_out = []
|
88
|
+
end
|
89
|
+
|
90
|
+
# Retrieve the connection associated with the current thread, or call
|
91
|
+
# #checkout to obtain one if necessary.
|
92
|
+
#
|
93
|
+
# #connection can be called any number of times; the connection is
|
94
|
+
# held in a hash keyed by the thread id.
|
95
|
+
def connection
|
96
|
+
@reserved_connections[current_connection_id] ||= checkout
|
97
|
+
end
|
98
|
+
|
99
|
+
# Signal that the thread is finished with the current connection.
|
100
|
+
# #release_connection releases the connection-thread association
|
101
|
+
# and returns the connection to the pool.
|
102
|
+
def release_connection(with_id = current_connection_id)
|
103
|
+
conn = @reserved_connections.delete(with_id)
|
104
|
+
checkin conn if conn
|
105
|
+
end
|
106
|
+
|
107
|
+
# If a connection already exists yield it to the block. If no connection
|
108
|
+
# exists checkout a connection, yield it to the block, and checkin the
|
109
|
+
# connection when finished.
|
110
|
+
def with_connection
|
111
|
+
connection_id = current_connection_id
|
112
|
+
fresh_connection = true unless @reserved_connections[connection_id]
|
113
|
+
yield connection
|
114
|
+
ensure
|
115
|
+
release_connection(connection_id) if fresh_connection
|
116
|
+
end
|
117
|
+
|
118
|
+
# Returns true if a connection has already been opened.
|
119
|
+
def connected?
|
120
|
+
!@connections.empty?
|
121
|
+
end
|
122
|
+
|
123
|
+
# Disconnects all connections in the pool, and clears the pool.
|
124
|
+
def disconnect!
|
125
|
+
@reserved_connections.each do |name,conn|
|
126
|
+
checkin conn
|
127
|
+
end
|
128
|
+
@reserved_connections = {}
|
129
|
+
@connections.each do |conn|
|
130
|
+
conn.disconnect!
|
131
|
+
end
|
132
|
+
@connections = []
|
133
|
+
end
|
134
|
+
|
135
|
+
# Clears the cache which maps classes
|
136
|
+
def clear_reloadable_connections!
|
137
|
+
@reserved_connections.each do |name, conn|
|
138
|
+
checkin conn
|
139
|
+
end
|
140
|
+
@reserved_connections = {}
|
141
|
+
@connections.each do |conn|
|
142
|
+
conn.disconnect! if conn.requires_reloading?
|
143
|
+
end
|
144
|
+
@connections.delete_if do |conn|
|
145
|
+
conn.requires_reloading?
|
146
|
+
end
|
147
|
+
end
|
148
|
+
|
149
|
+
# Verify active connections and remove and disconnect connections
|
150
|
+
# associated with stale threads.
|
151
|
+
def verify_active_connections! #:nodoc:
|
152
|
+
clear_stale_cached_connections!
|
153
|
+
@connections.each do |connection|
|
154
|
+
connection.verify!
|
155
|
+
end
|
156
|
+
end
|
157
|
+
|
158
|
+
# Return any checked-out connections back to the pool by threads that
|
159
|
+
# are no longer alive.
|
160
|
+
def clear_stale_cached_connections!
|
161
|
+
keys = @reserved_connections.keys - Thread.list.find_all { |t|
|
162
|
+
t.alive?
|
163
|
+
}.map { |thread| thread.object_id }
|
164
|
+
|
165
|
+
keys.each do |key|
|
166
|
+
checkin @reserved_connections[key]
|
167
|
+
@reserved_connections.delete(key)
|
168
|
+
end
|
169
|
+
end
|
170
|
+
|
171
|
+
# Check-out a database connection from the pool, indicating that you want
|
172
|
+
# to use it. You should call #checkin when you no longer need this.
|
173
|
+
#
|
174
|
+
# This is done by either returning an existing connection, or by creating
|
175
|
+
# a new connection. If the maximum number of connections for this pool has
|
176
|
+
# already been reached, but the pool is empty (i.e. they're all being used),
|
177
|
+
# then this method will wait until a thread has checked in a connection.
|
178
|
+
# The wait time is bounded however: if no connection can be checked out
|
179
|
+
# within the timeout specified for this pool, then a ConnectionTimeoutError
|
180
|
+
# exception will be raised.
|
181
|
+
#
|
182
|
+
# Returns: an AbstractAdapter object.
|
183
|
+
#
|
184
|
+
# Raises:
|
185
|
+
# - ConnectionTimeoutError: no connection can be obtained from the pool
|
186
|
+
# within the timeout period.
|
187
|
+
def checkout
|
188
|
+
# Checkout an available connection
|
189
|
+
@connection_mutex.synchronize do
|
190
|
+
loop do
|
191
|
+
conn = if @checked_out.size < @connections.size
|
192
|
+
checkout_existing_connection
|
193
|
+
elsif @connections.size < @size
|
194
|
+
checkout_new_connection
|
195
|
+
end
|
196
|
+
return conn if conn
|
197
|
+
# No connections available; wait for one
|
198
|
+
if @queue.wait(@timeout)
|
199
|
+
next
|
200
|
+
else
|
201
|
+
# try looting dead threads
|
202
|
+
clear_stale_cached_connections!
|
203
|
+
if @size == @checked_out.size
|
204
|
+
raise ConnectionTimeoutError, "could not obtain a database connection#{" within #{@timeout} seconds" if @timeout}. The max pool size is currently #{@size}; consider increasing it."
|
205
|
+
end
|
206
|
+
end
|
207
|
+
end
|
208
|
+
end
|
209
|
+
end
|
210
|
+
|
211
|
+
# Check-in a database connection back into the pool, indicating that you
|
212
|
+
# no longer need this connection.
|
213
|
+
#
|
214
|
+
# +conn+: an AbstractAdapter object, which was obtained by earlier by
|
215
|
+
# calling +checkout+ on this pool.
|
216
|
+
def checkin(conn)
|
217
|
+
@connection_mutex.synchronize do
|
218
|
+
conn.send(:_run_checkin_callbacks) do
|
219
|
+
@checked_out.delete conn
|
220
|
+
@queue.signal
|
221
|
+
end
|
222
|
+
end
|
223
|
+
end
|
224
|
+
|
225
|
+
synchronize :clear_reloadable_connections!, :verify_active_connections!,
|
226
|
+
:connected?, :disconnect!, :with => :@connection_mutex
|
227
|
+
|
228
|
+
private
|
229
|
+
def new_connection
|
230
|
+
ActiveRecord::Base.send(spec.adapter_method, spec.config)
|
231
|
+
end
|
232
|
+
|
233
|
+
def current_connection_id #:nodoc:
|
234
|
+
Thread.current.object_id
|
235
|
+
end
|
236
|
+
|
237
|
+
def checkout_new_connection
|
238
|
+
c = new_connection
|
239
|
+
@connections << c
|
240
|
+
checkout_and_verify(c)
|
241
|
+
end
|
242
|
+
|
243
|
+
def checkout_existing_connection
|
244
|
+
c = (@connections - @checked_out).first
|
245
|
+
checkout_and_verify(c)
|
246
|
+
end
|
247
|
+
|
248
|
+
def checkout_and_verify(c)
|
249
|
+
c.run_callbacks :checkout do
|
250
|
+
c.verify!
|
251
|
+
@checked_out << c
|
252
|
+
end
|
253
|
+
c
|
254
|
+
end
|
255
|
+
end
|
256
|
+
|
257
|
+
# ConnectionHandler is a collection of ConnectionPool objects. It is used
|
258
|
+
# for keeping separate connection pools for Active Record models that connect
|
259
|
+
# to different databases.
|
260
|
+
#
|
261
|
+
# For example, suppose that you have 5 models, with the following hierarchy:
|
262
|
+
#
|
263
|
+
# |
|
264
|
+
# +-- Book
|
265
|
+
# | |
|
266
|
+
# | +-- ScaryBook
|
267
|
+
# | +-- GoodBook
|
268
|
+
# +-- Author
|
269
|
+
# +-- BankAccount
|
270
|
+
#
|
271
|
+
# Suppose that Book is to connect to a separate database (i.e. one other
|
272
|
+
# than the default database). Then Book, ScaryBook and GoodBook will all use
|
273
|
+
# the same connection pool. Likewise, Author and BankAccount will use the
|
274
|
+
# same connection pool. However, the connection pool used by Author/BankAccount
|
275
|
+
# is not the same as the one used by Book/ScaryBook/GoodBook.
|
276
|
+
#
|
277
|
+
# Normally there is only a single ConnectionHandler instance, accessible via
|
278
|
+
# ActiveRecord::Base.connection_handler. Active Record models use this to
|
279
|
+
# determine that connection pool that they should use.
|
280
|
+
class ConnectionHandler
|
281
|
+
attr_reader :connection_pools
|
282
|
+
|
283
|
+
def initialize(pools = {})
|
284
|
+
@connection_pools = pools
|
285
|
+
end
|
286
|
+
|
287
|
+
def establish_connection(name, spec)
|
288
|
+
@connection_pools[name] = ConnectionAdapters::ConnectionPool.new(spec)
|
289
|
+
end
|
290
|
+
|
291
|
+
# Returns any connections in use by the current thread back to the pool,
|
292
|
+
# and also returns connections to the pool cached by threads that are no
|
293
|
+
# longer alive.
|
294
|
+
def clear_active_connections!
|
295
|
+
@connection_pools.each_value {|pool| pool.release_connection }
|
296
|
+
end
|
297
|
+
|
298
|
+
# Clears the cache which maps classes
|
299
|
+
def clear_reloadable_connections!
|
300
|
+
@connection_pools.each_value {|pool| pool.clear_reloadable_connections! }
|
301
|
+
end
|
302
|
+
|
303
|
+
def clear_all_connections!
|
304
|
+
@connection_pools.each_value {|pool| pool.disconnect! }
|
305
|
+
end
|
306
|
+
|
307
|
+
# Verify active connections.
|
308
|
+
def verify_active_connections! #:nodoc:
|
309
|
+
@connection_pools.each_value {|pool| pool.verify_active_connections! }
|
310
|
+
end
|
311
|
+
|
312
|
+
# Locate the connection of the nearest super class. This can be an
|
313
|
+
# active or defined connection: if it is the latter, it will be
|
314
|
+
# opened and set as the active connection for the class it was defined
|
315
|
+
# for (not necessarily the current class).
|
316
|
+
def retrieve_connection(klass) #:nodoc:
|
317
|
+
pool = retrieve_connection_pool(klass)
|
318
|
+
(pool && pool.connection) or raise ConnectionNotEstablished
|
319
|
+
end
|
320
|
+
|
321
|
+
# Returns true if a connection that's accessible to this class has
|
322
|
+
# already been opened.
|
323
|
+
def connected?(klass)
|
324
|
+
conn = retrieve_connection_pool(klass)
|
325
|
+
conn && conn.connected?
|
326
|
+
end
|
327
|
+
|
328
|
+
# Remove the connection for this class. This will close the active
|
329
|
+
# connection and the defined connection (if they exist). The result
|
330
|
+
# can be used as an argument for establish_connection, for easily
|
331
|
+
# re-establishing the connection.
|
332
|
+
def remove_connection(klass)
|
333
|
+
pool = @connection_pools[klass.name]
|
334
|
+
return nil unless pool
|
335
|
+
|
336
|
+
@connection_pools.delete_if { |key, value| value == pool }
|
337
|
+
pool.disconnect!
|
338
|
+
pool.spec.config
|
339
|
+
end
|
340
|
+
|
341
|
+
def retrieve_connection_pool(klass)
|
342
|
+
pool = @connection_pools[klass.name]
|
343
|
+
return pool if pool
|
344
|
+
return nil if ActiveRecord::Base == klass
|
345
|
+
retrieve_connection_pool klass.superclass
|
346
|
+
end
|
347
|
+
end
|
348
|
+
|
349
|
+
class ConnectionManagement
|
350
|
+
def initialize(app)
|
351
|
+
@app = app
|
352
|
+
end
|
353
|
+
|
354
|
+
def call(env)
|
355
|
+
@app.call(env)
|
356
|
+
ensure
|
357
|
+
# Don't return connection (and perform implicit rollback) if
|
358
|
+
# this request is a part of integration test
|
359
|
+
unless env.key?("rack.test")
|
360
|
+
ActiveRecord::Base.clear_active_connections!
|
361
|
+
end
|
362
|
+
end
|
363
|
+
end
|
364
|
+
end
|
365
|
+
end
|
@@ -0,0 +1,113 @@
|
|
1
|
+
module ActiveRecord
|
2
|
+
class Base
|
3
|
+
class ConnectionSpecification #:nodoc:
|
4
|
+
attr_reader :config, :adapter_method
|
5
|
+
def initialize (config, adapter_method)
|
6
|
+
@config, @adapter_method = config, adapter_method
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
##
|
11
|
+
# :singleton-method:
|
12
|
+
# The connection handler
|
13
|
+
class_attribute :connection_handler, :instance_writer => false
|
14
|
+
self.connection_handler = ConnectionAdapters::ConnectionHandler.new
|
15
|
+
|
16
|
+
# Returns the connection currently associated with the class. This can
|
17
|
+
# also be used to "borrow" the connection to do database work that isn't
|
18
|
+
# easily done without going straight to SQL.
|
19
|
+
def connection
|
20
|
+
self.class.connection
|
21
|
+
end
|
22
|
+
|
23
|
+
# Establishes the connection to the database. Accepts a hash as input where
|
24
|
+
# the <tt>:adapter</tt> key must be specified with the name of a database adapter (in lower-case)
|
25
|
+
# example for regular databases (MySQL, Postgresql, etc):
|
26
|
+
#
|
27
|
+
# ActiveRecord::Base.establish_connection(
|
28
|
+
# :adapter => "mysql",
|
29
|
+
# :host => "localhost",
|
30
|
+
# :username => "myuser",
|
31
|
+
# :password => "mypass",
|
32
|
+
# :database => "somedatabase"
|
33
|
+
# )
|
34
|
+
#
|
35
|
+
# Example for SQLite database:
|
36
|
+
#
|
37
|
+
# ActiveRecord::Base.establish_connection(
|
38
|
+
# :adapter => "sqlite",
|
39
|
+
# :database => "path/to/dbfile"
|
40
|
+
# )
|
41
|
+
#
|
42
|
+
# Also accepts keys as strings (for parsing from YAML for example):
|
43
|
+
#
|
44
|
+
# ActiveRecord::Base.establish_connection(
|
45
|
+
# "adapter" => "sqlite",
|
46
|
+
# "database" => "path/to/dbfile"
|
47
|
+
# )
|
48
|
+
#
|
49
|
+
# The exceptions AdapterNotSpecified, AdapterNotFound and ArgumentError
|
50
|
+
# may be returned on an error.
|
51
|
+
def self.establish_connection(spec = nil)
|
52
|
+
case spec
|
53
|
+
when nil
|
54
|
+
raise AdapterNotSpecified unless defined?(Rails.env)
|
55
|
+
establish_connection(Rails.env)
|
56
|
+
when ConnectionSpecification
|
57
|
+
self.connection_handler.establish_connection(name, spec)
|
58
|
+
when Symbol, String
|
59
|
+
if configuration = configurations[spec.to_s]
|
60
|
+
establish_connection(configuration)
|
61
|
+
else
|
62
|
+
raise AdapterNotSpecified, "#{spec} database is not configured"
|
63
|
+
end
|
64
|
+
else
|
65
|
+
spec = spec.symbolize_keys
|
66
|
+
unless spec.key?(:adapter) then raise AdapterNotSpecified, "database configuration does not specify adapter" end
|
67
|
+
|
68
|
+
begin
|
69
|
+
require "active_record/connection_adapters/#{spec[:adapter]}_adapter"
|
70
|
+
rescue LoadError
|
71
|
+
raise "Please install the #{spec[:adapter]} adapter: `gem install activerecord-#{spec[:adapter]}-adapter` (#{$!})"
|
72
|
+
end
|
73
|
+
|
74
|
+
adapter_method = "#{spec[:adapter]}_connection"
|
75
|
+
if !respond_to?(adapter_method)
|
76
|
+
raise AdapterNotFound, "database configuration specifies nonexistent #{spec[:adapter]} adapter"
|
77
|
+
end
|
78
|
+
|
79
|
+
remove_connection
|
80
|
+
establish_connection(ConnectionSpecification.new(spec, adapter_method))
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
class << self
|
85
|
+
# Returns the connection currently associated with the class. This can
|
86
|
+
# also be used to "borrow" the connection to do database work unrelated
|
87
|
+
# to any of the specific Active Records.
|
88
|
+
def connection
|
89
|
+
retrieve_connection
|
90
|
+
end
|
91
|
+
|
92
|
+
def connection_pool
|
93
|
+
connection_handler.retrieve_connection_pool(self)
|
94
|
+
end
|
95
|
+
|
96
|
+
def retrieve_connection
|
97
|
+
connection_handler.retrieve_connection(self)
|
98
|
+
end
|
99
|
+
|
100
|
+
# Returns true if Active Record is connected.
|
101
|
+
def connected?
|
102
|
+
connection_handler.connected?(self)
|
103
|
+
end
|
104
|
+
|
105
|
+
def remove_connection(klass = self)
|
106
|
+
connection_handler.remove_connection(klass)
|
107
|
+
end
|
108
|
+
|
109
|
+
delegate :clear_active_connections!, :clear_reloadable_connections!,
|
110
|
+
:clear_all_connections!,:verify_active_connections!, :to => :connection_handler
|
111
|
+
end
|
112
|
+
end
|
113
|
+
end
|
@@ -0,0 +1,57 @@
|
|
1
|
+
module ActiveRecord
|
2
|
+
module ConnectionAdapters # :nodoc:
|
3
|
+
module DatabaseLimits
|
4
|
+
|
5
|
+
# the maximum length of a table alias
|
6
|
+
def table_alias_length
|
7
|
+
255
|
8
|
+
end
|
9
|
+
|
10
|
+
# the maximum length of a column name
|
11
|
+
def column_name_length
|
12
|
+
64
|
13
|
+
end
|
14
|
+
|
15
|
+
# the maximum length of a table name
|
16
|
+
def table_name_length
|
17
|
+
64
|
18
|
+
end
|
19
|
+
|
20
|
+
# the maximum length of an index name
|
21
|
+
def index_name_length
|
22
|
+
64
|
23
|
+
end
|
24
|
+
|
25
|
+
# the maximum number of columns per table
|
26
|
+
def columns_per_table
|
27
|
+
1024
|
28
|
+
end
|
29
|
+
|
30
|
+
# the maximum number of indexes per table
|
31
|
+
def indexes_per_table
|
32
|
+
16
|
33
|
+
end
|
34
|
+
|
35
|
+
# the maximum number of columns in a multicolumn index
|
36
|
+
def columns_per_multicolumn_index
|
37
|
+
16
|
38
|
+
end
|
39
|
+
|
40
|
+
# the maximum number of elements in an IN (x,y,z) clause
|
41
|
+
def in_clause_length
|
42
|
+
65535
|
43
|
+
end
|
44
|
+
|
45
|
+
# the maximum length of an SQL query
|
46
|
+
def sql_query_length
|
47
|
+
1048575
|
48
|
+
end
|
49
|
+
|
50
|
+
# maximum number of joins in a single query
|
51
|
+
def joins_per_query
|
52
|
+
256
|
53
|
+
end
|
54
|
+
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|