seed_dump 0.1.3
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG.rdoc +0 -0
- data/MIT-LICENSE +20 -0
- data/README.rdoc +89 -0
- data/Rakefile +51 -0
- data/VERSION +1 -0
- data/lib/seed_dump.rb +3 -0
- data/lib/tasks/seed_dump_tasks.rake +78 -0
- data/lib/version.rb +9 -0
- data/seed_dump.gemspec +51 -0
- data/test/seed_dump_test.rb +8 -0
- data/test/test_helper.rb +3 -0
- metadata +75 -0
data/CHANGELOG.rdoc
ADDED
File without changes
|
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2010 Rob Halff
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,89 @@
|
|
1
|
+
= SeedDump
|
2
|
+
|
3
|
+
Seed dump is a simple plugin that adds a rake task named db:seed:dump.
|
4
|
+
|
5
|
+
It allows you to create a db/seeds.rb from your existing data in the database.
|
6
|
+
When there is no data in the database it will generate empty create statements.
|
7
|
+
|
8
|
+
|
9
|
+
=== Example Usage
|
10
|
+
|
11
|
+
Dump all data directly to db/seeds.rb:
|
12
|
+
|
13
|
+
rake db:seed:dump
|
14
|
+
|
15
|
+
Dump only data from the users and products table and dump a maximum amount of 10 records:
|
16
|
+
|
17
|
+
$ rake db:seed:dump MODELS=User,Product LIMIT=2
|
18
|
+
|
19
|
+
Result:
|
20
|
+
|
21
|
+
$ cat db/seeds.rb
|
22
|
+
# Autogenerated by the db:seed:dump task
|
23
|
+
# Do not hesitate to tweak this to your needs
|
24
|
+
|
25
|
+
products = Product.create(
|
26
|
+
{ :category_id => 1, :description => "Long Sleeve Shirt", :name => "Long Sleeve Shirt" },
|
27
|
+
{ :category_id => 3, :description => "Plain White Tee Shirt", :name => "Plain T-Shirt" }
|
28
|
+
)
|
29
|
+
|
30
|
+
users = User.create(
|
31
|
+
{ :id => 1, :password => "123456", :username => "test_1" },
|
32
|
+
{ :id => 2, :password => "234567", :username => "tes2" }
|
33
|
+
)
|
34
|
+
|
35
|
+
Append to db/seeds.rb instead of overwriting it:
|
36
|
+
|
37
|
+
rake db:seed:dump APPEND=true
|
38
|
+
|
39
|
+
Use another output file instead of db/seeds.rb
|
40
|
+
|
41
|
+
rake db:seed:dump FILE=db/categories.rb
|
42
|
+
|
43
|
+
By default the :id column will not be added to the generated Create statements
|
44
|
+
If you do want the :id to be included use WITH_ID:
|
45
|
+
|
46
|
+
rake db:seed:dump WITH_ID=1
|
47
|
+
|
48
|
+
If you don't want +seed_dump+ to dump any data allready available in the database use NO_DATA.
|
49
|
+
|
50
|
+
This will generate the dump with only 1 empty create statement.
|
51
|
+
It's up to you to edit these and change the values into something meaningful:
|
52
|
+
|
53
|
+
rake db:seed:dump MODEL=User NO_DATA=1 APPEND=true
|
54
|
+
|
55
|
+
Here is a full example using all of the options above:
|
56
|
+
|
57
|
+
rake db:seed:dump MODELS=Category LIMIT=10 APPEND=true FILE=db/categories.rb WITH_ID=1 NO_DATA=1
|
58
|
+
|
59
|
+
=== All environment variables
|
60
|
+
|
61
|
+
APPEND:
|
62
|
+
Append the data to db/seeds.rb instead of overwriting it.
|
63
|
+
|
64
|
+
FILE:
|
65
|
+
Use a different output file, default: db/seeds.rb
|
66
|
+
|
67
|
+
LIMIT:
|
68
|
+
Dump no more then this amount of data, default: no limit
|
69
|
+
|
70
|
+
MODEL(S):
|
71
|
+
A model name or a comma seperated list of models, default: all models
|
72
|
+
|
73
|
+
NO_DATA:
|
74
|
+
Don't dump any data from the db, instead generate empty Create options
|
75
|
+
|
76
|
+
WITH_ID:
|
77
|
+
Inlcude the +:id+ in the create options
|
78
|
+
|
79
|
+
== Note on Patches/Pull Requests
|
80
|
+
|
81
|
+
* Fork the project.
|
82
|
+
* Make your feature addition or bug fix.
|
83
|
+
* Add tests for it. This is important so I don't break it in a
|
84
|
+
future version unintentionally.
|
85
|
+
* Commit, do not mess with rakefile, version, or history.
|
86
|
+
(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)
|
87
|
+
* Send me a pull request. Bonus points for topic branches.
|
88
|
+
|
89
|
+
Copyright (c) 2010 Rob Halff, released under the MIT license
|
data/Rakefile
ADDED
@@ -0,0 +1,51 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'jeweler'
|
6
|
+
Jeweler::Tasks.new do |gem|
|
7
|
+
gem.name = "seed_dump"
|
8
|
+
gem.summary = "{Seed Dumper for Rails}"
|
9
|
+
gem.description = %Q{Dump (parts) of your database to db/seeds.rb to get a headstart creating a meaningful seeds.rb file}
|
10
|
+
gem.email = "rob.halff@gmail.com"
|
11
|
+
gem.homepage = "http://github.com/rhalff/seed_dump"
|
12
|
+
gem.authors = ["Rob Halff"]
|
13
|
+
end
|
14
|
+
Jeweler::GemcutterTasks.new
|
15
|
+
rescue LoadError
|
16
|
+
puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
|
17
|
+
end
|
18
|
+
|
19
|
+
require 'rake/testtask'
|
20
|
+
Rake::TestTask.new(:test) do |test|
|
21
|
+
test.libs << 'lib' << 'test'
|
22
|
+
test.pattern = 'test/**/test_*.rb'
|
23
|
+
test.verbose = true
|
24
|
+
end
|
25
|
+
|
26
|
+
begin
|
27
|
+
require 'rcov/rcovtask'
|
28
|
+
Rcov::RcovTask.new do |test|
|
29
|
+
test.libs << 'test'
|
30
|
+
test.pattern = 'test/**/test_*.rb'
|
31
|
+
test.verbose = true
|
32
|
+
end
|
33
|
+
rescue LoadError
|
34
|
+
task :rcov do
|
35
|
+
abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
task :test => :check_dependencies
|
40
|
+
|
41
|
+
task :default => :test
|
42
|
+
|
43
|
+
require 'rake/rdoctask'
|
44
|
+
Rake::RDocTask.new do |rdoc|
|
45
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
46
|
+
|
47
|
+
rdoc.rdoc_dir = 'rdoc'
|
48
|
+
rdoc.title = "seed_dump #{version}"
|
49
|
+
rdoc.rdoc_files.include('README*')
|
50
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
51
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.1.3
|
data/lib/seed_dump.rb
ADDED
@@ -0,0 +1,78 @@
|
|
1
|
+
namespace :db do
|
2
|
+
|
3
|
+
# tests:
|
4
|
+
# test empty opt.models
|
5
|
+
# test populated opt.models
|
6
|
+
# test no_id option
|
7
|
+
# test limit option
|
8
|
+
# test empty table but valid seed
|
9
|
+
# test no-data
|
10
|
+
# Write by default to db/seeds.rb
|
11
|
+
# make filename optional with FILE env variable
|
12
|
+
# should ignore append if file doesn't exist
|
13
|
+
#
|
14
|
+
# rake db:seed:dump WITH_ID=true MODELS=User,Product LIMIT=1 NO_DATA=true APPEND=true INDENT=8 FILE=/tmp/seeds.rb
|
15
|
+
#
|
16
|
+
namespace :seed do
|
17
|
+
desc =<<HERE
|
18
|
+
Dump records from the database into db/seeds
|
19
|
+
HERE
|
20
|
+
task :dump => :environment do
|
21
|
+
|
22
|
+
# config
|
23
|
+
opts = {}
|
24
|
+
opts['with_id'] = !ENV["WITH_ID"].nil?
|
25
|
+
opts['no-data'] = !ENV['NO_DATA'].nil?
|
26
|
+
opts['models'] = ENV['MODELS'] || (ENV['MODEL'] ? ENV['MODEL'] : "")
|
27
|
+
opts['file'] = ENV['FILE'] || "#{Rails.root}/db/seeds.rb"
|
28
|
+
opts['append'] = (!ENV['APPEND'].nil? && File.exists?(opts['file']) )
|
29
|
+
ar_options = ENV['LIMIT'].to_i > 0 ? { :limit => ENV['LIMIT'].to_i } : {}
|
30
|
+
indent = " " * (ENV['INDENT'].nil? ? 2 : ENV['INDENT'].to_i)
|
31
|
+
|
32
|
+
models = opts['models'].split(',').collect {|x| x.underscore.singularize }
|
33
|
+
|
34
|
+
new_line = "\r\n"
|
35
|
+
|
36
|
+
seed_rb = ""
|
37
|
+
Dir['app/models/*.rb'].each do |f|
|
38
|
+
model_name = File.basename(f, '.*')
|
39
|
+
if models.include?(model_name) || models.empty?
|
40
|
+
|
41
|
+
create_hash = ""
|
42
|
+
|
43
|
+
model = model_name.camelize.constantize
|
44
|
+
arr = []
|
45
|
+
arr = model.find(:all, ar_options) unless opts['no-data']
|
46
|
+
arr = arr.empty? ? [model.new] : arr
|
47
|
+
arr.each_with_index { |r,i|
|
48
|
+
|
49
|
+
attr_s = [];
|
50
|
+
r.attributes.each { |k,v| attr_s.push("#{k.to_sym.inspect} => #{v.inspect}") unless k == 'id' && !opts['with_id'] }
|
51
|
+
|
52
|
+
create_hash << (i > 0 ? ",#{new_line}" : new_line) << indent << '{ ' << attr_s.join(', ') << ' }'
|
53
|
+
|
54
|
+
}
|
55
|
+
|
56
|
+
seed_rb << "#{new_line}#{model_name.pluralize} = #{model_name.camelize}.create(#{create_hash}#{new_line})#{new_line}"
|
57
|
+
end
|
58
|
+
end
|
59
|
+
File.open(opts['file'], (opts['append'] ? "a" : "w")) { |f|
|
60
|
+
|
61
|
+
unless opts['append']
|
62
|
+
cont =<<HERE
|
63
|
+
# Autogenerated by the db:seed:dump task
|
64
|
+
# Do not hesitate to tweak this to your needs
|
65
|
+
HERE
|
66
|
+
f << cont
|
67
|
+
end
|
68
|
+
|
69
|
+
cont =<<HERE
|
70
|
+
#{seed_rb}
|
71
|
+
HERE
|
72
|
+
f << cont
|
73
|
+
|
74
|
+
}
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
end
|
data/lib/version.rb
ADDED
data/seed_dump.gemspec
ADDED
@@ -0,0 +1,51 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = %q{seed_dump}
|
8
|
+
s.version = "0.1.3"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["Rob Halff"]
|
12
|
+
s.date = %q{2010-07-29}
|
13
|
+
s.description = %q{Dump (parts) of your database to db/seeds.rb to get a headstart creating a meaningful seeds.rb file}
|
14
|
+
s.email = %q{rob.halff@gmail.com}
|
15
|
+
s.extra_rdoc_files = [
|
16
|
+
"README.rdoc"
|
17
|
+
]
|
18
|
+
s.files = [
|
19
|
+
"CHANGELOG.rdoc",
|
20
|
+
"MIT-LICENSE",
|
21
|
+
"README.rdoc",
|
22
|
+
"Rakefile",
|
23
|
+
"VERSION",
|
24
|
+
"lib/seed_dump.rb",
|
25
|
+
"lib/tasks/seed_dump_tasks.rake",
|
26
|
+
"lib/version.rb",
|
27
|
+
"seed_dump.gemspec",
|
28
|
+
"test/seed_dump_test.rb",
|
29
|
+
"test/test_helper.rb"
|
30
|
+
]
|
31
|
+
s.homepage = %q{http://github.com/rhalff/seed_dump}
|
32
|
+
s.rdoc_options = ["--charset=UTF-8"]
|
33
|
+
s.require_paths = ["lib"]
|
34
|
+
s.rubygems_version = %q{1.3.7}
|
35
|
+
s.summary = %q{{Seed Dumper for Rails}}
|
36
|
+
s.test_files = [
|
37
|
+
"test/test_helper.rb",
|
38
|
+
"test/seed_dump_test.rb"
|
39
|
+
]
|
40
|
+
|
41
|
+
if s.respond_to? :specification_version then
|
42
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
43
|
+
s.specification_version = 3
|
44
|
+
|
45
|
+
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
46
|
+
else
|
47
|
+
end
|
48
|
+
else
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
data/test/test_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,75 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: seed_dump
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 1
|
8
|
+
- 3
|
9
|
+
version: 0.1.3
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- Rob Halff
|
13
|
+
autorequire:
|
14
|
+
bindir: bin
|
15
|
+
cert_chain: []
|
16
|
+
|
17
|
+
date: 2010-07-29 00:00:00 +02:00
|
18
|
+
default_executable:
|
19
|
+
dependencies: []
|
20
|
+
|
21
|
+
description: Dump (parts) of your database to db/seeds.rb to get a headstart creating a meaningful seeds.rb file
|
22
|
+
email: rob.halff@gmail.com
|
23
|
+
executables: []
|
24
|
+
|
25
|
+
extensions: []
|
26
|
+
|
27
|
+
extra_rdoc_files:
|
28
|
+
- README.rdoc
|
29
|
+
files:
|
30
|
+
- CHANGELOG.rdoc
|
31
|
+
- MIT-LICENSE
|
32
|
+
- README.rdoc
|
33
|
+
- Rakefile
|
34
|
+
- VERSION
|
35
|
+
- lib/seed_dump.rb
|
36
|
+
- lib/tasks/seed_dump_tasks.rake
|
37
|
+
- lib/version.rb
|
38
|
+
- seed_dump.gemspec
|
39
|
+
- test/seed_dump_test.rb
|
40
|
+
- test/test_helper.rb
|
41
|
+
has_rdoc: true
|
42
|
+
homepage: http://github.com/rhalff/seed_dump
|
43
|
+
licenses: []
|
44
|
+
|
45
|
+
post_install_message:
|
46
|
+
rdoc_options:
|
47
|
+
- --charset=UTF-8
|
48
|
+
require_paths:
|
49
|
+
- lib
|
50
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
51
|
+
none: false
|
52
|
+
requirements:
|
53
|
+
- - ">="
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
segments:
|
56
|
+
- 0
|
57
|
+
version: "0"
|
58
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
59
|
+
none: false
|
60
|
+
requirements:
|
61
|
+
- - ">="
|
62
|
+
- !ruby/object:Gem::Version
|
63
|
+
segments:
|
64
|
+
- 0
|
65
|
+
version: "0"
|
66
|
+
requirements: []
|
67
|
+
|
68
|
+
rubyforge_project:
|
69
|
+
rubygems_version: 1.3.7
|
70
|
+
signing_key:
|
71
|
+
specification_version: 3
|
72
|
+
summary: "{Seed Dumper for Rails}"
|
73
|
+
test_files:
|
74
|
+
- test/test_helper.rb
|
75
|
+
- test/seed_dump_test.rb
|