activerecord 3.0.0 → 4.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.

Files changed (181) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGELOG.md +2102 -0
  3. data/MIT-LICENSE +20 -0
  4. data/README.rdoc +35 -44
  5. data/examples/performance.rb +110 -100
  6. data/lib/active_record/aggregations.rb +59 -75
  7. data/lib/active_record/associations/alias_tracker.rb +76 -0
  8. data/lib/active_record/associations/association.rb +248 -0
  9. data/lib/active_record/associations/association_scope.rb +135 -0
  10. data/lib/active_record/associations/belongs_to_association.rb +60 -59
  11. data/lib/active_record/associations/belongs_to_polymorphic_association.rb +16 -59
  12. data/lib/active_record/associations/builder/association.rb +108 -0
  13. data/lib/active_record/associations/builder/belongs_to.rb +98 -0
  14. data/lib/active_record/associations/builder/collection_association.rb +89 -0
  15. data/lib/active_record/associations/builder/has_and_belongs_to_many.rb +39 -0
  16. data/lib/active_record/associations/builder/has_many.rb +15 -0
  17. data/lib/active_record/associations/builder/has_one.rb +25 -0
  18. data/lib/active_record/associations/builder/singular_association.rb +32 -0
  19. data/lib/active_record/associations/collection_association.rb +608 -0
  20. data/lib/active_record/associations/collection_proxy.rb +986 -0
  21. data/lib/active_record/associations/has_and_belongs_to_many_association.rb +40 -112
  22. data/lib/active_record/associations/has_many_association.rb +83 -76
  23. data/lib/active_record/associations/has_many_through_association.rb +147 -66
  24. data/lib/active_record/associations/has_one_association.rb +67 -108
  25. data/lib/active_record/associations/has_one_through_association.rb +21 -25
  26. data/lib/active_record/associations/join_dependency/join_association.rb +174 -0
  27. data/lib/active_record/associations/join_dependency/join_base.rb +24 -0
  28. data/lib/active_record/associations/join_dependency/join_part.rb +78 -0
  29. data/lib/active_record/associations/join_dependency.rb +235 -0
  30. data/lib/active_record/associations/join_helper.rb +45 -0
  31. data/lib/active_record/associations/preloader/association.rb +121 -0
  32. data/lib/active_record/associations/preloader/belongs_to.rb +17 -0
  33. data/lib/active_record/associations/preloader/collection_association.rb +24 -0
  34. data/lib/active_record/associations/preloader/has_and_belongs_to_many.rb +60 -0
  35. data/lib/active_record/associations/preloader/has_many.rb +17 -0
  36. data/lib/active_record/associations/preloader/has_many_through.rb +19 -0
  37. data/lib/active_record/associations/preloader/has_one.rb +23 -0
  38. data/lib/active_record/associations/preloader/has_one_through.rb +9 -0
  39. data/lib/active_record/associations/preloader/singular_association.rb +21 -0
  40. data/lib/active_record/associations/preloader/through_association.rb +63 -0
  41. data/lib/active_record/associations/preloader.rb +178 -0
  42. data/lib/active_record/associations/singular_association.rb +64 -0
  43. data/lib/active_record/associations/through_association.rb +87 -0
  44. data/lib/active_record/associations.rb +512 -1224
  45. data/lib/active_record/attribute_assignment.rb +201 -0
  46. data/lib/active_record/attribute_methods/before_type_cast.rb +49 -12
  47. data/lib/active_record/attribute_methods/dirty.rb +51 -28
  48. data/lib/active_record/attribute_methods/primary_key.rb +94 -22
  49. data/lib/active_record/attribute_methods/query.rb +5 -4
  50. data/lib/active_record/attribute_methods/read.rb +63 -72
  51. data/lib/active_record/attribute_methods/serialization.rb +162 -0
  52. data/lib/active_record/attribute_methods/time_zone_conversion.rb +39 -41
  53. data/lib/active_record/attribute_methods/write.rb +39 -13
  54. data/lib/active_record/attribute_methods.rb +362 -29
  55. data/lib/active_record/autosave_association.rb +132 -75
  56. data/lib/active_record/base.rb +83 -1627
  57. data/lib/active_record/callbacks.rb +69 -47
  58. data/lib/active_record/coders/yaml_column.rb +38 -0
  59. data/lib/active_record/connection_adapters/abstract/connection_pool.rb +411 -138
  60. data/lib/active_record/connection_adapters/abstract/database_limits.rb +21 -11
  61. data/lib/active_record/connection_adapters/abstract/database_statements.rb +234 -173
  62. data/lib/active_record/connection_adapters/abstract/query_cache.rb +36 -22
  63. data/lib/active_record/connection_adapters/abstract/quoting.rb +82 -25
  64. data/lib/active_record/connection_adapters/abstract/schema_definitions.rb +176 -414
  65. data/lib/active_record/connection_adapters/abstract/schema_dumper.rb +70 -0
  66. data/lib/active_record/connection_adapters/abstract/schema_statements.rb +562 -232
  67. data/lib/active_record/connection_adapters/abstract/transaction.rb +203 -0
  68. data/lib/active_record/connection_adapters/abstract_adapter.rb +281 -53
  69. data/lib/active_record/connection_adapters/abstract_mysql_adapter.rb +782 -0
  70. data/lib/active_record/connection_adapters/column.rb +318 -0
  71. data/lib/active_record/connection_adapters/connection_specification.rb +96 -0
  72. data/lib/active_record/connection_adapters/mysql2_adapter.rb +273 -0
  73. data/lib/active_record/connection_adapters/mysql_adapter.rb +365 -450
  74. data/lib/active_record/connection_adapters/postgresql/array_parser.rb +97 -0
  75. data/lib/active_record/connection_adapters/postgresql/cast.rb +152 -0
  76. data/lib/active_record/connection_adapters/postgresql/database_statements.rb +242 -0
  77. data/lib/active_record/connection_adapters/postgresql/oid.rb +366 -0
  78. data/lib/active_record/connection_adapters/postgresql/quoting.rb +171 -0
  79. data/lib/active_record/connection_adapters/postgresql/referential_integrity.rb +30 -0
  80. data/lib/active_record/connection_adapters/postgresql/schema_statements.rb +489 -0
  81. data/lib/active_record/connection_adapters/postgresql_adapter.rb +672 -752
  82. data/lib/active_record/connection_adapters/schema_cache.rb +129 -0
  83. data/lib/active_record/connection_adapters/sqlite3_adapter.rb +588 -17
  84. data/lib/active_record/connection_adapters/statement_pool.rb +40 -0
  85. data/lib/active_record/connection_handling.rb +98 -0
  86. data/lib/active_record/core.rb +463 -0
  87. data/lib/active_record/counter_cache.rb +108 -101
  88. data/lib/active_record/dynamic_matchers.rb +131 -0
  89. data/lib/active_record/errors.rb +54 -13
  90. data/lib/active_record/explain.rb +38 -0
  91. data/lib/active_record/explain_registry.rb +30 -0
  92. data/lib/active_record/explain_subscriber.rb +29 -0
  93. data/lib/active_record/fixture_set/file.rb +55 -0
  94. data/lib/active_record/fixtures.rb +703 -785
  95. data/lib/active_record/inheritance.rb +200 -0
  96. data/lib/active_record/integration.rb +60 -0
  97. data/lib/active_record/locale/en.yml +8 -1
  98. data/lib/active_record/locking/optimistic.rb +69 -60
  99. data/lib/active_record/locking/pessimistic.rb +34 -12
  100. data/lib/active_record/log_subscriber.rb +40 -6
  101. data/lib/active_record/migration/command_recorder.rb +164 -0
  102. data/lib/active_record/migration/join_table.rb +15 -0
  103. data/lib/active_record/migration.rb +614 -216
  104. data/lib/active_record/model_schema.rb +345 -0
  105. data/lib/active_record/nested_attributes.rb +248 -119
  106. data/lib/active_record/null_relation.rb +65 -0
  107. data/lib/active_record/persistence.rb +275 -57
  108. data/lib/active_record/query_cache.rb +29 -9
  109. data/lib/active_record/querying.rb +62 -0
  110. data/lib/active_record/railtie.rb +135 -21
  111. data/lib/active_record/railties/console_sandbox.rb +5 -0
  112. data/lib/active_record/railties/controller_runtime.rb +17 -5
  113. data/lib/active_record/railties/databases.rake +249 -359
  114. data/lib/active_record/railties/jdbcmysql_error.rb +16 -0
  115. data/lib/active_record/readonly_attributes.rb +30 -0
  116. data/lib/active_record/reflection.rb +283 -103
  117. data/lib/active_record/relation/batches.rb +38 -34
  118. data/lib/active_record/relation/calculations.rb +252 -139
  119. data/lib/active_record/relation/delegation.rb +125 -0
  120. data/lib/active_record/relation/finder_methods.rb +182 -188
  121. data/lib/active_record/relation/merger.rb +161 -0
  122. data/lib/active_record/relation/predicate_builder.rb +86 -21
  123. data/lib/active_record/relation/query_methods.rb +917 -134
  124. data/lib/active_record/relation/spawn_methods.rb +53 -92
  125. data/lib/active_record/relation.rb +405 -143
  126. data/lib/active_record/result.rb +67 -0
  127. data/lib/active_record/runtime_registry.rb +17 -0
  128. data/lib/active_record/sanitization.rb +168 -0
  129. data/lib/active_record/schema.rb +20 -14
  130. data/lib/active_record/schema_dumper.rb +55 -46
  131. data/lib/active_record/schema_migration.rb +39 -0
  132. data/lib/active_record/scoping/default.rb +146 -0
  133. data/lib/active_record/scoping/named.rb +175 -0
  134. data/lib/active_record/scoping.rb +82 -0
  135. data/lib/active_record/serialization.rb +8 -46
  136. data/lib/active_record/serializers/xml_serializer.rb +21 -68
  137. data/lib/active_record/statement_cache.rb +26 -0
  138. data/lib/active_record/store.rb +156 -0
  139. data/lib/active_record/tasks/database_tasks.rb +203 -0
  140. data/lib/active_record/tasks/firebird_database_tasks.rb +56 -0
  141. data/lib/active_record/tasks/mysql_database_tasks.rb +143 -0
  142. data/lib/active_record/tasks/oracle_database_tasks.rb +45 -0
  143. data/lib/active_record/tasks/postgresql_database_tasks.rb +90 -0
  144. data/lib/active_record/tasks/sqlite_database_tasks.rb +51 -0
  145. data/lib/active_record/tasks/sqlserver_database_tasks.rb +48 -0
  146. data/lib/active_record/test_case.rb +57 -28
  147. data/lib/active_record/timestamp.rb +49 -18
  148. data/lib/active_record/transactions.rb +106 -63
  149. data/lib/active_record/translation.rb +22 -0
  150. data/lib/active_record/validations/associated.rb +25 -24
  151. data/lib/active_record/validations/presence.rb +65 -0
  152. data/lib/active_record/validations/uniqueness.rb +123 -83
  153. data/lib/active_record/validations.rb +29 -29
  154. data/lib/active_record/version.rb +7 -5
  155. data/lib/active_record.rb +83 -34
  156. data/lib/rails/generators/active_record/migration/migration_generator.rb +46 -9
  157. data/lib/rails/generators/active_record/migration/templates/create_table_migration.rb +19 -0
  158. data/lib/rails/generators/active_record/migration/templates/migration.rb +30 -8
  159. data/lib/rails/generators/active_record/model/model_generator.rb +15 -5
  160. data/lib/rails/generators/active_record/model/templates/model.rb +7 -2
  161. data/lib/rails/generators/active_record/model/templates/module.rb +3 -1
  162. data/lib/rails/generators/active_record.rb +4 -8
  163. metadata +163 -121
  164. data/CHANGELOG +0 -6023
  165. data/examples/associations.png +0 -0
  166. data/lib/active_record/association_preload.rb +0 -403
  167. data/lib/active_record/associations/association_collection.rb +0 -562
  168. data/lib/active_record/associations/association_proxy.rb +0 -295
  169. data/lib/active_record/associations/through_association_scope.rb +0 -154
  170. data/lib/active_record/connection_adapters/abstract/connection_specification.rb +0 -113
  171. data/lib/active_record/connection_adapters/sqlite_adapter.rb +0 -401
  172. data/lib/active_record/dynamic_finder_match.rb +0 -53
  173. data/lib/active_record/dynamic_scope_match.rb +0 -32
  174. data/lib/active_record/named_scope.rb +0 -138
  175. data/lib/active_record/observer.rb +0 -140
  176. data/lib/active_record/session_store.rb +0 -340
  177. data/lib/rails/generators/active_record/model/templates/migration.rb +0 -16
  178. data/lib/rails/generators/active_record/observer/observer_generator.rb +0 -15
  179. data/lib/rails/generators/active_record/observer/templates/observer.rb +0 -2
  180. data/lib/rails/generators/active_record/session_migration/session_migration_generator.rb +0 -24
  181. data/lib/rails/generators/active_record/session_migration/templates/migration.rb +0 -16
@@ -10,11 +10,11 @@ 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
- config.generators.orm :active_record, :migration => true,
17
- :timestamps => true
16
+ config.app_generators.orm :active_record, :migration => true,
17
+ :timestamps => true
18
18
 
19
19
  config.app_middleware.insert_after "::ActionDispatch::Callbacks",
20
20
  "ActiveRecord::QueryCache"
@@ -22,14 +22,54 @@ module ActiveRecord
22
22
  config.app_middleware.insert_after "::ActionDispatch::Callbacks",
23
23
  "ActiveRecord::ConnectionAdapters::ConnectionManagement"
24
24
 
25
+ config.action_dispatch.rescue_responses.merge!(
26
+ 'ActiveRecord::RecordNotFound' => :not_found,
27
+ 'ActiveRecord::StaleObjectError' => :conflict,
28
+ 'ActiveRecord::RecordInvalid' => :unprocessable_entity,
29
+ 'ActiveRecord::RecordNotSaved' => :unprocessable_entity
30
+ )
31
+
32
+
33
+ config.active_record.use_schema_cache_dump = true
34
+
35
+ config.eager_load_namespaces << ActiveRecord
36
+
25
37
  rake_tasks do
38
+ require "active_record/base"
39
+
40
+ ActiveRecord::Tasks::DatabaseTasks.seed_loader = Rails.application
41
+ ActiveRecord::Tasks::DatabaseTasks.env = Rails.env
42
+
43
+ namespace :db do
44
+ task :load_config do
45
+ ActiveRecord::Tasks::DatabaseTasks.db_dir = Rails.application.config.paths["db"].first
46
+ ActiveRecord::Tasks::DatabaseTasks.database_configuration = Rails.application.config.database_configuration
47
+ ActiveRecord::Tasks::DatabaseTasks.migrations_paths = Rails.application.paths['db/migrate'].to_a
48
+ ActiveRecord::Tasks::DatabaseTasks.fixtures_path = File.join Rails.root, 'test', 'fixtures'
49
+
50
+ if defined?(ENGINE_PATH) && engine = Rails::Engine.find(ENGINE_PATH)
51
+ if engine.paths['db/migrate'].existent
52
+ ActiveRecord::Tasks::DatabaseTasks.migrations_paths += engine.paths['db/migrate'].to_a
53
+ end
54
+ end
55
+ end
56
+ end
57
+
26
58
  load "active_record/railties/databases.rake"
27
59
  end
28
60
 
29
- # When loading console, force ActiveRecord to be loaded to avoid cross
30
- # references when loading a constant for the first time.
31
- console do
32
- ActiveRecord::Base
61
+ # When loading console, force ActiveRecord::Base to be loaded
62
+ # to avoid cross references when loading a constant for the
63
+ # first time. Also, make it output to STDERR.
64
+ console do |app|
65
+ require "active_record/railties/console_sandbox" if app.sandbox?
66
+ require "active_record/base"
67
+ console = ActiveSupport::Logger.new(STDERR)
68
+ Rails.logger.extend ActiveSupport::Logger.broadcast console
69
+ end
70
+
71
+ runner do
72
+ require "active_record/base"
33
73
  end
34
74
 
35
75
  initializer "active_record.initialize_timezone" do
@@ -43,8 +83,84 @@ module ActiveRecord
43
83
  ActiveSupport.on_load(:active_record) { self.logger ||= ::Rails.logger }
44
84
  end
45
85
 
86
+ initializer "active_record.migration_error" do
87
+ if config.active_record.delete(:migration_error) == :page_load
88
+ config.app_middleware.insert_after "::ActionDispatch::Callbacks",
89
+ "ActiveRecord::Migration::CheckPending"
90
+ end
91
+ end
92
+
93
+ initializer "active_record.check_schema_cache_dump" do
94
+ if config.active_record.delete(:use_schema_cache_dump)
95
+ config.after_initialize do |app|
96
+ ActiveSupport.on_load(:active_record) do
97
+ filename = File.join(app.config.paths["db"].first, "schema_cache.dump")
98
+
99
+ if File.file?(filename)
100
+ cache = Marshal.load File.binread filename
101
+ if cache.version == ActiveRecord::Migrator.current_version
102
+ self.connection.schema_cache = cache
103
+ else
104
+ 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}."
105
+ end
106
+ end
107
+ end
108
+ end
109
+ end
110
+ end
111
+
46
112
  initializer "active_record.set_configs" do |app|
47
113
  ActiveSupport.on_load(:active_record) do
114
+ begin
115
+ old_behavior, ActiveSupport::Deprecation.behavior = ActiveSupport::Deprecation.behavior, :stderr
116
+ whitelist_attributes = app.config.active_record.delete(:whitelist_attributes)
117
+
118
+ if respond_to?(:mass_assignment_sanitizer=)
119
+ mass_assignment_sanitizer = nil
120
+ else
121
+ mass_assignment_sanitizer = app.config.active_record.delete(:mass_assignment_sanitizer)
122
+ end
123
+
124
+ unless whitelist_attributes.nil? && mass_assignment_sanitizer.nil?
125
+ ActiveSupport::Deprecation.warn <<-EOF.strip_heredoc, []
126
+ Model based mass assignment security has been extracted
127
+ out of Rails into a gem. Please use the new recommended protection model for
128
+ params or add `protected_attributes` to your Gemfile to use the old one.
129
+
130
+ To disable this message remove the `whitelist_attributes` option from your
131
+ `config/application.rb` file and any `mass_assignment_sanitizer` options
132
+ from your `config/environments/*.rb` files.
133
+
134
+ See http://guides.rubyonrails.org/security.html#mass-assignment for more information.
135
+ EOF
136
+ end
137
+
138
+ unless app.config.active_record.delete(:auto_explain_threshold_in_seconds).nil?
139
+ ActiveSupport::Deprecation.warn <<-EOF.strip_heredoc, []
140
+ The Active Record auto explain feature has been removed.
141
+
142
+ To disable this message remove the `active_record.auto_explain_threshold_in_seconds`
143
+ option from the `config/environments/*.rb` config file.
144
+
145
+ See http://guides.rubyonrails.org/4_0_release_notes.html for more information.
146
+ EOF
147
+ end
148
+
149
+ unless app.config.active_record.delete(:observers).nil?
150
+ ActiveSupport::Deprecation.warn <<-EOF.strip_heredoc, []
151
+ Active Record Observers has been extracted out of Rails into a gem.
152
+ Please use callbacks or add `rails-observers` to your Gemfile to use observers.
153
+
154
+ To disable this message remove the `observers` option from your
155
+ `config/application.rb` or from your initializers.
156
+
157
+ See http://guides.rubyonrails.org/4_0_release_notes.html for more information.
158
+ EOF
159
+ end
160
+ ensure
161
+ ActiveSupport::Deprecation.behavior = old_behavior
162
+ end
163
+
48
164
  app.config.active_record.each do |k,v|
49
165
  send "#{k}=", v
50
166
  end
@@ -55,37 +171,35 @@ module ActiveRecord
55
171
  # and then establishes the connection.
56
172
  initializer "active_record.initialize_database" do |app|
57
173
  ActiveSupport.on_load(:active_record) do
58
- self.configurations = app.config.database_configuration
174
+ self.configurations = app.config.database_configuration || {}
59
175
  establish_connection
60
176
  end
61
177
  end
62
178
 
63
179
  # Expose database runtime to controller for logging.
64
- initializer "active_record.log_runtime" do |app|
180
+ initializer "active_record.log_runtime" do
65
181
  require "active_record/railties/controller_runtime"
66
182
  ActiveSupport.on_load(:action_controller) do
67
183
  include ActiveRecord::Railties::ControllerRuntime
68
184
  end
69
185
  end
70
186
 
71
- initializer "active_record.set_dispatch_hooks", :before => :set_clear_dependencies_hook do |app|
72
- unless app.config.cache_classes
73
- ActiveSupport.on_load(:active_record) do
74
- ActionDispatch::Callbacks.after do
187
+ initializer "active_record.set_reloader_hooks" do |app|
188
+ hook = app.config.reload_classes_only_on_change ? :to_prepare : :to_cleanup
189
+
190
+ ActiveSupport.on_load(:active_record) do
191
+ ActionDispatch::Reloader.send(hook) do
192
+ if ActiveRecord::Base.connected?
75
193
  ActiveRecord::Base.clear_reloadable_connections!
194
+ ActiveRecord::Base.clear_cache!
76
195
  end
77
196
  end
78
197
  end
79
198
  end
80
199
 
81
- config.after_initialize do
82
- ActiveSupport.on_load(:active_record) do
83
- instantiate_observers
84
-
85
- ActionDispatch::Callbacks.to_prepare(:activerecord_instantiate_observers) do
86
- ActiveRecord::Base.instantiate_observers
87
- end
88
- end
200
+ initializer "active_record.add_watchable_files" do |app|
201
+ path = app.paths["db"].first
202
+ config.watchable_files.concat ["#{path}/schema.rb", "#{path}/structure.sql"]
89
203
  end
90
204
  end
91
205
  end
@@ -0,0 +1,5 @@
1
+ ActiveRecord::Base.connection.begin_transaction(joinable: false)
2
+
3
+ at_exit do
4
+ ActiveRecord::Base.connection.rollback_transaction
5
+ end
@@ -1,20 +1,30 @@
1
1
  require 'active_support/core_ext/module/attr_internal'
2
+ require 'active_record/log_subscriber'
2
3
 
3
4
  module ActiveRecord
4
- module Railties
5
- module ControllerRuntime
5
+ module Railties # :nodoc:
6
+ module ControllerRuntime #:nodoc:
6
7
  extend ActiveSupport::Concern
7
8
 
8
9
  protected
9
10
 
10
11
  attr_internal :db_runtime
11
12
 
13
+ def process_action(action, *args)
14
+ # We also need to reset the runtime before each action
15
+ # because of queries in middleware or in cases we are streaming
16
+ # and it won't be cleaned up by the method below.
17
+ ActiveRecord::LogSubscriber.reset_runtime
18
+ super
19
+ end
20
+
12
21
  def cleanup_view_runtime
13
22
  if ActiveRecord::Base.connected?
14
23
  db_rt_before_render = ActiveRecord::LogSubscriber.reset_runtime
24
+ self.db_runtime = (db_runtime || 0) + db_rt_before_render
15
25
  runtime = super
16
26
  db_rt_after_render = ActiveRecord::LogSubscriber.reset_runtime
17
- self.db_runtime = db_rt_before_render + db_rt_after_render
27
+ self.db_runtime += db_rt_after_render
18
28
  runtime - db_rt_after_render
19
29
  else
20
30
  super
@@ -23,10 +33,12 @@ module ActiveRecord
23
33
 
24
34
  def append_info_to_payload(payload)
25
35
  super
26
- payload[:db_runtime] = db_runtime
36
+ if ActiveRecord::Base.connected?
37
+ payload[:db_runtime] = (db_runtime || 0) + ActiveRecord::LogSubscriber.reset_runtime
38
+ end
27
39
  end
28
40
 
29
- module ClassMethods
41
+ module ClassMethods # :nodoc:
30
42
  def log_process_action(payload)
31
43
  messages, db_runtime = super, payload[:db_runtime]
32
44
  messages << ("ActiveRecord: %.1fms" % db_runtime.to_f) if db_runtime