seed_dump 3.3.1 → 3.4.1

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/seed_dump.gemspec DELETED
@@ -1,74 +0,0 @@
1
- # Generated by jeweler
2
- # DO NOT EDIT THIS FILE DIRECTLY
3
- # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
- # -*- encoding: utf-8 -*-
5
- # stub: seed_dump 3.3.1 ruby lib
6
-
7
- Gem::Specification.new do |s|
8
- s.name = "seed_dump".freeze
9
- s.version = "3.3.1"
10
-
11
- s.required_rubygems_version = Gem::Requirement.new(">= 0".freeze) if s.respond_to? :required_rubygems_version=
12
- s.require_paths = ["lib".freeze]
13
- s.authors = ["Rob Halff".freeze, "Ryan Oblak".freeze]
14
- s.date = "2018-05-08"
15
- s.description = "Dump (parts) of your database to db/seeds.rb to get a headstart creating a meaningful seeds.rb file".freeze
16
- s.email = "rroblak@gmail.com".freeze
17
- s.extra_rdoc_files = [
18
- "README.md"
19
- ]
20
- s.files = [
21
- ".rspec",
22
- "Gemfile",
23
- "MIT-LICENSE",
24
- "README.md",
25
- "Rakefile",
26
- "VERSION",
27
- "lib/seed_dump.rb",
28
- "lib/seed_dump/dump_methods.rb",
29
- "lib/seed_dump/dump_methods/enumeration.rb",
30
- "lib/seed_dump/environment.rb",
31
- "lib/seed_dump/railtie.rb",
32
- "lib/tasks/seed_dump.rake",
33
- "seed_dump.gemspec",
34
- "spec/dump_methods_spec.rb",
35
- "spec/environment_spec.rb",
36
- "spec/factories/another_samples.rb",
37
- "spec/factories/samples.rb",
38
- "spec/factories/yet_another_samples.rb",
39
- "spec/helpers.rb",
40
- "spec/spec_helper.rb"
41
- ]
42
- s.homepage = "https://github.com/rroblak/seed_dump".freeze
43
- s.licenses = ["MIT".freeze]
44
- s.rubygems_version = "2.7.6".freeze
45
- s.summary = "{Seed Dumper for Rails}".freeze
46
-
47
- if s.respond_to? :specification_version then
48
- s.specification_version = 4
49
-
50
- if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
51
- s.add_runtime_dependency(%q<activesupport>.freeze, [">= 4"])
52
- s.add_runtime_dependency(%q<activerecord>.freeze, [">= 4"])
53
- s.add_development_dependency(%q<byebug>.freeze, ["~> 2.0"])
54
- s.add_development_dependency(%q<factory_bot>.freeze, ["~> 4.8.2"])
55
- s.add_development_dependency(%q<activerecord-import>.freeze, ["~> 0.4"])
56
- s.add_development_dependency(%q<jeweler>.freeze, ["~> 2.0"])
57
- else
58
- s.add_dependency(%q<activesupport>.freeze, [">= 4"])
59
- s.add_dependency(%q<activerecord>.freeze, [">= 4"])
60
- s.add_dependency(%q<byebug>.freeze, ["~> 2.0"])
61
- s.add_dependency(%q<factory_bot>.freeze, ["~> 4.8.2"])
62
- s.add_dependency(%q<activerecord-import>.freeze, ["~> 0.4"])
63
- s.add_dependency(%q<jeweler>.freeze, ["~> 2.0"])
64
- end
65
- else
66
- s.add_dependency(%q<activesupport>.freeze, [">= 4"])
67
- s.add_dependency(%q<activerecord>.freeze, [">= 4"])
68
- s.add_dependency(%q<byebug>.freeze, ["~> 2.0"])
69
- s.add_dependency(%q<factory_bot>.freeze, ["~> 4.8.2"])
70
- s.add_dependency(%q<activerecord-import>.freeze, ["~> 0.4"])
71
- s.add_dependency(%q<jeweler>.freeze, ["~> 2.0"])
72
- end
73
- end
74
-
@@ -1,176 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe SeedDump do
4
-
5
- def expected_output(include_id = false, id_offset = 0)
6
- output = "Sample.create!([\n "
7
-
8
- data = []
9
- ((1 + id_offset)..(3 + id_offset)).each do |i|
10
- data << "{#{include_id ? "id: #{i}, " : ''}string: \"string\", text: \"text\", integer: 42, float: 3.14, decimal: \"2.72\", datetime: \"1776-07-04 19:14:00\", time: \"2000-01-01 03:15:00\", date: \"1863-11-19\", binary: \"binary\", boolean: false}"
11
- end
12
-
13
- output + data.join(",\n ") + "\n])\n"
14
- end
15
-
16
- describe '.dump' do
17
- before do
18
- Rails.application.eager_load!
19
-
20
- create_db
21
-
22
- FactoryBot.create_list(:sample, 3)
23
- end
24
-
25
- context 'without file option' do
26
- it 'should return the dump of the models passed in' do
27
- SeedDump.dump(Sample).should eq(expected_output)
28
- end
29
- end
30
-
31
- context 'with file option' do
32
- before do
33
- @filename = Tempfile.new(File.join(Dir.tmpdir, 'foo'), nil)
34
- end
35
-
36
- after do
37
- File.unlink(@filename)
38
- end
39
-
40
- it 'should dump the models to the specified file' do
41
- SeedDump.dump(Sample, file: @filename)
42
-
43
- File.open(@filename) { |file| file.read.should eq(expected_output) }
44
- end
45
-
46
- context 'with append option' do
47
- it 'should append to the file rather than overwriting it' do
48
- SeedDump.dump(Sample, file: @filename)
49
- SeedDump.dump(Sample, file: @filename, append: true)
50
-
51
- File.open(@filename) { |file| file.read.should eq(expected_output + expected_output) }
52
- end
53
- end
54
- end
55
-
56
- context 'ActiveRecord relation' do
57
- it 'should return nil if the count is 0' do
58
- SeedDump.dump(EmptyModel).should be(nil)
59
- end
60
-
61
- context 'with an order parameter' do
62
- it 'should dump the models in the specified order' do
63
- Sample.delete_all
64
- samples = 3.times {|i| FactoryBot.create(:sample, integer: i) }
65
-
66
- SeedDump.dump(Sample.order('integer DESC')).should eq("Sample.create!([\n {string: \"string\", text: \"text\", integer: 2, float: 3.14, decimal: \"2.72\", datetime: \"1776-07-04 19:14:00\", time: \"2000-01-01 03:15:00\", date: \"1863-11-19\", binary: \"binary\", boolean: false},\n {string: \"string\", text: \"text\", integer: 1, float: 3.14, decimal: \"2.72\", datetime: \"1776-07-04 19:14:00\", time: \"2000-01-01 03:15:00\", date: \"1863-11-19\", binary: \"binary\", boolean: false},\n {string: \"string\", text: \"text\", integer: 0, float: 3.14, decimal: \"2.72\", datetime: \"1776-07-04 19:14:00\", time: \"2000-01-01 03:15:00\", date: \"1863-11-19\", binary: \"binary\", boolean: false}\n])\n")
67
- end
68
- end
69
-
70
- context 'without an order parameter' do
71
- it 'should dump the models sorted by primary key ascending' do
72
- SeedDump.dump(Sample).should eq(expected_output)
73
- end
74
- end
75
-
76
- context 'with a limit parameter' do
77
- it 'should dump the number of models specified by the limit when the limit is smaller than the batch size' do
78
- expected_output = "Sample.create!([\n {string: \"string\", text: \"text\", integer: 42, float: 3.14, decimal: \"2.72\", datetime: \"1776-07-04 19:14:00\", time: \"2000-01-01 03:15:00\", date: \"1863-11-19\", binary: \"binary\", boolean: false}\n])\n"
79
-
80
- SeedDump.dump(Sample.limit(1)).should eq(expected_output)
81
- end
82
-
83
- it 'should dump the number of models specified by the limit when the limit is larger than the batch size but not a multiple of the batch size' do
84
- Sample.delete_all
85
- 4.times { FactoryBot.create(:sample) }
86
-
87
- SeedDump.dump(Sample.limit(3), batch_size: 2).should eq(expected_output(false, 3))
88
- end
89
- end
90
- end
91
-
92
- context 'with a batch_size parameter' do
93
- it 'should not raise an exception' do
94
- SeedDump.dump(Sample, batch_size: 100)
95
- end
96
-
97
- it 'should not cause records to not be dumped' do
98
- SeedDump.dump(Sample, batch_size: 2).should eq(expected_output)
99
-
100
- SeedDump.dump(Sample, batch_size: 1).should eq(expected_output)
101
- end
102
- end
103
-
104
- context 'Array' do
105
- it 'should return the dump of the models passed in' do
106
- SeedDump.dump(Sample.all.to_a, batch_size: 2).should eq(expected_output)
107
- end
108
-
109
- it 'should return nil if the array is empty' do
110
- SeedDump.dump([]).should be(nil)
111
- end
112
- end
113
-
114
- context 'with an exclude parameter' do
115
- it 'should exclude the specified attributes from the dump' do
116
- expected_output = "Sample.create!([\n {text: \"text\", integer: 42, decimal: \"2.72\", time: \"2000-01-01 03:15:00\", date: \"1863-11-19\", binary: \"binary\", boolean: false},\n {text: \"text\", integer: 42, decimal: \"2.72\", time: \"2000-01-01 03:15:00\", date: \"1863-11-19\", binary: \"binary\", boolean: false},\n {text: \"text\", integer: 42, decimal: \"2.72\", time: \"2000-01-01 03:15:00\", date: \"1863-11-19\", binary: \"binary\", boolean: false}\n])\n"
117
-
118
- SeedDump.dump(Sample, exclude: [:id, :created_at, :updated_at, :string, :float, :datetime]).should eq(expected_output)
119
- end
120
- end
121
-
122
- context 'Range' do
123
- it 'should dump a class with ranges' do
124
- expected_output = "RangeSample.create!([\n {range_with_end_included: \"[1,3]\", range_with_end_excluded: \"[1,3)\", positive_infinite_range: \"[1,]\", negative_infinite_range: \"[,1]\", infinite_range: \"[,]\"}\n])\n"
125
-
126
- SeedDump.dump([RangeSample.new]).should eq(expected_output)
127
- end
128
- end
129
-
130
- context 'activerecord-import' do
131
- it 'should dump in the activerecord-import format when import is true' do
132
- SeedDump.dump(Sample, import: true, exclude: []).should eq <<-RUBY
133
- Sample.import([:id, :string, :text, :integer, :float, :decimal, :datetime, :time, :date, :binary, :boolean, :created_at, :updated_at], [
134
- [1, "string", "text", 42, 3.14, "2.72", "1776-07-04 19:14:00", "2000-01-01 03:15:00", "1863-11-19", "binary", false, "1969-07-20 20:18:00", "1989-11-10 04:20:00"],
135
- [2, "string", "text", 42, 3.14, "2.72", "1776-07-04 19:14:00", "2000-01-01 03:15:00", "1863-11-19", "binary", false, "1969-07-20 20:18:00", "1989-11-10 04:20:00"],
136
- [3, "string", "text", 42, 3.14, "2.72", "1776-07-04 19:14:00", "2000-01-01 03:15:00", "1863-11-19", "binary", false, "1969-07-20 20:18:00", "1989-11-10 04:20:00"]
137
- ])
138
- RUBY
139
- end
140
-
141
- it 'should omit excluded columns if they are specified' do
142
- SeedDump.dump(Sample, import: true, exclude: [:id, :created_at, :updated_at]).should eq <<-RUBY
143
- Sample.import([:string, :text, :integer, :float, :decimal, :datetime, :time, :date, :binary, :boolean], [
144
- ["string", "text", 42, 3.14, "2.72", "1776-07-04 19:14:00", "2000-01-01 03:15:00", "1863-11-19", "binary", false],
145
- ["string", "text", 42, 3.14, "2.72", "1776-07-04 19:14:00", "2000-01-01 03:15:00", "1863-11-19", "binary", false],
146
- ["string", "text", 42, 3.14, "2.72", "1776-07-04 19:14:00", "2000-01-01 03:15:00", "1863-11-19", "binary", false]
147
- ])
148
- RUBY
149
- end
150
-
151
- context 'should add the params to the output if they are specified' do
152
- it 'should dump in the activerecord-import format when import is true' do
153
- SeedDump.dump(Sample, import: { validate: false }, exclude: []).should eq <<-RUBY
154
- Sample.import([:id, :string, :text, :integer, :float, :decimal, :datetime, :time, :date, :binary, :boolean, :created_at, :updated_at], [
155
- [1, "string", "text", 42, 3.14, "2.72", "1776-07-04 19:14:00", "2000-01-01 03:15:00", "1863-11-19", "binary", false, "1969-07-20 20:18:00", "1989-11-10 04:20:00"],
156
- [2, "string", "text", 42, 3.14, "2.72", "1776-07-04 19:14:00", "2000-01-01 03:15:00", "1863-11-19", "binary", false, "1969-07-20 20:18:00", "1989-11-10 04:20:00"],
157
- [3, "string", "text", 42, 3.14, "2.72", "1776-07-04 19:14:00", "2000-01-01 03:15:00", "1863-11-19", "binary", false, "1969-07-20 20:18:00", "1989-11-10 04:20:00"]
158
- ], validate: false)
159
- RUBY
160
- end
161
- end
162
- end
163
- end
164
- end
165
-
166
- class RangeSample
167
- def attributes
168
- {
169
- "range_with_end_included" => (1..3),
170
- "range_with_end_excluded" => (1...3),
171
- "positive_infinite_range" => (1..Float::INFINITY),
172
- "negative_infinite_range" => (-Float::INFINITY..1),
173
- "infinite_range" => (-Float::INFINITY..Float::INFINITY)
174
- }
175
- end
176
- end
@@ -1,144 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe SeedDump do
4
- describe '.dump_using_environment' do
5
- before(:all) do
6
- create_db
7
- end
8
-
9
- before(:each) do
10
- Rails.application.eager_load!
11
-
12
- FactoryBot.create(:sample)
13
- end
14
-
15
- describe 'APPEND' do
16
- it "should specify append as true if the APPEND env var is 'true'" do
17
- SeedDump.should_receive(:dump).with(anything, include(append: true))
18
-
19
- SeedDump.dump_using_environment('APPEND' => 'true')
20
- end
21
-
22
- it "should specify append as true if the APPEND env var is 'TRUE'" do
23
- SeedDump.should_receive(:dump).with(anything, include(append: true))
24
-
25
- SeedDump.dump_using_environment('APPEND' => 'TRUE')
26
- end
27
-
28
- it "should specify append as false the first time if the APPEND env var is not 'true' (and true after that)" do
29
- FactoryBot.create(:another_sample)
30
-
31
- SeedDump.should_receive(:dump).with(anything, include(append: false)).ordered
32
- SeedDump.should_receive(:dump).with(anything, include(append: true)).ordered
33
-
34
- SeedDump.dump_using_environment('APPEND' => 'false')
35
- end
36
- end
37
-
38
- describe 'BATCH_SIZE' do
39
- it 'should pass along the specified batch size' do
40
- SeedDump.should_receive(:dump).with(anything, include(batch_size: 17))
41
-
42
- SeedDump.dump_using_environment('BATCH_SIZE' => '17')
43
- end
44
-
45
- it 'should pass along a nil batch size if BATCH_SIZE is not specified' do
46
- SeedDump.should_receive(:dump).with(anything, include(batch_size: nil))
47
-
48
- SeedDump.dump_using_environment
49
- end
50
- end
51
-
52
- describe 'EXCLUDE' do
53
- it 'should pass along any attributes to be excluded' do
54
- SeedDump.should_receive(:dump).with(anything, include(exclude: [:baggins, :saggins]))
55
-
56
- SeedDump.dump_using_environment('EXCLUDE' => 'baggins,saggins')
57
- end
58
- end
59
-
60
- describe 'FILE' do
61
- it 'should pass the FILE parameter to the dump method correctly' do
62
- SeedDump.should_receive(:dump).with(anything, include(file: 'blargle'))
63
-
64
- SeedDump.dump_using_environment('FILE' => 'blargle')
65
- end
66
-
67
- it 'should pass db/seeds.rb as the file parameter if no FILE is specified' do
68
- SeedDump.should_receive(:dump).with(anything, include(file: 'db/seeds.rb'))
69
-
70
- SeedDump.dump_using_environment
71
- end
72
- end
73
-
74
- describe 'LIMIT' do
75
- it 'should apply the specified limit to the records' do
76
- relation_double = double('ActiveRecord relation double')
77
- Sample.should_receive(:limit).with(5).and_return(relation_double)
78
-
79
- SeedDump.should_receive(:dump).with(relation_double, anything)
80
- SeedDump.stub(:dump)
81
-
82
- SeedDump.dump_using_environment('LIMIT' => '5')
83
- end
84
- end
85
-
86
- ['', 'S'].each do |model_suffix|
87
- model_env = 'MODEL' + model_suffix
88
-
89
- describe model_env do
90
- context "if #{model_env} is not specified" do
91
- it "should dump all non-empty models" do
92
- FactoryBot.create(:another_sample)
93
-
94
- [Sample, AnotherSample].each do |model|
95
- SeedDump.should_receive(:dump).with(model, anything)
96
- end
97
-
98
- SeedDump.dump_using_environment
99
- end
100
- end
101
-
102
- context "if #{model_env} is specified" do
103
- it "should dump only the specified model" do
104
- FactoryBot.create(:another_sample)
105
-
106
- SeedDump.should_receive(:dump).with(Sample, anything)
107
-
108
- SeedDump.dump_using_environment(model_env => 'Sample')
109
- end
110
-
111
- it "should not dump empty models" do
112
- SeedDump.should_not_receive(:dump).with(EmptyModel, anything)
113
-
114
- SeedDump.dump_using_environment(model_env => 'EmptyModel, Sample')
115
- end
116
- end
117
- end
118
- end
119
-
120
- describe "MODELS_EXCLUDE" do
121
- it "should dump all non-empty models except the specified models" do
122
- FactoryBot.create(:another_sample)
123
-
124
- SeedDump.should_receive(:dump).with(Sample, anything)
125
-
126
- SeedDump.dump_using_environment('MODELS_EXCLUDE' => 'AnotherSample')
127
- end
128
- end
129
-
130
- it 'should run ok without ActiveRecord::SchemaMigration being set (needed for Rails Engines)' do
131
- schema_migration = ActiveRecord::SchemaMigration
132
-
133
- ActiveRecord.send(:remove_const, :SchemaMigration)
134
-
135
- SeedDump.stub(:dump)
136
-
137
- begin
138
- SeedDump.dump_using_environment
139
- ensure
140
- ActiveRecord.const_set(:SchemaMigration, schema_migration)
141
- end
142
- end
143
- end
144
- end
@@ -1,14 +0,0 @@
1
- FactoryBot.define do
2
- factory :another_sample do
3
- string 'string'
4
- text 'text'
5
- integer 42
6
- float 3.14
7
- decimal 2.72
8
- datetime DateTime.parse('July 4, 1776 7:14pm UTC')
9
- time Time.parse('3:15am UTC')
10
- date Date.parse('November 19, 1863')
11
- binary 'binary'
12
- boolean false
13
- end
14
- end
@@ -1,16 +0,0 @@
1
- FactoryBot.define do
2
- factory :sample do
3
- string 'string'
4
- text 'text'
5
- integer 42
6
- float 3.14
7
- decimal 2.72
8
- datetime DateTime.parse('July 4, 1776 7:14pm UTC')
9
- time Time.parse('3:15am UTC')
10
- date Date.parse('November 19, 1863')
11
- binary 'binary'
12
- boolean false
13
- created_at DateTime.parse('July 20, 1969 20:18 UTC')
14
- updated_at DateTime.parse('November 10, 1989 4:20 UTC')
15
- end
16
- end
@@ -1,14 +0,0 @@
1
- FactoryBot.define do
2
- factory :yet_another_sample do
3
- string 'string'
4
- text 'text'
5
- integer 42
6
- float 3.14
7
- decimal 2.72
8
- datetime DateTime.parse('July 4, 1776 7:14pm UTC')
9
- time Time.parse('3:15am UTC')
10
- date Date.parse('November 19, 1863')
11
- binary 'binary'
12
- boolean false
13
- end
14
- end
data/spec/helpers.rb DELETED
@@ -1,92 +0,0 @@
1
- # Mock Rails.application.eager_load! and define some
2
- # Rails models for use in specs.
3
- class Rails
4
- def self.application
5
- self
6
- end
7
-
8
- def self.eager_load!
9
- @already_called ||= false
10
-
11
- if !@already_called
12
- Object.const_set('Sample', Class.new(ActiveRecord::Base))
13
-
14
- Object.const_set('AnotherSample', Class.new(ActiveRecord::Base))
15
-
16
- Object.const_set('YetAnotherSample', Class.new(ActiveRecord::Base))
17
-
18
- Object.const_set('NoTableModel', Class.new(ActiveRecord::Base))
19
-
20
- Object.const_set('EmptyModel', Class.new(ActiveRecord::Base))
21
-
22
- @already_called = true
23
- end
24
- end
25
-
26
- def self.env
27
- 'test'
28
- end
29
- end
30
-
31
- module Helpers
32
- def create_db
33
- ActiveRecord::Migration.verbose = false
34
-
35
- ActiveRecord::Schema.define(:version => 1) do
36
- create_table 'samples', :force => true do |t|
37
- t.string 'string'
38
- t.text 'text'
39
- t.integer 'integer'
40
- t.float 'float'
41
- t.decimal 'decimal'
42
- t.datetime 'datetime'
43
- t.time 'time'
44
- t.date 'date'
45
- t.binary 'binary'
46
- t.boolean 'boolean'
47
- t.datetime 'created_at', :null => false
48
- t.datetime 'updated_at', :null => false
49
- end
50
-
51
- create_table 'another_samples', :force => true do |t|
52
- t.string 'string'
53
- t.text 'text'
54
- t.integer 'integer'
55
- t.float 'float'
56
- t.decimal 'decimal'
57
- t.datetime 'datetime'
58
- t.time 'time'
59
- t.date 'date'
60
- t.binary 'binary'
61
- t.boolean 'boolean'
62
- t.datetime 'created_at', :null => false
63
- t.datetime 'updated_at', :null => false
64
- end
65
-
66
- create_table 'yet_another_samples', :force => true do |t|
67
- t.string 'string'
68
- t.text 'text'
69
- t.integer 'integer'
70
- t.float 'float'
71
- t.decimal 'decimal'
72
- t.datetime 'datetime'
73
- t.time 'time'
74
- t.date 'date'
75
- t.binary 'binary'
76
- t.boolean 'boolean'
77
- t.datetime 'created_at', :null => false
78
- t.datetime 'updated_at', :null => false
79
- end
80
-
81
- create_table 'empty_models', force: true
82
- end
83
- end
84
-
85
- def load_sample_data
86
- Rails.application.eager_load!
87
-
88
- Sample.create!
89
-
90
- ChildSample.create!
91
- end
92
- end
data/spec/spec_helper.rb DELETED
@@ -1,36 +0,0 @@
1
- require 'seed_dump'
2
-
3
- require 'active_support'
4
- require 'active_record'
5
-
6
- require 'tempfile'
7
-
8
- require 'byebug'
9
-
10
- require 'database_cleaner'
11
- require 'factory_bot'
12
-
13
- require './spec/helpers'
14
-
15
- ActiveRecord::Base.establish_connection(:adapter => 'sqlite3', :database => ':memory:')
16
-
17
- FactoryBot.find_definitions
18
-
19
- RSpec.configure do |config|
20
- config.order = 'random'
21
-
22
- config.include Helpers
23
-
24
- config.before(:suite) do
25
- DatabaseCleaner.strategy = :transaction
26
- DatabaseCleaner.clean_with(:truncation)
27
- end
28
-
29
- config.before(:each) do
30
- DatabaseCleaner.start
31
- end
32
-
33
- config.after(:each) do
34
- DatabaseCleaner.clean
35
- end
36
- end