2007-01-22 14:43:50 +01:00
namespace :db do
2009-02-04 21:26:08 +01:00
task :load_config = > :rails_env do
require 'active_record'
ActiveRecord :: Base . configurations = Rails :: Configuration . new . database_configuration
end
2007-12-21 08:48:59 +01:00
namespace :create do
desc 'Create all the local databases defined in config/database.yml'
2009-02-04 21:26:08 +01:00
task :all = > :load_config do
2007-12-21 08:48:59 +01:00
ActiveRecord :: Base . configurations . each_value do | config |
# Skip entries that don't have a database key, such as the first entry here:
#
2008-05-18 06:22:34 +02:00
# defaults: &defaults
# adapter: mysql
2007-12-21 08:48:59 +01:00
# username: root
2008-05-18 06:22:34 +02:00
# password:
2007-12-21 08:48:59 +01:00
# host: localhost
2008-05-18 06:22:34 +02:00
#
# development:
2007-12-21 08:48:59 +01:00
# database: blog_development
# <<: *defaults
next unless config [ 'database' ]
# Only connect to local databases
2008-05-18 06:22:34 +02:00
local_database? ( config ) { create_database ( config ) }
2007-12-21 08:48:59 +01:00
end
end
end
desc 'Create the database defined in config/database.yml for the current RAILS_ENV'
2009-02-04 21:26:08 +01:00
task :create = > :load_config do
2007-12-21 08:48:59 +01:00
create_database ( ActiveRecord :: Base . configurations [ RAILS_ENV ] )
end
def create_database ( config )
begin
2008-10-27 07:47:01 +01: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
2010-10-15 17:47:59 +02:00
return # Skip the else clause of begin/rescue
2008-10-27 07:47:01 +01:00
else
ActiveRecord :: Base . establish_connection ( config )
ActiveRecord :: Base . connection
end
2007-12-21 08:48:59 +01:00
rescue
case config [ 'adapter' ]
2010-10-15 17:47:59 +02:00
when / ^mysql /
2007-12-21 08:48:59 +01:00
@charset = ENV [ 'CHARSET' ] || 'utf8'
2009-09-05 09:01:46 +02:00
@collation = ENV [ 'COLLATION' ] || 'utf8_unicode_ci'
2007-12-21 08:48:59 +01:00
begin
2008-05-18 06:22:34 +02:00
ActiveRecord :: Base . establish_connection ( config . merge ( 'database' = > nil ) )
ActiveRecord :: Base . connection . create_database ( config [ 'database' ] , :charset = > ( config [ 'charset' ] || @charset ) , :collation = > ( config [ 'collation' ] || @collation ) )
2007-12-21 08:48:59 +01:00
ActiveRecord :: Base . establish_connection ( config )
rescue
2008-05-18 06:22:34 +02:00
$stderr . puts " Couldn't create database for #{ config . inspect } , charset: #{ config [ 'charset' ] || @charset } , collation: #{ config [ 'collation' ] || @collation } (if you set the charset manually, make sure you have a matching collation) "
2007-12-21 08:48:59 +01:00
end
when 'postgresql'
2008-05-18 06:22:34 +02:00
@encoding = config [ :encoding ] || ENV [ 'CHARSET' ] || 'utf8'
begin
2008-06-02 08:35:38 +02:00
ActiveRecord :: Base . establish_connection ( config . merge ( 'database' = > 'postgres' , 'schema_search_path' = > 'public' ) )
2008-05-18 06:22:34 +02:00
ActiveRecord :: Base . connection . create_database ( config [ 'database' ] , config . merge ( 'encoding' = > @encoding ) )
ActiveRecord :: Base . establish_connection ( config )
rescue
$stderr . puts $! , * ( $! . backtrace )
$stderr . puts " Couldn't create database for #{ config . inspect } "
end
2007-12-21 08:48:59 +01:00
end
else
2008-05-18 06:22:34 +02:00
$stderr . puts " #{ config [ 'database' ] } already exists "
2007-12-21 08:48:59 +01:00
end
end
namespace :drop do
desc 'Drops all the local databases defined in config/database.yml'
2009-02-04 21:26:08 +01:00
task :all = > :load_config do
2007-12-21 08:48:59 +01:00
ActiveRecord :: Base . configurations . each_value do | config |
# Skip entries that don't have a database key
next unless config [ 'database' ]
# Only connect to local databases
2008-05-18 06:22:34 +02:00
local_database? ( config ) { drop_database ( config ) }
2007-12-21 08:48:59 +01:00
end
end
end
desc 'Drops the database for the current RAILS_ENV'
2009-02-04 21:26:08 +01:00
task :drop = > :load_config do
2008-05-18 06:22:34 +02:00
config = ActiveRecord :: Base . configurations [ RAILS_ENV || 'development' ]
2010-05-25 19:45:45 +02:00
drop_database ( config )
2008-05-18 06:22:34 +02:00
end
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
2007-12-21 08:48:59 +01:00
end
2008-05-18 06:22:34 +02:00
2009-02-04 21:26:08 +01: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. "
2007-01-22 14:43:50 +01:00
task :migrate = > :environment do
2007-12-21 08:48:59 +01:00
ActiveRecord :: Migration . verbose = ENV [ " VERBOSE " ] ? ENV [ " VERBOSE " ] == " true " : true
2007-01-22 14:43:50 +01: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
end
2007-12-21 08:48:59 +01:00
namespace :migrate do
2008-10-27 07:47:01 +01: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-21 08:48:59 +01:00
desc 'Resets your database using your migrations for the current environment'
task :reset = > [ " db:drop " , " db:create " , " db:migrate " ]
2008-05-18 06:22:34 +02: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 )
Rake :: Task [ " db:schema:dump " ] . invoke if ActiveRecord :: Base . schema_format == :ruby
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 )
Rake :: Task [ " db:schema:dump " ] . invoke if ActiveRecord :: Base . schema_format == :ruby
end
2007-12-21 08:48:59 +01:00
end
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-05-18 06:22:34 +02:00
ActiveRecord :: Migrator . rollback ( 'db/migrate/' , step )
Rake :: Task [ " db:schema:dump " ] . invoke if ActiveRecord :: Base . schema_format == :ruby
2007-12-21 08:48:59 +01:00
end
2009-09-05 09:01:46 +02: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-12-21 08:48:59 +01:00
desc " Retrieves the charset for the current environment's database "
task :charset = > :environment do
config = ActiveRecord :: Base . configurations [ RAILS_ENV || 'development' ]
case config [ 'adapter' ]
2010-10-15 17:47:59 +02:00
when / ^mysql /
2007-12-21 08:48:59 +01:00
ActiveRecord :: Base . establish_connection ( config )
puts ActiveRecord :: Base . connection . charset
2008-10-27 07:47:01 +01:00
when 'postgresql'
ActiveRecord :: Base . establish_connection ( config )
puts ActiveRecord :: Base . connection . encoding
2007-12-21 08:48:59 +01:00
else
puts 'sorry, your database adapter is not supported yet, feel free to submit a patch'
end
end
desc " Retrieves the collation for the current environment's database "
task :collation = > :environment do
config = ActiveRecord :: Base . configurations [ RAILS_ENV || 'development' ]
case config [ 'adapter' ]
2010-10-15 17:47:59 +02:00
when / ^mysql /
2007-12-21 08:48:59 +01:00
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
desc " Retrieves the current schema version number "
task :version = > :environment do
puts " Current version: #{ ActiveRecord :: Migrator . current_version } "
end
desc " Raises an error if there are pending migrations "
task :abort_if_pending_migrations = > :environment do
if defined? ActiveRecord
pending_migrations = ActiveRecord :: Migrator . new ( :up , 'db/migrate' ) . pending_migrations
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-06-02 08:35:38 +02:00
abort %{ Run "rake db:migrate" to update your database then try again. }
2007-12-21 08:48:59 +01:00
end
end
end
2009-09-05 09:01:46 +02: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
2007-02-09 09:04:31 +01:00
namespace :fixtures do
2008-10-27 07:47:01 +01: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. "
2007-02-09 09:04:31 +01:00
task :load = > :environment do
require 'active_record/fixtures'
2008-10-27 07:47:01 +01:00
ActiveRecord :: Base . establish_connection ( Rails . env )
base_dir = ENV [ 'FIXTURES_PATH' ] ? File . join ( Rails . root , ENV [ 'FIXTURES_PATH' ] ) : File . join ( Rails . root , 'test' , 'fixtures' )
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 , '.*' ) )
2007-02-09 09:04:31 +01:00
end
end
2008-05-18 06:22:34 +02:00
2008-10-27 07:47:01 +01: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-21 08:48:59 +01: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-05-18 06:22:34 +02:00
2007-12-21 08:48:59 +01:00
puts %Q( The fixture ID for " #{ label } " is #{ Fixtures . identify ( label ) } . ) if label
2008-05-18 06:22:34 +02:00
2008-10-27 07:47:01 +01: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-21 08:48:59 +01:00
if data = YAML :: load ( ERB . new ( IO . read ( file ) ) . result )
data . keys . each do | key |
key_id = Fixtures . identify ( key )
2008-05-18 06:22:34 +02:00
2007-12-21 08:48:59 +01:00
if key == label || key_id == id . to_i
puts " #{ file } : #{ key } ( #{ key_id } ) "
end
end
end
end
end
2007-02-09 09:04:31 +01:00
end
2007-01-22 14:43:50 +01: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-09-07 07:54:05 +02:00
File . open ( ENV [ 'SCHEMA' ] || " #{ RAILS_ROOT } /db/schema.rb " , " w " ) do | file |
2007-01-22 14:43:50 +01:00
ActiveRecord :: SchemaDumper . dump ( ActiveRecord :: Base . connection , file )
end
2009-02-28 02:23:00 +01:00
Rake :: Task [ " db:schema:dump " ] . reenable
2007-01-22 14:43:50 +01:00
end
desc " Load a schema.rb file into the database "
task :load = > :environment do
2008-09-07 07:54:05 +02:00
file = ENV [ 'SCHEMA' ] || " #{ RAILS_ROOT } /db/schema.rb "
2009-09-05 09:01:46 +02: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
2007-01-22 14:43:50 +01:00
end
end
namespace :structure do
desc " Dump the database structure to a SQL file "
task :dump = > :environment do
abcs = ActiveRecord :: Base . configurations
2007-02-09 09:04:31 +01:00
case abcs [ RAILS_ENV ] [ " adapter " ]
2010-10-15 17:47:59 +02:00
when / ^mysql / , " oci " , " oracle "
2007-12-21 08:48:59 +01:00
ActiveRecord :: Base . establish_connection ( abcs [ RAILS_ENV ] )
2008-09-07 07:54:05 +02:00
File . open ( " #{ RAILS_ROOT } /db/ #{ RAILS_ENV } _structure.sql " , " w+ " ) { | f | f << ActiveRecord :: Base . connection . structure_dump }
2007-12-21 08:48:59 +01: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 " ]
search_path = " --schema= #{ search_path } " if search_path
` 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
when " sqlite " , " sqlite3 "
dbfile = abcs [ RAILS_ENV ] [ " database " ] || abcs [ RAILS_ENV ] [ " dbfile " ]
` #{ abcs [ RAILS_ENV ] [ " adapter " ] } #{ dbfile } .schema > db/ #{ RAILS_ENV } _structure.sql `
when " sqlserver "
` 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 `
when " firebird "
set_firebird_env ( abcs [ RAILS_ENV ] )
db_string = firebird_db_string ( abcs [ RAILS_ENV ] )
2008-09-07 07:54:05 +02:00
sh " isql -a #{ db_string } > #{ RAILS_ROOT } /db/ #{ RAILS_ENV } _structure.sql "
2007-12-21 08:48:59 +01:00
else
raise " Task not supported by ' #{ abcs [ " test " ] [ " adapter " ] } ' "
2007-01-22 14:43:50 +01:00
end
if ActiveRecord :: Base . connection . supports_migrations?
2008-09-07 07:54:05 +02:00
File . open ( " #{ RAILS_ROOT } /db/ #{ RAILS_ENV } _structure.sql " , " a " ) { | f | f << ActiveRecord :: Base . connection . dump_schema_information }
2007-01-22 14:43:50 +01:00
end
end
end
namespace :test do
2008-09-07 07:54:05 +02:00
desc " Recreate the test database from the current schema.rb "
task :load = > 'db:test:purge' do
2007-01-22 14:43:50 +01:00
ActiveRecord :: Base . establish_connection ( ActiveRecord :: Base . configurations [ 'test' ] )
ActiveRecord :: Schema . verbose = false
Rake :: Task [ " db:schema:load " ] . invoke
end
2008-09-07 07:54:05 +02:00
desc " Recreate the test database from the current environment's database schema "
task :clone = > %w( db:schema:dump db:test:load )
2007-02-09 09:04:31 +01:00
2007-01-22 14:43:50 +01: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 " ]
2010-10-15 17:47:59 +02:00
when / ^mysql /
2007-12-21 08:48:59 +01:00
ActiveRecord :: Base . establish_connection ( :test )
ActiveRecord :: Base . connection . execute ( 'SET foreign_key_checks = 0' )
2008-09-07 07:54:05 +02:00
IO . readlines ( " #{ RAILS_ROOT } /db/ #{ RAILS_ENV } _structure.sql " ) . join . split ( " \n \n " ) . each do | table |
2007-12-21 08:48:59 +01:00
ActiveRecord :: Base . connection . execute ( table )
end
when " postgresql "
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-09-07 07:54:05 +02:00
` psql -U " #{ abcs [ " test " ] [ " username " ] } " -f #{ RAILS_ROOT } /db/ #{ RAILS_ENV } _structure.sql #{ abcs [ " test " ] [ " database " ] } `
2007-12-21 08:48:59 +01:00
when " sqlite " , " sqlite3 "
dbfile = abcs [ " test " ] [ " database " ] || abcs [ " test " ] [ " dbfile " ]
2008-09-07 07:54:05 +02:00
` #{ abcs [ " test " ] [ " adapter " ] } #{ dbfile } < #{ RAILS_ROOT } /db/ #{ RAILS_ENV } _structure.sql `
2007-12-21 08:48:59 +01:00
when " sqlserver "
` osql -E -S #{ abcs [ " test " ] [ " host " ] } -d #{ abcs [ " test " ] [ " database " ] } -i db \\ #{ RAILS_ENV } _structure.sql `
when " oci " , " oracle "
ActiveRecord :: Base . establish_connection ( :test )
2008-09-07 07:54:05 +02:00
IO . readlines ( " #{ RAILS_ROOT } /db/ #{ RAILS_ENV } _structure.sql " ) . join . split ( " ; \n \n " ) . each do | ddl |
2007-12-21 08:48:59 +01:00
ActiveRecord :: Base . connection . execute ( ddl )
end
when " firebird "
set_firebird_env ( abcs [ " test " ] )
db_string = firebird_db_string ( abcs [ " test " ] )
2008-09-07 07:54:05 +02:00
sh " isql -i #{ RAILS_ROOT } /db/ #{ RAILS_ENV } _structure.sql #{ db_string } "
2007-12-21 08:48:59 +01:00
else
raise " Task not supported by ' #{ abcs [ " test " ] [ " adapter " ] } ' "
2007-01-22 14:43:50 +01:00
end
end
desc " Empty the test database "
task :purge = > :environment do
abcs = ActiveRecord :: Base . configurations
case abcs [ " test " ] [ " adapter " ]
2010-10-15 17:47:59 +02:00
when / ^mysql /
2007-12-21 08:48:59 +01:00
ActiveRecord :: Base . establish_connection ( :test )
2008-10-27 07:47:01 +01:00
ActiveRecord :: Base . connection . recreate_database ( abcs [ " test " ] [ " database " ] , abcs [ " test " ] )
2007-12-21 08:48:59 +01:00
when " postgresql "
ActiveRecord :: Base . clear_active_connections!
2008-05-18 06:22:34 +02:00
drop_database ( abcs [ 'test' ] )
create_database ( abcs [ 'test' ] )
2010-10-15 17:47:59 +02:00
when " sqlite " , " sqlite3 "
2007-12-21 08:48:59 +01:00
dbfile = abcs [ " test " ] [ " database " ] || abcs [ " test " ] [ " dbfile " ]
File . delete ( dbfile ) if File . exist? ( dbfile )
when " sqlserver "
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 `
when " oci " , " oracle "
ActiveRecord :: Base . establish_connection ( :test )
ActiveRecord :: Base . connection . structure_drop . split ( " ; \n \n " ) . each do | ddl |
ActiveRecord :: Base . connection . execute ( ddl )
end
when " firebird "
ActiveRecord :: Base . establish_connection ( :test )
ActiveRecord :: Base . connection . recreate_database!
else
raise " Task not supported by ' #{ abcs [ " test " ] [ " adapter " ] } ' "
2007-01-22 14:43:50 +01:00
end
end
2008-09-07 07:54:05 +02:00
desc 'Check for pending migrations and load the test schema'
task :prepare = > 'db:abort_if_pending_migrations' do
2007-12-21 08:48:59 +01:00
if defined? ( ActiveRecord ) && ! ActiveRecord :: Base . configurations . blank?
2008-09-07 07:54:05 +02:00
Rake :: Task [ { :sql = > " db:test:clone_structure " , :ruby = > " db:test:load " } [ ActiveRecord :: Base . schema_format ] ] . invoke
2007-02-09 09:04:31 +01:00
end
2007-01-22 14:43:50 +01:00
end
end
namespace :sessions do
2009-02-04 21:26:08 +01:00
desc " Creates a sessions migration for use with ActiveRecord::SessionStore "
2007-01-22 14:43:50 +01:00
task :create = > :environment do
raise " Task unavailable to this database (no migration support) " unless ActiveRecord :: Base . connection . supports_migrations?
require 'rails_generator'
require 'rails_generator/scripts/generate'
2007-12-21 08:48:59 +01:00
Rails :: Generator :: Scripts :: Generate . new . run ( [ " session_migration " , ENV [ " MIGRATION " ] || " CreateSessions " ] )
2007-01-22 14:43:50 +01:00
end
desc " Clear the sessions table "
task :clear = > :environment do
2008-05-18 06:22:34 +02:00
ActiveRecord :: Base . connection . execute " DELETE FROM #{ session_table_name } "
2007-01-22 14:43:50 +01:00
end
end
end
2007-12-21 08:48:59 +01:00
def drop_database ( config )
2010-05-25 19:45:45 +02:00
begin
case config [ 'adapter' ]
2010-10-15 17:47:59 +02:00
when / ^mysql /
2010-05-25 19:45:45 +02:00
ActiveRecord :: Base . establish_connection ( config )
ActiveRecord :: Base . connection . drop_database config [ 'database' ]
when / ^sqlite /
FileUtils . rm ( File . join ( RAILS_ROOT , config [ 'database' ] ) )
when 'postgresql'
ActiveRecord :: Base . establish_connection ( config . merge ( 'database' = > 'postgres' , 'schema_search_path' = > 'public' ) )
ActiveRecord :: Base . connection . drop_database config [ 'database' ]
end
rescue Exception = > e
puts " Couldn't drop #{ config [ 'database' ] } : #{ e . inspect } "
2007-12-21 08:48:59 +01:00
end
end
2007-01-22 14:43:50 +01:00
def session_table_name
ActiveRecord :: Base . pluralize_table_names ? :sessions : :session
end
2007-02-09 09:04:31 +01: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 )
end