2007-06-28 14:23:41 -04:00
namespace :db do
2008-12-01 15:19:55 -05:00
task :load_config = > :rails_env do
require 'active_record'
ActiveRecord :: Base . configurations = Rails :: Configuration . new . database_configuration
end
2007-06-28 14:23:41 -04:00
namespace :create do
desc 'Create all the local databases defined in config/database.yml'
2008-12-01 15:19:55 -05:00
task :all = > :load_config do
2007-06-28 14:23:41 -04:00
ActiveRecord :: Base . configurations . each_value do | config |
2007-10-01 23:20:52 -04:00
# Skip entries that don't have a database key, such as the first entry here:
#
2008-04-01 01:01:33 -04:00
# defaults: &defaults
# adapter: mysql
2007-10-01 23:20:52 -04:00
# username: root
2008-04-01 01:01:33 -04:00
# password:
2007-10-01 23:20:52 -04:00
# host: localhost
2008-04-01 01:01:33 -04:00
#
# development:
2007-10-01 23:20:52 -04:00
# database: blog_development
# <<: *defaults
next unless config [ 'database' ]
# Only connect to local databases
2008-01-02 19:35:23 -05:00
local_database? ( config ) { create_database ( config ) }
2007-06-28 14:23:41 -04:00
end
end
end
2007-10-01 23:20:52 -04:00
desc 'Create the database defined in config/database.yml for the current RAILS_ENV'
2008-12-01 15:19:55 -05:00
task :create = > :load_config do
2007-10-01 23:20:52 -04:00
create_database ( ActiveRecord :: Base . configurations [ RAILS_ENV ] )
2007-06-28 14:23:41 -04:00
end
2007-10-01 23:20:52 -04:00
def create_database ( config )
begin
2008-07-13 11:49:05 -04:00
if config [ 'adapter' ] =~ / sqlite /
if File . exist? ( config [ 'database' ] )
$stderr . puts " #{ config [ 'database' ] } already exists "
else
begin
# Create the SQLite database
ActiveRecord :: Base . establish_connection ( config )
ActiveRecord :: Base . connection
rescue
$stderr . puts $! , * ( $! . backtrace )
$stderr . puts " Couldn't create database for #{ config . inspect } "
end
end
return # Skip the else clause of begin/rescue
else
ActiveRecord :: Base . establish_connection ( config )
ActiveRecord :: Base . connection
end
2007-10-01 23:20:52 -04:00
rescue
case config [ 'adapter' ]
2008-05-08 23:01:57 -04:00
when 'mysql'
2007-10-01 23:20:52 -04:00
@charset = ENV [ 'CHARSET' ] || 'utf8'
2009-08-28 02:07:43 -04:00
@collation = ENV [ 'COLLATION' ] || 'utf8_unicode_ci'
2008-11-24 08:17:39 -05:00
creation_options = { :charset = > ( config [ 'charset' ] || @charset ) , :collation = > ( config [ 'collation' ] || @collation ) }
2007-10-01 23:20:52 -04:00
begin
2008-04-01 01:01:33 -04:00
ActiveRecord :: Base . establish_connection ( config . merge ( 'database' = > nil ) )
2008-11-24 08:17:39 -05:00
ActiveRecord :: Base . connection . create_database ( config [ 'database' ] , creation_options )
2007-10-01 23:20:52 -04:00
ActiveRecord :: Base . establish_connection ( config )
2008-11-24 08:17:39 -05:00
rescue Mysql :: Error = > sqlerr
if sqlerr . errno == Mysql :: Error :: ER_ACCESS_DENIED_ERROR
print " #{ sqlerr . error } . \n Please provide the root password for your mysql installation \n > "
root_password = $stdin . gets . strip
grant_statement = " GRANT ALL PRIVILEGES ON #{ config [ 'database' ] } .* " \
" TO ' #{ config [ 'username' ] } '@'localhost' " \
" IDENTIFIED BY ' #{ config [ 'password' ] } ' WITH GRANT OPTION; "
ActiveRecord :: Base . establish_connection ( config . merge (
'database' = > nil , 'username' = > 'root' , 'password' = > root_password ) )
ActiveRecord :: Base . connection . create_database ( config [ 'database' ] , creation_options )
ActiveRecord :: Base . connection . execute grant_statement
ActiveRecord :: Base . establish_connection ( config )
else
$stderr . puts sqlerr . error
$stderr . puts " Couldn't create database for #{ config . inspect } , charset: #{ config [ 'charset' ] || @charset } , collation: #{ config [ 'collation' ] || @collation } "
$stderr . puts " (if you set the charset manually, make sure you have a matching collation) " if config [ 'charset' ]
end
2007-05-25 20:24:58 -04:00
end
2008-05-08 23:01:57 -04:00
when 'postgresql'
2008-04-01 01:01:33 -04:00
@encoding = config [ :encoding ] || ENV [ 'CHARSET' ] || 'utf8'
begin
2008-05-13 13:20:52 -04:00
ActiveRecord :: Base . establish_connection ( config . merge ( 'database' = > 'postgres' , 'schema_search_path' = > 'public' ) )
2008-05-03 23:41:10 -04:00
ActiveRecord :: Base . connection . create_database ( config [ 'database' ] , config . merge ( 'encoding' = > @encoding ) )
2008-04-01 01:01:33 -04:00
ActiveRecord :: Base . establish_connection ( config )
rescue
$stderr . puts $! , * ( $! . backtrace )
$stderr . puts " Couldn't create database for #{ config . inspect } "
end
2007-05-25 20:24:58 -04:00
end
2007-06-28 14:23:41 -04:00
else
2008-04-01 01:01:33 -04:00
$stderr . puts " #{ config [ 'database' ] } already exists "
2007-05-25 20:24:58 -04:00
end
end
2007-06-28 14:23:41 -04:00
2007-09-15 18:56:07 -04:00
namespace :drop do
desc 'Drops all the local databases defined in config/database.yml'
2008-12-01 15:19:55 -05:00
task :all = > :load_config do
2007-09-15 18:56:07 -04:00
ActiveRecord :: Base . configurations . each_value do | config |
2007-10-01 23:20:52 -04:00
# Skip entries that don't have a database key
next unless config [ 'database' ]
2009-08-08 12:53:47 -04:00
begin
# Only connect to local databases
local_database? ( config ) { drop_database ( config ) }
rescue Exception = > e
puts " Couldn't drop #{ config [ 'database' ] } : #{ e . inspect } "
end
2007-09-15 18:56:07 -04:00
end
2007-05-25 20:24:58 -04:00
end
end
2007-06-28 14:23:41 -04:00
2007-09-15 18:56:07 -04:00
desc 'Drops the database for the current RAILS_ENV'
2008-12-01 15:19:55 -05:00
task :drop = > :load_config do
2008-03-10 07:42:01 -04:00
config = ActiveRecord :: Base . configurations [ RAILS_ENV || 'development' ]
begin
drop_database ( config )
rescue Exception = > e
puts " Couldn't drop #{ config [ 'database' ] } : #{ e . inspect } "
end
2007-09-15 18:56:07 -04:00
end
2008-01-02 19:35:23 -05:00
def local_database? ( config , & block )
if %w( 127.0.0.1 localhost ) . include? ( config [ 'host' ] ) || config [ 'host' ] . blank?
yield
else
puts " This task only modifies local databases. #{ config [ 'database' ] } is on a remote host. "
end
end
2008-12-06 21:27:53 -05:00
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. "
2006-02-26 23:38:39 -05:00
task :migrate = > :environment do
2007-09-22 14:28:53 -04:00
ActiveRecord :: Migration . verbose = ENV [ " VERBOSE " ] ? ENV [ " VERBOSE " ] == " true " : true
2006-02-26 23:38:39 -05:00
ActiveRecord :: Migrator . migrate ( " db/migrate/ " , ENV [ " VERSION " ] ? ENV [ " VERSION " ] . to_i : nil )
Rake :: Task [ " db:schema:dump " ] . invoke if ActiveRecord :: Base . schema_format == :ruby
2005-09-03 11:13:06 -04:00
end
2007-12-14 12:47:56 -05:00
namespace :migrate do
2008-09-16 13:38:13 -04:00
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.'
task :redo = > :environment do
if ENV [ " VERSION " ]
Rake :: Task [ " db:migrate:down " ] . invoke
Rake :: Task [ " db:migrate:up " ] . invoke
else
Rake :: Task [ " db:rollback " ] . invoke
Rake :: Task [ " db:migrate " ] . invoke
end
end
2007-12-14 12:51:02 -05:00
desc 'Resets your database using your migrations for the current environment'
task :reset = > [ " db:drop " , " db:create " , " db:migrate " ]
2008-04-01 01:01:33 -04:00
2008-03-28 17:21:01 -04:00
desc 'Runs the "up" for a given migration VERSION.'
task :up = > :environment do
version = ENV [ " VERSION " ] ? ENV [ " VERSION " ] . to_i : nil
raise " VERSION is required " unless version
ActiveRecord :: Migrator . run ( :up , " db/migrate/ " , version )
2008-03-28 18:14:04 -04:00
Rake :: Task [ " db:schema:dump " ] . invoke if ActiveRecord :: Base . schema_format == :ruby
2008-03-28 17:21:01 -04:00
end
desc 'Runs the "down" for a given migration VERSION.'
task :down = > :environment do
version = ENV [ " VERSION " ] ? ENV [ " VERSION " ] . to_i : nil
raise " VERSION is required " unless version
ActiveRecord :: Migrator . run ( :down , " db/migrate/ " , version )
2008-03-28 18:14:04 -04:00
Rake :: Task [ " db:schema:dump " ] . invoke if ActiveRecord :: Base . schema_format == :ruby
2008-04-01 01:01:33 -04:00
end
2007-12-14 12:47:56 -05:00
end
2007-10-26 17:11:07 -04:00
desc 'Rolls the schema back to the previous version. Specify the number of steps with STEP=n'
task :rollback = > :environment do
step = ENV [ 'STEP' ] ? ENV [ 'STEP' ] . to_i : 1
2008-03-28 17:21:01 -04:00
ActiveRecord :: Migrator . rollback ( 'db/migrate/' , step )
2008-03-28 18:14:04 -04:00
Rake :: Task [ " db:schema:dump " ] . invoke if ActiveRecord :: Base . schema_format == :ruby
2009-08-08 12:39:31 -04:00
end
desc 'Pushes the schema to the next version. Specify the number of steps with STEP=n'
task :forward = > :environment do
step = ENV [ 'STEP' ] ? ENV [ 'STEP' ] . to_i : 1
ActiveRecord :: Migrator . forward ( 'db/migrate/' , step )
Rake :: Task [ " db:schema:dump " ] . invoke if ActiveRecord :: Base . schema_format == :ruby
2007-10-26 17:11:07 -04:00
end
2009-05-11 13:08:13 -04:00
desc 'Drops and recreates the database from db/schema.rb for the current environment and loads the seeds.'
task :reset = > [ 'db:drop' , 'db:setup' ]
2007-06-28 14:23:41 -04:00
2007-06-23 13:36:52 -04:00
desc " Retrieves the charset for the current environment's database "
2007-05-25 20:24:58 -04:00
task :charset = > :environment do
config = ActiveRecord :: Base . configurations [ RAILS_ENV || 'development' ]
case config [ 'adapter' ]
when 'mysql'
ActiveRecord :: Base . establish_connection ( config )
puts ActiveRecord :: Base . connection . charset
2008-07-08 00:14:11 -04:00
when 'postgresql'
ActiveRecord :: Base . establish_connection ( config )
puts ActiveRecord :: Base . connection . encoding
2007-05-25 20:24:58 -04:00
else
puts 'sorry, your database adapter is not supported yet, feel free to submit a patch'
end
end
2007-06-23 13:36:52 -04:00
desc " Retrieves the collation for the current environment's database "
2007-05-25 20:24:58 -04:00
task :collation = > :environment do
config = ActiveRecord :: Base . configurations [ RAILS_ENV || 'development' ]
case config [ 'adapter' ]
when 'mysql'
ActiveRecord :: Base . establish_connection ( config )
puts ActiveRecord :: Base . connection . collation
else
puts 'sorry, your database adapter is not supported yet, feel free to submit a patch'
end
end
2007-06-25 11:15:20 -04:00
desc " Retrieves the current schema version number "
task :version = > :environment do
2007-06-28 14:23:41 -04:00
puts " Current version: #{ ActiveRecord :: Migrator . current_version } "
2007-06-25 11:15:20 -04:00
end
2007-12-06 13:57:19 -05:00
desc " Raises an error if there are pending migrations "
task :abort_if_pending_migrations = > :environment do
2007-12-08 23:18:28 -05:00
if defined? ActiveRecord
pending_migrations = ActiveRecord :: Migrator . new ( :up , 'db/migrate' ) . pending_migrations
2007-12-06 13:57:19 -05:00
2007-12-08 23:18:28 -05:00
if pending_migrations . any?
puts " You have #{ pending_migrations . size } pending migrations: "
pending_migrations . each do | pending_migration |
puts ' %4d %s' % [ pending_migration . version , pending_migration . name ]
end
2008-05-25 07:29:00 -04:00
abort %{ Run "rake db:migrate" to update your database then try again. }
2007-12-06 13:57:19 -05:00
end
end
end
2009-05-11 13:08:13 -04:00
desc 'Create the database, load the schema, and initialize with the seed data'
task :setup = > [ 'db:create' , 'db:schema:load' , 'db:seed' ]
desc 'Load the seed data from db/seeds.rb'
task :seed = > :environment do
seed_file = File . join ( Rails . root , 'db' , 'seeds.rb' )
load ( seed_file ) if File . exist? ( seed_file )
end
2006-06-28 19:51:50 -04:00
namespace :fixtures do
2008-08-09 12:04:54 -04:00
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. "
2006-06-28 19:51:50 -04:00
task :load = > :environment do
require 'active_record/fixtures'
2008-07-09 15:34:04 -04:00
ActiveRecord :: Base . establish_connection ( Rails . env )
2008-08-09 12:04:54 -04:00
base_dir = ENV [ 'FIXTURES_PATH' ] ? File . join ( Rails . root , ENV [ 'FIXTURES_PATH' ] ) : File . join ( Rails . root , 'test' , 'fixtures' )
2008-07-09 15:34:04 -04:00
fixtures_dir = ENV [ 'FIXTURES_DIR' ] ? File . join ( base_dir , ENV [ 'FIXTURES_DIR' ] ) : base_dir
( ENV [ 'FIXTURES' ] ? ENV [ 'FIXTURES' ] . split ( / , / ) . map { | f | File . join ( fixtures_dir , f ) } : Dir . glob ( File . join ( fixtures_dir , '*.{yml,csv}' ) ) ) . each do | fixture_file |
Fixtures . create_fixtures ( File . dirname ( fixture_file ) , File . basename ( fixture_file , '.*' ) )
2006-06-28 19:51:50 -04:00
end
end
2008-04-01 01:01:33 -04:00
2008-08-09 12:04:54 -04:00
desc " Search for a fixture given a LABEL or ID. Specify an alternative path (eg. spec/fixtures) using FIXTURES_PATH=spec/fixtures. "
2007-12-01 15:53:15 -05:00
task :identify = > :environment do
require " active_record/fixtures "
label , id = ENV [ " LABEL " ] , ENV [ " ID " ]
raise " LABEL or ID required " if label . blank? && id . blank?
2008-04-01 01:01:33 -04:00
2007-12-01 15:53:15 -05:00
puts %Q( The fixture ID for " #{ label } " is #{ Fixtures . identify ( label ) } . ) if label
2008-04-01 01:01:33 -04:00
2008-08-09 12:04:54 -04:00
base_dir = ENV [ 'FIXTURES_PATH' ] ? File . join ( Rails . root , ENV [ 'FIXTURES_PATH' ] ) : File . join ( Rails . root , 'test' , 'fixtures' )
Dir [ " #{ base_dir } /**/*.yml " ] . each do | file |
2007-12-01 15:53:15 -05:00
if data = YAML :: load ( ERB . new ( IO . read ( file ) ) . result )
data . keys . each do | key |
key_id = Fixtures . identify ( key )
2008-04-01 01:01:33 -04:00
2007-12-01 15:53:15 -05:00
if key == label || key_id == id . to_i
puts " #{ file } : #{ key } ( #{ key_id } ) "
end
end
end
end
end
2006-06-28 19:51:50 -04:00
end
2005-10-13 00:12:32 -04:00
2006-02-26 23:38:39 -05:00
namespace :schema do
desc " Create a db/schema.rb file that can be portably used against any DB supported by AR "
task :dump = > :environment do
require 'active_record/schema_dumper'
2008-07-13 15:19:03 -04:00
File . open ( ENV [ 'SCHEMA' ] || " #{ RAILS_ROOT } /db/schema.rb " , " w " ) do | file |
2006-02-26 23:38:39 -05:00
ActiveRecord :: SchemaDumper . dump ( ActiveRecord :: Base . connection , file )
end
2008-11-19 04:44:54 -05:00
Rake :: Task [ " db:schema:dump " ] . reenable
2006-02-26 23:38:39 -05:00
end
2005-10-30 03:10:04 -05:00
2006-02-26 23:38:39 -05:00
desc " Load a schema.rb file into the database "
task :load = > :environment do
2008-07-13 15:19:03 -04:00
file = ENV [ 'SCHEMA' ] || " #{ RAILS_ROOT } /db/schema.rb "
2009-08-08 17:19:47 -04:00
if File . exists? ( file )
load ( file )
else
abort %{ #{ file } doesn't exist yet. Run "rake db:migrate" to create it then try again. If you do not intend to use a database, you should instead alter #{ RAILS_ROOT } /config/environment.rb to prevent active_record from loading: config.frameworks -= [ :active_record ] }
end
2006-02-26 23:38:39 -05:00
end
2005-10-13 00:12:32 -04:00
end
2006-02-26 23:38:39 -05:00
namespace :structure do
desc " Dump the database structure to a SQL file "
task :dump = > :environment do
abcs = ActiveRecord :: Base . configurations
2006-07-05 14:35:06 -04:00
case abcs [ RAILS_ENV ] [ " adapter " ]
2008-05-08 23:01:57 -04:00
when " mysql " , " oci " , " oracle "
2007-06-28 14:23:41 -04:00
ActiveRecord :: Base . establish_connection ( abcs [ RAILS_ENV ] )
2008-07-13 15:19:03 -04:00
File . open ( " #{ RAILS_ROOT } /db/ #{ RAILS_ENV } _structure.sql " , " w+ " ) { | f | f << ActiveRecord :: Base . connection . structure_dump }
2007-06-28 14:23:41 -04:00
when " postgresql "
ENV [ 'PGHOST' ] = abcs [ RAILS_ENV ] [ " host " ] if abcs [ RAILS_ENV ] [ " host " ]
ENV [ 'PGPORT' ] = abcs [ RAILS_ENV ] [ " port " ] . to_s if abcs [ RAILS_ENV ] [ " port " ]
ENV [ 'PGPASSWORD' ] = abcs [ RAILS_ENV ] [ " password " ] . to_s if abcs [ RAILS_ENV ] [ " password " ]
search_path = abcs [ RAILS_ENV ] [ " schema_search_path " ]
2009-05-22 00:32:32 -04:00
unless search_path . blank?
search_path = search_path . split ( " , " ) . map { | search_path | " --schema= #{ search_path . strip } " } . join ( " " )
end
2007-06-28 14:23:41 -04:00
` pg_dump -i -U " #{ abcs [ RAILS_ENV ] [ " username " ] } " -s -x -O -f db/ #{ RAILS_ENV } _structure.sql #{ search_path } #{ abcs [ RAILS_ENV ] [ " database " ] } `
raise " Error dumping database " if $? . exitstatus == 1
2008-05-08 23:01:57 -04:00
when " sqlite " , " sqlite3 "
2007-06-28 14:23:41 -04:00
dbfile = abcs [ RAILS_ENV ] [ " database " ] || abcs [ RAILS_ENV ] [ " dbfile " ]
` #{ abcs [ RAILS_ENV ] [ " adapter " ] } #{ dbfile } .schema > db/ #{ RAILS_ENV } _structure.sql `
2008-05-08 23:01:57 -04:00
when " sqlserver "
2007-06-28 14:23:41 -04:00
` scptxfr /s #{ abcs [ RAILS_ENV ] [ " host " ] } /d #{ abcs [ RAILS_ENV ] [ " database " ] } /I /f db \\ #{ RAILS_ENV } _structure.sql /q /A /r `
` scptxfr /s #{ abcs [ RAILS_ENV ] [ " host " ] } /d #{ abcs [ RAILS_ENV ] [ " database " ] } /I /F db \ /q /A /r `
2008-05-08 23:01:57 -04:00
when " firebird "
2007-06-28 14:23:41 -04:00
set_firebird_env ( abcs [ RAILS_ENV ] )
db_string = firebird_db_string ( abcs [ RAILS_ENV ] )
2008-07-13 15:19:03 -04:00
sh " isql -a #{ db_string } > #{ RAILS_ROOT } /db/ #{ RAILS_ENV } _structure.sql "
2007-06-28 14:23:41 -04:00
else
raise " Task not supported by ' #{ abcs [ " test " ] [ " adapter " ] } ' "
2005-09-03 11:13:06 -04:00
end
2006-02-26 23:38:39 -05:00
if ActiveRecord :: Base . connection . supports_migrations?
2008-07-13 15:19:03 -04:00
File . open ( " #{ RAILS_ROOT } /db/ #{ RAILS_ENV } _structure.sql " , " a " ) { | f | f << ActiveRecord :: Base . connection . dump_schema_information }
2005-09-03 11:13:06 -04:00
end
2006-02-26 23:38:39 -05:00
end
2005-09-03 11:13:06 -04:00
end
2005-10-13 00:12:32 -04:00
2006-02-26 23:38:39 -05:00
namespace :test do
2008-06-17 06:52:01 -04:00
desc " Recreate the test database from the current schema.rb "
task :load = > 'db:test:purge' do
2006-02-26 23:38:39 -05:00
ActiveRecord :: Base . establish_connection ( ActiveRecord :: Base . configurations [ 'test' ] )
2006-03-05 15:11:08 -05:00
ActiveRecord :: Schema . verbose = false
2006-02-27 01:03:50 -05:00
Rake :: Task [ " db:schema:load " ] . invoke
2006-02-26 23:38:39 -05:00
end
2005-12-18 15:20:49 -05:00
2008-06-17 06:52:01 -04:00
desc " Recreate the test database from the current environment's database schema "
task :clone = > %w( db:schema:dump db:test:load )
2006-07-05 14:35:06 -04:00
2006-02-26 23:38:39 -05:00
desc " Recreate the test databases from the development structure "
task :clone_structure = > [ " db:structure:dump " , " db:test:purge " ] do
abcs = ActiveRecord :: Base . configurations
case abcs [ " test " ] [ " adapter " ]
2008-05-08 23:01:57 -04:00
when " mysql "
2007-06-28 14:23:41 -04:00
ActiveRecord :: Base . establish_connection ( :test )
ActiveRecord :: Base . connection . execute ( 'SET foreign_key_checks = 0' )
2008-07-13 15:19:03 -04:00
IO . readlines ( " #{ RAILS_ROOT } /db/ #{ RAILS_ENV } _structure.sql " ) . join . split ( " \n \n " ) . each do | table |
2007-06-28 14:23:41 -04:00
ActiveRecord :: Base . connection . execute ( table )
end
2008-05-08 23:01:57 -04:00
when " postgresql "
2007-06-28 14:23:41 -04:00
ENV [ 'PGHOST' ] = abcs [ " test " ] [ " host " ] if abcs [ " test " ] [ " host " ]
ENV [ 'PGPORT' ] = abcs [ " test " ] [ " port " ] . to_s if abcs [ " test " ] [ " port " ]
ENV [ 'PGPASSWORD' ] = abcs [ " test " ] [ " password " ] . to_s if abcs [ " test " ] [ " password " ]
2008-07-13 15:19:03 -04:00
` psql -U " #{ abcs [ " test " ] [ " username " ] } " -f #{ RAILS_ROOT } /db/ #{ RAILS_ENV } _structure.sql #{ abcs [ " test " ] [ " database " ] } `
2007-06-28 14:23:41 -04:00
when " sqlite " , " sqlite3 "
dbfile = abcs [ " test " ] [ " database " ] || abcs [ " test " ] [ " dbfile " ]
2008-07-13 15:19:03 -04:00
` #{ abcs [ " test " ] [ " adapter " ] } #{ dbfile } < #{ RAILS_ROOT } /db/ #{ RAILS_ENV } _structure.sql `
2007-06-28 14:23:41 -04:00
when " sqlserver "
` osql -E -S #{ abcs [ " test " ] [ " host " ] } -d #{ abcs [ " test " ] [ " database " ] } -i db \\ #{ RAILS_ENV } _structure.sql `
2008-05-08 23:01:57 -04:00
when " oci " , " oracle "
2007-06-28 14:23:41 -04:00
ActiveRecord :: Base . establish_connection ( :test )
2008-07-13 15:19:03 -04:00
IO . readlines ( " #{ RAILS_ROOT } /db/ #{ RAILS_ENV } _structure.sql " ) . join . split ( " ; \n \n " ) . each do | ddl |
2007-06-28 14:23:41 -04:00
ActiveRecord :: Base . connection . execute ( ddl )
end
2008-05-08 23:01:57 -04:00
when " firebird "
2007-06-28 14:23:41 -04:00
set_firebird_env ( abcs [ " test " ] )
db_string = firebird_db_string ( abcs [ " test " ] )
2008-07-13 15:19:03 -04:00
sh " isql -i #{ RAILS_ROOT } /db/ #{ RAILS_ENV } _structure.sql #{ db_string } "
2007-06-28 14:23:41 -04:00
else
raise " Task not supported by ' #{ abcs [ " test " ] [ " adapter " ] } ' "
2006-02-26 23:38:39 -05:00
end
end
2005-10-13 00:12:32 -04:00
2006-02-26 23:38:39 -05:00
desc " Empty the test database "
task :purge = > :environment do
abcs = ActiveRecord :: Base . configurations
case abcs [ " test " ] [ " adapter " ]
2008-05-08 23:01:57 -04:00
when " mysql "
2007-06-28 14:23:41 -04:00
ActiveRecord :: Base . establish_connection ( :test )
2008-10-03 10:08:17 -04:00
ActiveRecord :: Base . connection . recreate_database ( abcs [ " test " ] [ " database " ] , abcs [ " test " ] )
2007-06-28 14:23:41 -04:00
when " postgresql "
ActiveRecord :: Base . clear_active_connections!
2008-05-03 23:41:10 -04:00
drop_database ( abcs [ 'test' ] )
create_database ( abcs [ 'test' ] )
2007-06-28 14:23:41 -04:00
when " sqlite " , " sqlite3 "
dbfile = abcs [ " test " ] [ " database " ] || abcs [ " test " ] [ " dbfile " ]
File . delete ( dbfile ) if File . exist? ( dbfile )
2008-05-08 23:01:57 -04:00
when " sqlserver "
2007-06-28 14:23:41 -04:00
dropfkscript = " #{ abcs [ " test " ] [ " host " ] } . #{ abcs [ " test " ] [ " database " ] } .DP1 " . gsub ( / \\ / , '-' )
` osql -E -S #{ abcs [ " test " ] [ " host " ] } -d #{ abcs [ " test " ] [ " database " ] } -i db \\ #{ dropfkscript } `
` osql -E -S #{ abcs [ " test " ] [ " host " ] } -d #{ abcs [ " test " ] [ " database " ] } -i db \\ #{ RAILS_ENV } _structure.sql `
2008-05-08 23:01:57 -04:00
when " oci " , " oracle "
2007-06-28 14:23:41 -04:00
ActiveRecord :: Base . establish_connection ( :test )
ActiveRecord :: Base . connection . structure_drop . split ( " ; \n \n " ) . each do | ddl |
ActiveRecord :: Base . connection . execute ( ddl )
end
2008-05-08 23:01:57 -04:00
when " firebird "
2007-06-28 14:23:41 -04:00
ActiveRecord :: Base . establish_connection ( :test )
ActiveRecord :: Base . connection . recreate_database!
else
raise " Task not supported by ' #{ abcs [ " test " ] [ " adapter " ] } ' "
2006-02-26 23:38:39 -05:00
end
end
2005-09-12 13:30:40 -04:00
2008-06-16 06:47:15 -04:00
desc 'Check for pending migrations and load the test schema'
task :prepare = > 'db:abort_if_pending_migrations' do
2007-12-08 23:18:28 -05:00
if defined? ( ActiveRecord ) && ! ActiveRecord :: Base . configurations . blank?
2008-06-17 06:52:01 -04:00
Rake :: Task [ { :sql = > " db:test:clone_structure " , :ruby = > " db:test:load " } [ ActiveRecord :: Base . schema_format ] ] . invoke
2006-06-28 19:51:50 -04:00
end
2006-02-26 23:38:39 -05:00
end
2005-09-12 13:30:40 -04:00
end
2006-02-26 23:38:39 -05:00
namespace :sessions do
2008-12-15 17:33:31 -05:00
desc " Creates a sessions migration for use with ActiveRecord::SessionStore "
2006-02-26 23:38:39 -05:00
task :create = > :environment do
raise " Task unavailable to this database (no migration support) " unless ActiveRecord :: Base . connection . supports_migrations?
2009-09-24 17:01:31 -04:00
require 'rails/generators'
require 'rails/generators/rails/session_migration/session_migration_generator'
2009-07-21 05:28:54 -04:00
Rails :: Generators :: SessionMigrationGenerator . start [ ENV [ " MIGRATION " ] || " add_sessions_table " ]
2006-02-26 23:38:39 -05:00
end
2006-03-20 00:45:22 -05:00
desc " Clear the sessions table "
task :clear = > :environment do
2008-01-02 04:03:56 -05:00
ActiveRecord :: Base . connection . execute " DELETE FROM #{ session_table_name } "
2006-02-26 23:38:39 -05:00
end
end
2005-09-12 13:30:40 -04:00
end
2007-09-15 18:56:07 -04:00
def drop_database ( config )
case config [ 'adapter' ]
2008-05-08 23:01:57 -04:00
when 'mysql'
2008-11-28 10:09:46 -05:00
ActiveRecord :: Base . establish_connection ( config )
2007-09-15 18:56:07 -04:00
ActiveRecord :: Base . connection . drop_database config [ 'database' ]
2008-05-08 23:01:57 -04:00
when / ^sqlite /
2009-08-08 20:53:50 -04:00
require 'pathname'
path = Pathname . new ( config [ 'database' ] )
file = path . absolute? ? path . to_s : File . join ( RAILS_ROOT , path )
FileUtils . rm ( file )
2007-09-15 18:56:07 -04:00
when 'postgresql'
2008-05-13 13:20:52 -04:00
ActiveRecord :: Base . establish_connection ( config . merge ( 'database' = > 'postgres' , 'schema_search_path' = > 'public' ) )
2008-04-01 01:01:33 -04:00
ActiveRecord :: Base . connection . drop_database config [ 'database' ]
2007-09-15 18:56:07 -04:00
end
end
2006-02-26 23:38:39 -05:00
def session_table_name
2009-08-08 17:06:45 -04:00
ActiveRecord :: SessionStore :: Session . table_name
2005-09-23 09:45:32 -04:00
end
2006-07-08 20:36:49 -04:00
def set_firebird_env ( config )
ENV [ " ISC_USER " ] = config [ " username " ] . to_s if config [ " username " ]
ENV [ " ISC_PASSWORD " ] = config [ " password " ] . to_s if config [ " password " ]
end
def firebird_db_string ( config )
FireRuby :: Database . db_string_for ( config . symbolize_keys )
2008-04-01 01:01:33 -04:00
end