seed_dump 0.6.0 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 7bb4e4606e31915e6334725645e3d7f7f0238309
4
- data.tar.gz: 509dbc5e4adc981ab4333fff1162067c3889b12b
3
+ metadata.gz: 6af3d1433e6c77d4aa40fa1ea392cc9ac21ee728
4
+ data.tar.gz: eb9a5434bc51a69c1f24828c91be082679bf6668
5
5
  SHA512:
6
- metadata.gz: 53da1240fc843bef8a697ca3e8d6d0f02af2830cbc3646fe7920e57440b37af2532e163694b0810bfc4eb9e35cfdb31edf561c8170a64340d5526f2aabba249f
7
- data.tar.gz: c2ae668cd7f797c3ab5a5bf66ad2c899697e96d6d3e2780f9b76ac7c82566ba81a0a371cbf87d94dbce4d7756eaef55c2ec143b8ff42ed94c24a7acb32700bbd
6
+ metadata.gz: a995c0615b35cc3a7332a8d2eef59dea404ece69fa9b8ec10ab46ff3c0338fed07de039c14673ec582fa4cb86877ad90c5f6f81c1ac5a76cb30e60e282546595
7
+ data.tar.gz: 59aeb2c7666dfba8471406b8428598025f2e3a522f15fdd6419a06179c4671bfce6ca89eb14ee121a12522b0e1923200f9ba728737136903d077b234ce4c0921
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format progress
data/Gemfile ADDED
@@ -0,0 +1,13 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gem 'activesupport'
4
+ gem 'activerecord'
5
+
6
+ group :development do
7
+ gem 'jeweler', :git => 'git://github.com/technicalpickles/jeweler.git'
8
+ end
9
+
10
+ group :test do
11
+ gem 'rspec'
12
+ gem 'sqlite3'
13
+ end
data/README.md ADDED
@@ -0,0 +1,89 @@
1
+ Seed Dump
2
+ ========
3
+
4
+ Seed Dump is a Rails 4 plugin that adds a rake task named `db:seed:dump`.
5
+
6
+ It allows you to create a `db/seeds.rb` from the existing data in your database.
7
+
8
+ Note: if you want to use Seed Dump with Rails 3 or earlier, use [version 0.5.3](http://rubygems.org/gems/seed_dump/versions/0.5.3) or earlier.
9
+
10
+ Example Usage
11
+ -------------
12
+
13
+ Dump all data directly to `db/seeds.rb`:
14
+
15
+ $ rake db:seed:dump
16
+
17
+ Dump only data from the users and products table and dump a maximum amount of 2 records:
18
+
19
+ $ rake db:seed:dump MODELS=User,Product LIMIT=2
20
+
21
+ Result:
22
+
23
+ $ cat db/seeds.rb
24
+ # Autogenerated by the db:seed:dump task
25
+ # Do not hesitate to tweak this to your needs
26
+
27
+ products = Product.create([
28
+ { :category_id => 1, :description => "Long Sleeve Shirt", :name => "Long Sleeve Shirt" },
29
+ { :category_id => 3, :description => "Plain White Tee Shirt", :name => "Plain T-Shirt" }
30
+ ])
31
+
32
+ users = User.create([
33
+ { :id => 1, :password => "123456", :username => "test_1" },
34
+ { :id => 2, :password => "234567", :username => "tes2" }
35
+ ])
36
+
37
+ Append to `db/seeds.rb` instead of overwriting it:
38
+
39
+ rake db:seed:dump APPEND=true
40
+
41
+ Use another output file instead of `db/seeds.rb`:
42
+
43
+ rake db:seed:dump FILE=db/categories.rb
44
+
45
+ If you want the dump to use `create!` rather than `create`:
46
+
47
+ rake db:seed:dump CREATE_METHOD='create!'
48
+
49
+ There are more environment variables that can be set— see below for all of them.
50
+
51
+
52
+ Installation
53
+ ------------
54
+
55
+ Add it to your Gemfile with:
56
+
57
+ gem 'seed_dump'
58
+
59
+ Or install it by hand:
60
+
61
+ $ gem install seed_dump
62
+
63
+
64
+ All environment variables
65
+ -------------------------
66
+
67
+ `APPEND`: Append the data to the file instead of overwriting it.
68
+
69
+ `CREATE_METHOD`: Use the specified create method rather than `create` (the default). Note: if you are using bash and want to use `create!`, be sure to use single quotes on the command line (i.e. `CREATE_METHOD='create!'`).
70
+
71
+ `FILE`: Use a different output file. Default: `db/seeds.rb`.
72
+
73
+ `LIMIT`: Dump no more then this amount of data. Default: no limit.
74
+
75
+ `MAX`: Split one create action per model into several create actions with MAX elements in each. Default: no limit. Useful for large data dumping to reduce memory usage.
76
+
77
+ `MODEL[S]`: A model name or a comma-separated list of models. Default: all models.
78
+
79
+ `NO_DATA`: Don't dump any data from the db, instead generate empty `create` options.
80
+
81
+ `WITH_ID`: Include the `:id` in the `create` options.
82
+
83
+ `TIMESTAMPS`: If true, include the `:created_by` and `:updated_by` timestamps. If false, don't include them. Default: true.
84
+
85
+ `SKIP_CALLBACKS`: Deactivate callbacks while importing.
86
+
87
+ `PG_SCHEMA`: Postgres schema support.
88
+
89
+ `MODEL_DIR`: Specify an alternate model dir.
data/Rakefile CHANGED
@@ -16,30 +16,6 @@ rescue LoadError
16
16
  puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
17
17
  end
18
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
19
  require 'rdoc/task'
44
20
  Rake::RDocTask.new do |rdoc|
45
21
  version = File.exist?('VERSION') ? File.read('VERSION') : ""
@@ -49,3 +25,7 @@ Rake::RDocTask.new do |rdoc|
49
25
  rdoc.rdoc_files.include('README*')
50
26
  rdoc.rdoc_files.include('lib/**/*.rb')
51
27
  end
28
+
29
+ require 'rspec/core/rake_task'
30
+ RSpec::Core::RakeTask.new(:spec)
31
+ task :default => :spec
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.6.0
1
+ 1.0.0
@@ -22,7 +22,6 @@ module SeedDump
22
22
  @opts['with_id'] = env["WITH_ID"].true?
23
23
  @opts['timestamps'] = env["TIMESTAMPS"].true? || env["TIMESTAMPS"].nil?
24
24
  @opts['no-data'] = env['NO_DATA'].true?
25
- @opts['without_protection'] = env['WITHOUT_PROTECTION'].true? || (env['WITHOUT_PROTECTION'].nil? && @opts['timestamps'])
26
25
  @opts['skip_callbacks'] = env['SKIP_CALLBACKS'].true?
27
26
  @opts['models'] = env['MODELS'] || (env['MODEL'] ? env['MODEL'] : "")
28
27
  @opts['file'] = env['FILE'] || "#{Rails.root}/db/seeds.rb"
@@ -34,41 +33,46 @@ module SeedDump
34
33
  @opts['schema'] = env['PG_SCHEMA']
35
34
  @opts['model_dir'] = env['MODEL_DIR'] || @model_dir
36
35
  @opts['create_method'] = env['CREATE_METHOD'] || 'create'
37
- @opts['rails4'] = env['RAILS4'].true?
38
- monkeypatch_AR if @opts['rails4']
39
36
  end
40
37
 
41
- def monkeypatch_AR
42
- ActiveRecord::Base.instance_eval do
43
- def attr_accessible(*opts)
44
- nil
45
- end
46
- end
38
+ def log(msg)
39
+ puts msg if @opts['debug']
47
40
  end
48
41
 
49
42
  def load_models
50
- puts "Searching in #{@opts['model_dir']} for models" if @opts['debug']
51
- Dir[Dir.pwd + '/' + @opts['model_dir']].sort.each do |f|
52
- puts "Processing file #{f}" if @opts['debug']
53
- # parse file name and path leading up to file name and assume the path is a module
54
- f =~ /models\/(.*).rb/
55
- # split path by /, camelize the constituents, and then reform as a formal class name
56
- parts = $1.split("/").map {|x| x.camelize}
43
+ log("Searching in #{@opts['model_dir']} for models")
44
+
45
+ Dir[File.join(Dir.pwd, @opts['model_dir'])].sort.each do |f|
46
+ log("Processing file #{f}")
47
+
48
+ dirname, basename = File.split(f)
49
+
50
+ dir_array = dirname.split(File::SEPARATOR)
51
+
52
+ # Find index of last occurence of 'models' in path
53
+ models_index = nil
54
+ dir_array.each_with_index {|x, i| models_index = i if x == 'models'}
55
+
56
+ model_dir_array = dir_array[models_index + 1..-1]
57
57
 
58
58
  # Initialize nested model namespaces
59
- parts.clip.inject(Object) do |x, y|
60
- if x.const_defined?(y)
61
- x.const_get(y)
59
+ model_dir_array.inject(Object) do |parent, child|
60
+ child = child.camelize
61
+
62
+ if parent.const_defined?(child)
63
+ parent.const_get(child)
62
64
  else
63
- x.const_set(y, Module.new)
65
+ parent.const_set(child, Module.new)
64
66
  end
65
67
  end
66
68
 
67
- model = parts.join("::")
68
69
  require f
69
70
 
70
- puts "Detected model #{model}" if @opts['debug']
71
- @models.push model if @opts['models'].include?(model) || @opts['models'].empty?
71
+ model = File.join(model_dir_array + [File.basename(basename, '.rb')]).camelize
72
+
73
+ log("Detected model #{model}")
74
+
75
+ @models << model if @opts['models'].include?(model) || @opts['models'].empty?
72
76
  end
73
77
  end
74
78
 
@@ -103,25 +107,22 @@ module SeedDump
103
107
  create_hash = ""
104
108
  options = ''
105
109
  rows = []
106
- arr = []
107
- arr = model.find(:all, @ar_options) unless @opts['no-data']
110
+ arr = nil
111
+ unless @opts['no-data']
112
+ arr = model.all
113
+ arr.limit(@ar_options[:limit]) if @ar_options[:limit]
114
+ end
108
115
  arr = arr.empty? ? [model.new] : arr
109
116
 
110
117
  arr.each_with_index { |r,i|
111
118
  attr_s = [];
112
119
  r.attributes.each do |k,v|
113
- if (@opts['rails4'] || (model.attr_accessible[:default].include? k) || @opts['without_protection'] || @opts['with_id'])
114
- pushed_key = dump_attribute(attr_s,r,k,v)
115
- @last_record.push k if pushed_key
116
- end
120
+ pushed_key = dump_attribute(attr_s,r,k,v)
121
+ @last_record.push k if pushed_key
117
122
  end
118
123
  rows.push "#{@indent}{ " << attr_s.join(', ') << " }"
119
124
  }
120
125
 
121
- if @opts['without_protection']
122
- options = ', :without_protection => true '
123
- end
124
-
125
126
  if @opts['max']
126
127
  splited_rows = rows.each_slice(@opts['max']).to_a
127
128
  maxsarr = []
@@ -182,11 +183,8 @@ module SeedDump
182
183
  end
183
184
 
184
185
  def run(env)
185
-
186
186
  setup env
187
187
 
188
- puts "Protection is disabled." if @opts['verbose'] && @opts['without_protection']
189
-
190
188
  set_search_path @opts['schema'] if @opts['schema']
191
189
 
192
190
  load_models
data/seed_dump.gemspec CHANGED
@@ -5,20 +5,22 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "seed_dump"
8
- s.version = "0.6.0"
8
+ s.version = "1.0.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Rob Halff", "Ryan Oblak"]
12
- s.date = "2013-08-01"
12
+ s.date = "2013-09-18"
13
13
  s.description = "Dump (parts) of your database to db/seeds.rb to get a headstart creating a meaningful seeds.rb file"
14
14
  s.email = "rroblak@gmail.com"
15
15
  s.extra_rdoc_files = [
16
- "README.rdoc"
16
+ "README.md"
17
17
  ]
18
18
  s.files = [
19
+ ".rspec",
19
20
  "CHANGELOG.rdoc",
21
+ "Gemfile",
20
22
  "MIT-LICENSE",
21
- "README.rdoc",
23
+ "README.md",
22
24
  "Rakefile",
23
25
  "VERSION",
24
26
  "lib/clip.rb",
@@ -28,17 +30,34 @@ Gem::Specification.new do |s|
28
30
  "lib/tasks/seed_dump.rake",
29
31
  "lib/true.rb",
30
32
  "seed_dump.gemspec",
31
- "test/fixtures/samples.yml",
32
- "test/models/abstract_sample.rb",
33
- "test/models/child_sample.rb",
34
- "test/models/nested/sample.rb",
35
- "test/models/sample.rb",
36
- "test/seed_dump_test.rb",
37
- "test/test_helper.rb"
33
+ "spec/models/abstract_sample.rb",
34
+ "spec/models/child_sample.rb",
35
+ "spec/models/nested/sample.rb",
36
+ "spec/models/sample.rb",
37
+ "spec/seed_dump_perform_spec.rb",
38
+ "spec/spec_helper.rb"
38
39
  ]
39
40
  s.homepage = "https://github.com/rroblak/seed_dump"
40
41
  s.require_paths = ["lib"]
41
42
  s.rubygems_version = "2.0.3"
42
43
  s.summary = "{Seed Dumper for Rails}"
44
+
45
+ if s.respond_to? :specification_version then
46
+ s.specification_version = 4
47
+
48
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
49
+ s.add_runtime_dependency(%q<activesupport>, [">= 0"])
50
+ s.add_runtime_dependency(%q<activerecord>, [">= 0"])
51
+ s.add_development_dependency(%q<jeweler>, [">= 0"])
52
+ else
53
+ s.add_dependency(%q<activesupport>, [">= 0"])
54
+ s.add_dependency(%q<activerecord>, [">= 0"])
55
+ s.add_dependency(%q<jeweler>, [">= 0"])
56
+ end
57
+ else
58
+ s.add_dependency(%q<activesupport>, [">= 0"])
59
+ s.add_dependency(%q<activerecord>, [">= 0"])
60
+ s.add_dependency(%q<jeweler>, [">= 0"])
61
+ end
43
62
  end
44
63
 
File without changes
@@ -1,4 +1,3 @@
1
1
  class ChildSample < AbstractSample
2
- attr_accessible :name
3
2
  end
4
3
 
@@ -1,3 +1,2 @@
1
1
  class Nested::Sample < ActiveRecord::Base
2
- attr_accessible :name
3
2
  end
@@ -0,0 +1,2 @@
1
+ class Sample < ActiveRecord::Base
2
+ end
@@ -0,0 +1,118 @@
1
+ require 'spec_helper'
2
+
3
+ describe SeedDump::Perform do
4
+ before(:all) do
5
+ ActiveRecord::Base.establish_connection(:adapter => 'sqlite3', :database => ':memory:')
6
+
7
+ ActiveRecord::Schema.define(:version => 1) do
8
+ create_table 'child_samples', :force => true do |t|
9
+ t.string 'name'
10
+ t.datetime 'created_at', :null => false
11
+ t.datetime 'updated_at', :null => false
12
+ end
13
+
14
+ create_table 'samples', :force => true do |t|
15
+ t.string 'string'
16
+ t.text 'text'
17
+ t.integer 'integer'
18
+ t.float 'float'
19
+ t.decimal 'decimal'
20
+ t.datetime 'datetime'
21
+ t.datetime 'timestamp'
22
+ t.time 'time'
23
+ t.date 'date'
24
+ t.binary 'binary'
25
+ t.boolean 'boolean'
26
+ t.datetime 'created_at', :null => false
27
+ t.datetime 'updated_at', :null => false
28
+ end
29
+ end
30
+ end
31
+
32
+ before do
33
+ @sd = SeedDump::Perform.new
34
+
35
+ @env = {'MODEL_DIR' => 'spec/models/*.rb',
36
+ 'FILE' => Dir.pwd + '/spec/db/seeds.rb',
37
+ 'VERBOSE' => false,
38
+ 'DEBUG' => false}
39
+ end
40
+
41
+ it 'should load models from the specified directory' do
42
+ @sd.setup(@env)
43
+
44
+ @sd.load_models
45
+
46
+ @sd.models.should eq(["AbstractSample", "ChildSample", "Sample"])
47
+ end
48
+
49
+ it 'should support nested models' do
50
+ @env['MODEL_DIR'] = 'spec/models/**/*.rb'
51
+
52
+ @sd.setup @env
53
+
54
+ @sd.load_models
55
+
56
+ @sd.models.should eq(['AbstractSample', 'ChildSample', 'Nested::Sample', 'Sample'])
57
+ end
58
+
59
+ it 'should not include timestamps if the TIMESTAMPS parameter is false' do
60
+ @env['TIMESTAMPS'] = false
61
+
62
+ @sd.setup @env
63
+
64
+ @sd.load_models
65
+
66
+ @sd.dump_models
67
+
68
+ @sd.last_record.should_not include('created_at')
69
+ end
70
+
71
+ it 'should include timestamps if the TIMESTAMPS parameter is true' do
72
+ @env['TIMESTAMPS'] = true
73
+
74
+ @sd.setup @env
75
+
76
+ @sd.load_models
77
+
78
+ @sd.dump_models
79
+
80
+ @sd.last_record.should include('created_at')
81
+ end
82
+
83
+ it 'should include ids if the WITH_ID parameter is true' do
84
+ @env['WITH_ID'] = true
85
+
86
+ @sd.setup @env
87
+
88
+ @sd.load_models
89
+
90
+ @sd.dump_models
91
+
92
+ @sd.last_record.should include('id')
93
+ end
94
+
95
+ it 'should skip abstract models' do
96
+ @env['MODELS'] = 'AbstractSample'
97
+
98
+ @sd.setup @env
99
+
100
+ @sd.load_models
101
+
102
+ @sd.dump_models
103
+
104
+ @sd.last_record.should eq([])
105
+ end
106
+
107
+ it 'should use the create method specified in the CREATE_METHOD parameter' do
108
+ @env['CREATE_METHOD'] = 'create!'
109
+
110
+ @sd.setup @env
111
+
112
+ @sd.load_models
113
+
114
+ @sd.dump_models
115
+
116
+ @sd.instance_variable_get(:@seed_rb).should include('create!')
117
+ end
118
+ end
@@ -0,0 +1,15 @@
1
+ require 'seed_dump/perform'
2
+ require 'active_support/core_ext/string'
3
+ require 'active_record'
4
+
5
+ RSpec.configure do |config|
6
+ config.treat_symbols_as_metadata_keys_with_true_values = true
7
+ config.run_all_when_everything_filtered = true
8
+ config.filter_run :focus
9
+
10
+ # Run specs in random order to surface order dependencies. If you find an
11
+ # order dependency and want to debug it, you can fix the order by providing
12
+ # the seed, which is printed after each run.
13
+ # --seed 1234
14
+ config.order = 'random'
15
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: seed_dump
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.0
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Rob Halff
@@ -9,19 +9,63 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-08-01 00:00:00.000000000 Z
13
- dependencies: []
12
+ date: 2013-09-18 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: activesupport
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - '>='
19
+ - !ruby/object:Gem::Version
20
+ version: '0'
21
+ type: :runtime
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - '>='
26
+ - !ruby/object:Gem::Version
27
+ version: '0'
28
+ - !ruby/object:Gem::Dependency
29
+ name: activerecord
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - '>='
33
+ - !ruby/object:Gem::Version
34
+ version: '0'
35
+ type: :runtime
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - '>='
40
+ - !ruby/object:Gem::Version
41
+ version: '0'
42
+ - !ruby/object:Gem::Dependency
43
+ name: jeweler
44
+ requirement: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - '>='
47
+ - !ruby/object:Gem::Version
48
+ version: '0'
49
+ type: :development
50
+ prerelease: false
51
+ version_requirements: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - '>='
54
+ - !ruby/object:Gem::Version
55
+ version: '0'
14
56
  description: Dump (parts) of your database to db/seeds.rb to get a headstart creating
15
57
  a meaningful seeds.rb file
16
58
  email: rroblak@gmail.com
17
59
  executables: []
18
60
  extensions: []
19
61
  extra_rdoc_files:
20
- - README.rdoc
62
+ - README.md
21
63
  files:
64
+ - .rspec
22
65
  - CHANGELOG.rdoc
66
+ - Gemfile
23
67
  - MIT-LICENSE
24
- - README.rdoc
68
+ - README.md
25
69
  - Rakefile
26
70
  - VERSION
27
71
  - lib/clip.rb
@@ -31,13 +75,12 @@ files:
31
75
  - lib/tasks/seed_dump.rake
32
76
  - lib/true.rb
33
77
  - seed_dump.gemspec
34
- - test/fixtures/samples.yml
35
- - test/models/abstract_sample.rb
36
- - test/models/child_sample.rb
37
- - test/models/nested/sample.rb
38
- - test/models/sample.rb
39
- - test/seed_dump_test.rb
40
- - test/test_helper.rb
78
+ - spec/models/abstract_sample.rb
79
+ - spec/models/child_sample.rb
80
+ - spec/models/nested/sample.rb
81
+ - spec/models/sample.rb
82
+ - spec/seed_dump_perform_spec.rb
83
+ - spec/spec_helper.rb
41
84
  homepage: https://github.com/rroblak/seed_dump
42
85
  licenses: []
43
86
  metadata: {}
data/README.rdoc DELETED
@@ -1,114 +0,0 @@
1
- = SeedDump
2
-
3
- Seed dump is a Rails 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
- It mainly exists for people who are too lazy writing create statements in db/seeds.rb themselves
9
- and need something to dump data from their existing database data into seeds.rb
10
-
11
- Note: for Rails 4 compatibility please add "RAILS4=true WITHOUT_PROTECTION=false". This will be cleaned up in the future.
12
-
13
- == Example Usage
14
-
15
- Dump all data directly to db/seeds.rb:
16
-
17
- rake db:seed:dump
18
-
19
- Dump only data from the users and products table and dump a maximum amount of 2 records:
20
-
21
- $ rake db:seed:dump MODELS=User,Product LIMIT=2
22
-
23
- Result:
24
-
25
- $ cat db/seeds.rb
26
- # Autogenerated by the db:seed:dump task
27
- # Do not hesitate to tweak this to your needs
28
-
29
- products = Product.create([
30
- { :category_id => 1, :description => "Long Sleeve Shirt", :name => "Long Sleeve Shirt" },
31
- { :category_id => 3, :description => "Plain White Tee Shirt", :name => "Plain T-Shirt" }
32
- ])
33
-
34
- users = User.create([
35
- { :id => 1, :password => "123456", :username => "test_1" },
36
- { :id => 2, :password => "234567", :username => "tes2" }
37
- ])
38
-
39
- Append to db/seeds.rb instead of overwriting it:
40
-
41
- rake db:seed:dump APPEND=true
42
-
43
- Use another output file instead of db/seeds.rb
44
-
45
- rake db:seed:dump FILE=db/categories.rb
46
-
47
- By default the :id column will not be added to the generated create statements.
48
- If you do want the :id to be included use WITH_ID:
49
-
50
- rake db:seed:dump WITH_ID=1
51
-
52
- If you don't want +seed_dump+ to dump any data allready available in the database use NO_DATA.
53
-
54
- This will generate the dump with only 1 empty create statement.
55
- It's up to you to edit these and change the values into something meaningful:
56
-
57
- rake db:seed:dump MODEL=User NO_DATA=1 APPEND=true
58
-
59
- If you want the dump to use `create!` rather than `create`:
60
-
61
- rake db:seed:dump CREATE_METHOD='create!'
62
-
63
- Here is a full example using all of the options above:
64
-
65
- rake db:seed:dump MODELS=Category LIMIT=10 APPEND=true FILE=db/categories.rb WITH_ID=1 NO_DATA=1 CREATE_METHOD='create!'
66
-
67
- == All environment variables
68
-
69
- RAILS4:
70
- Specify as "true" for Rails 4 compatibility.
71
-
72
- APPEND:
73
- Append the data to db/seeds.rb instead of overwriting it.
74
-
75
- CREATE_METHOD:
76
- Use the specified create method rather than 'create' (the default). Note: if you are using bash and want to use 'create!', be sure to use single quotes on the command line (i.e. CREATE_METHOD='create!').
77
-
78
- FILE:
79
- Use a different output file, default: db/seeds.rb
80
-
81
- LIMIT:
82
- Dump no more then this amount of data, default: no limit
83
-
84
- MAX:
85
- Split one create action per model to several create actions with MAX elements in each, default: no limit
86
- It usefull for large data dumping to reduce memory usage
87
-
88
- MODEL(S):
89
- A model name or a comma seperated list of models, default: all models
90
-
91
- NO_DATA:
92
- Don't dump any data from the db, instead generate empty Create options
93
-
94
- WITH_ID:
95
- Inlcude the +:id+ in the create options
96
-
97
- TIMESTAMPS:
98
- Include the :created_by and :updated_by timestamps (default)
99
-
100
- SKIP_CALLBACKS:
101
- Deactivate callbacks while importing.
102
-
103
- PG_SCHEMA:
104
- Postgres schema support
105
-
106
- WITHOUT_PROTECTION:
107
- Skip protection for columns that are protected by default.
108
- Note : WITH_ID and TIMESTAMPS automatically override protection
109
-
110
- MODEL_DIR:
111
- Specify an alternative model dir
112
-
113
-
114
- Copyright (c) 2010 Rob Halff, released under the MIT license
@@ -1,54 +0,0 @@
1
- # Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/Fixtures.html
2
-
3
- bharathiyar:
4
- string: Poetry of Subramaniya Bharathiyar
5
- text: யாமறிந்த மொழிகளிலே தமிழ்மொழி போல் இனிதாவது எங்கும் காணோம்,
6
- பாமரராய் விலங்குகளாய், உலகனைத்தும் இகழ்ச்சிசொலப் பான்மை கெட்டு,
7
- நாமமது தமிழரெனக் கொண்டு இங்கு வாழ்ந்திடுதல் நன்றோ? சொல்லீர்!
8
- தேமதுரத் தமிழோசை உலகமெலாம் பரவும்வகை செய்தல் வேண்டும்.
9
- integer: 1
10
- float: 3.3333
11
- decimal: 9.99
12
- datetime: 1902-07-21 17:01:31
13
- timestamp: 1900-07-21 17:01:31
14
- time: 1903-07-21 17:01:31
15
- date: 1904-07-21
16
- binary:
17
- boolean: false
18
-
19
- pushkin:
20
- string: Pushkin's Bronze Horseman
21
- text: На берегу пустынных волн
22
- Стоял он, дум великих полн,
23
- И вдаль глядел. Пред ним широко
24
- Река неслася; бедный чёлн
25
- По ней стремился одиноко.
26
- По мшистым, топким берегам
27
- Чернели избы здесь и там,
28
- Приют убогого чухонца;
29
- И лес, неведомый лучам
30
- В тумане спрятанного солнца,
31
- Кругом шумел.
32
- integer: 1
33
- float: 1.5
34
- decimal: 9.99
35
- datetime: 2012-07-21 17:01:31
36
- timestamp: 2012-07-21 17:01:31
37
- time: 2012-07-21 17:01:31
38
- date: 2012-07-21
39
- binary:
40
- boolean: false
41
- poem:
42
- string: Anglo-Saxon Rune Poem
43
- text: ᚠᛇᚻ᛫ᛒᛦᚦ᛫ᚠᚱᚩᚠᚢᚱ᛫ᚠᛁᚱᚪ᛫ᚷᛖᚻᚹᛦᛚᚳᚢᛗ
44
- ᛋᚳᛖᚪᛚ᛫ᚦᛖᚪᚻ᛫ᛗᚪᚾᚾᚪ᛫ᚷᛖᚻᚹᛦᛚᚳ᛫ᛗᛁᚳᛚᚢᚾ᛫ᚻᛦᛏ᛫ᛞᚫᛚᚪᚾ
45
- ᚷᛁᚠ᛫ᚻᛖ᛫ᚹᛁᛚᛖ᛫ᚠᚩᚱ᛫ᛞᚱᛁᚻᛏᚾᛖ᛫ᛞᚩᛗᛖᛋ᛫ᚻᛚᛇᛏᚪᚾ
46
- integer: 1
47
- float: 1.9
48
- decimal: 4.55
49
- datetime: 2012-07-21 17:01:31
50
- timestamp: 2012-07-21 17:01:31
51
- time: 2012-07-21 17:01:31
52
- date: 2012-07-21
53
- binary:
54
- boolean: false
@@ -1,3 +0,0 @@
1
- class Sample < ActiveRecord::Base
2
- attr_accessible :binary, :boolean, :date, :datetime, :decimal, :float, :integer, :string, :text, :time, :timestamp
3
- end
@@ -1,77 +0,0 @@
1
- require 'test_helper'
2
- require "seed_dump/perform"
3
-
4
- class SeedDumpTest < ActiveSupport::TestCase
5
-
6
- setup do
7
- @sd = SeedDump::Perform.new
8
- # universial options for every test
9
- @env = {
10
- "MODEL_DIR" => 'test/models/**.rb',
11
- "FILE" => Dir.pwd + '/test/db/seeds.rb',
12
- "VERBOSE" => false,
13
- "DEBUG" => false,
14
- "RAILS4" => ENV['RAILS4']
15
- }
16
- end
17
-
18
- test "load sample model" do
19
- @env['MODEL_DIR'] = 'test/models/*.rb'
20
- @sd.setup @env
21
- @sd.load_models
22
- assert_equal ["AbstractSample", "ChildSample", "Sample"], @sd.models
23
- end
24
-
25
- test "support nested models" do
26
- @env['MODEL_DIR'] = 'test/models/**/*.rb'
27
- @sd.setup @env
28
- @sd.load_models
29
- assert_equal ["AbstractSample", "ChildSample", "Nested::Sample", "Sample"], @sd.models
30
- end
31
-
32
- test "without timestamps" do
33
- @env['MODEL_DIR'] = 'test/models/*.rb'
34
- @env['TIMESTAMPS'] = false
35
- @sd.setup @env
36
- @sd.load_models
37
- @sd.dump_models
38
- assert !@sd.last_record.include?("created_at"), "Should not include created_at if timestamps are off"
39
- end
40
-
41
- test "with timestamps" do
42
- @env['MODEL_DIR'] = 'test/models/*.rb'
43
- @env['TIMESTAMPS'] = true
44
- @sd.setup @env
45
- @sd.load_models
46
- @sd.dump_models
47
- assert @sd.last_record.include?("created_at"), "Must include created_at if timestamps are desired"
48
- end
49
-
50
- test "with id" do
51
- @env['MODEL_DIR'] = 'test/models/*.rb'
52
- @env['WITH_ID'] = true
53
- @sd.setup @env
54
- @sd.load_models
55
- @sd.dump_models
56
- assert @sd.last_record.include?("id"), "WITH_ID must include id"
57
- end
58
-
59
- test "skip abstract model" do
60
- @env['MODELS'] = "AbstractSample"
61
- @env['MODEL_DIR'] = 'test/models/*.rb'
62
- @env['TIMESTAMPS'] = false
63
- @sd.setup @env
64
- @sd.load_models
65
- @sd.dump_models
66
- assert_equal [], @sd.last_record
67
- end
68
-
69
- test "create method" do
70
- @env['MODEL_DIR'] = 'test/models/*.rb'
71
- @env['CREATE_METHOD'] = 'create!'
72
- @sd.setup @env
73
- @sd.load_models
74
- @sd.dump_models
75
- assert @sd.instance_variable_get(:@seed_rb) =~ /create!/, 'CREATE_METHOD must specify the creation method'
76
- end
77
- end
data/test/test_helper.rb DELETED
@@ -1,36 +0,0 @@
1
- $LOAD_PATH << File.join(File.dirname(__FILE__), '..', 'lib')
2
- require 'rubygems'
3
- require 'test/unit'
4
- require 'active_support'
5
- require 'active_record'
6
-
7
- #require 'your_thing'
8
-
9
- ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :database => ":memory:")
10
-
11
- ActiveRecord::Schema.define(:version => 1) do
12
- create_table "child_samples", :force => true do |t|
13
- t.string "name"
14
- t.datetime "created_at", :null => false
15
- t.datetime "updated_at", :null => false
16
- end
17
-
18
- create_table "samples", :force => true do |t|
19
- t.string "string"
20
- t.text "text"
21
- t.integer "integer"
22
- t.float "float"
23
- t.decimal "decimal"
24
- t.datetime "datetime"
25
- t.datetime "timestamp"
26
- t.time "time"
27
- t.date "date"
28
- t.binary "binary"
29
- t.boolean "boolean"
30
- t.datetime "created_at", :null => false
31
- t.datetime "updated_at", :null => false
32
- end
33
- end
34
-
35
- #class Sample < ActiveRecord::Base
36
- #end