activerecord 3.2.22.5 → 4.2.11.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of activerecord might be problematic. Click here for more details.
- checksums.yaml +5 -5
- data/CHANGELOG.md +1632 -609
- data/MIT-LICENSE +1 -1
- data/README.rdoc +37 -41
- data/examples/performance.rb +31 -19
- data/examples/simple.rb +4 -4
- data/lib/active_record/aggregations.rb +56 -42
- data/lib/active_record/association_relation.rb +35 -0
- data/lib/active_record/associations/alias_tracker.rb +47 -36
- data/lib/active_record/associations/association.rb +73 -55
- data/lib/active_record/associations/association_scope.rb +143 -82
- data/lib/active_record/associations/belongs_to_association.rb +65 -25
- data/lib/active_record/associations/belongs_to_polymorphic_association.rb +7 -2
- data/lib/active_record/associations/builder/association.rb +125 -31
- data/lib/active_record/associations/builder/belongs_to.rb +89 -61
- data/lib/active_record/associations/builder/collection_association.rb +69 -49
- data/lib/active_record/associations/builder/has_and_belongs_to_many.rb +113 -42
- data/lib/active_record/associations/builder/has_many.rb +8 -64
- data/lib/active_record/associations/builder/has_one.rb +12 -51
- data/lib/active_record/associations/builder/singular_association.rb +23 -17
- data/lib/active_record/associations/collection_association.rb +251 -177
- data/lib/active_record/associations/collection_proxy.rb +963 -63
- data/lib/active_record/associations/foreign_association.rb +11 -0
- data/lib/active_record/associations/has_many_association.rb +113 -22
- data/lib/active_record/associations/has_many_through_association.rb +99 -39
- data/lib/active_record/associations/has_one_association.rb +43 -20
- data/lib/active_record/associations/has_one_through_association.rb +1 -1
- data/lib/active_record/associations/join_dependency/join_association.rb +76 -107
- data/lib/active_record/associations/join_dependency/join_base.rb +7 -9
- data/lib/active_record/associations/join_dependency/join_part.rb +30 -37
- data/lib/active_record/associations/join_dependency.rb +230 -156
- data/lib/active_record/associations/preloader/association.rb +96 -55
- data/lib/active_record/associations/preloader/collection_association.rb +3 -3
- data/lib/active_record/associations/preloader/has_many_through.rb +7 -3
- data/lib/active_record/associations/preloader/has_one.rb +1 -1
- data/lib/active_record/associations/preloader/singular_association.rb +3 -3
- data/lib/active_record/associations/preloader/through_association.rb +62 -33
- data/lib/active_record/associations/preloader.rb +101 -79
- data/lib/active_record/associations/singular_association.rb +29 -13
- data/lib/active_record/associations/through_association.rb +30 -16
- data/lib/active_record/associations.rb +463 -345
- data/lib/active_record/attribute.rb +163 -0
- data/lib/active_record/attribute_assignment.rb +142 -151
- data/lib/active_record/attribute_decorators.rb +66 -0
- data/lib/active_record/attribute_methods/before_type_cast.rb +52 -7
- data/lib/active_record/attribute_methods/dirty.rb +137 -57
- data/lib/active_record/attribute_methods/primary_key.rb +50 -36
- data/lib/active_record/attribute_methods/query.rb +5 -4
- data/lib/active_record/attribute_methods/read.rb +73 -106
- data/lib/active_record/attribute_methods/serialization.rb +44 -94
- data/lib/active_record/attribute_methods/time_zone_conversion.rb +49 -45
- data/lib/active_record/attribute_methods/write.rb +57 -44
- data/lib/active_record/attribute_methods.rb +301 -141
- data/lib/active_record/attribute_set/builder.rb +106 -0
- data/lib/active_record/attribute_set.rb +81 -0
- data/lib/active_record/attributes.rb +147 -0
- data/lib/active_record/autosave_association.rb +246 -217
- data/lib/active_record/base.rb +70 -474
- data/lib/active_record/callbacks.rb +66 -28
- data/lib/active_record/coders/json.rb +13 -0
- data/lib/active_record/coders/yaml_column.rb +18 -21
- data/lib/active_record/connection_adapters/abstract/connection_pool.rb +396 -219
- data/lib/active_record/connection_adapters/abstract/database_limits.rb +9 -0
- data/lib/active_record/connection_adapters/abstract/database_statements.rb +167 -164
- data/lib/active_record/connection_adapters/abstract/query_cache.rb +29 -24
- data/lib/active_record/connection_adapters/abstract/quoting.rb +74 -55
- data/lib/active_record/connection_adapters/abstract/savepoints.rb +21 -0
- data/lib/active_record/connection_adapters/abstract/schema_creation.rb +125 -0
- data/lib/active_record/connection_adapters/abstract/schema_definitions.rb +261 -169
- data/lib/active_record/connection_adapters/abstract/schema_dumper.rb +50 -0
- data/lib/active_record/connection_adapters/abstract/schema_statements.rb +707 -259
- data/lib/active_record/connection_adapters/abstract/transaction.rb +215 -0
- data/lib/active_record/connection_adapters/abstract_adapter.rb +298 -89
- data/lib/active_record/connection_adapters/abstract_mysql_adapter.rb +466 -196
- data/lib/active_record/connection_adapters/column.rb +31 -245
- data/lib/active_record/connection_adapters/connection_specification.rb +275 -0
- data/lib/active_record/connection_adapters/mysql2_adapter.rb +45 -57
- data/lib/active_record/connection_adapters/mysql_adapter.rb +180 -123
- data/lib/active_record/connection_adapters/postgresql/array_parser.rb +93 -0
- data/lib/active_record/connection_adapters/postgresql/column.rb +20 -0
- data/lib/active_record/connection_adapters/postgresql/database_statements.rb +232 -0
- data/lib/active_record/connection_adapters/postgresql/oid/array.rb +100 -0
- data/lib/active_record/connection_adapters/postgresql/oid/bit.rb +52 -0
- data/lib/active_record/connection_adapters/postgresql/oid/bit_varying.rb +13 -0
- data/lib/active_record/connection_adapters/postgresql/oid/bytea.rb +15 -0
- data/lib/active_record/connection_adapters/postgresql/oid/cidr.rb +46 -0
- data/lib/active_record/connection_adapters/postgresql/oid/date.rb +11 -0
- data/lib/active_record/connection_adapters/postgresql/oid/date_time.rb +36 -0
- data/lib/active_record/connection_adapters/postgresql/oid/decimal.rb +13 -0
- data/lib/active_record/connection_adapters/postgresql/oid/enum.rb +19 -0
- data/lib/active_record/connection_adapters/postgresql/oid/float.rb +21 -0
- data/lib/active_record/connection_adapters/postgresql/oid/hstore.rb +59 -0
- data/lib/active_record/connection_adapters/postgresql/oid/inet.rb +13 -0
- data/lib/active_record/connection_adapters/postgresql/oid/infinity.rb +13 -0
- data/lib/active_record/connection_adapters/postgresql/oid/integer.rb +11 -0
- data/lib/active_record/connection_adapters/postgresql/oid/json.rb +35 -0
- data/lib/active_record/connection_adapters/postgresql/oid/jsonb.rb +23 -0
- data/lib/active_record/connection_adapters/postgresql/oid/money.rb +43 -0
- data/lib/active_record/connection_adapters/postgresql/oid/point.rb +43 -0
- data/lib/active_record/connection_adapters/postgresql/oid/range.rb +79 -0
- data/lib/active_record/connection_adapters/postgresql/oid/specialized_string.rb +19 -0
- data/lib/active_record/connection_adapters/postgresql/oid/time.rb +11 -0
- data/lib/active_record/connection_adapters/postgresql/oid/type_map_initializer.rb +109 -0
- data/lib/active_record/connection_adapters/postgresql/oid/uuid.rb +21 -0
- data/lib/active_record/connection_adapters/postgresql/oid/vector.rb +26 -0
- data/lib/active_record/connection_adapters/postgresql/oid/xml.rb +28 -0
- data/lib/active_record/connection_adapters/postgresql/oid.rb +36 -0
- data/lib/active_record/connection_adapters/postgresql/quoting.rb +108 -0
- data/lib/active_record/connection_adapters/postgresql/referential_integrity.rb +30 -0
- data/lib/active_record/connection_adapters/postgresql/schema_definitions.rb +152 -0
- data/lib/active_record/connection_adapters/postgresql/schema_statements.rb +596 -0
- data/lib/active_record/connection_adapters/postgresql/utils.rb +77 -0
- data/lib/active_record/connection_adapters/postgresql_adapter.rb +430 -999
- data/lib/active_record/connection_adapters/schema_cache.rb +52 -27
- data/lib/active_record/connection_adapters/sqlite3_adapter.rb +579 -22
- data/lib/active_record/connection_handling.rb +132 -0
- data/lib/active_record/core.rb +579 -0
- data/lib/active_record/counter_cache.rb +157 -105
- data/lib/active_record/dynamic_matchers.rb +119 -63
- data/lib/active_record/enum.rb +197 -0
- data/lib/active_record/errors.rb +94 -36
- data/lib/active_record/explain.rb +15 -63
- data/lib/active_record/explain_registry.rb +30 -0
- data/lib/active_record/explain_subscriber.rb +9 -5
- data/lib/active_record/fixture_set/file.rb +56 -0
- data/lib/active_record/fixtures.rb +302 -215
- data/lib/active_record/gem_version.rb +15 -0
- data/lib/active_record/inheritance.rb +143 -70
- data/lib/active_record/integration.rb +65 -12
- data/lib/active_record/legacy_yaml_adapter.rb +30 -0
- data/lib/active_record/locale/en.yml +8 -1
- data/lib/active_record/locking/optimistic.rb +73 -52
- data/lib/active_record/locking/pessimistic.rb +5 -5
- data/lib/active_record/log_subscriber.rb +24 -21
- data/lib/active_record/migration/command_recorder.rb +124 -32
- data/lib/active_record/migration/join_table.rb +15 -0
- data/lib/active_record/migration.rb +511 -213
- data/lib/active_record/model_schema.rb +91 -117
- data/lib/active_record/nested_attributes.rb +184 -130
- data/lib/active_record/no_touching.rb +52 -0
- data/lib/active_record/null_relation.rb +81 -0
- data/lib/active_record/persistence.rb +276 -117
- data/lib/active_record/query_cache.rb +19 -37
- data/lib/active_record/querying.rb +28 -18
- data/lib/active_record/railtie.rb +73 -40
- data/lib/active_record/railties/console_sandbox.rb +3 -4
- data/lib/active_record/railties/controller_runtime.rb +4 -3
- data/lib/active_record/railties/databases.rake +141 -416
- data/lib/active_record/railties/jdbcmysql_error.rb +1 -1
- data/lib/active_record/readonly_attributes.rb +1 -4
- data/lib/active_record/reflection.rb +513 -154
- data/lib/active_record/relation/batches.rb +91 -43
- data/lib/active_record/relation/calculations.rb +199 -161
- data/lib/active_record/relation/delegation.rb +116 -25
- data/lib/active_record/relation/finder_methods.rb +362 -248
- data/lib/active_record/relation/merger.rb +193 -0
- data/lib/active_record/relation/predicate_builder/array_handler.rb +48 -0
- data/lib/active_record/relation/predicate_builder/relation_handler.rb +13 -0
- data/lib/active_record/relation/predicate_builder.rb +135 -43
- data/lib/active_record/relation/query_methods.rb +928 -167
- data/lib/active_record/relation/spawn_methods.rb +48 -149
- data/lib/active_record/relation.rb +352 -207
- data/lib/active_record/result.rb +101 -10
- data/lib/active_record/runtime_registry.rb +22 -0
- data/lib/active_record/sanitization.rb +56 -59
- data/lib/active_record/schema.rb +19 -13
- data/lib/active_record/schema_dumper.rb +106 -63
- data/lib/active_record/schema_migration.rb +53 -0
- data/lib/active_record/scoping/default.rb +50 -57
- data/lib/active_record/scoping/named.rb +73 -109
- data/lib/active_record/scoping.rb +58 -123
- data/lib/active_record/serialization.rb +6 -2
- data/lib/active_record/serializers/xml_serializer.rb +12 -22
- data/lib/active_record/statement_cache.rb +111 -0
- data/lib/active_record/store.rb +168 -15
- data/lib/active_record/tasks/database_tasks.rb +299 -0
- data/lib/active_record/tasks/mysql_database_tasks.rb +159 -0
- data/lib/active_record/tasks/postgresql_database_tasks.rb +101 -0
- data/lib/active_record/tasks/sqlite_database_tasks.rb +55 -0
- data/lib/active_record/timestamp.rb +23 -16
- data/lib/active_record/transactions.rb +125 -79
- data/lib/active_record/type/big_integer.rb +13 -0
- data/lib/active_record/type/binary.rb +50 -0
- data/lib/active_record/type/boolean.rb +31 -0
- data/lib/active_record/type/date.rb +50 -0
- data/lib/active_record/type/date_time.rb +54 -0
- data/lib/active_record/type/decimal.rb +64 -0
- data/lib/active_record/type/decimal_without_scale.rb +11 -0
- data/lib/active_record/type/decorator.rb +14 -0
- data/lib/active_record/type/float.rb +19 -0
- data/lib/active_record/type/hash_lookup_type_map.rb +23 -0
- data/lib/active_record/type/integer.rb +59 -0
- data/lib/active_record/type/mutable.rb +16 -0
- data/lib/active_record/type/numeric.rb +36 -0
- data/lib/active_record/type/serialized.rb +62 -0
- data/lib/active_record/type/string.rb +40 -0
- data/lib/active_record/type/text.rb +11 -0
- data/lib/active_record/type/time.rb +26 -0
- data/lib/active_record/type/time_value.rb +38 -0
- data/lib/active_record/type/type_map.rb +64 -0
- data/lib/active_record/type/unsigned_integer.rb +15 -0
- data/lib/active_record/type/value.rb +110 -0
- data/lib/active_record/type.rb +23 -0
- data/lib/active_record/validations/associated.rb +24 -16
- data/lib/active_record/validations/presence.rb +67 -0
- data/lib/active_record/validations/uniqueness.rb +123 -64
- data/lib/active_record/validations.rb +36 -29
- data/lib/active_record/version.rb +5 -7
- data/lib/active_record.rb +66 -46
- data/lib/rails/generators/active_record/migration/migration_generator.rb +53 -8
- data/lib/rails/generators/active_record/{model/templates/migration.rb → migration/templates/create_table_migration.rb} +5 -1
- data/lib/rails/generators/active_record/migration/templates/migration.rb +20 -15
- data/lib/rails/generators/active_record/migration.rb +11 -8
- data/lib/rails/generators/active_record/model/model_generator.rb +9 -4
- data/lib/rails/generators/active_record/model/templates/model.rb +4 -6
- data/lib/rails/generators/active_record/model/templates/module.rb +1 -1
- data/lib/rails/generators/active_record.rb +3 -11
- metadata +101 -45
- data/examples/associations.png +0 -0
- data/lib/active_record/associations/has_and_belongs_to_many_association.rb +0 -63
- data/lib/active_record/associations/join_helper.rb +0 -55
- data/lib/active_record/associations/preloader/has_and_belongs_to_many.rb +0 -60
- data/lib/active_record/attribute_methods/deprecated_underscore_read.rb +0 -32
- data/lib/active_record/connection_adapters/abstract/connection_specification.rb +0 -191
- data/lib/active_record/connection_adapters/sqlite_adapter.rb +0 -583
- data/lib/active_record/dynamic_finder_match.rb +0 -68
- data/lib/active_record/dynamic_scope_match.rb +0 -23
- data/lib/active_record/fixtures/file.rb +0 -65
- data/lib/active_record/identity_map.rb +0 -162
- data/lib/active_record/observer.rb +0 -121
- data/lib/active_record/session_store.rb +0 -360
- data/lib/active_record/test_case.rb +0 -73
- data/lib/rails/generators/active_record/observer/observer_generator.rb +0 -15
- data/lib/rails/generators/active_record/observer/templates/observer.rb +0 -4
- data/lib/rails/generators/active_record/session_migration/session_migration_generator.rb +0 -25
- data/lib/rails/generators/active_record/session_migration/templates/migration.rb +0 -12
@@ -1,10 +1,9 @@
|
|
1
|
-
require 'active_support/core_ext/object/blank'
|
2
|
-
|
3
1
|
module ActiveRecord
|
4
2
|
# = Active Record Query Cache
|
5
3
|
class QueryCache
|
6
4
|
module ClassMethods
|
7
5
|
# Enable the query cache within the block if Active Record is configured.
|
6
|
+
# If it's not, it will execute the given block.
|
8
7
|
def cache(&block)
|
9
8
|
if ActiveRecord::Base.connected?
|
10
9
|
connection.cache(&block)
|
@@ -14,6 +13,7 @@ module ActiveRecord
|
|
14
13
|
end
|
15
14
|
|
16
15
|
# Disable the query cache within the block if Active Record is configured.
|
16
|
+
# If it's not, it will execute the given block.
|
17
17
|
def uncached(&block)
|
18
18
|
if ActiveRecord::Base.connected?
|
19
19
|
connection.uncached(&block)
|
@@ -27,48 +27,30 @@ module ActiveRecord
|
|
27
27
|
@app = app
|
28
28
|
end
|
29
29
|
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
end
|
36
|
-
|
37
|
-
def method_missing(method_sym, *arguments, &block)
|
38
|
-
@target.send(method_sym, *arguments, &block)
|
39
|
-
end
|
40
|
-
|
41
|
-
def respond_to?(method_sym, include_private = false)
|
42
|
-
super || @target.respond_to?(method_sym)
|
43
|
-
end
|
30
|
+
def call(env)
|
31
|
+
connection = ActiveRecord::Base.connection
|
32
|
+
enabled = connection.query_cache_enabled
|
33
|
+
connection_id = ActiveRecord::Base.connection_id
|
34
|
+
connection.enable_query_cache!
|
44
35
|
|
45
|
-
|
46
|
-
|
36
|
+
response = @app.call(env)
|
37
|
+
response[2] = Rack::BodyProxy.new(response[2]) do
|
38
|
+
restore_query_cache_settings(connection_id, enabled)
|
47
39
|
end
|
48
40
|
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
ActiveRecord::Base.connection.clear_query_cache
|
54
|
-
unless @original_cache_value
|
55
|
-
ActiveRecord::Base.connection.disable_query_cache!
|
56
|
-
end
|
57
|
-
end
|
41
|
+
response
|
42
|
+
rescue Exception => e
|
43
|
+
restore_query_cache_settings(connection_id, enabled)
|
44
|
+
raise e
|
58
45
|
end
|
59
46
|
|
60
|
-
|
61
|
-
old = ActiveRecord::Base.connection.query_cache_enabled
|
62
|
-
ActiveRecord::Base.connection.enable_query_cache!
|
47
|
+
private
|
63
48
|
|
64
|
-
|
65
|
-
|
66
|
-
rescue Exception => e
|
49
|
+
def restore_query_cache_settings(connection_id, enabled)
|
50
|
+
ActiveRecord::Base.connection_id = connection_id
|
67
51
|
ActiveRecord::Base.connection.clear_query_cache
|
68
|
-
unless
|
69
|
-
ActiveRecord::Base.connection.disable_query_cache!
|
70
|
-
end
|
71
|
-
raise e
|
52
|
+
ActiveRecord::Base.connection.disable_query_cache! unless enabled
|
72
53
|
end
|
54
|
+
|
73
55
|
end
|
74
56
|
end
|
@@ -1,20 +1,22 @@
|
|
1
|
-
require 'active_support/core_ext/module/delegation'
|
2
|
-
|
3
1
|
module ActiveRecord
|
4
2
|
module Querying
|
5
|
-
delegate :find, :first, :first!, :last, :last!, :
|
6
|
-
delegate :
|
7
|
-
delegate :
|
8
|
-
delegate :
|
3
|
+
delegate :find, :take, :take!, :first, :first!, :last, :last!, :exists?, :any?, :many?, to: :all
|
4
|
+
delegate :second, :second!, :third, :third!, :fourth, :fourth!, :fifth, :fifth!, :forty_two, :forty_two!, to: :all
|
5
|
+
delegate :first_or_create, :first_or_create!, :first_or_initialize, to: :all
|
6
|
+
delegate :find_or_create_by, :find_or_create_by!, :find_or_initialize_by, to: :all
|
7
|
+
delegate :find_by, :find_by!, to: :all
|
8
|
+
delegate :destroy, :destroy_all, :delete, :delete_all, :update, :update_all, to: :all
|
9
|
+
delegate :find_each, :find_in_batches, to: :all
|
9
10
|
delegate :select, :group, :order, :except, :reorder, :limit, :offset, :joins,
|
10
|
-
:where, :preload, :eager_load, :includes, :from, :lock, :readonly,
|
11
|
-
:having, :create_with, :uniq, :
|
12
|
-
delegate :count, :average, :minimum, :maximum, :sum, :calculate, :
|
11
|
+
:where, :rewhere, :preload, :eager_load, :includes, :from, :lock, :readonly,
|
12
|
+
:having, :create_with, :uniq, :distinct, :references, :none, :unscope, to: :all
|
13
|
+
delegate :count, :average, :minimum, :maximum, :sum, :calculate, to: :all
|
14
|
+
delegate :pluck, :ids, to: :all
|
13
15
|
|
14
16
|
# Executes a custom SQL query against your database and returns all the results. The results will
|
15
17
|
# be returned as an array with columns requested encapsulated as attributes of the model you call
|
16
18
|
# this method from. If you call <tt>Product.find_by_sql</tt> then the results will be returned in
|
17
|
-
# a Product object with the attributes you specified in the SQL query.
|
19
|
+
# a +Product+ object with the attributes you specified in the SQL query.
|
18
20
|
#
|
19
21
|
# If you call a complicated SQL query which spans multiple tables the columns specified by the
|
20
22
|
# SELECT will be attributes of the model, whether or not they are columns of the corresponding
|
@@ -25,17 +27,27 @@ module ActiveRecord
|
|
25
27
|
# MySQL specific terms will lock you to using that particular database engine or require you to
|
26
28
|
# change your call if you switch engines.
|
27
29
|
#
|
28
|
-
# ==== Examples
|
29
30
|
# # A simple SQL query spanning multiple tables
|
30
31
|
# Post.find_by_sql "SELECT p.title, c.author FROM posts p, comments c WHERE p.id = c.post_id"
|
31
|
-
#
|
32
|
+
# # => [#<Post:0x36bff9c @attributes={"title"=>"Ruby Meetup", "first_name"=>"Quentin"}>, ...]
|
33
|
+
#
|
34
|
+
# You can use the same string replacement techniques as you can with <tt>ActiveRecord::QueryMethods#where</tt>:
|
32
35
|
#
|
33
|
-
# # You can use the same string replacement techniques as you can with ActiveRecord#find
|
34
36
|
# Post.find_by_sql ["SELECT title FROM posts WHERE author = ? AND created > ?", author_id, start_date]
|
35
|
-
#
|
37
|
+
# Post.find_by_sql ["SELECT body FROM comments WHERE author = :user_id OR approved_by = :user_id", { :user_id => user_id }]
|
36
38
|
def find_by_sql(sql, binds = [])
|
37
|
-
|
38
|
-
|
39
|
+
result_set = connection.select_all(sanitize_sql(sql), "#{name} Load", binds)
|
40
|
+
column_types = result_set.column_types.dup
|
41
|
+
columns_hash.each_key { |k| column_types.delete k }
|
42
|
+
message_bus = ActiveSupport::Notifications.instrumenter
|
43
|
+
|
44
|
+
payload = {
|
45
|
+
record_count: result_set.length,
|
46
|
+
class_name: name
|
47
|
+
}
|
48
|
+
|
49
|
+
message_bus.instrument('instantiation.active_record', payload) do
|
50
|
+
result_set.map { |record| instantiate(record, column_types) }
|
39
51
|
end
|
40
52
|
end
|
41
53
|
|
@@ -47,8 +59,6 @@ module ActiveRecord
|
|
47
59
|
#
|
48
60
|
# * +sql+ - An SQL statement which should return a count query from the database, see the example below.
|
49
61
|
#
|
50
|
-
# ==== Examples
|
51
|
-
#
|
52
62
|
# Product.count_by_sql "SELECT COUNT(*) FROM sales s, customers c WHERE s.customer_id = c.id"
|
53
63
|
def count_by_sql(sql)
|
54
64
|
sql = sanitize_conditions(sql)
|
@@ -10,7 +10,7 @@ require "action_controller/railtie"
|
|
10
10
|
|
11
11
|
module ActiveRecord
|
12
12
|
# = Active Record Railtie
|
13
|
-
class Railtie < Rails::Railtie
|
13
|
+
class Railtie < Rails::Railtie # :nodoc:
|
14
14
|
config.active_record = ActiveSupport::OrderedOptions.new
|
15
15
|
|
16
16
|
config.app_generators.orm :active_record, :migration => true,
|
@@ -29,8 +29,25 @@ module ActiveRecord
|
|
29
29
|
'ActiveRecord::RecordNotSaved' => :unprocessable_entity
|
30
30
|
)
|
31
31
|
|
32
|
+
|
33
|
+
config.active_record.use_schema_cache_dump = true
|
34
|
+
config.active_record.maintain_test_schema = true
|
35
|
+
|
36
|
+
config.eager_load_namespaces << ActiveRecord
|
37
|
+
|
32
38
|
rake_tasks do
|
33
|
-
|
39
|
+
namespace :db do
|
40
|
+
task :load_config do
|
41
|
+
ActiveRecord::Tasks::DatabaseTasks.database_configuration = Rails.application.config.database_configuration
|
42
|
+
|
43
|
+
if defined?(ENGINE_PATH) && engine = Rails::Engine.find(ENGINE_PATH)
|
44
|
+
if engine.paths['db/migrate'].existent
|
45
|
+
ActiveRecord::Tasks::DatabaseTasks.migrations_paths += engine.paths['db/migrate'].to_a
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
34
51
|
load "active_record/railties/databases.rake"
|
35
52
|
end
|
36
53
|
|
@@ -40,10 +57,13 @@ module ActiveRecord
|
|
40
57
|
console do |app|
|
41
58
|
require "active_record/railties/console_sandbox" if app.sandbox?
|
42
59
|
require "active_record/base"
|
43
|
-
|
60
|
+
unless ActiveSupport::Logger.logger_outputs_to?(Rails.logger, STDERR, STDOUT)
|
61
|
+
console = ActiveSupport::Logger.new(STDERR)
|
62
|
+
Rails.logger.extend ActiveSupport::Logger.broadcast console
|
63
|
+
end
|
44
64
|
end
|
45
65
|
|
46
|
-
runner do
|
66
|
+
runner do
|
47
67
|
require "active_record/base"
|
48
68
|
end
|
49
69
|
|
@@ -58,16 +78,34 @@ module ActiveRecord
|
|
58
78
|
ActiveSupport.on_load(:active_record) { self.logger ||= ::Rails.logger }
|
59
79
|
end
|
60
80
|
|
61
|
-
initializer "active_record.
|
62
|
-
config.
|
63
|
-
"
|
81
|
+
initializer "active_record.migration_error" do
|
82
|
+
if config.active_record.delete(:migration_error) == :page_load
|
83
|
+
config.app_middleware.insert_after "::ActionDispatch::Callbacks",
|
84
|
+
"ActiveRecord::Migration::CheckPending"
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
initializer "active_record.check_schema_cache_dump" do
|
89
|
+
if config.active_record.delete(:use_schema_cache_dump)
|
90
|
+
config.after_initialize do |app|
|
91
|
+
ActiveSupport.on_load(:active_record) do
|
92
|
+
filename = File.join(app.config.paths["db"].first, "schema_cache.dump")
|
93
|
+
|
94
|
+
if File.file?(filename)
|
95
|
+
cache = Marshal.load File.binread filename
|
96
|
+
if cache.version == ActiveRecord::Migrator.current_version
|
97
|
+
self.connection.schema_cache = cache
|
98
|
+
else
|
99
|
+
warn "Ignoring db/schema_cache.dump because it has expired. The current schema version is #{ActiveRecord::Migrator.current_version}, but the one in the cache is #{cache.version}."
|
100
|
+
end
|
101
|
+
end
|
102
|
+
end
|
103
|
+
end
|
104
|
+
end
|
64
105
|
end
|
65
106
|
|
66
107
|
initializer "active_record.set_configs" do |app|
|
67
108
|
ActiveSupport.on_load(:active_record) do
|
68
|
-
if app.config.active_record.delete(:whitelist_attributes)
|
69
|
-
attr_accessible(nil)
|
70
|
-
end
|
71
109
|
app.config.active_record.each do |k,v|
|
72
110
|
send "#{k}=", v
|
73
111
|
end
|
@@ -78,19 +116,27 @@ module ActiveRecord
|
|
78
116
|
# and then establishes the connection.
|
79
117
|
initializer "active_record.initialize_database" do |app|
|
80
118
|
ActiveSupport.on_load(:active_record) do
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
119
|
+
self.configurations = Rails.application.config.database_configuration
|
120
|
+
|
121
|
+
begin
|
122
|
+
establish_connection
|
123
|
+
rescue ActiveRecord::NoDatabaseError
|
124
|
+
warn <<-end_warning
|
125
|
+
Oops - You have a database configured, but it doesn't exist yet!
|
87
126
|
|
88
|
-
|
127
|
+
Here's how to get started:
|
128
|
+
|
129
|
+
1. Configure your database in config/database.yml.
|
130
|
+
2. Run `bin/rake db:create` to create the database.
|
131
|
+
3. Run `bin/rake db:setup` to load your database schema.
|
132
|
+
end_warning
|
133
|
+
raise
|
134
|
+
end
|
89
135
|
end
|
90
136
|
end
|
91
137
|
|
92
138
|
# Expose database runtime to controller for logging.
|
93
|
-
initializer "active_record.log_runtime" do
|
139
|
+
initializer "active_record.log_runtime" do
|
94
140
|
require "active_record/railties/controller_runtime"
|
95
141
|
ActiveSupport.on_load(:action_controller) do
|
96
142
|
include ActiveRecord::Railties::ControllerRuntime
|
@@ -98,34 +144,21 @@ module ActiveRecord
|
|
98
144
|
end
|
99
145
|
|
100
146
|
initializer "active_record.set_reloader_hooks" do |app|
|
101
|
-
hook =
|
102
|
-
ActiveRecord::Base.clear_reloadable_connections!
|
103
|
-
ActiveRecord::Base.clear_cache!
|
104
|
-
end
|
147
|
+
hook = app.config.reload_classes_only_on_change ? :to_prepare : :to_cleanup
|
105
148
|
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
ActionDispatch::Reloader.to_cleanup(&hook)
|
149
|
+
ActiveSupport.on_load(:active_record) do
|
150
|
+
ActionDispatch::Reloader.send(hook) do
|
151
|
+
if ActiveRecord::Base.connected?
|
152
|
+
ActiveRecord::Base.clear_cache!
|
153
|
+
ActiveRecord::Base.clear_reloadable_connections!
|
154
|
+
end
|
113
155
|
end
|
114
156
|
end
|
115
157
|
end
|
116
158
|
|
117
159
|
initializer "active_record.add_watchable_files" do |app|
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
config.after_initialize do
|
122
|
-
ActiveSupport.on_load(:active_record) do
|
123
|
-
instantiate_observers
|
124
|
-
|
125
|
-
ActionDispatch::Reloader.to_prepare do
|
126
|
-
ActiveRecord::Base.instantiate_observers
|
127
|
-
end
|
128
|
-
end
|
160
|
+
path = app.paths["db"].first
|
161
|
+
config.watchable_files.concat ["#{path}/schema.rb", "#{path}/structure.sql"]
|
129
162
|
end
|
130
163
|
end
|
131
164
|
end
|
@@ -1,6 +1,5 @@
|
|
1
|
-
ActiveRecord::Base.connection.
|
2
|
-
|
1
|
+
ActiveRecord::Base.connection.begin_transaction(joinable: false)
|
2
|
+
|
3
3
|
at_exit do
|
4
|
-
ActiveRecord::Base.connection.
|
5
|
-
ActiveRecord::Base.connection.decrement_open_transactions
|
4
|
+
ActiveRecord::Base.connection.rollback_transaction
|
6
5
|
end
|
@@ -2,7 +2,7 @@ require 'active_support/core_ext/module/attr_internal'
|
|
2
2
|
require 'active_record/log_subscriber'
|
3
3
|
|
4
4
|
module ActiveRecord
|
5
|
-
module Railties
|
5
|
+
module Railties # :nodoc:
|
6
6
|
module ControllerRuntime #:nodoc:
|
7
7
|
extend ActiveSupport::Concern
|
8
8
|
|
@@ -21,9 +21,10 @@ module ActiveRecord
|
|
21
21
|
def cleanup_view_runtime
|
22
22
|
if ActiveRecord::Base.connected?
|
23
23
|
db_rt_before_render = ActiveRecord::LogSubscriber.reset_runtime
|
24
|
+
self.db_runtime = (db_runtime || 0) + db_rt_before_render
|
24
25
|
runtime = super
|
25
26
|
db_rt_after_render = ActiveRecord::LogSubscriber.reset_runtime
|
26
|
-
self.db_runtime
|
27
|
+
self.db_runtime += db_rt_after_render
|
27
28
|
runtime - db_rt_after_render
|
28
29
|
else
|
29
30
|
super
|
@@ -37,7 +38,7 @@ module ActiveRecord
|
|
37
38
|
end
|
38
39
|
end
|
39
40
|
|
40
|
-
module ClassMethods
|
41
|
+
module ClassMethods # :nodoc:
|
41
42
|
def log_process_action(payload)
|
42
43
|
messages, db_runtime = super, payload[:db_runtime]
|
43
44
|
messages << ("ActiveRecord: %.1fms" % db_runtime.to_f) if db_runtime
|