rails / rails

Ruby on Rails

This URL has Read+Write access

rails / railties / lib / tasks / databases.rake
f26141c9 » jeremy 2007-06-28 db:create creates the datab... 1 namespace :db do
a8fc494d » NZKoz 2008-12-01 Manually load the DB config... 2 task :load_config => :rails_env do
3 require 'active_record'
4 ActiveRecord::Base.configurations = Rails::Configuration.new.database_configuration
5 end
6
f26141c9 » jeremy 2007-06-28 db:create creates the datab... 7 namespace :create do
8 desc 'Create all the local databases defined in config/database.yml'
a8fc494d » NZKoz 2008-12-01 Manually load the DB config... 9 task :all => :load_config do
f26141c9 » jeremy 2007-06-28 db:create creates the datab... 10 ActiveRecord::Base.configurations.each_value do |config|
9264bdc8 » jeremy 2007-10-01 db:create works with remote... 11 # Skip entries that don't have a database key, such as the first entry here:
12 #
25eeea71 » jeremy 2008-03-31 PostgreSQL: use create_ and... 13 # defaults: &defaults
14 # adapter: mysql
9264bdc8 » jeremy 2007-10-01 db:create works with remote... 15 # username: root
25eeea71 » jeremy 2008-03-31 PostgreSQL: use create_ and... 16 # password:
9264bdc8 » jeremy 2007-10-01 db:create works with remote... 17 # host: localhost
25eeea71 » jeremy 2008-03-31 PostgreSQL: use create_ and... 18 #
19 # development:
9264bdc8 » jeremy 2007-10-01 db:create works with remote... 20 # database: blog_development
21 # <<: *defaults
22 next unless config['database']
23 # Only connect to local databases
64b4c18e » dhh 2008-01-02 Refactor check for local da... 24 local_database?(config) { create_database(config) }
f26141c9 » jeremy 2007-06-28 db:create creates the datab... 25 end
26 end
27 end
28
9264bdc8 » jeremy 2007-10-01 db:create works with remote... 29 desc 'Create the database defined in config/database.yml for the current RAILS_ENV'
a8fc494d » NZKoz 2008-12-01 Manually load the DB config... 30 task :create => :load_config do
9264bdc8 » jeremy 2007-10-01 db:create works with remote... 31 create_database(ActiveRecord::Base.configurations[RAILS_ENV])
f26141c9 » jeremy 2007-06-28 db:create creates the datab... 32 end
33
9264bdc8 » jeremy 2007-10-01 db:create works with remote... 34 def create_database(config)
35 begin
854cf3d7 » acangiano 2008-07-13 Fix for SQLite's db creatio... 36 if config['adapter'] =~ /sqlite/
37 if File.exist?(config['database'])
38 $stderr.puts "#{config['database']} already exists"
39 else
40 begin
41 # Create the SQLite database
42 ActiveRecord::Base.establish_connection(config)
43 ActiveRecord::Base.connection
44 rescue
45 $stderr.puts $!, *($!.backtrace)
46 $stderr.puts "Couldn't create database for #{config.inspect}"
47 end
48 end
49 return # Skip the else clause of begin/rescue
50 else
51 ActiveRecord::Base.establish_connection(config)
52 ActiveRecord::Base.connection
53 end
9264bdc8 » jeremy 2007-10-01 db:create works with remote... 54 rescue
55 case config['adapter']
3151d966 » nicksieger 2008-05-08 Revert "Change all database... 56 when 'mysql'
9264bdc8 » jeremy 2007-10-01 db:create works with remote... 57 @charset = ENV['CHARSET'] || 'utf8'
58 @collation = ENV['COLLATION'] || 'utf8_general_ci'
64756e8f » geekq 2008-11-24 better db:create for mysql ... 59 creation_options = {:charset => (config['charset'] || @charset), :collation => (config['collation'] || @collation)}
9264bdc8 » jeremy 2007-10-01 db:create works with remote... 60 begin
25eeea71 » jeremy 2008-03-31 PostgreSQL: use create_ and... 61 ActiveRecord::Base.establish_connection(config.merge('database' => nil))
64756e8f » geekq 2008-11-24 better db:create for mysql ... 62 ActiveRecord::Base.connection.create_database(config['database'], creation_options)
9264bdc8 » jeremy 2007-10-01 db:create works with remote... 63 ActiveRecord::Base.establish_connection(config)
64756e8f » geekq 2008-11-24 better db:create for mysql ... 64 rescue Mysql::Error => sqlerr
65 if sqlerr.errno == Mysql::Error::ER_ACCESS_DENIED_ERROR
66 print "#{sqlerr.error}. \nPlease provide the root password for your mysql installation\n>"
67 root_password = $stdin.gets.strip
68 grant_statement = "GRANT ALL PRIVILEGES ON #{config['database']}.* " \
69 "TO '#{config['username']}'@'localhost' " \
70 "IDENTIFIED BY '#{config['password']}' WITH GRANT OPTION;"
71 ActiveRecord::Base.establish_connection(config.merge(
72 'database' => nil, 'username' => 'root', 'password' => root_password))
73 ActiveRecord::Base.connection.create_database(config['database'], creation_options)
74 ActiveRecord::Base.connection.execute grant_statement
75 ActiveRecord::Base.establish_connection(config)
76 else
77 $stderr.puts sqlerr.error
78 $stderr.puts "Couldn't create database for #{config.inspect}, charset: #{config['charset'] || @charset}, collation: #{config['collation'] || @collation}"
79 $stderr.puts "(if you set the charset manually, make sure you have a matching collation)" if config['charset']
80 end
1f03c511 » jeremy 2007-05-25 Add db:create, drop, reset,... 81 end
3151d966 » nicksieger 2008-05-08 Revert "Change all database... 82 when 'postgresql'
25eeea71 » jeremy 2008-03-31 PostgreSQL: use create_ and... 83 @encoding = config[:encoding] || ENV['CHARSET'] || 'utf8'
84 begin
3fee2378 » tarmo 2008-05-13 Use 'public' schema path wh... 85 ActiveRecord::Base.establish_connection(config.merge('database' => 'postgres', 'schema_search_path' => 'public'))
c26d1056 » fragility 2008-05-03 PostgreSQL: update rake tas... Comment 86 ActiveRecord::Base.connection.create_database(config['database'], config.merge('encoding' => @encoding))
25eeea71 » jeremy 2008-03-31 PostgreSQL: use create_ and... 87 ActiveRecord::Base.establish_connection(config)
88 rescue
89 $stderr.puts $!, *($!.backtrace)
90 $stderr.puts "Couldn't create database for #{config.inspect}"
91 end
1f03c511 » jeremy 2007-05-25 Add db:create, drop, reset,... 92 end
f26141c9 » jeremy 2007-06-28 db:create creates the datab... 93 else
25eeea71 » jeremy 2008-03-31 PostgreSQL: use create_ and... 94 $stderr.puts "#{config['database']} already exists"
1f03c511 » jeremy 2007-05-25 Add db:create, drop, reset,... 95 end
96 end
f26141c9 » jeremy 2007-06-28 db:create creates the datab... 97
694f1575 » dhh 2007-09-15 Added db:drop:all to drop a... 98 namespace :drop do
99 desc 'Drops all the local databases defined in config/database.yml'
a8fc494d » NZKoz 2008-12-01 Manually load the DB config... 100 task :all => :load_config do
694f1575 » dhh 2007-09-15 Added db:drop:all to drop a... 101 ActiveRecord::Base.configurations.each_value do |config|
9264bdc8 » jeremy 2007-10-01 db:create works with remote... 102 # Skip entries that don't have a database key
103 next unless config['database']
104 # Only connect to local databases
64b4c18e » dhh 2008-01-02 Refactor check for local da... 105 local_database?(config) { drop_database(config) }
694f1575 » dhh 2007-09-15 Added db:drop:all to drop a... 106 end
1f03c511 » jeremy 2007-05-25 Add db:create, drop, reset,... 107 end
108 end
f26141c9 » jeremy 2007-06-28 db:create creates the datab... 109
694f1575 » dhh 2007-09-15 Added db:drop:all to drop a... 110 desc 'Drops the database for the current RAILS_ENV'
a8fc494d » NZKoz 2008-12-01 Manually load the DB config... 111 task :drop => :load_config do
3d2177df » lifo 2008-03-10 Fix database rake tasks to ... 112 config = ActiveRecord::Base.configurations[RAILS_ENV || 'development']
113 begin
114 drop_database(config)
115 rescue Exception => e
116 puts "Couldn't drop #{config['database']} : #{e.inspect}"
117 end
694f1575 » dhh 2007-09-15 Added db:drop:all to drop a... 118 end
119
64b4c18e » dhh 2008-01-02 Refactor check for local da... 120 def local_database?(config, &block)
121 if %w( 127.0.0.1 localhost ).include?(config['host']) || config['host'].blank?
122 yield
123 else
124 puts "This task only modifies local databases. #{config['database']} is on a remote host."
125 end
126 end
127
128
dbbae5e0 » lifo 2008-12-06 Merge with docrails 129 desc "Migrate the database through scripts in db/migrate and update db/schema.rb by invoking db:schema:dump. Target specific version with VERSION=x. Turn off output with VERBOSE=false."
985cb441 » dhh 2006-02-26 Added namespaces to all tas... 130 task :migrate => :environment do
9809dc45 » dhh 2007-09-22 Added VERBOSE option to rak... 131 ActiveRecord::Migration.verbose = ENV["VERBOSE"] ? ENV["VERBOSE"] == "true" : true
985cb441 » dhh 2006-02-26 Added namespaces to all tas... 132 ActiveRecord::Migrator.migrate("db/migrate/", ENV["VERSION"] ? ENV["VERSION"].to_i : nil)
133 Rake::Task["db:schema:dump"].invoke if ActiveRecord::Base.schema_format == :ruby
bc011df2 » dhh 2005-09-03 Moved all the shared tasks ... 134 end
135
9187ed86 » dhh 2007-12-14 Added db:migrate:redo for r... 136 namespace :migrate do
c47525a5 » phallstrom 2008-09-16 make db:migrate:redo rake t... Comment 137 desc 'Rollbacks the database one migration and re migrate up. If you want to rollback more than one step, define STEP=x. Target specific version with VERSION=x.'
138 task :redo => :environment do
139 if ENV["VERSION"]
140 Rake::Task["db:migrate:down"].invoke
141 Rake::Task["db:migrate:up"].invoke
142 else
143 Rake::Task["db:rollback"].invoke
144 Rake::Task["db:migrate"].invoke
145 end
146 end
3aa54c5e » dhh 2007-12-14 Added db:migrate:redo and d... 147
148 desc 'Resets your database using your migrations for the current environment'
149 task :reset => ["db:drop", "db:create", "db:migrate"]
25eeea71 » jeremy 2008-03-31 PostgreSQL: use create_ and... 150
c00de99f » dhh 2008-03-28 Switched to UTC-timebased v... 151 desc 'Runs the "up" for a given migration VERSION.'
152 task :up => :environment do
153 version = ENV["VERSION"] ? ENV["VERSION"].to_i : nil
154 raise "VERSION is required" unless version
155 ActiveRecord::Migrator.run(:up, "db/migrate/", version)
3704f4ba » dhh 2008-03-28 Fix new migration versions 156 Rake::Task["db:schema:dump"].invoke if ActiveRecord::Base.schema_format == :ruby
c00de99f » dhh 2008-03-28 Switched to UTC-timebased v... 157 end
158
159 desc 'Runs the "down" for a given migration VERSION.'
160 task :down => :environment do
161 version = ENV["VERSION"] ? ENV["VERSION"].to_i : nil
162 raise "VERSION is required" unless version
163 ActiveRecord::Migrator.run(:down, "db/migrate/", version)
3704f4ba » dhh 2008-03-28 Fix new migration versions 164 Rake::Task["db:schema:dump"].invoke if ActiveRecord::Base.schema_format == :ruby
25eeea71 » jeremy 2008-03-31 PostgreSQL: use create_ and... 165 end
9187ed86 » dhh 2007-12-14 Added db:migrate:redo for r... 166 end
167
2a74d71e » dhh 2007-10-26 Added db:rollback to rollba... 168 desc 'Rolls the schema back to the previous version. Specify the number of steps with STEP=n'
169 task :rollback => :environment do
170 step = ENV['STEP'] ? ENV['STEP'].to_i : 1
c00de99f » dhh 2008-03-28 Switched to UTC-timebased v... 171 ActiveRecord::Migrator.rollback('db/migrate/', step)
3704f4ba » dhh 2008-03-28 Fix new migration versions 172 Rake::Task["db:schema:dump"].invoke if ActiveRecord::Base.schema_format == :ruby
2a74d71e » dhh 2007-10-26 Added db:rollback to rollba... 173 end
174
4932f7b3 » dhh 2009-05-11 Added db/seeds.rb as a defa... Comment 175 desc 'Drops and recreates the database from db/schema.rb for the current environment and loads the seeds.'
176 task :reset => [ 'db:drop', 'db:setup' ]
f26141c9 » jeremy 2007-06-28 db:create creates the datab... 177
376f1210 » dhh 2007-06-23 Docfix (closes #8480) 178 desc "Retrieves the charset for the current environment's database"
1f03c511 » jeremy 2007-05-25 Add db:create, drop, reset,... 179 task :charset => :environment do
180 config = ActiveRecord::Base.configurations[RAILS_ENV || 'development']
181 case config['adapter']
182 when 'mysql'
183 ActiveRecord::Base.establish_connection(config)
184 puts ActiveRecord::Base.connection.charset
0176e6ad » sespindola 2008-07-07 Added db:charset support to... 185 when 'postgresql'
186 ActiveRecord::Base.establish_connection(config)
187 puts ActiveRecord::Base.connection.encoding
1f03c511 » jeremy 2007-05-25 Add db:create, drop, reset,... 188 else
189 puts 'sorry, your database adapter is not supported yet, feel free to submit a patch'
190 end
191 end
192
376f1210 » dhh 2007-06-23 Docfix (closes #8480) 193 desc "Retrieves the collation for the current environment's database"
1f03c511 » jeremy 2007-05-25 Add db:create, drop, reset,... 194 task :collation => :environment do
195 config = ActiveRecord::Base.configurations[RAILS_ENV || 'development']
196 case config['adapter']
197 when 'mysql'
198 ActiveRecord::Base.establish_connection(config)
199 puts ActiveRecord::Base.connection.collation
200 else
201 puts 'sorry, your database adapter is not supported yet, feel free to submit a patch'
202 end
203 end
7cc67eb6 » dhh 2007-06-25 Added db:version to get the... 204
205 desc "Retrieves the current schema version number"
206 task :version => :environment do
f26141c9 » jeremy 2007-06-28 db:create creates the datab... 207 puts "Current version: #{ActiveRecord::Migrator.current_version}"
7cc67eb6 » dhh 2007-06-25 Added db:version to get the... 208 end
209
699da700 » jeremy 2007-12-06 The test task stops with a ... 210 desc "Raises an error if there are pending migrations"
211 task :abort_if_pending_migrations => :environment do
2c951efe » jeremy 2007-12-08 Don't check for pending mig... 212 if defined? ActiveRecord
213 pending_migrations = ActiveRecord::Migrator.new(:up, 'db/migrate').pending_migrations
699da700 » jeremy 2007-12-06 The test task stops with a ... 214
2c951efe » jeremy 2007-12-08 Don't check for pending mig... 215 if pending_migrations.any?
216 puts "You have #{pending_migrations.size} pending migrations:"
217 pending_migrations.each do |pending_migration|
218 puts ' %4d %s' % [pending_migration.version, pending_migration.name]
219 end
98dc5827 » lifo 2008-05-25 Merge docrails. Comment 220 abort %{Run "rake db:migrate" to update your database then try again.}
699da700 » jeremy 2007-12-06 The test task stops with a ... 221 end
222 end
223 end
224
4932f7b3 » dhh 2009-05-11 Added db/seeds.rb as a defa... Comment 225 desc 'Create the database, load the schema, and initialize with the seed data'
226 task :setup => [ 'db:create', 'db:schema:load', 'db:seed' ]
227
228 desc 'Load the seed data from db/seeds.rb'
229 task :seed => :environment do
230 seed_file = File.join(Rails.root, 'db', 'seeds.rb')
231 load(seed_file) if File.exist?(seed_file)
232 end
233
ce458a74 » jeremy 2006-06-28 Don't assume Active Record ... 234 namespace :fixtures do
eb4668b2 » matthewrudy 2008-08-09 rake db:fixtures:load and d... 235 desc "Load fixtures into the current environment's database. Load specific fixtures using FIXTURES=x,y. Load from subdirectory in test/fixtures using FIXTURES_DIR=z. Specify an alternative path (eg. spec/fixtures) using FIXTURES_PATH=spec/fixtures."
ce458a74 » jeremy 2006-06-28 Don't assume Active Record ... 236 task :load => :environment do
237 require 'active_record/fixtures'
697ee1a5 » ncr 2008-07-09 Enable loading fixtures fro... 238 ActiveRecord::Base.establish_connection(Rails.env)
eb4668b2 » matthewrudy 2008-08-09 rake db:fixtures:load and d... 239 base_dir = ENV['FIXTURES_PATH'] ? File.join(Rails.root, ENV['FIXTURES_PATH']) : File.join(Rails.root, 'test', 'fixtures')
697ee1a5 » ncr 2008-07-09 Enable loading fixtures fro... 240 fixtures_dir = ENV['FIXTURES_DIR'] ? File.join(base_dir, ENV['FIXTURES_DIR']) : base_dir
241
242 (ENV['FIXTURES'] ? ENV['FIXTURES'].split(/,/).map {|f| File.join(fixtures_dir, f) } : Dir.glob(File.join(fixtures_dir, '*.{yml,csv}'))).each do |fixture_file|
243 Fixtures.create_fixtures(File.dirname(fixture_file), File.basename(fixture_file, '.*'))
ce458a74 » jeremy 2006-06-28 Don't assume Active Record ... 244 end
245 end
25eeea71 » jeremy 2008-03-31 PostgreSQL: use create_ and... 246
eb4668b2 » matthewrudy 2008-08-09 rake db:fixtures:load and d... 247 desc "Search for a fixture given a LABEL or ID. Specify an alternative path (eg. spec/fixtures) using FIXTURES_PATH=spec/fixtures."
e47392b8 » dhh 2007-12-01 Added db:fixtures:identity ... 248 task :identify => :environment do
249 require "active_record/fixtures"
250
251 label, id = ENV["LABEL"], ENV["ID"]
252 raise "LABEL or ID required" if label.blank? && id.blank?
25eeea71 » jeremy 2008-03-31 PostgreSQL: use create_ and... 253
e47392b8 » dhh 2007-12-01 Added db:fixtures:identity ... 254 puts %Q(The fixture ID for "#{label}" is #{Fixtures.identify(label)}.) if label
25eeea71 » jeremy 2008-03-31 PostgreSQL: use create_ and... 255
eb4668b2 » matthewrudy 2008-08-09 rake db:fixtures:load and d... 256 base_dir = ENV['FIXTURES_PATH'] ? File.join(Rails.root, ENV['FIXTURES_PATH']) : File.join(Rails.root, 'test', 'fixtures')
257 Dir["#{base_dir}/**/*.yml"].each do |file|
e47392b8 » dhh 2007-12-01 Added db:fixtures:identity ... 258 if data = YAML::load(ERB.new(IO.read(file)).result)
259 data.keys.each do |key|
260 key_id = Fixtures.identify(key)
25eeea71 » jeremy 2008-03-31 PostgreSQL: use create_ and... 261
e47392b8 » dhh 2007-12-01 Added db:fixtures:identity ... 262 if key == label || key_id == id.to_i
263 puts "#{file}: #{key} (#{key_id})"
264 end
265 end
266 end
267 end
268 end
ce458a74 » jeremy 2006-06-28 Don't assume Active Record ... 269 end
24c3599c » sstephenson 2005-10-12 Support using different dat... 270
985cb441 » dhh 2006-02-26 Added namespaces to all tas... 271 namespace :schema do
272 desc "Create a db/schema.rb file that can be portably used against any DB supported by AR"
273 task :dump => :environment do
274 require 'active_record/schema_dumper'
0d241f44 » Tapajós 2008-07-13 Use full path in database t... 275 File.open(ENV['SCHEMA'] || "#{RAILS_ROOT}/db/schema.rb", "w") do |file|
985cb441 » dhh 2006-02-26 Added namespaces to all tas... 276 ActiveRecord::SchemaDumper.dump(ActiveRecord::Base.connection, file)
277 end
ba146a84 » willbryant 2008-11-19 re-enable db:schema:dump so... 278 Rake::Task["db:schema:dump"].reenable
985cb441 » dhh 2006-02-26 Added namespaces to all tas... 279 end
c21fdf31 » dhh 2005-10-30 Changed :dbfile to :databas... 280
985cb441 » dhh 2006-02-26 Added namespaces to all tas... 281 desc "Load a schema.rb file into the database"
282 task :load => :environment do
0d241f44 » Tapajós 2008-07-13 Use full path in database t... 283 file = ENV['SCHEMA'] || "#{RAILS_ROOT}/db/schema.rb"
985cb441 » dhh 2006-02-26 Added namespaces to all tas... 284 load(file)
285 end
24c3599c » sstephenson 2005-10-12 Support using different dat... 286 end
287
985cb441 » dhh 2006-02-26 Added namespaces to all tas... 288 namespace :structure do
289 desc "Dump the database structure to a SQL file"
290 task :dump => :environment do
291 abcs = ActiveRecord::Base.configurations
3398f74d » jeremy 2006-07-05 db:test:clone should remove... 292 case abcs[RAILS_ENV]["adapter"]
3151d966 » nicksieger 2008-05-08 Revert "Change all database... 293 when "mysql", "oci", "oracle"
f26141c9 » jeremy 2007-06-28 db:create creates the datab... 294 ActiveRecord::Base.establish_connection(abcs[RAILS_ENV])
0d241f44 » Tapajós 2008-07-13 Use full path in database t... 295 File.open("#{RAILS_ROOT}/db/#{RAILS_ENV}_structure.sql", "w+") { |f| f << ActiveRecord::Base.connection.structure_dump }
f26141c9 » jeremy 2007-06-28 db:create creates the datab... 296 when "postgresql"
297 ENV['PGHOST'] = abcs[RAILS_ENV]["host"] if abcs[RAILS_ENV]["host"]
298 ENV['PGPORT'] = abcs[RAILS_ENV]["port"].to_s if abcs[RAILS_ENV]["port"]
299 ENV['PGPASSWORD'] = abcs[RAILS_ENV]["password"].to_s if abcs[RAILS_ENV]["password"]
300 search_path = abcs[RAILS_ENV]["schema_search_path"]
b3839f1c » zilkey 2009-05-21 Updated the db:structure:du... 301 unless search_path.blank?
302 search_path = search_path.split(",").map{|search_path| "--schema=#{search_path.strip}" }.join(" ")
303 end
f26141c9 » jeremy 2007-06-28 db:create creates the datab... 304 `pg_dump -i -U "#{abcs[RAILS_ENV]["username"]}" -s -x -O -f db/#{RAILS_ENV}_structure.sql #{search_path} #{abcs[RAILS_ENV]["database"]}`
305 raise "Error dumping database" if $?.exitstatus == 1
3151d966 » nicksieger 2008-05-08 Revert "Change all database... 306 when "sqlite", "sqlite3"
f26141c9 » jeremy 2007-06-28 db:create creates the datab... 307 dbfile = abcs[RAILS_ENV]["database"] || abcs[RAILS_ENV]["dbfile"]
308 `#{abcs[RAILS_ENV]["adapter"]} #{dbfile} .schema > db/#{RAILS_ENV}_structure.sql`
3151d966 » nicksieger 2008-05-08 Revert "Change all database... 309 when "sqlserver"
f26141c9 » jeremy 2007-06-28 db:create creates the datab... 310 `scptxfr /s #{abcs[RAILS_ENV]["host"]} /d #{abcs[RAILS_ENV]["database"]} /I /f db\\#{RAILS_ENV}_structure.sql /q /A /r`
311 `scptxfr /s #{abcs[RAILS_ENV]["host"]} /d #{abcs[RAILS_ENV]["database"]} /I /F db\ /q /A /r`
3151d966 » nicksieger 2008-05-08 Revert "Change all database... 312 when "firebird"
f26141c9 » jeremy 2007-06-28 db:create creates the datab... 313 set_firebird_env(abcs[RAILS_ENV])
314 db_string = firebird_db_string(abcs[RAILS_ENV])
0d241f44 » Tapajós 2008-07-13 Use full path in database t... 315 sh "isql -a #{db_string} > #{RAILS_ROOT}/db/#{RAILS_ENV}_structure.sql"
f26141c9 » jeremy 2007-06-28 db:create creates the datab... 316 else
317 raise "Task not supported by '#{abcs["test"]["adapter"]}'"
bc011df2 » dhh 2005-09-03 Moved all the shared tasks ... 318 end
319
985cb441 » dhh 2006-02-26 Added namespaces to all tas... 320 if ActiveRecord::Base.connection.supports_migrations?
0d241f44 » Tapajós 2008-07-13 Use full path in database t... 321 File.open("#{RAILS_ROOT}/db/#{RAILS_ENV}_structure.sql", "a") { |f| f << ActiveRecord::Base.connection.dump_schema_information }
bc011df2 » dhh 2005-09-03 Moved all the shared tasks ... 322 end
985cb441 » dhh 2006-02-26 Added namespaces to all tas... 323 end
bc011df2 » dhh 2005-09-03 Moved all the shared tasks ... 324 end
24c3599c » sstephenson 2005-10-12 Support using different dat... 325
985cb441 » dhh 2006-02-26 Added namespaces to all tas... 326 namespace :test do
6ffe3216 » jeremy 2008-06-17 Rely on quieter db:test:loa... 327 desc "Recreate the test database from the current schema.rb"
328 task :load => 'db:test:purge' do
985cb441 » dhh 2006-02-26 Added namespaces to all tas... 329 ActiveRecord::Base.establish_connection(ActiveRecord::Base.configurations['test'])
b8134000 » jamis 2006-03-05 Silence the migration messa... 330 ActiveRecord::Schema.verbose = false
abe5b157 » dhh 2006-02-26 Load, not dump, schema to test 331 Rake::Task["db:schema:load"].invoke
985cb441 » dhh 2006-02-26 Added namespaces to all tas... 332 end
56234bee » Marcel Molina 2005-12-18 Honor ActiveRecord::Base.pl... 333
6ffe3216 » jeremy 2008-06-17 Rely on quieter db:test:loa... 334 desc "Recreate the test database from the current environment's database schema"
335 task :clone => %w(db:schema:dump db:test:load)
3398f74d » jeremy 2006-07-05 db:test:clone should remove... 336
985cb441 » dhh 2006-02-26 Added namespaces to all tas... 337 desc "Recreate the test databases from the development structure"
338 task :clone_structure => [ "db:structure:dump", "db:test:purge" ] do
339 abcs = ActiveRecord::Base.configurations
340 case abcs["test"]["adapter"]
3151d966 » nicksieger 2008-05-08 Revert "Change all database... 341 when "mysql"
f26141c9 » jeremy 2007-06-28 db:create creates the datab... 342 ActiveRecord::Base.establish_connection(:test)
343 ActiveRecord::Base.connection.execute('SET foreign_key_checks = 0')
0d241f44 » Tapajós 2008-07-13 Use full path in database t... 344 IO.readlines("#{RAILS_ROOT}/db/#{RAILS_ENV}_structure.sql").join.split("\n\n").each do |table|
f26141c9 » jeremy 2007-06-28 db:create creates the datab... 345 ActiveRecord::Base.connection.execute(table)
346 end
3151d966 » nicksieger 2008-05-08 Revert "Change all database... 347 when "postgresql"
f26141c9 » jeremy 2007-06-28 db:create creates the datab... 348 ENV['PGHOST'] = abcs["test"]["host"] if abcs["test"]["host"]
349 ENV['PGPORT'] = abcs["test"]["port"].to_s if abcs["test"]["port"]
350 ENV['PGPASSWORD'] = abcs["test"]["password"].to_s if abcs["test"]["password"]
0d241f44 » Tapajós 2008-07-13 Use full path in database t... 351 `psql -U "#{abcs["test"]["username"]}" -f #{RAILS_ROOT}/db/#{RAILS_ENV}_structure.sql #{abcs["test"]["database"]}`
f26141c9 » jeremy 2007-06-28 db:create creates the datab... 352 when "sqlite", "sqlite3"
353 dbfile = abcs["test"]["database"] || abcs["test"]["dbfile"]
0d241f44 » Tapajós 2008-07-13 Use full path in database t... 354 `#{abcs["test"]["adapter"]} #{dbfile} < #{RAILS_ROOT}/db/#{RAILS_ENV}_structure.sql`
f26141c9 » jeremy 2007-06-28 db:create creates the datab... 355 when "sqlserver"
356 `osql -E -S #{abcs["test"]["host"]} -d #{abcs["test"]["database"]} -i db\\#{RAILS_ENV}_structure.sql`
3151d966 » nicksieger 2008-05-08 Revert "Change all database... 357 when "oci", "oracle"
f26141c9 » jeremy 2007-06-28 db:create creates the datab... 358 ActiveRecord::Base.establish_connection(:test)
0d241f44 » Tapajós 2008-07-13 Use full path in database t... 359 IO.readlines("#{RAILS_ROOT}/db/#{RAILS_ENV}_structure.sql").join.split(";\n\n").each do |ddl|
f26141c9 » jeremy 2007-06-28 db:create creates the datab... 360 ActiveRecord::Base.connection.execute(ddl)
361 end
3151d966 » nicksieger 2008-05-08 Revert "Change all database... 362 when "firebird"
f26141c9 » jeremy 2007-06-28 db:create creates the datab... 363 set_firebird_env(abcs["test"])
364 db_string = firebird_db_string(abcs["test"])
0d241f44 » Tapajós 2008-07-13 Use full path in database t... 365 sh "isql -i #{RAILS_ROOT}/db/#{RAILS_ENV}_structure.sql #{db_string}"
f26141c9 » jeremy 2007-06-28 db:create creates the datab... 366 else
367 raise "Task not supported by '#{abcs["test"]["adapter"]}'"
985cb441 » dhh 2006-02-26 Added namespaces to all tas... 368 end
369 end
24c3599c » sstephenson 2005-10-12 Support using different dat... 370
985cb441 » dhh 2006-02-26 Added namespaces to all tas... 371 desc "Empty the test database"
372 task :purge => :environment do
373 abcs = ActiveRecord::Base.configurations
374 case abcs["test"]["adapter"]
3151d966 » nicksieger 2008-05-08 Revert "Change all database... 375 when "mysql"
f26141c9 » jeremy 2007-06-28 db:create creates the datab... 376 ActiveRecord::Base.establish_connection(:test)
1bc267d2 » jodosha 2008-10-03 Make sure recreate MySQL te... 377 ActiveRecord::Base.connection.recreate_database(abcs["test"]["database"], abcs["test"])
f26141c9 » jeremy 2007-06-28 db:create creates the datab... 378 when "postgresql"
379 ActiveRecord::Base.clear_active_connections!
c26d1056 » fragility 2008-05-03 PostgreSQL: update rake tas... Comment 380 drop_database(abcs['test'])
381 create_database(abcs['test'])
f26141c9 » jeremy 2007-06-28 db:create creates the datab... 382 when "sqlite","sqlite3"
383 dbfile = abcs["test"]["database"] || abcs["test"]["dbfile"]
384 File.delete(dbfile) if File.exist?(dbfile)
3151d966 » nicksieger 2008-05-08 Revert "Change all database... 385 when "sqlserver"
f26141c9 » jeremy 2007-06-28 db:create creates the datab... 386 dropfkscript = "#{abcs["test"]["host"]}.#{abcs["test"]["database"]}.DP1".gsub(/\\/,'-')
387 `osql -E -S #{abcs["test"]["host"]} -d #{abcs["test"]["database"]} -i db\\#{dropfkscript}`
388 `osql -E -S #{abcs["test"]["host"]} -d #{abcs["test"]["database"]} -i db\\#{RAILS_ENV}_structure.sql`
3151d966 » nicksieger 2008-05-08 Revert "Change all database... 389 when "oci", "oracle"
f26141c9 » jeremy 2007-06-28 db:create creates the datab... 390 ActiveRecord::Base.establish_connection(:test)
391 ActiveRecord::Base.connection.structure_drop.split(";\n\n").each do |ddl|
392 ActiveRecord::Base.connection.execute(ddl)
393 end
3151d966 » nicksieger 2008-05-08 Revert "Change all database... 394 when "firebird"
f26141c9 » jeremy 2007-06-28 db:create creates the datab... 395 ActiveRecord::Base.establish_connection(:test)
396 ActiveRecord::Base.connection.recreate_database!
397 else
398 raise "Task not supported by '#{abcs["test"]["adapter"]}'"
985cb441 » dhh 2006-02-26 Added namespaces to all tas... 399 end
400 end
d25fe84a » dhh 2005-09-12 Added create_session_table,... 401
8e74a434 » jeremy 2008-06-16 Don't dump schema for every... 402 desc 'Check for pending migrations and load the test schema'
403 task :prepare => 'db:abort_if_pending_migrations' do
2c951efe » jeremy 2007-12-08 Don't check for pending mig... 404 if defined?(ActiveRecord) && !ActiveRecord::Base.configurations.blank?
6ffe3216 » jeremy 2008-06-17 Rely on quieter db:test:loa... 405 Rake::Task[{ :sql => "db:test:clone_structure", :ruby => "db:test:load" }[ActiveRecord::Base.schema_format]].invoke
ce458a74 » jeremy 2006-06-28 Don't assume Active Record ... 406 end
985cb441 » dhh 2006-02-26 Added namespaces to all tas... 407 end
d25fe84a » dhh 2005-09-12 Added create_session_table,... 408 end
409
985cb441 » dhh 2006-02-26 Added namespaces to all tas... 410 namespace :sessions do
ed708307 » josh 2008-12-15 Switch to Rack based sessio... Comment 411 desc "Creates a sessions migration for use with ActiveRecord::SessionStore"
985cb441 » dhh 2006-02-26 Added namespaces to all tas... 412 task :create => :environment do
413 raise "Task unavailable to this database (no migration support)" unless ActiveRecord::Base.connection.supports_migrations?
30c42b21 » technoweenie 2006-03-19 Replaced old session rake t... 414 require 'rails_generator'
415 require 'rails_generator/scripts/generate'
f0055871 » dhh 2007-09-23 Follow our own conventions ... 416 Rails::Generator::Scripts::Generate.new.run(["session_migration", ENV["MIGRATION"] || "CreateSessions"])
985cb441 » dhh 2006-02-26 Added namespaces to all tas... 417 end
418
30c42b21 » technoweenie 2006-03-19 Replaced old session rake t... 419 desc "Clear the sessions table"
420 task :clear => :environment do
e21c48ba » jeremy 2008-01-02 db:sessions:clear task uses... 421 ActiveRecord::Base.connection.execute "DELETE FROM #{session_table_name}"
985cb441 » dhh 2006-02-26 Added namespaces to all tas... 422 end
423 end
d25fe84a » dhh 2005-09-12 Added create_session_table,... 424 end
425
694f1575 » dhh 2007-09-15 Added db:drop:all to drop a... 426 def drop_database(config)
427 case config['adapter']
3151d966 » nicksieger 2008-05-08 Revert "Change all database... 428 when 'mysql'
a0bc480e » alk 2008-11-28 establish mysql connection ... 429 ActiveRecord::Base.establish_connection(config)
694f1575 » dhh 2007-09-15 Added db:drop:all to drop a... 430 ActiveRecord::Base.connection.drop_database config['database']
3151d966 » nicksieger 2008-05-08 Revert "Change all database... 431 when /^sqlite/
072b9d9f » jeremy 2008-01-02 SQLite: db:drop:all doesn't... 432 FileUtils.rm(File.join(RAILS_ROOT, config['database']))
694f1575 » dhh 2007-09-15 Added db:drop:all to drop a... 433 when 'postgresql'
3fee2378 » tarmo 2008-05-13 Use 'public' schema path wh... 434 ActiveRecord::Base.establish_connection(config.merge('database' => 'postgres', 'schema_search_path' => 'public'))
25eeea71 » jeremy 2008-03-31 PostgreSQL: use create_ and... 435 ActiveRecord::Base.connection.drop_database config['database']
694f1575 » dhh 2007-09-15 Added db:drop:all to drop a... 436 end
437 end
438
985cb441 » dhh 2006-02-26 Added namespaces to all tas... 439 def session_table_name
440 ActiveRecord::Base.pluralize_table_names ? :sessions : :session
119855c4 » jamis 2005-09-23 Add db_schema_dump and db_s... 441 end
e34de4dc » jeremy 2006-07-08 Firebird database tasks. 442
443 def set_firebird_env(config)
444 ENV["ISC_USER"] = config["username"].to_s if config["username"]
445 ENV["ISC_PASSWORD"] = config["password"].to_s if config["password"]
446 end
447
448 def firebird_db_string(config)
449 FireRuby::Database.db_string_for(config.symbolize_keys)
25eeea71 » jeremy 2008-03-31 PostgreSQL: use create_ and... 450 end