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
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2004-2013 David Heinemeier Hansson
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc CHANGED
@@ -3,7 +3,7 @@
3
3
  Active Record connects classes to relational database tables to establish an
4
4
  almost zero-configuration persistence layer for applications. The library
5
5
  provides a base class that, when subclassed, sets up a mapping between the new
6
- class and an existing table in the database. In context of an application,
6
+ class and an existing table in the database. In the context of an application,
7
7
  these classes are commonly referred to as *models*. Models can also be
8
8
  connected to other models; this is done by defining *associations*.
9
9
 
@@ -49,10 +49,10 @@ A short rundown of some of the major features:
49
49
  * Aggregations of value objects.
50
50
 
51
51
  class Account < ActiveRecord::Base
52
- composed_of :balance, :class_name => "Money",
53
- :mapping => %w(balance amount)
52
+ composed_of :balance, class_name: 'Money',
53
+ mapping: %w(balance amount)
54
54
  composed_of :address,
55
- :mapping => [%w(address_street street), %w(address_city city)]
55
+ mapping: [%w(address_street street), %w(address_city city)]
56
56
  end
57
57
 
58
58
  {Learn more}[link:classes/ActiveRecord/Aggregations/ClassMethods.html]
@@ -61,16 +61,16 @@ A short rundown of some of the major features:
61
61
  * Validation rules that can differ for new or existing objects.
62
62
 
63
63
  class Account < ActiveRecord::Base
64
- validates_presence_of :subdomain, :name, :email_address, :password
65
- validates_uniqueness_of :subdomain
66
- validates_acceptance_of :terms_of_service, :on => :create
67
- validates_confirmation_of :password, :email_address, :on => :create
64
+ validates :subdomain, :name, :email_address, :password, presence: true
65
+ validates :subdomain, uniqueness: true
66
+ validates :terms_of_service, acceptance: true, on: :create
67
+ validates :password, :email_address, confirmation: true, on: :create
68
68
  end
69
69
 
70
70
  {Learn more}[link:classes/ActiveRecord/Validations.html]
71
71
 
72
72
 
73
- * Callbacks available for the entire life cycle (instantiation, saving, destroying, validating, etc.)
73
+ * Callbacks available for the entire life cycle (instantiation, saving, destroying, validating, etc.).
74
74
 
75
75
  class Person < ActiveRecord::Base
76
76
  before_destroy :invalidate_payment_plan
@@ -80,18 +80,7 @@ A short rundown of some of the major features:
80
80
  {Learn more}[link:classes/ActiveRecord/Callbacks.html]
81
81
 
82
82
 
83
- * Observers that react to changes in a model
84
-
85
- class CommentObserver < ActiveRecord::Observer
86
- def after_create(comment) # is called just after Comment#save
87
- Notifications.deliver_new_comment("david@loudthinking.com", comment)
88
- end
89
- end
90
-
91
- {Learn more}[link:classes/ActiveRecord/Observer.html]
92
-
93
-
94
- * Inheritance hierarchies
83
+ * Inheritance hierarchies.
95
84
 
96
85
  class Company < ActiveRecord::Base; end
97
86
  class Firm < Company; end
@@ -101,7 +90,7 @@ A short rundown of some of the major features:
101
90
  {Learn more}[link:classes/ActiveRecord/Base.html]
102
91
 
103
92
 
104
- * Transactions
93
+ * Transactions.
105
94
 
106
95
  # Database transaction
107
96
  Account.transaction do
@@ -112,7 +101,7 @@ A short rundown of some of the major features:
112
101
  {Learn more}[link:classes/ActiveRecord/Transactions/ClassMethods.html]
113
102
 
114
103
 
115
- * Reflections on columns, associations, and aggregations
104
+ * Reflections on columns, associations, and aggregations.
116
105
 
117
106
  reflection = Firm.reflect_on_association(:clients)
118
107
  reflection.klass # => Client (class)
@@ -121,18 +110,18 @@ A short rundown of some of the major features:
121
110
  {Learn more}[link:classes/ActiveRecord/Reflection/ClassMethods.html]
122
111
 
123
112
 
124
- * Database abstraction through simple adapters
113
+ * Database abstraction through simple adapters.
125
114
 
126
115
  # connect to SQLite3
127
- ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :database => "dbfile.sqlite3")
116
+ ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: 'dbfile.sqlite3')
128
117
 
129
118
  # connect to MySQL with authentication
130
119
  ActiveRecord::Base.establish_connection(
131
- :adapter => "mysql",
132
- :host => "localhost",
133
- :username => "me",
134
- :password => "secret",
135
- :database => "activerecord"
120
+ adapter: 'mysql2',
121
+ host: 'localhost',
122
+ username: 'me',
123
+ password: 'secret',
124
+ database: 'activerecord'
136
125
  )
137
126
 
138
127
  {Learn more}[link:classes/ActiveRecord/Base.html] and read about the built-in support for
@@ -141,16 +130,16 @@ A short rundown of some of the major features:
141
130
  SQLite3[link:classes/ActiveRecord/ConnectionAdapters/SQLite3Adapter.html].
142
131
 
143
132
 
144
- * Logging support for Log4r[http://log4r.sourceforge.net] and Logger[http://www.ruby-doc.org/stdlib/libdoc/logger/rdoc]
133
+ * Logging support for Log4r[http://log4r.rubyforge.org] and Logger[http://www.ruby-doc.org/stdlib/libdoc/logger/rdoc].
145
134
 
146
- ActiveRecord::Base.logger = Logger.new(STDOUT)
147
- ActiveRecord::Base.logger = Log4r::Logger.new("Application Log")
135
+ ActiveRecord::Base.logger = ActiveSupport::Logger.new(STDOUT)
136
+ ActiveRecord::Base.logger = Log4r::Logger.new('Application Log')
148
137
 
149
138
 
150
- * Database agnostic schema management with Migrations
139
+ * Database agnostic schema management with Migrations.
151
140
 
152
141
  class AddSystemSettings < ActiveRecord::Migration
153
- def self.up
142
+ def up
154
143
  create_table :system_settings do |t|
155
144
  t.string :name
156
145
  t.string :label
@@ -159,10 +148,10 @@ A short rundown of some of the major features:
159
148
  t.integer :position
160
149
  end
161
150
 
162
- SystemSetting.create :name => "notice", :label => "Use notice?", :value => 1
151
+ SystemSetting.create name: 'notice', label: 'Use notice?', value: 1
163
152
  end
164
153
 
165
- def self.down
154
+ def down
166
155
  drop_table :system_settings
167
156
  end
168
157
  end
@@ -197,26 +186,28 @@ Admit the Database:
197
186
 
198
187
  == Download and installation
199
188
 
200
- The latest version of Active Record can be installed with Rubygems:
189
+ The latest version of Active Record can be installed with RubyGems:
201
190
 
202
191
  % [sudo] gem install activerecord
203
192
 
204
- Source code can be downloaded as part of the Rails project on GitHub
193
+ Source code can be downloaded as part of the Rails project on GitHub:
205
194
 
206
- * http://github.com/rails/rails/tree/master/activerecord/
195
+ * https://github.com/rails/rails/tree/master/activerecord
207
196
 
208
197
 
209
198
  == License
210
199
 
211
- Active Record is released under the MIT license.
200
+ Active Record is released under the MIT license:
201
+
202
+ * http://www.opensource.org/licenses/MIT
212
203
 
213
204
 
214
205
  == Support
215
206
 
216
- API documentation is at
207
+ API documentation is at:
217
208
 
218
- * http://api.rubyonrails.com
209
+ * http://api.rubyonrails.org
219
210
 
220
211
  Bug reports and feature requests can be filed with the rest for the Ruby on Rails project here:
221
212
 
222
- * https://rails.lighthouseapp.com/projects/8994-ruby-on-rails/tickets
213
+ * https://github.com/rails/rails/issues
@@ -1,31 +1,11 @@
1
- #!/usr/bin/env ruby -KU
2
-
3
- TIMES = (ENV['N'] || 10000).to_i
4
- require 'rubygems'
5
-
6
- gem 'addressable', '~>2.0'
7
- gem 'faker', '~>0.3.1'
8
- gem 'rbench', '~>0.2.3'
9
-
10
- require 'addressable/uri'
11
- require 'faker'
12
- require 'rbench'
13
-
14
- require File.expand_path("../../../load_paths", __FILE__)
1
+ require File.expand_path('../../../load_paths', __FILE__)
15
2
  require "active_record"
3
+ require 'benchmark/ips'
16
4
 
17
- conn = { :adapter => 'mysql',
18
- :database => 'activerecord_unittest',
19
- :username => 'rails', :password => '',
20
- :encoding => 'utf8' }
5
+ TIME = (ENV['BENCHMARK_TIME'] || 20).to_i
6
+ RECORDS = (ENV['BENCHMARK_RECORDS'] || TIME*1000).to_i
21
7
 
22
- conn[:socket] = Pathname.glob(%w[
23
- /opt/local/var/run/mysql5/mysqld.sock
24
- /tmp/mysqld.sock
25
- /tmp/mysql.sock
26
- /var/mysql/mysql.sock
27
- /var/run/mysqld/mysqld.sock
28
- ]).find { |path| path.socket? }
8
+ conn = { :adapter => 'sqlite3', :database => ':memory:' }
29
9
 
30
10
  ActiveRecord::Base.establish_connection(conn)
31
11
 
@@ -51,112 +31,142 @@ class Exhibit < ActiveRecord::Base
51
31
  def look; attributes end
52
32
  def feel; look; user.name end
53
33
 
34
+ def self.with_name
35
+ where("name IS NOT NULL")
36
+ end
37
+
38
+ def self.with_notes
39
+ where("notes IS NOT NULL")
40
+ end
41
+
54
42
  def self.look(exhibits) exhibits.each { |e| e.look } end
55
43
  def self.feel(exhibits) exhibits.each { |e| e.feel } end
56
44
  end
57
45
 
58
- sqlfile = File.expand_path("../performance.sql", __FILE__)
59
-
60
- if File.exists?(sqlfile)
61
- mysql_bin = %w[mysql mysql5].detect { |bin| `which #{bin}`.length > 0 }
62
- `#{mysql_bin} -u #{conn[:username]} #{"-p#{conn[:password]}" unless conn[:password].blank?} #{conn[:database]} < #{sqlfile}`
63
- else
64
- puts 'Generating data...'
65
-
66
- # pre-compute the insert statements and fake data compilation,
67
- # so the benchmarks below show the actual runtime for the execute
68
- # method, minus the setup steps
69
-
70
- # Using the same paragraph for all exhibits because it is very slow
71
- # to generate unique paragraphs for all exhibits.
72
- notes = Faker::Lorem.paragraphs.join($/)
73
- today = Date.today
74
-
75
- puts 'Inserting 10,000 users and exhibits...'
76
- 10_000.times do
77
- user = User.create(
78
- :created_at => today,
79
- :name => Faker::Name.name,
80
- :email => Faker::Internet.email
81
- )
82
-
83
- Exhibit.create(
84
- :created_at => today,
85
- :name => Faker::Company.name,
86
- :user => user,
87
- :notes => notes
88
- )
89
- end
90
-
91
- mysqldump_bin = %w[mysqldump mysqldump5].select { |bin| `which #{bin}`.length > 0 }
92
- `#{mysqldump_bin} -u #{conn[:username]} #{"-p#{conn[:password]}" unless conn[:password].blank?} #{conn[:database]} exhibits users > #{sqlfile}`
46
+ puts 'Generating data...'
47
+
48
+ module ActiveRecord
49
+ class Faker
50
+ LOREM = %Q{Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse non aliquet diam. Curabitur vel urna metus, quis malesuada elit.
51
+ Integer consequat tincidunt felis. Etiam non erat dolor. Vivamus imperdiet nibh sit amet diam eleifend id posuere diam malesuada. Mauris at accumsan sem.
52
+ Donec id lorem neque. Fusce erat lorem, ornare eu congue vitae, malesuada quis neque. Maecenas vel urna a velit pretium fermentum. Donec tortor enim,
53
+ tempor venenatis egestas a, tempor sed ipsum. Ut arcu justo, faucibus non imperdiet ac, interdum at diam. Pellentesque ipsum enim, venenatis ut iaculis vitae,
54
+ varius vitae sem. Sed rutrum quam ac elit euismod bibendum. Donec ultricies ultricies magna, at lacinia libero mollis aliquam. Sed ac arcu in tortor elementum
55
+ tincidunt vel interdum sem. Curabitur eget erat arcu. Praesent eget eros leo. Nam magna enim, sollicitudin vehicula scelerisque in, vulputate ut libero.
56
+ Praesent varius tincidunt commodo}.split
57
+
58
+ def self.name
59
+ LOREM.grep(/^\w*$/).sort_by { rand }.first(2).join ' '
60
+ end
61
+
62
+ def self.email
63
+ LOREM.grep(/^\w*$/).sort_by { rand }.first(2).join('@') + ".com"
64
+ end
65
+ end
93
66
  end
94
67
 
95
- RBench.run(TIMES) do
96
- column :times
97
- column :ar
68
+ # pre-compute the insert statements and fake data compilation,
69
+ # so the benchmarks below show the actual runtime for the execute
70
+ # method, minus the setup steps
71
+
72
+ # Using the same paragraph for all exhibits because it is very slow
73
+ # to generate unique paragraphs for all exhibits.
74
+ notes = ActiveRecord::Faker::LOREM.join ' '
75
+ today = Date.today
76
+
77
+ puts "Inserting #{RECORDS} users and exhibits..."
78
+ RECORDS.times do
79
+ user = User.create(
80
+ :created_at => today,
81
+ :name => ActiveRecord::Faker.name,
82
+ :email => ActiveRecord::Faker.email
83
+ )
84
+
85
+ Exhibit.create(
86
+ :created_at => today,
87
+ :name => ActiveRecord::Faker.name,
88
+ :user => user,
89
+ :notes => notes
90
+ )
91
+ end
98
92
 
99
- report 'Model#id', (TIMES * 100).ceil do
100
- ar_obj = Exhibit.find(1)
93
+ Benchmark.ips(TIME) do |x|
94
+ ar_obj = Exhibit.find(1)
95
+ attrs = { :name => 'sam' }
96
+ attrs_first = { :name => 'sam' }
97
+ attrs_second = { :name => 'tom' }
98
+ exhibit = {
99
+ :name => ActiveRecord::Faker.name,
100
+ :notes => notes,
101
+ :created_at => Date.today
102
+ }
101
103
 
102
- ar { ar_obj.id }
104
+ x.report("Model#id") do
105
+ ar_obj.id
103
106
  end
104
107
 
105
- report 'Model.new (instantiation)' do
106
- ar { Exhibit.new }
108
+ x.report 'Model.new (instantiation)' do
109
+ Exhibit.new
107
110
  end
108
111
 
109
- report 'Model.new (setting attributes)' do
110
- attrs = { :name => 'sam' }
111
- ar { Exhibit.new(attrs) }
112
+ x.report 'Model.new (setting attributes)' do
113
+ Exhibit.new(attrs)
112
114
  end
113
115
 
114
- report 'Model.first' do
115
- ar { Exhibit.first.look }
116
+ x.report 'Model.first' do
117
+ Exhibit.first.look
116
118
  end
117
119
 
118
- report 'Model.all limit(100)', (TIMES / 10).ceil do
119
- ar { Exhibit.look Exhibit.limit(100) }
120
+ x.report("Model.all limit(100)") do
121
+ Exhibit.look Exhibit.limit(100)
120
122
  end
121
123
 
122
- report 'Model.all limit(100) with relationship', (TIMES / 10).ceil do
123
- ar { Exhibit.feel Exhibit.limit(100).includes(:user) }
124
+ x.report "Model.all limit(100) with relationship" do
125
+ Exhibit.feel Exhibit.limit(100).includes(:user)
124
126
  end
125
127
 
126
- report 'Model.all limit(10,000)', (TIMES / 1000).ceil do
127
- ar { Exhibit.look Exhibit.limit(10000) }
128
+ x.report "Model.all limit(10,000)" do
129
+ Exhibit.look Exhibit.limit(10000)
128
130
  end
129
131
 
130
- exhibit = {
131
- :name => Faker::Company.name,
132
- :notes => Faker::Lorem.paragraphs.join($/),
133
- :created_at => Date.today
134
- }
132
+ x.report 'Model.named_scope' do
133
+ Exhibit.limit(10).with_name.with_notes
134
+ end
135
135
 
136
- report 'Model.create' do
137
- ar { Exhibit.create(exhibit) }
136
+ x.report 'Model.create' do
137
+ Exhibit.create(exhibit)
138
138
  end
139
139
 
140
- report 'Resource#attributes=' do
141
- attrs_first = { :name => 'sam' }
142
- attrs_second = { :name => 'tom' }
143
- ar { exhibit = Exhibit.new(attrs_first); exhibit.attributes = attrs_second }
140
+ x.report 'Resource#attributes=' do
141
+ e = Exhibit.new(attrs_first)
142
+ e.attributes = attrs_second
144
143
  end
145
144
 
146
- report 'Resource#update' do
147
- ar { Exhibit.first.update_attributes(:name => 'bob') }
145
+ x.report 'Resource#update' do
146
+ Exhibit.first.update(name: 'bob')
148
147
  end
149
148
 
150
- report 'Resource#destroy' do
151
- ar { Exhibit.first.destroy }
149
+ x.report 'Resource#destroy' do
150
+ Exhibit.first.destroy
152
151
  end
153
152
 
154
- report 'Model.transaction' do
155
- ar { Exhibit.transaction { Exhibit.new } }
153
+ x.report 'Model.transaction' do
154
+ Exhibit.transaction { Exhibit.new }
156
155
  end
157
156
 
158
- summary 'Total'
159
- end
157
+ x.report 'Model.find(id)' do
158
+ User.find(1)
159
+ end
160
160
 
161
- ActiveRecord::Migration.drop_table "exhibits"
162
- ActiveRecord::Migration.drop_table "users"
161
+ x.report 'Model.find_by_sql' do
162
+ Exhibit.find_by_sql("SELECT * FROM exhibits WHERE id = #{(rand * 1000 + 1).to_i}").first
163
+ end
164
+
165
+ x.report "Model.log" do
166
+ Exhibit.connection.send(:log, "hello", "world") {}
167
+ end
168
+
169
+ x.report "AR.execute(query)" do
170
+ ActiveRecord::Base.connection.execute("Select * from exhibits where id = #{(rand * 1000 + 1).to_i}")
171
+ end
172
+ end