ambitious_seeder 0.0.1 → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.md +63 -0
- data/VERSION +1 -1
- data/ambitious_seeder.gemspec +8 -4
- data/lib/ambitious_seeder.rb +108 -0
- data/lib/generators/rails/seed/seed_generator.rb +20 -0
- data/lib/generators/rails/seed/templates/template_seed.rb +14 -0
- data/lib/tasks/ambitious_seeder.rake +35 -0
- data/lib/tasks/support/rake_task_helpers.rb +24 -0
- metadata +9 -5
- data/README.rdoc +0 -17
data/README.md
ADDED
@@ -0,0 +1,63 @@
|
|
1
|
+
# AmbitiousSeeder
|
2
|
+
|
3
|
+
Very, very alpha version of gem to add seeds to rails app. At the moment this is very much something I can use - which I may make a bit more production friendly in the future.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
This gem only works with Rails 3. To install, add it to your gemfile:
|
8
|
+
|
9
|
+
gem 'ambitious_seeder'
|
10
|
+
|
11
|
+
## Seeding
|
12
|
+
|
13
|
+
To generate a seed:
|
14
|
+
|
15
|
+
rails generate seed [seed_name]
|
16
|
+
|
17
|
+
Say your seed_name was 'rooms', this will create a seed at:
|
18
|
+
|
19
|
+
[app-root]/db/seeds/rooms_seeds.rb
|
20
|
+
|
21
|
+
You can add any logic to this file that would work elsewhere in your rails app.
|
22
|
+
|
23
|
+
To run, go with:
|
24
|
+
|
25
|
+
rake db:seed:up
|
26
|
+
|
27
|
+
Once run, this seed cannot be run again - you'll need to reverse it and then run it another time :)
|
28
|
+
|
29
|
+
To reverse, try:
|
30
|
+
|
31
|
+
rake db:seed:down SEED=[seed_name]
|
32
|
+
|
33
|
+
To get a list of all available seeds:
|
34
|
+
|
35
|
+
rake db:seed:list
|
36
|
+
|
37
|
+
## Upcoming features
|
38
|
+
|
39
|
+
Stuff I guess I'll need, and notes:
|
40
|
+
|
41
|
+
* Provision in generator to stop duplicated seeds
|
42
|
+
* Probably need the ability to depend on a certain version of the schema - this can be added when the seed is created?
|
43
|
+
* Transactions?
|
44
|
+
* Why isn't query logging going to the normal rails logs?
|
45
|
+
* Add better information logging to STDOUT
|
46
|
+
|
47
|
+
# Note on Patches/Pull Requests
|
48
|
+
|
49
|
+
* Fork the project.
|
50
|
+
* Make your feature addition or bug fix.
|
51
|
+
* Add tests for it. This is important so I don't break it in a
|
52
|
+
future version unintentionally.
|
53
|
+
* Commit, do not mess with rakefile, version, or history.
|
54
|
+
(if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
|
55
|
+
* Send me a pull request. Bonus points for topic branches.
|
56
|
+
|
57
|
+
## Copyright
|
58
|
+
|
59
|
+
Copyright (c) 2011 Sam Phillips. See LICENSE for details.
|
60
|
+
|
61
|
+
## AmbitionIsGood (tm)
|
62
|
+
|
63
|
+
Well... it is!
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.0
|
1
|
+
0.1.0
|
data/ambitious_seeder.gemspec
CHANGED
@@ -5,25 +5,29 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{ambitious_seeder}
|
8
|
-
s.version = "0.0
|
8
|
+
s.version = "0.1.0"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Sam Phillips"]
|
12
|
-
s.date = %q{2011-
|
12
|
+
s.date = %q{2011-03-06}
|
13
13
|
s.description = %q{Create seeds for rails apps and load them in. Make it easier to manage data between your different environments.}
|
14
14
|
s.email = %q{sam@samdanavia.com}
|
15
15
|
s.extra_rdoc_files = [
|
16
16
|
"LICENSE",
|
17
|
-
"README.
|
17
|
+
"README.md"
|
18
18
|
]
|
19
19
|
s.files = [
|
20
20
|
".document",
|
21
21
|
"LICENSE",
|
22
|
-
"README.
|
22
|
+
"README.md",
|
23
23
|
"Rakefile",
|
24
24
|
"VERSION",
|
25
25
|
"ambitious_seeder.gemspec",
|
26
26
|
"lib/ambitious_seeder.rb",
|
27
|
+
"lib/generators/rails/seed/seed_generator.rb",
|
28
|
+
"lib/generators/rails/seed/templates/template_seed.rb",
|
29
|
+
"lib/tasks/ambitious_seeder.rake",
|
30
|
+
"lib/tasks/support/rake_task_helpers.rb",
|
27
31
|
"test/helper.rb",
|
28
32
|
"test/test_ambitious_seeder.rb"
|
29
33
|
]
|
data/lib/ambitious_seeder.rb
CHANGED
@@ -0,0 +1,108 @@
|
|
1
|
+
module AmbitiousSeeder
|
2
|
+
class Railtie < Rails::Railtie
|
3
|
+
rake_tasks do
|
4
|
+
load 'tasks/ambitious_seeder.rake'
|
5
|
+
end
|
6
|
+
end
|
7
|
+
|
8
|
+
class NoSeedUpError < Exception
|
9
|
+
end
|
10
|
+
|
11
|
+
class NoSeedDownError < Exception
|
12
|
+
end
|
13
|
+
|
14
|
+
module Seeder
|
15
|
+
def self.up(seeds)
|
16
|
+
self.initialize_schema_seeds_table
|
17
|
+
|
18
|
+
if seeds.kind_of?(Array)
|
19
|
+
seeds.each { |seed| self.up(seed) }
|
20
|
+
return
|
21
|
+
end
|
22
|
+
|
23
|
+
seed = seeds
|
24
|
+
|
25
|
+
return if seed_already_up?(seed)
|
26
|
+
|
27
|
+
begin
|
28
|
+
seed.send(:up)
|
29
|
+
rescue AmbitiousSeeder::NoSeedUpError
|
30
|
+
abort "No upwards method for #{seed}!"
|
31
|
+
end
|
32
|
+
|
33
|
+
record_seed(seed)
|
34
|
+
end
|
35
|
+
|
36
|
+
def self.down(seed)
|
37
|
+
self.initialize_schema_seeds_table
|
38
|
+
|
39
|
+
return unless seed_already_up?(seed)
|
40
|
+
|
41
|
+
begin
|
42
|
+
seed.send(:down)
|
43
|
+
rescue AmbitiousSeeder::NoSeedDownError
|
44
|
+
abort "No downwards method for #{seed}!"
|
45
|
+
end
|
46
|
+
|
47
|
+
expunge_seed(seed)
|
48
|
+
end
|
49
|
+
|
50
|
+
private
|
51
|
+
def self.db
|
52
|
+
@db ||= setup_db
|
53
|
+
end
|
54
|
+
|
55
|
+
def self.setup_db
|
56
|
+
ActiveRecord::Base.configurations = Rails.application.config.database_configuration
|
57
|
+
ActiveRecord::Base.establish_connection(ActiveRecord::Base.configurations[Rails.env])
|
58
|
+
ActiveRecord::Base.connection
|
59
|
+
end
|
60
|
+
|
61
|
+
def self.seeds_table_name
|
62
|
+
'schema_seeds'
|
63
|
+
end
|
64
|
+
|
65
|
+
def self.seeds_table
|
66
|
+
Arel::Table.new(seeds_table_name)
|
67
|
+
end
|
68
|
+
|
69
|
+
def self.initialize_schema_seeds_table
|
70
|
+
seeds_table = seeds_table_name
|
71
|
+
unless db.table_exists?(seeds_table)
|
72
|
+
db.create_table(seeds_table, :id => false) do |table|
|
73
|
+
table.column :seed, :string, :null => false
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
def self.seed_already_up?(seed)
|
79
|
+
self.completed_seeds.include? seed.seed_name
|
80
|
+
end
|
81
|
+
|
82
|
+
def self.record_seed(seed)
|
83
|
+
seeds_table.insert seeds_table["seed"] => seed.seed_name
|
84
|
+
end
|
85
|
+
|
86
|
+
def self.expunge_seed(seed)
|
87
|
+
seeds_table.where(seeds_table["seed"].eq(seed.seed_name)).delete
|
88
|
+
end
|
89
|
+
|
90
|
+
def self.completed_seeds
|
91
|
+
db.select_values(seeds_table.project(seeds_table['seed']).to_sql)
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
class Seed
|
96
|
+
def self.seed_name
|
97
|
+
self.to_s.gsub(/seed$/i, '').underscore
|
98
|
+
end
|
99
|
+
|
100
|
+
def self.up
|
101
|
+
raise AmbitiousSeeder::NoSeedUpError
|
102
|
+
end
|
103
|
+
|
104
|
+
def self.down
|
105
|
+
raise AmbitiousSeeder::NoSeedDownError
|
106
|
+
end
|
107
|
+
end
|
108
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
module Rails
|
2
|
+
module Generators
|
3
|
+
class SeedGenerator < Rails::Generators::NamedBase
|
4
|
+
desc <<DESC
|
5
|
+
Description:
|
6
|
+
Create a seed file
|
7
|
+
|
8
|
+
DESC
|
9
|
+
|
10
|
+
def self.source_root
|
11
|
+
@source_root ||= File.expand_path(File.join(File.dirname(__FILE__), 'templates'))
|
12
|
+
end
|
13
|
+
|
14
|
+
def create_seed
|
15
|
+
empty_directory 'db/seeds'
|
16
|
+
template "template_seed.rb", "db/seeds/#{file_name}.rb"
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
class <%= file_name.camelize %>Seed < AmbitiousSeeder::Seed
|
2
|
+
def self.up
|
3
|
+
# Interact with classes as normal here, eg:
|
4
|
+
# Product.create(:sku => '1234')
|
5
|
+
end
|
6
|
+
|
7
|
+
def self.down
|
8
|
+
# If possible, reverse the migration.
|
9
|
+
# Product.delete(Product.where(:sku => '1234'))
|
10
|
+
|
11
|
+
# You may also want to consider any associations, and whether they should deleted automatically, or manually in this method.
|
12
|
+
# If it's not possible (or not a good idea) to reverse this migration, remove the method :)
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
load 'tasks/support/rake_task_helpers.rb'
|
2
|
+
|
3
|
+
namespace :db do
|
4
|
+
include RakeTaskHelpers
|
5
|
+
|
6
|
+
namespace :seed do
|
7
|
+
desc "List all available seeds"
|
8
|
+
task :list do
|
9
|
+
puts seeds.inspect
|
10
|
+
end
|
11
|
+
|
12
|
+
desc "Up all seeds, or a seed specified in the SEED environment variable"
|
13
|
+
task :up => :environment do
|
14
|
+
if ENV["SEED"]
|
15
|
+
seed_name = ENV["SEED"]
|
16
|
+
verify_seed(seed_name)
|
17
|
+
|
18
|
+
seed_klass = seeds[seed_name]
|
19
|
+
AmbitiousSeeder::Seeder.up(seed_klass)
|
20
|
+
else
|
21
|
+
AmbitiousSeeder::Seeder.up(seeds.values)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
desc "Down the seed specific in the SEED environment variable"
|
26
|
+
task :down => :environment do
|
27
|
+
seed = ENV["SEED"]
|
28
|
+
abort "Specify the seed to down: 'rake db:seed:down SEED=seed_name'" unless seed
|
29
|
+
verify_seed(seed)
|
30
|
+
|
31
|
+
seed_klass = seeds[seed]
|
32
|
+
AmbitiousSeeder::Seeder.down(seed_klass)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
module RakeTaskHelpers
|
2
|
+
def seeds
|
3
|
+
@seeds ||= get_seeds
|
4
|
+
end
|
5
|
+
|
6
|
+
def get_seeds
|
7
|
+
Dir[File.join(Rails.root, 'db','seeds', '*')].reduce({}) do |seeds, seed|
|
8
|
+
load(seed)
|
9
|
+
|
10
|
+
seed_klass = seed_klass_from_path(seed)
|
11
|
+
seeds[seed_klass.seed_name] = seed_klass
|
12
|
+
seeds
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
def verify_seed(seed)
|
17
|
+
abort "Unknown seed #{seed}" unless seeds[seed]
|
18
|
+
end
|
19
|
+
|
20
|
+
def seed_klass_from_path(path)
|
21
|
+
puts path
|
22
|
+
(path.split("/").last.gsub(/\.rb/,'') + 'Seed').classify.constantize
|
23
|
+
end
|
24
|
+
end
|
metadata
CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
|
|
4
4
|
prerelease: false
|
5
5
|
segments:
|
6
6
|
- 0
|
7
|
-
- 0
|
8
7
|
- 1
|
9
|
-
|
8
|
+
- 0
|
9
|
+
version: 0.1.0
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Sam Phillips
|
@@ -14,7 +14,7 @@ autorequire:
|
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
16
|
|
17
|
-
date: 2011-
|
17
|
+
date: 2011-03-06 00:00:00 +00:00
|
18
18
|
default_executable:
|
19
19
|
dependencies: []
|
20
20
|
|
@@ -26,15 +26,19 @@ extensions: []
|
|
26
26
|
|
27
27
|
extra_rdoc_files:
|
28
28
|
- LICENSE
|
29
|
-
- README.
|
29
|
+
- README.md
|
30
30
|
files:
|
31
31
|
- .document
|
32
32
|
- LICENSE
|
33
|
-
- README.
|
33
|
+
- README.md
|
34
34
|
- Rakefile
|
35
35
|
- VERSION
|
36
36
|
- ambitious_seeder.gemspec
|
37
37
|
- lib/ambitious_seeder.rb
|
38
|
+
- lib/generators/rails/seed/seed_generator.rb
|
39
|
+
- lib/generators/rails/seed/templates/template_seed.rb
|
40
|
+
- lib/tasks/ambitious_seeder.rake
|
41
|
+
- lib/tasks/support/rake_task_helpers.rb
|
38
42
|
- test/helper.rb
|
39
43
|
- test/test_ambitious_seeder.rb
|
40
44
|
has_rdoc: true
|
data/README.rdoc
DELETED
@@ -1,17 +0,0 @@
|
|
1
|
-
= ambitious_seeder
|
2
|
-
|
3
|
-
Description goes here.
|
4
|
-
|
5
|
-
== Note on Patches/Pull Requests
|
6
|
-
|
7
|
-
* Fork the project.
|
8
|
-
* Make your feature addition or bug fix.
|
9
|
-
* Add tests for it. This is important so I don't break it in a
|
10
|
-
future version unintentionally.
|
11
|
-
* Commit, do not mess with rakefile, version, or history.
|
12
|
-
(if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
|
13
|
-
* Send me a pull request. Bonus points for topic branches.
|
14
|
-
|
15
|
-
== Copyright
|
16
|
-
|
17
|
-
Copyright (c) 2011 Sam Phillips. See LICENSE for details.
|