kaplan 0.1.2 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -14,7 +14,7 @@ Doesn't Rails already provide a `db:reset` Rake task? Yes, but it runs `db:schem
14
14
 
15
15
  Also, Rails doesn't give you a way to programmatically reset (or at least seed) your database. So if you want to do it inside of a script, you're forced to `require 'rake'` and then say `Rake::Task.invoke["db:reset"]`. That's ridiculous. Kaplan gives you methods for plowing and seeding the database.
16
16
 
17
- Additionally, Kaplan improves Rails' seeding by 1) truncating the tables that will be seeded first, 2) allowing you to keep environment-specific seed files, and 2) allowing you to use YAML or text files since that's a simpler way of representing data (and you're probably used to seeing YAML for fixture data).
17
+ Additionally, Kaplan improves Rails' seeding by 1) truncating the tables that will be seeded first, 2) allowing you to keep environment-specific seed files, and 3) allowing you to use YAML or text files since that's a simpler way of representing data (and you're probably used to seeing YAML for fixture data).
18
18
 
19
19
  Finally, Kaplan was designed to be framework- and ORM-agnostic. Currently the following are supported:
20
20
 
@@ -76,6 +76,10 @@ Then, when I run `rake kaplan:db:seed` in different environments, this is which
76
76
  | production | people |
77
77
  | test | people, cars, car_types |
78
78
  +-------------+-------------------------+
79
+
80
+ Besides running seeds in a certain environment, you also have the option to run specific seed files, regardless of which environment they're stored under. So if for some reason we wanted to run the cars.rb seed in the development environment and the car_types.yml seed in the test environment, we could do that like this:
81
+
82
+ rake db:seed ONLY=seeds/development/cars.rb,seeds/test/car_types.yml
79
83
 
80
84
  ## Author/License
81
85
 
data/lib/kaplan.rb CHANGED
@@ -97,10 +97,20 @@ module Kaplan
97
97
  end
98
98
  end
99
99
 
100
- def seeds(env)
101
- (Dir["#{project_root}/seeds/*.{yml,rb,txt,csv}"] + Dir["#{project_root}/seeds/#{env}/*.{yml,rb,txt,csv}"]).
102
- reject {|filename| File.directory?(filename) }.
103
- map do |filename|
100
+ def seeds(env, options={})
101
+ if options[:only]
102
+ files = options[:only].
103
+ map {|path| Dir["#{project_root}/#{path}"] }.
104
+ flatten.
105
+ select {|file| File.file?(file) && file =~ /\.(yml|yaml|rb|txt|csv)/ }
106
+
107
+ if files.empty?
108
+ raise "Error in Kaplan.seeds: Couldn't find the seed files you specified."
109
+ end
110
+ else
111
+ files = Dir["#{project_root}/seeds/*.{yml,yaml,rb,txt,csv}"] + Dir["#{project_root}/seeds/#{env}/*.{yml,yaml,rb,txt,csv}"]
112
+ end
113
+ files.map do |filename|
104
114
  basename = ::File.basename(filename)
105
115
  basename =~ /^(.+?)\.([^.]+)$/
106
116
  collection_name = $1
@@ -117,7 +127,8 @@ module Kaplan
117
127
  options.reverse_merge!(:env => current_environment)
118
128
  puts "Seeding the #{options[:env]} database..." if level > 0
119
129
  establish_database(options[:env])
120
- seeds(options[:env]).each do |filename, ext, collection_name, model|
130
+ seeds = seeds(options[:env], :only => options[:only])
131
+ seeds.each do |filename, ext, collection_name, model|
121
132
  if ext == "rb"
122
133
  puts " - Adding data for #{collection_name}..." if level > 1
123
134
  records = eval(File.read(filename))
@@ -141,16 +152,16 @@ module Kaplan
141
152
  options.reverse_merge!(:env => current_environment)
142
153
  puts "Plowing the #{options[:env]} database..." if level > 0
143
154
  establish_database(options[:env])
144
- collections = options[:all] ? all_collections : seedable_collections(options[:env])
155
+ collections = options[:all] ? all_collections : seedable_collections(options[:env], :only => options[:only])
145
156
  collections.each do |coll|
146
157
  plow_collection(coll)
147
158
  puts " - Plowed #{coll}" if level > 1
148
159
  end
149
160
  end
150
161
 
151
- def seedable_collections(env)
162
+ def seedable_collections(env, options={})
152
163
  # Remove collections that don't exist
153
- seeds(env).map {|filename, ext, collection_name, model| collection_name } & all_collections
164
+ seeds(env, options).map {|filename, ext, collection_name, model| collection_name } & all_collections
154
165
  end
155
166
 
156
167
  private
@@ -4,35 +4,38 @@ namespace :kaplan do
4
4
  task :reset, [:env] => :environment do |t, args|
5
5
  env = args[:env] || ENV["RAILS_ENV"]
6
6
  raise "Can't reset the dev database" if env == "development"
7
- #raise "No environment specified. Pass the environment to the task as an argument to specify the environment." unless env
8
7
  puts "Resetting #{env} database by cloning dev schema and seeding db..."
9
8
  Rake::Task['kaplan:db:clone_structure'].invoke(env)
10
9
  Rake::Task['kaplan:db:seed'].invoke(env)
11
10
  end
12
11
 
13
12
  desc "Seeds a database of your choice (default: development) with bootstrap data.\nThe relevant tables are truncated first so you don't have to."
14
- task :seed, [:env] => :environment do |t, args|
13
+ task :seed, [:env, :only] => :environment do |t, args|
15
14
  env = args[:env] || ENV["RAILS_ENV"] || "development"
16
- #raise "No environment specified. Pass the environment to the task as an argument to specify the environment." unless env
17
- Rake::Task['kaplan:db:plow'].invoke(env)
18
- Kaplan.seed_database(:env => env)
15
+ if only = args[:only] || ENV["ONLY"]
16
+ only = only.split(",") unless Array === only
17
+ end
18
+ Rake::Task['kaplan:db:plow'].invoke(env, only)
19
+ Kaplan.seed_database(:env => env, :only => only)
19
20
  Kaplan.establish_database(env)
20
21
  Rake::Task['db:seed'].invoke
21
22
  end
22
23
 
23
24
  desc "Truncates tables in a database of your choice (default: development).\nBy default this just truncates the seed tables, if you want all of them pass ALL=true."
24
- task :plow, [:env] => :environment do |t, args|
25
+ task :plow, [:env, :only] => :environment do |t, args|
25
26
  env = args[:env] || ENV["RAILS_ENV"] || "development"
26
- #raise "No environment specified. Pass the environment to the task as an argument to specify the environment." unless env
27
- all = !!ENV["ALL"]
28
- Kaplan.plow_database(:env => env, :all => all)
27
+ unless all = !!ENV["ALL"]
28
+ if only = args[:only] || ENV["ONLY"]
29
+ only = only.split(",") unless Array === only
30
+ end
31
+ end
32
+ Kaplan.plow_database(:env => env, :only => only, :all => all)
29
33
  end
30
34
 
31
35
  desc "Dumps the structure of the development database to file and copies it to the database of your choice.\nAdapters must be the same."
32
36
  task :clone_structure, [:env] => :environment do |t, args|
33
37
  env = args[:env] || ENV["RAILS_ENV"]
34
38
  raise "Can't clone the dev database to the dev database" if env == "development"
35
- #raise "No environment specified. Pass the environment to the task as an argument to specify the environment." unless env
36
39
  puts "Cloning dev structure to #{env} database..."
37
40
 
38
41
  abcs = ActiveRecord::Base.configurations
@@ -74,7 +77,6 @@ namespace :kaplan do
74
77
  desc "Creates a database of your choice (default: development)"
75
78
  task :create, [:env] => :environment do |t, args|
76
79
  env = args[:env] || ENV["RAILS_ENV"] || "development"
77
- #raise "No environment specified. Pass the environment to the task as an argument to specify the environment." unless env
78
80
  puts "Creating #{env} database..."
79
81
  Kaplan.current_environment = env
80
82
  Kaplan.establish_database
@@ -84,7 +86,6 @@ namespace :kaplan do
84
86
  desc "Drops the database of your choice (default: development)"
85
87
  task :drop, [:env] => :environment do |t, args|
86
88
  env = args[:env] || ENV["RAILS_ENV"] || "development"
87
- #raise "No environment specified. Pass the environment to the task as an argument to specify the environment." unless env
88
89
  puts "Dropping #{env} database..."
89
90
  Kaplan.current_environment = env
90
91
  Kaplan.establish_database
@@ -1,3 +1,3 @@
1
1
  module Kaplan
2
- VERSION = "0.1.2"
2
+ VERSION = "0.2.0"
3
3
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: kaplan
3
3
  version: !ruby/object:Gem::Version
4
- hash: 31
4
+ hash: 23
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
- - 1
9
8
  - 2
10
- version: 0.1.2
9
+ - 0
10
+ version: 0.2.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Elliot Winkler
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-01-17 00:00:00 -07:00
18
+ date: 2011-01-19 00:00:00 -07:00
19
19
  default_executable:
20
20
  dependencies: []
21
21