/
genesis.rake
88 lines (71 loc) · 2.69 KB
/
genesis.rake
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
namespace :db do
desc "Loads seed data for the current environment."
task :genesis => :environment do
Genesis::Seeder.verify_or_create_version_table
ignores = %w(genesis_common.rb)
seeds = Dir[File.join( Rails.root, 'db', 'seeds', '*.rb' )] +
Dir[File.join( Rails.root, 'db', 'seeds', Rails.env, '*.rb') ]
contexts = ENV['CONTEXTS']
unless contexts.nil? || contexts.empty?
using_contexts = true
contexts = expand_contexts if contexts == 'all'
contexts.split( ',' ).each do |context|
seeds += Dir[File.join( Rails.root, 'db', 'seeds', 'contexts', context, '*.rb' )]
end
end
puts "", message( contexts, :using_contexts => using_contexts, :start => true ), ""
Genesis::Seeder.run( seeds, ENV['VERSION'] || nil, ignores )
puts message( contexts, :using_contexts => using_contexts ), "", ""
end
desc "Drops and recreates all tables along with seeding the database"
task :mulligan => :environment do
Rake::Task['db:migrate:reset'].invoke
Rake::Task['db:genesis'].invoke
end
desc "An alias for the db:genesis task"
task :seed => :environment do
Rake::Task['db:genesis'].invoke
end
desc "An alias for the db:regenesis task"
task :reseed => :environment do
Rake::Task['db:regenesis'].invoke
end
desc "Removes all data, runs migrations and then seeds the database"
task :regenesis => :environment do
ActiveRecord::Base.connection.tables.select { |t| !['schema_migrations', 'schema_seeds', 'versions', 'sessions'].include?( t ) }.each do |table|
puts "Emptying the #{table} table"
klass = table.classify.to_s.constantize
klass.delete_all
end
puts ''
Genesis::SchemaSeed.delete_all
ActiveRecord::Base.connection.execute( 'DELETE FROM `versions`' )
ActiveRecord::Base.connection.execute( 'DELETE FROM `sessions`' )
Rake::Task['db:migrate'].invoke
Rake::Task['db:test:prepare'].invoke
Rake::Task['db:genesis'].invoke
end
namespace :genesis do
desc "Returns the current seed version from teh schema_seeds table"
task :version => :environment do
puts "[Genesis Seed Version] #{Genesis::Seeder.get_current_version}"
end
end
end
def seeds_root
File.join( Rails.root, 'db', 'seeds' )
end
def contexts_root
File.join( seeds_root, 'contexts' )
end
def expand_contexts
Dir[File.join( contexts_root, '*' )].map { |d| d.split( '/' ).last }.join ','
end
def message( contexts, options={} )
msg = options[:using_contexts] ?
"*** #{start_or_end_word( options )} seeding (contexts: #{contexts.split(',').join(', ')})" :
"*** #{start_or_end_word( options )} seeding"
end
def start_or_end_word( options )
return options[:start] ? 'Start' : 'End'
end