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.
- checksums.yaml +7 -0
- data/CHANGELOG.md +2102 -0
- data/MIT-LICENSE +20 -0
- data/README.rdoc +35 -44
- data/examples/performance.rb +110 -100
- data/lib/active_record/aggregations.rb +59 -75
- data/lib/active_record/associations/alias_tracker.rb +76 -0
- data/lib/active_record/associations/association.rb +248 -0
- data/lib/active_record/associations/association_scope.rb +135 -0
- data/lib/active_record/associations/belongs_to_association.rb +60 -59
- data/lib/active_record/associations/belongs_to_polymorphic_association.rb +16 -59
- data/lib/active_record/associations/builder/association.rb +108 -0
- data/lib/active_record/associations/builder/belongs_to.rb +98 -0
- data/lib/active_record/associations/builder/collection_association.rb +89 -0
- data/lib/active_record/associations/builder/has_and_belongs_to_many.rb +39 -0
- data/lib/active_record/associations/builder/has_many.rb +15 -0
- data/lib/active_record/associations/builder/has_one.rb +25 -0
- data/lib/active_record/associations/builder/singular_association.rb +32 -0
- data/lib/active_record/associations/collection_association.rb +608 -0
- data/lib/active_record/associations/collection_proxy.rb +986 -0
- data/lib/active_record/associations/has_and_belongs_to_many_association.rb +40 -112
- data/lib/active_record/associations/has_many_association.rb +83 -76
- data/lib/active_record/associations/has_many_through_association.rb +147 -66
- data/lib/active_record/associations/has_one_association.rb +67 -108
- data/lib/active_record/associations/has_one_through_association.rb +21 -25
- data/lib/active_record/associations/join_dependency/join_association.rb +174 -0
- data/lib/active_record/associations/join_dependency/join_base.rb +24 -0
- data/lib/active_record/associations/join_dependency/join_part.rb +78 -0
- data/lib/active_record/associations/join_dependency.rb +235 -0
- data/lib/active_record/associations/join_helper.rb +45 -0
- data/lib/active_record/associations/preloader/association.rb +121 -0
- data/lib/active_record/associations/preloader/belongs_to.rb +17 -0
- data/lib/active_record/associations/preloader/collection_association.rb +24 -0
- data/lib/active_record/associations/preloader/has_and_belongs_to_many.rb +60 -0
- data/lib/active_record/associations/preloader/has_many.rb +17 -0
- data/lib/active_record/associations/preloader/has_many_through.rb +19 -0
- data/lib/active_record/associations/preloader/has_one.rb +23 -0
- data/lib/active_record/associations/preloader/has_one_through.rb +9 -0
- data/lib/active_record/associations/preloader/singular_association.rb +21 -0
- data/lib/active_record/associations/preloader/through_association.rb +63 -0
- data/lib/active_record/associations/preloader.rb +178 -0
- data/lib/active_record/associations/singular_association.rb +64 -0
- data/lib/active_record/associations/through_association.rb +87 -0
- data/lib/active_record/associations.rb +512 -1224
- data/lib/active_record/attribute_assignment.rb +201 -0
- data/lib/active_record/attribute_methods/before_type_cast.rb +49 -12
- data/lib/active_record/attribute_methods/dirty.rb +51 -28
- data/lib/active_record/attribute_methods/primary_key.rb +94 -22
- data/lib/active_record/attribute_methods/query.rb +5 -4
- data/lib/active_record/attribute_methods/read.rb +63 -72
- data/lib/active_record/attribute_methods/serialization.rb +162 -0
- data/lib/active_record/attribute_methods/time_zone_conversion.rb +39 -41
- data/lib/active_record/attribute_methods/write.rb +39 -13
- data/lib/active_record/attribute_methods.rb +362 -29
- data/lib/active_record/autosave_association.rb +132 -75
- data/lib/active_record/base.rb +83 -1627
- data/lib/active_record/callbacks.rb +69 -47
- data/lib/active_record/coders/yaml_column.rb +38 -0
- data/lib/active_record/connection_adapters/abstract/connection_pool.rb +411 -138
- data/lib/active_record/connection_adapters/abstract/database_limits.rb +21 -11
- data/lib/active_record/connection_adapters/abstract/database_statements.rb +234 -173
- data/lib/active_record/connection_adapters/abstract/query_cache.rb +36 -22
- data/lib/active_record/connection_adapters/abstract/quoting.rb +82 -25
- data/lib/active_record/connection_adapters/abstract/schema_definitions.rb +176 -414
- data/lib/active_record/connection_adapters/abstract/schema_dumper.rb +70 -0
- data/lib/active_record/connection_adapters/abstract/schema_statements.rb +562 -232
- data/lib/active_record/connection_adapters/abstract/transaction.rb +203 -0
- data/lib/active_record/connection_adapters/abstract_adapter.rb +281 -53
- data/lib/active_record/connection_adapters/abstract_mysql_adapter.rb +782 -0
- data/lib/active_record/connection_adapters/column.rb +318 -0
- data/lib/active_record/connection_adapters/connection_specification.rb +96 -0
- data/lib/active_record/connection_adapters/mysql2_adapter.rb +273 -0
- data/lib/active_record/connection_adapters/mysql_adapter.rb +365 -450
- data/lib/active_record/connection_adapters/postgresql/array_parser.rb +97 -0
- data/lib/active_record/connection_adapters/postgresql/cast.rb +152 -0
- data/lib/active_record/connection_adapters/postgresql/database_statements.rb +242 -0
- data/lib/active_record/connection_adapters/postgresql/oid.rb +366 -0
- data/lib/active_record/connection_adapters/postgresql/quoting.rb +171 -0
- data/lib/active_record/connection_adapters/postgresql/referential_integrity.rb +30 -0
- data/lib/active_record/connection_adapters/postgresql/schema_statements.rb +489 -0
- data/lib/active_record/connection_adapters/postgresql_adapter.rb +672 -752
- data/lib/active_record/connection_adapters/schema_cache.rb +129 -0
- data/lib/active_record/connection_adapters/sqlite3_adapter.rb +588 -17
- data/lib/active_record/connection_adapters/statement_pool.rb +40 -0
- data/lib/active_record/connection_handling.rb +98 -0
- data/lib/active_record/core.rb +463 -0
- data/lib/active_record/counter_cache.rb +108 -101
- data/lib/active_record/dynamic_matchers.rb +131 -0
- data/lib/active_record/errors.rb +54 -13
- data/lib/active_record/explain.rb +38 -0
- data/lib/active_record/explain_registry.rb +30 -0
- data/lib/active_record/explain_subscriber.rb +29 -0
- data/lib/active_record/fixture_set/file.rb +55 -0
- data/lib/active_record/fixtures.rb +703 -785
- data/lib/active_record/inheritance.rb +200 -0
- data/lib/active_record/integration.rb +60 -0
- data/lib/active_record/locale/en.yml +8 -1
- data/lib/active_record/locking/optimistic.rb +69 -60
- data/lib/active_record/locking/pessimistic.rb +34 -12
- data/lib/active_record/log_subscriber.rb +40 -6
- data/lib/active_record/migration/command_recorder.rb +164 -0
- data/lib/active_record/migration/join_table.rb +15 -0
- data/lib/active_record/migration.rb +614 -216
- data/lib/active_record/model_schema.rb +345 -0
- data/lib/active_record/nested_attributes.rb +248 -119
- data/lib/active_record/null_relation.rb +65 -0
- data/lib/active_record/persistence.rb +275 -57
- data/lib/active_record/query_cache.rb +29 -9
- data/lib/active_record/querying.rb +62 -0
- data/lib/active_record/railtie.rb +135 -21
- data/lib/active_record/railties/console_sandbox.rb +5 -0
- data/lib/active_record/railties/controller_runtime.rb +17 -5
- data/lib/active_record/railties/databases.rake +249 -359
- data/lib/active_record/railties/jdbcmysql_error.rb +16 -0
- data/lib/active_record/readonly_attributes.rb +30 -0
- data/lib/active_record/reflection.rb +283 -103
- data/lib/active_record/relation/batches.rb +38 -34
- data/lib/active_record/relation/calculations.rb +252 -139
- data/lib/active_record/relation/delegation.rb +125 -0
- data/lib/active_record/relation/finder_methods.rb +182 -188
- data/lib/active_record/relation/merger.rb +161 -0
- data/lib/active_record/relation/predicate_builder.rb +86 -21
- data/lib/active_record/relation/query_methods.rb +917 -134
- data/lib/active_record/relation/spawn_methods.rb +53 -92
- data/lib/active_record/relation.rb +405 -143
- data/lib/active_record/result.rb +67 -0
- data/lib/active_record/runtime_registry.rb +17 -0
- data/lib/active_record/sanitization.rb +168 -0
- data/lib/active_record/schema.rb +20 -14
- data/lib/active_record/schema_dumper.rb +55 -46
- data/lib/active_record/schema_migration.rb +39 -0
- data/lib/active_record/scoping/default.rb +146 -0
- data/lib/active_record/scoping/named.rb +175 -0
- data/lib/active_record/scoping.rb +82 -0
- data/lib/active_record/serialization.rb +8 -46
- data/lib/active_record/serializers/xml_serializer.rb +21 -68
- data/lib/active_record/statement_cache.rb +26 -0
- data/lib/active_record/store.rb +156 -0
- data/lib/active_record/tasks/database_tasks.rb +203 -0
- data/lib/active_record/tasks/firebird_database_tasks.rb +56 -0
- data/lib/active_record/tasks/mysql_database_tasks.rb +143 -0
- data/lib/active_record/tasks/oracle_database_tasks.rb +45 -0
- data/lib/active_record/tasks/postgresql_database_tasks.rb +90 -0
- data/lib/active_record/tasks/sqlite_database_tasks.rb +51 -0
- data/lib/active_record/tasks/sqlserver_database_tasks.rb +48 -0
- data/lib/active_record/test_case.rb +57 -28
- data/lib/active_record/timestamp.rb +49 -18
- data/lib/active_record/transactions.rb +106 -63
- data/lib/active_record/translation.rb +22 -0
- data/lib/active_record/validations/associated.rb +25 -24
- data/lib/active_record/validations/presence.rb +65 -0
- data/lib/active_record/validations/uniqueness.rb +123 -83
- data/lib/active_record/validations.rb +29 -29
- data/lib/active_record/version.rb +7 -5
- data/lib/active_record.rb +83 -34
- data/lib/rails/generators/active_record/migration/migration_generator.rb +46 -9
- data/lib/rails/generators/active_record/migration/templates/create_table_migration.rb +19 -0
- data/lib/rails/generators/active_record/migration/templates/migration.rb +30 -8
- data/lib/rails/generators/active_record/model/model_generator.rb +15 -5
- data/lib/rails/generators/active_record/model/templates/model.rb +7 -2
- data/lib/rails/generators/active_record/model/templates/module.rb +3 -1
- data/lib/rails/generators/active_record.rb +4 -8
- metadata +163 -121
- data/CHANGELOG +0 -6023
- data/examples/associations.png +0 -0
- data/lib/active_record/association_preload.rb +0 -403
- data/lib/active_record/associations/association_collection.rb +0 -562
- data/lib/active_record/associations/association_proxy.rb +0 -295
- data/lib/active_record/associations/through_association_scope.rb +0 -154
- data/lib/active_record/connection_adapters/abstract/connection_specification.rb +0 -113
- data/lib/active_record/connection_adapters/sqlite_adapter.rb +0 -401
- data/lib/active_record/dynamic_finder_match.rb +0 -53
- data/lib/active_record/dynamic_scope_match.rb +0 -32
- data/lib/active_record/named_scope.rb +0 -138
- data/lib/active_record/observer.rb +0 -140
- data/lib/active_record/session_store.rb +0 -340
- data/lib/rails/generators/active_record/model/templates/migration.rb +0 -16
- data/lib/rails/generators/active_record/observer/observer_generator.rb +0 -15
- data/lib/rails/generators/active_record/observer/templates/observer.rb +0 -2
- data/lib/rails/generators/active_record/session_migration/session_migration_generator.rb +0 -24
- 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, :
|
53
|
-
:
|
52
|
+
composed_of :balance, class_name: 'Money',
|
53
|
+
mapping: %w(balance amount)
|
54
54
|
composed_of :address,
|
55
|
-
:
|
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
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
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
|
-
*
|
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(:
|
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
|
-
:
|
132
|
-
:
|
133
|
-
:
|
134
|
-
:
|
135
|
-
:
|
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.
|
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(
|
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
|
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 :
|
151
|
+
SystemSetting.create name: 'notice', label: 'Use notice?', value: 1
|
163
152
|
end
|
164
153
|
|
165
|
-
def
|
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
|
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
|
-
*
|
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.
|
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://
|
213
|
+
* https://github.com/rails/rails/issues
|
data/examples/performance.rb
CHANGED
@@ -1,31 +1,11 @@
|
|
1
|
-
|
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
|
-
|
18
|
-
|
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
|
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
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
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
|
-
|
96
|
-
|
97
|
-
|
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
|
-
|
100
|
-
|
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
|
-
|
104
|
+
x.report("Model#id") do
|
105
|
+
ar_obj.id
|
103
106
|
end
|
104
107
|
|
105
|
-
report 'Model.new (instantiation)' do
|
106
|
-
|
108
|
+
x.report 'Model.new (instantiation)' do
|
109
|
+
Exhibit.new
|
107
110
|
end
|
108
111
|
|
109
|
-
report 'Model.new (setting attributes)' do
|
110
|
-
attrs
|
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
|
-
|
116
|
+
x.report 'Model.first' do
|
117
|
+
Exhibit.first.look
|
116
118
|
end
|
117
119
|
|
118
|
-
report
|
119
|
-
|
120
|
+
x.report("Model.all limit(100)") do
|
121
|
+
Exhibit.look Exhibit.limit(100)
|
120
122
|
end
|
121
123
|
|
122
|
-
report
|
123
|
-
|
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
|
127
|
-
|
128
|
+
x.report "Model.all limit(10,000)" do
|
129
|
+
Exhibit.look Exhibit.limit(10000)
|
128
130
|
end
|
129
131
|
|
130
|
-
|
131
|
-
|
132
|
-
|
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
|
-
|
136
|
+
x.report 'Model.create' do
|
137
|
+
Exhibit.create(exhibit)
|
138
138
|
end
|
139
139
|
|
140
|
-
report 'Resource#attributes=' do
|
141
|
-
|
142
|
-
|
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
|
-
|
145
|
+
x.report 'Resource#update' do
|
146
|
+
Exhibit.first.update(name: 'bob')
|
148
147
|
end
|
149
148
|
|
150
|
-
report 'Resource#destroy' do
|
151
|
-
|
149
|
+
x.report 'Resource#destroy' do
|
150
|
+
Exhibit.first.destroy
|
152
151
|
end
|
153
152
|
|
154
|
-
report 'Model.transaction' do
|
155
|
-
|
153
|
+
x.report 'Model.transaction' do
|
154
|
+
Exhibit.transaction { Exhibit.new }
|
156
155
|
end
|
157
156
|
|
158
|
-
|
159
|
-
|
157
|
+
x.report 'Model.find(id)' do
|
158
|
+
User.find(1)
|
159
|
+
end
|
160
160
|
|
161
|
-
|
162
|
-
|
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
|