activerecord 3.0.0.beta4 → 3.0.0.rc
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of activerecord might be problematic. Click here for more details.
- data/CHANGELOG +267 -254
- data/README.rdoc +222 -0
- data/examples/performance.rb +9 -9
- data/lib/active_record/aggregations.rb +3 -4
- data/lib/active_record/association_preload.rb +15 -10
- data/lib/active_record/associations.rb +54 -37
- data/lib/active_record/associations/association_collection.rb +43 -17
- data/lib/active_record/associations/association_proxy.rb +2 -0
- data/lib/active_record/associations/belongs_to_association.rb +1 -0
- data/lib/active_record/associations/belongs_to_polymorphic_association.rb +1 -0
- data/lib/active_record/associations/has_and_belongs_to_many_association.rb +22 -7
- data/lib/active_record/associations/has_many_association.rb +6 -1
- data/lib/active_record/associations/has_many_through_association.rb +1 -0
- data/lib/active_record/associations/has_one_association.rb +1 -0
- data/lib/active_record/associations/has_one_through_association.rb +1 -0
- data/lib/active_record/associations/through_association_scope.rb +3 -2
- data/lib/active_record/attribute_methods.rb +1 -0
- data/lib/active_record/autosave_association.rb +4 -6
- data/lib/active_record/base.rb +106 -240
- data/lib/active_record/callbacks.rb +4 -25
- data/lib/active_record/connection_adapters/abstract/connection_pool.rb +22 -29
- data/lib/active_record/connection_adapters/abstract/connection_specification.rb +2 -8
- data/lib/active_record/connection_adapters/abstract/database_statements.rb +2 -2
- data/lib/active_record/connection_adapters/abstract/schema_definitions.rb +10 -0
- data/lib/active_record/connection_adapters/abstract/schema_statements.rb +56 -7
- data/lib/active_record/connection_adapters/abstract_adapter.rb +10 -18
- data/lib/active_record/connection_adapters/mysql_adapter.rb +2 -2
- data/lib/active_record/connection_adapters/postgresql_adapter.rb +65 -69
- data/lib/active_record/connection_adapters/sqlite3_adapter.rb +19 -6
- data/lib/active_record/connection_adapters/sqlite_adapter.rb +20 -46
- data/lib/active_record/counter_cache.rb +14 -4
- data/lib/active_record/dynamic_finder_match.rb +9 -0
- data/lib/active_record/dynamic_scope_match.rb +7 -0
- data/lib/active_record/errors.rb +3 -0
- data/lib/active_record/fixtures.rb +5 -6
- data/lib/active_record/locale/en.yml +1 -1
- data/lib/active_record/locking/optimistic.rb +1 -0
- data/lib/active_record/log_subscriber.rb +48 -0
- data/lib/active_record/migration.rb +64 -37
- data/lib/active_record/named_scope.rb +33 -19
- data/lib/active_record/nested_attributes.rb +17 -13
- data/lib/active_record/observer.rb +13 -6
- data/lib/active_record/persistence.rb +55 -22
- data/lib/active_record/query_cache.rb +1 -0
- data/lib/active_record/railtie.rb +14 -8
- data/lib/active_record/railties/controller_runtime.rb +2 -2
- data/lib/active_record/railties/databases.rake +63 -33
- data/lib/active_record/reflection.rb +46 -28
- data/lib/active_record/relation.rb +38 -24
- data/lib/active_record/relation/finder_methods.rb +5 -5
- data/lib/active_record/relation/predicate_builder.rb +2 -4
- data/lib/active_record/relation/query_methods.rb +134 -115
- data/lib/active_record/relation/spawn_methods.rb +1 -1
- data/lib/active_record/schema.rb +2 -0
- data/lib/active_record/schema_dumper.rb +15 -12
- data/lib/active_record/serialization.rb +2 -0
- data/lib/active_record/session_store.rb +93 -79
- data/lib/active_record/test_case.rb +3 -0
- data/lib/active_record/timestamp.rb +49 -29
- data/lib/active_record/transactions.rb +5 -2
- data/lib/active_record/validations.rb +5 -2
- data/lib/active_record/validations/associated.rb +1 -1
- data/lib/active_record/validations/uniqueness.rb +1 -1
- data/lib/active_record/version.rb +1 -1
- data/lib/rails/generators/active_record/migration/templates/migration.rb +12 -6
- data/lib/rails/generators/active_record/model/model_generator.rb +1 -1
- metadata +27 -14
- data/README +0 -351
- data/lib/active_record/railties/log_subscriber.rb +0 -32
@@ -1,6 +1,8 @@
|
|
1
1
|
require 'active_support/core_ext/class/attribute'
|
2
2
|
|
3
3
|
module ActiveRecord
|
4
|
+
# = Active Record Observer
|
5
|
+
#
|
4
6
|
# Observer classes respond to lifecycle callbacks to implement trigger-like
|
5
7
|
# behavior outside the original class. This is a great way to reduce the
|
6
8
|
# clutter that normally comes when the model class is burdened with
|
@@ -66,7 +68,7 @@ module ActiveRecord
|
|
66
68
|
# == Configuration
|
67
69
|
#
|
68
70
|
# In order to activate an observer, list it in the <tt>config.active_record.observers</tt> configuration setting in your
|
69
|
-
# <tt>config/
|
71
|
+
# <tt>config/application.rb</tt> file.
|
70
72
|
#
|
71
73
|
# config.active_record.observers = :comment_observer, :signup_observer
|
72
74
|
#
|
@@ -88,21 +90,26 @@ module ActiveRecord
|
|
88
90
|
#
|
89
91
|
class Observer < ActiveModel::Observer
|
90
92
|
class_attribute :observed_methods
|
91
|
-
self.observed_methods = []
|
93
|
+
self.observed_methods = [].freeze
|
92
94
|
|
93
95
|
def initialize
|
94
96
|
super
|
95
|
-
|
97
|
+
observed_descendants.each { |klass| add_observer!(klass) }
|
96
98
|
end
|
97
99
|
|
98
100
|
def self.method_added(method)
|
99
101
|
method = method.to_sym
|
100
|
-
|
102
|
+
|
103
|
+
if ActiveRecord::Callbacks::CALLBACKS.include?(method)
|
104
|
+
self.observed_methods += [method]
|
105
|
+
self.observed_methods.freeze
|
106
|
+
end
|
101
107
|
end
|
102
108
|
|
103
109
|
protected
|
104
|
-
|
105
|
-
|
110
|
+
|
111
|
+
def observed_descendants
|
112
|
+
observed_classes.sum([]) { |klass| klass.descendants }
|
106
113
|
end
|
107
114
|
|
108
115
|
def observe_callbacks?
|
@@ -1,6 +1,8 @@
|
|
1
1
|
module ActiveRecord
|
2
|
+
# = Active Record Persistence
|
2
3
|
module Persistence
|
3
|
-
# Returns true if this object hasn't been saved yet -- that is, a record
|
4
|
+
# Returns true if this object hasn't been saved yet -- that is, a record
|
5
|
+
# for the object doesn't exist in the data store yet; otherwise, returns false.
|
4
6
|
def new_record?
|
5
7
|
@new_record
|
6
8
|
end
|
@@ -10,7 +12,8 @@ module ActiveRecord
|
|
10
12
|
@destroyed
|
11
13
|
end
|
12
14
|
|
13
|
-
# Returns if the record is persisted, i.e. it's not a new record and it was
|
15
|
+
# Returns if the record is persisted, i.e. it's not a new record and it was
|
16
|
+
# not destroyed.
|
14
17
|
def persisted?
|
15
18
|
!(new_record? || destroyed?)
|
16
19
|
end
|
@@ -69,8 +72,8 @@ module ActiveRecord
|
|
69
72
|
freeze
|
70
73
|
end
|
71
74
|
|
72
|
-
# Deletes the record in the database and freezes this instance to reflect
|
73
|
-
# be made (since they can't be persisted).
|
75
|
+
# Deletes the record in the database and freezes this instance to reflect
|
76
|
+
# that no changes should be made (since they can't be persisted).
|
74
77
|
def destroy
|
75
78
|
if persisted?
|
76
79
|
self.class.unscoped.where(self.class.arel_table[self.class.primary_key].eq(id)).delete_all
|
@@ -80,10 +83,13 @@ module ActiveRecord
|
|
80
83
|
freeze
|
81
84
|
end
|
82
85
|
|
83
|
-
# Returns an instance of the specified +klass+ with the attributes of the
|
84
|
-
#
|
85
|
-
#
|
86
|
-
#
|
86
|
+
# Returns an instance of the specified +klass+ with the attributes of the
|
87
|
+
# current record. This is mostly useful in relation to single-table
|
88
|
+
# inheritance structures where you want a subclass to appear as the
|
89
|
+
# superclass. This can be used along with record identification in
|
90
|
+
# Action Pack to allow, say, <tt>Client < Company</tt> to do something
|
91
|
+
# like render <tt>:partial => @client.becomes(Company)</tt> to render that
|
92
|
+
# instance using the companies/company partial instead of clients/client.
|
87
93
|
#
|
88
94
|
# Note: The new instance will share a link to the same attributes as the original class. So any change to the attributes in either
|
89
95
|
# instance will affect the other.
|
@@ -96,25 +102,52 @@ module ActiveRecord
|
|
96
102
|
became
|
97
103
|
end
|
98
104
|
|
99
|
-
# Updates a single attribute and saves the record
|
100
|
-
# This is especially useful for boolean flags on existing records.
|
101
|
-
#
|
105
|
+
# Updates a single attribute and saves the record.
|
106
|
+
# This is especially useful for boolean flags on existing records. Also note that
|
107
|
+
#
|
108
|
+
# * validation is skipped
|
109
|
+
# * No callbacks are invoked
|
110
|
+
# * updated_at/updated_on column is updated if that column is available
|
111
|
+
# * does not work on associations
|
112
|
+
# * does not work on attr_accessor attributes. The attribute that is being updated must be column name.
|
113
|
+
#
|
102
114
|
def update_attribute(name, value)
|
103
|
-
|
104
|
-
|
115
|
+
raise ActiveRecordError, "#{name.to_s} is marked as readonly" if self.class.readonly_attributes.include? name.to_s
|
116
|
+
|
117
|
+
changes = record_update_timestamps || {}
|
118
|
+
|
119
|
+
if name
|
120
|
+
name = name.to_s
|
121
|
+
send("#{name}=", value)
|
122
|
+
changes[name] = read_attribute(name)
|
123
|
+
end
|
124
|
+
|
125
|
+
@changed_attributes.except!(*changes.keys)
|
126
|
+
primary_key = self.class.primary_key
|
127
|
+
self.class.update_all(changes, { primary_key => self[primary_key] }) == 1
|
105
128
|
end
|
106
129
|
|
107
|
-
# Updates
|
108
|
-
#
|
130
|
+
# Updates the attributes of the model from the passed-in hash and saves the
|
131
|
+
# record, all wrapped in a transaction. If the object is invalid, the saving
|
132
|
+
# will fail and false will be returned.
|
109
133
|
def update_attributes(attributes)
|
110
|
-
|
111
|
-
|
134
|
+
# The following transaction covers any possible database side-effects of the
|
135
|
+
# attributes assignment. For example, setting the IDs of a child collection.
|
136
|
+
with_transaction_returning_status do
|
137
|
+
self.attributes = attributes
|
138
|
+
save
|
139
|
+
end
|
112
140
|
end
|
113
141
|
|
114
|
-
# Updates
|
142
|
+
# Updates its receiver just like +update_attributes+ but calls <tt>save!</tt> instead
|
143
|
+
# of +save+, so an exception is raised if the record is invalid.
|
115
144
|
def update_attributes!(attributes)
|
116
|
-
|
117
|
-
|
145
|
+
# The following transaction covers any possible database side-effects of the
|
146
|
+
# attributes assignment. For example, setting the IDs of a child collection.
|
147
|
+
with_transaction_returning_status do
|
148
|
+
self.attributes = attributes
|
149
|
+
save!
|
150
|
+
end
|
118
151
|
end
|
119
152
|
|
120
153
|
# Initializes +attribute+ to zero if +nil+ and adds the value passed as +by+ (default is 1).
|
@@ -175,7 +208,7 @@ module ActiveRecord
|
|
175
208
|
def reload(options = nil)
|
176
209
|
clear_aggregation_cache
|
177
210
|
clear_association_cache
|
178
|
-
@attributes.update(self.class.
|
211
|
+
@attributes.update(self.class.unscoped { self.class.find(self.id, options) }.instance_variable_get('@attributes'))
|
179
212
|
@attributes_cache = {}
|
180
213
|
self
|
181
214
|
end
|
@@ -227,4 +260,4 @@ module ActiveRecord
|
|
227
260
|
end
|
228
261
|
end
|
229
262
|
end
|
230
|
-
end
|
263
|
+
end
|
@@ -9,24 +9,24 @@ require "active_model/railtie"
|
|
9
9
|
require "action_controller/railtie"
|
10
10
|
|
11
11
|
module ActiveRecord
|
12
|
+
# = Active Record Railtie
|
12
13
|
class Railtie < Rails::Railtie
|
13
14
|
config.active_record = ActiveSupport::OrderedOptions.new
|
14
15
|
|
15
16
|
config.generators.orm :active_record, :migration => true,
|
16
17
|
:timestamps => true
|
17
18
|
|
18
|
-
config.app_middleware.insert_after "::ActionDispatch::Callbacks",
|
19
|
-
"ActiveRecord::QueryCache"
|
20
|
-
|
21
|
-
config.app_middleware.insert_after "::ActionDispatch::Callbacks",
|
22
|
-
"ActiveRecord::ConnectionAdapters::ConnectionManagement"
|
19
|
+
config.app_middleware.insert_after "::ActionDispatch::Callbacks", "ActiveRecord::QueryCache"
|
23
20
|
|
24
21
|
rake_tasks do
|
25
22
|
load "active_record/railties/databases.rake"
|
26
23
|
end
|
27
24
|
|
28
|
-
|
29
|
-
|
25
|
+
# When loading console, force ActiveRecord to be loaded to avoid cross
|
26
|
+
# references when loading a constant for the first time.
|
27
|
+
console do
|
28
|
+
ActiveRecord::Base
|
29
|
+
end
|
30
30
|
|
31
31
|
initializer "active_record.initialize_timezone" do
|
32
32
|
ActiveSupport.on_load(:active_record) do
|
@@ -68,13 +68,19 @@ module ActiveRecord
|
|
68
68
|
unless app.config.cache_classes
|
69
69
|
ActiveSupport.on_load(:active_record) do
|
70
70
|
ActionDispatch::Callbacks.after do
|
71
|
-
ActiveRecord::Base.reset_subclasses
|
72
71
|
ActiveRecord::Base.clear_reloadable_connections!
|
73
72
|
end
|
74
73
|
end
|
75
74
|
end
|
76
75
|
end
|
77
76
|
|
77
|
+
initializer "active_record.add_concurrency_middleware" do |app|
|
78
|
+
if app.config.allow_concurrency
|
79
|
+
app.config.middleware.insert_after "::ActionDispatch::Callbacks",
|
80
|
+
"ActiveRecord::ConnectionAdapters::ConnectionManagement"
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
78
84
|
config.after_initialize do
|
79
85
|
ActiveSupport.on_load(:active_record) do
|
80
86
|
instantiate_observers
|
@@ -11,9 +11,9 @@ module ActiveRecord
|
|
11
11
|
|
12
12
|
def cleanup_view_runtime
|
13
13
|
if ActiveRecord::Base.connected?
|
14
|
-
db_rt_before_render = ActiveRecord::
|
14
|
+
db_rt_before_render = ActiveRecord::LogSubscriber.reset_runtime
|
15
15
|
runtime = super
|
16
|
-
db_rt_after_render = ActiveRecord::
|
16
|
+
db_rt_after_render = ActiveRecord::LogSubscriber.reset_runtime
|
17
17
|
self.db_runtime = db_rt_before_render + db_rt_after_render
|
18
18
|
runtime - db_rt_after_render
|
19
19
|
else
|
@@ -1,11 +1,11 @@
|
|
1
1
|
namespace :db do
|
2
2
|
task :load_config => :rails_env do
|
3
3
|
require 'active_record'
|
4
|
-
ActiveRecord::Base.configurations = Rails
|
4
|
+
ActiveRecord::Base.configurations = Rails.application.config.database_configuration
|
5
5
|
end
|
6
6
|
|
7
7
|
namespace :create do
|
8
|
-
desc 'Create all the local databases defined in config/database.yml'
|
8
|
+
# desc 'Create all the local databases defined in config/database.yml'
|
9
9
|
task :all => :load_config do
|
10
10
|
ActiveRecord::Base.configurations.each_value do |config|
|
11
11
|
# Skip entries that don't have a database key, such as the first entry here:
|
@@ -26,7 +26,7 @@ namespace :db do
|
|
26
26
|
end
|
27
27
|
end
|
28
28
|
|
29
|
-
desc 'Create the database
|
29
|
+
desc 'Create the database from config/database.yml for the current Rails.env (use db:create:all to create all dbs in the config)'
|
30
30
|
task :create => :load_config do
|
31
31
|
# Make the test database at the same time as the development one, if it exists
|
32
32
|
if Rails.env.development? && ActiveRecord::Base.configurations['test']
|
@@ -57,7 +57,7 @@ namespace :db do
|
|
57
57
|
end
|
58
58
|
rescue
|
59
59
|
case config['adapter']
|
60
|
-
when
|
60
|
+
when /mysql/
|
61
61
|
@charset = ENV['CHARSET'] || 'utf8'
|
62
62
|
@collation = ENV['COLLATION'] || 'utf8_unicode_ci'
|
63
63
|
creation_options = {:charset => (config['charset'] || @charset), :collation => (config['collation'] || @collation)}
|
@@ -100,7 +100,7 @@ namespace :db do
|
|
100
100
|
end
|
101
101
|
|
102
102
|
namespace :drop do
|
103
|
-
desc 'Drops all the local databases defined in config/database.yml'
|
103
|
+
# desc 'Drops all the local databases defined in config/database.yml'
|
104
104
|
task :all => :load_config do
|
105
105
|
ActiveRecord::Base.configurations.each_value do |config|
|
106
106
|
# Skip entries that don't have a database key
|
@@ -115,7 +115,7 @@ namespace :db do
|
|
115
115
|
end
|
116
116
|
end
|
117
117
|
|
118
|
-
desc 'Drops the database for the current Rails.env'
|
118
|
+
desc 'Drops the database for the current Rails.env (use db:drop:all to drop all databases)'
|
119
119
|
task :drop => :load_config do
|
120
120
|
config = ActiveRecord::Base.configurations[Rails.env || 'development']
|
121
121
|
begin
|
@@ -134,7 +134,7 @@ namespace :db do
|
|
134
134
|
end
|
135
135
|
|
136
136
|
|
137
|
-
desc "Migrate the database
|
137
|
+
desc "Migrate the database (options: VERSION=x, VERBOSE=false)."
|
138
138
|
task :migrate => :environment do
|
139
139
|
ActiveRecord::Migration.verbose = ENV["VERBOSE"] ? ENV["VERBOSE"] == "true" : true
|
140
140
|
ActiveRecord::Migrator.migrate("db/migrate/", ENV["VERSION"] ? ENV["VERSION"].to_i : nil)
|
@@ -142,7 +142,7 @@ namespace :db do
|
|
142
142
|
end
|
143
143
|
|
144
144
|
namespace :migrate do
|
145
|
-
desc 'Rollbacks the database one migration and re migrate up
|
145
|
+
# desc 'Rollbacks the database one migration and re migrate up (options: STEP=x, VERSION=x).'
|
146
146
|
task :redo => :environment do
|
147
147
|
if ENV["VERSION"]
|
148
148
|
Rake::Task["db:migrate:down"].invoke
|
@@ -153,10 +153,10 @@ namespace :db do
|
|
153
153
|
end
|
154
154
|
end
|
155
155
|
|
156
|
-
desc 'Resets your database using your migrations for the current environment'
|
156
|
+
# desc 'Resets your database using your migrations for the current environment'
|
157
157
|
task :reset => ["db:drop", "db:create", "db:migrate"]
|
158
158
|
|
159
|
-
desc 'Runs the "up" for a given migration VERSION.'
|
159
|
+
# desc 'Runs the "up" for a given migration VERSION.'
|
160
160
|
task :up => :environment do
|
161
161
|
version = ENV["VERSION"] ? ENV["VERSION"].to_i : nil
|
162
162
|
raise "VERSION is required" unless version
|
@@ -164,37 +164,67 @@ namespace :db do
|
|
164
164
|
Rake::Task["db:schema:dump"].invoke if ActiveRecord::Base.schema_format == :ruby
|
165
165
|
end
|
166
166
|
|
167
|
-
desc 'Runs the "down" for a given migration VERSION.'
|
167
|
+
# desc 'Runs the "down" for a given migration VERSION.'
|
168
168
|
task :down => :environment do
|
169
169
|
version = ENV["VERSION"] ? ENV["VERSION"].to_i : nil
|
170
170
|
raise "VERSION is required" unless version
|
171
171
|
ActiveRecord::Migrator.run(:down, "db/migrate/", version)
|
172
172
|
Rake::Task["db:schema:dump"].invoke if ActiveRecord::Base.schema_format == :ruby
|
173
173
|
end
|
174
|
+
|
175
|
+
desc "Display status of migrations"
|
176
|
+
task :status => :environment do
|
177
|
+
config = ActiveRecord::Base.configurations[Rails.env || 'development']
|
178
|
+
ActiveRecord::Base.establish_connection(config)
|
179
|
+
unless ActiveRecord::Base.connection.table_exists?(ActiveRecord::Migrator.schema_migrations_table_name)
|
180
|
+
puts 'Schema migrations table does not exist yet.'
|
181
|
+
next # means "return" for rake task
|
182
|
+
end
|
183
|
+
db_list = ActiveRecord::Base.connection.select_values("SELECT version FROM #{ActiveRecord::Migrator.schema_migrations_table_name}")
|
184
|
+
file_list = []
|
185
|
+
Dir.foreach(File.join(Rails.root, 'db', 'migrate')) do |file|
|
186
|
+
# only files matching "20091231235959_some_name.rb" pattern
|
187
|
+
if match_data = /(\d{14})_(.+)\.rb/.match(file)
|
188
|
+
status = db_list.delete(match_data[1]) ? 'up' : 'down'
|
189
|
+
file_list << [status, match_data[1], match_data[2]]
|
190
|
+
end
|
191
|
+
end
|
192
|
+
# output
|
193
|
+
puts "\ndatabase: #{config['database']}\n\n"
|
194
|
+
puts "#{"Status".center(8)} #{"Migration ID".ljust(14)} Migration Name"
|
195
|
+
puts "-" * 50
|
196
|
+
file_list.each do |file|
|
197
|
+
puts "#{file[0].center(8)} #{file[1].ljust(14)} #{file[2].humanize}"
|
198
|
+
end
|
199
|
+
db_list.each do |version|
|
200
|
+
puts "#{'up'.center(8)} #{version.ljust(14)} *** NO FILE ***"
|
201
|
+
end
|
202
|
+
puts
|
203
|
+
end
|
174
204
|
end
|
175
205
|
|
176
|
-
desc 'Rolls the schema back to the previous version
|
206
|
+
desc 'Rolls the schema back to the previous version (specify steps w/ STEP=n).'
|
177
207
|
task :rollback => :environment do
|
178
208
|
step = ENV['STEP'] ? ENV['STEP'].to_i : 1
|
179
209
|
ActiveRecord::Migrator.rollback('db/migrate/', step)
|
180
210
|
Rake::Task["db:schema:dump"].invoke if ActiveRecord::Base.schema_format == :ruby
|
181
211
|
end
|
182
212
|
|
183
|
-
desc 'Pushes the schema to the next version
|
213
|
+
# desc 'Pushes the schema to the next version (specify steps w/ STEP=n).'
|
184
214
|
task :forward => :environment do
|
185
215
|
step = ENV['STEP'] ? ENV['STEP'].to_i : 1
|
186
216
|
ActiveRecord::Migrator.forward('db/migrate/', step)
|
187
217
|
Rake::Task["db:schema:dump"].invoke if ActiveRecord::Base.schema_format == :ruby
|
188
218
|
end
|
189
219
|
|
190
|
-
desc 'Drops and recreates the database from db/schema.rb for the current environment and loads the seeds.'
|
220
|
+
# desc 'Drops and recreates the database from db/schema.rb for the current environment and loads the seeds.'
|
191
221
|
task :reset => [ 'db:drop', 'db:setup' ]
|
192
222
|
|
193
|
-
desc "Retrieves the charset for the current environment's database"
|
223
|
+
# desc "Retrieves the charset for the current environment's database"
|
194
224
|
task :charset => :environment do
|
195
225
|
config = ActiveRecord::Base.configurations[Rails.env || 'development']
|
196
226
|
case config['adapter']
|
197
|
-
when
|
227
|
+
when /mysql/
|
198
228
|
ActiveRecord::Base.establish_connection(config)
|
199
229
|
puts ActiveRecord::Base.connection.charset
|
200
230
|
when 'postgresql'
|
@@ -208,11 +238,11 @@ namespace :db do
|
|
208
238
|
end
|
209
239
|
end
|
210
240
|
|
211
|
-
desc "Retrieves the collation for the current environment's database"
|
241
|
+
# desc "Retrieves the collation for the current environment's database"
|
212
242
|
task :collation => :environment do
|
213
243
|
config = ActiveRecord::Base.configurations[Rails.env || 'development']
|
214
244
|
case config['adapter']
|
215
|
-
when
|
245
|
+
when /mysql/
|
216
246
|
ActiveRecord::Base.establish_connection(config)
|
217
247
|
puts ActiveRecord::Base.connection.collation
|
218
248
|
else
|
@@ -225,7 +255,7 @@ namespace :db do
|
|
225
255
|
puts "Current version: #{ActiveRecord::Migrator.current_version}"
|
226
256
|
end
|
227
257
|
|
228
|
-
desc "Raises an error if there are pending migrations"
|
258
|
+
# desc "Raises an error if there are pending migrations"
|
229
259
|
task :abort_if_pending_migrations => :environment do
|
230
260
|
if defined? ActiveRecord
|
231
261
|
pending_migrations = ActiveRecord::Migrator.new(:up, 'db/migrate').pending_migrations
|
@@ -240,11 +270,11 @@ namespace :db do
|
|
240
270
|
end
|
241
271
|
end
|
242
272
|
|
243
|
-
desc 'Create the database, load the schema, and initialize with the seed data'
|
273
|
+
desc 'Create the database, load the schema, and initialize with the seed data (use db:reset to also drop the db first)'
|
244
274
|
task :setup => [ 'db:create', 'db:schema:load', 'db:seed' ]
|
245
275
|
|
246
276
|
desc 'Load the seed data from db/seeds.rb'
|
247
|
-
task :seed => :
|
277
|
+
task :seed => 'db:abort_if_pending_migrations' do
|
248
278
|
seed_file = File.join(Rails.root, 'db', 'seeds.rb')
|
249
279
|
load(seed_file) if File.exist?(seed_file)
|
250
280
|
end
|
@@ -263,7 +293,7 @@ namespace :db do
|
|
263
293
|
end
|
264
294
|
end
|
265
295
|
|
266
|
-
desc "Search for a fixture given a LABEL or ID. Specify an alternative path (eg. spec/fixtures) using FIXTURES_PATH=spec/fixtures."
|
296
|
+
# desc "Search for a fixture given a LABEL or ID. Specify an alternative path (eg. spec/fixtures) using FIXTURES_PATH=spec/fixtures."
|
267
297
|
task :identify => :environment do
|
268
298
|
require 'active_record/fixtures'
|
269
299
|
|
@@ -313,7 +343,7 @@ namespace :db do
|
|
313
343
|
task :dump => :environment do
|
314
344
|
abcs = ActiveRecord::Base.configurations
|
315
345
|
case abcs[Rails.env]["adapter"]
|
316
|
-
when
|
346
|
+
when /mysql/, "oci", "oracle"
|
317
347
|
ActiveRecord::Base.establish_connection(abcs[Rails.env])
|
318
348
|
File.open("#{Rails.root}/db/#{Rails.env}_structure.sql", "w+") { |f| f << ActiveRecord::Base.connection.structure_dump }
|
319
349
|
when "postgresql"
|
@@ -347,21 +377,21 @@ namespace :db do
|
|
347
377
|
end
|
348
378
|
|
349
379
|
namespace :test do
|
350
|
-
desc "Recreate the test database from the current schema.rb"
|
380
|
+
# desc "Recreate the test database from the current schema.rb"
|
351
381
|
task :load => 'db:test:purge' do
|
352
382
|
ActiveRecord::Base.establish_connection(ActiveRecord::Base.configurations['test'])
|
353
383
|
ActiveRecord::Schema.verbose = false
|
354
384
|
Rake::Task["db:schema:load"].invoke
|
355
385
|
end
|
356
386
|
|
357
|
-
desc "Recreate the test database from the current environment's database schema"
|
387
|
+
# desc "Recreate the test database from the current environment's database schema"
|
358
388
|
task :clone => %w(db:schema:dump db:test:load)
|
359
389
|
|
360
|
-
desc "Recreate the test databases from the development structure"
|
390
|
+
# desc "Recreate the test databases from the development structure"
|
361
391
|
task :clone_structure => [ "db:structure:dump", "db:test:purge" ] do
|
362
392
|
abcs = ActiveRecord::Base.configurations
|
363
393
|
case abcs["test"]["adapter"]
|
364
|
-
when
|
394
|
+
when /mysql/
|
365
395
|
ActiveRecord::Base.establish_connection(:test)
|
366
396
|
ActiveRecord::Base.connection.execute('SET foreign_key_checks = 0')
|
367
397
|
IO.readlines("#{Rails.root}/db/#{Rails.env}_structure.sql").join.split("\n\n").each do |table|
|
@@ -391,11 +421,11 @@ namespace :db do
|
|
391
421
|
end
|
392
422
|
end
|
393
423
|
|
394
|
-
desc "Empty the test database"
|
424
|
+
# desc "Empty the test database"
|
395
425
|
task :purge => :environment do
|
396
426
|
abcs = ActiveRecord::Base.configurations
|
397
427
|
case abcs["test"]["adapter"]
|
398
|
-
when
|
428
|
+
when /mysql/
|
399
429
|
ActiveRecord::Base.establish_connection(:test)
|
400
430
|
ActiveRecord::Base.connection.recreate_database(abcs["test"]["database"], abcs["test"])
|
401
431
|
when "postgresql"
|
@@ -422,7 +452,7 @@ namespace :db do
|
|
422
452
|
end
|
423
453
|
end
|
424
454
|
|
425
|
-
desc 'Check for pending migrations and load the test schema'
|
455
|
+
# desc 'Check for pending migrations and load the test schema'
|
426
456
|
task :prepare => 'db:abort_if_pending_migrations' do
|
427
457
|
if defined?(ActiveRecord) && !ActiveRecord::Base.configurations.blank?
|
428
458
|
Rake::Task[{ :sql => "db:test:clone_structure", :ruby => "db:test:load" }[ActiveRecord::Base.schema_format]].invoke
|
@@ -431,7 +461,7 @@ namespace :db do
|
|
431
461
|
end
|
432
462
|
|
433
463
|
namespace :sessions do
|
434
|
-
desc "Creates a sessions migration for use with ActiveRecord::SessionStore"
|
464
|
+
# desc "Creates a sessions migration for use with ActiveRecord::SessionStore"
|
435
465
|
task :create => :environment do
|
436
466
|
raise "Task unavailable to this database (no migration support)" unless ActiveRecord::Base.connection.supports_migrations?
|
437
467
|
require 'rails/generators'
|
@@ -440,7 +470,7 @@ namespace :db do
|
|
440
470
|
Rails::Generators::SessionMigrationGenerator.start [ ENV["MIGRATION"] || "add_sessions_table" ]
|
441
471
|
end
|
442
472
|
|
443
|
-
desc "Clear the sessions table"
|
473
|
+
# desc "Clear the sessions table"
|
444
474
|
task :clear => :environment do
|
445
475
|
ActiveRecord::Base.connection.execute "DELETE FROM #{session_table_name}"
|
446
476
|
end
|
@@ -451,7 +481,7 @@ task 'test:prepare' => 'db:test:prepare'
|
|
451
481
|
|
452
482
|
def drop_database(config)
|
453
483
|
case config['adapter']
|
454
|
-
when
|
484
|
+
when /mysql/
|
455
485
|
ActiveRecord::Base.establish_connection(config)
|
456
486
|
ActiveRecord::Base.connection.drop_database config['database']
|
457
487
|
when /^sqlite/
|