seed_dump 0.5.3 → 0.6.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.
- checksums.yaml +7 -0
- data/README.rdoc +7 -2
- data/VERSION +1 -1
- data/lib/seed_dump/perform.rb +27 -14
- data/seed_dump.gemspec +3 -12
- data/test/seed_dump_test.rb +14 -13
- metadata +8 -10
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 7bb4e4606e31915e6334725645e3d7f7f0238309
|
4
|
+
data.tar.gz: 509dbc5e4adc981ab4333fff1162067c3889b12b
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 53da1240fc843bef8a697ca3e8d6d0f02af2830cbc3646fe7920e57440b37af2532e163694b0810bfc4eb9e35cfdb31edf561c8170a64340d5526f2aabba249f
|
7
|
+
data.tar.gz: c2ae668cd7f797c3ab5a5bf66ad2c899697e96d6d3e2780f9b76ac7c82566ba81a0a371cbf87d94dbce4d7756eaef55c2ec143b8ff42ed94c24a7acb32700bbd
|
data/README.rdoc
CHANGED
@@ -1,12 +1,14 @@
|
|
1
1
|
= SeedDump
|
2
2
|
|
3
|
-
Seed dump is a
|
3
|
+
Seed dump is a Rails plugin that adds a rake task named db:seed:dump.
|
4
4
|
|
5
5
|
It allows you to create a db/seeds.rb from your existing data in the database.
|
6
6
|
When there is no data in the database it will generate empty create statements.
|
7
7
|
|
8
8
|
It mainly exists for people who are too lazy writing create statements in db/seeds.rb themselves
|
9
|
-
and need something
|
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.
|
10
12
|
|
11
13
|
== Example Usage
|
12
14
|
|
@@ -64,6 +66,9 @@ Here is a full example using all of the options above:
|
|
64
66
|
|
65
67
|
== All environment variables
|
66
68
|
|
69
|
+
RAILS4:
|
70
|
+
Specify as "true" for Rails 4 compatibility.
|
71
|
+
|
67
72
|
APPEND:
|
68
73
|
Append the data to db/seeds.rb instead of overwriting it.
|
69
74
|
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.6.0
|
data/lib/seed_dump/perform.rb
CHANGED
@@ -34,9 +34,19 @@ module SeedDump
|
|
34
34
|
@opts['schema'] = env['PG_SCHEMA']
|
35
35
|
@opts['model_dir'] = env['MODEL_DIR'] || @model_dir
|
36
36
|
@opts['create_method'] = env['CREATE_METHOD'] || 'create'
|
37
|
+
@opts['rails4'] = env['RAILS4'].true?
|
38
|
+
monkeypatch_AR if @opts['rails4']
|
37
39
|
end
|
38
40
|
|
39
|
-
def
|
41
|
+
def monkeypatch_AR
|
42
|
+
ActiveRecord::Base.instance_eval do
|
43
|
+
def attr_accessible(*opts)
|
44
|
+
nil
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
def load_models
|
40
50
|
puts "Searching in #{@opts['model_dir']} for models" if @opts['debug']
|
41
51
|
Dir[Dir.pwd + '/' + @opts['model_dir']].sort.each do |f|
|
42
52
|
puts "Processing file #{f}" if @opts['debug']
|
@@ -70,7 +80,8 @@ module SeedDump
|
|
70
80
|
@last_record
|
71
81
|
end
|
72
82
|
|
73
|
-
def
|
83
|
+
def dump_attribute(a_s, r, k, v)
|
84
|
+
pushed = false
|
74
85
|
if v.is_a?(BigDecimal)
|
75
86
|
v = v.to_s
|
76
87
|
else
|
@@ -80,11 +91,13 @@ module SeedDump
|
|
80
91
|
unless k == 'id' && !@opts['with_id']
|
81
92
|
if (!(k == 'created_at' || k == 'updated_at') || @opts['timestamps'])
|
82
93
|
a_s.push("#{k.to_sym.inspect} => #{v}")
|
94
|
+
pushed = true
|
83
95
|
end
|
84
96
|
end
|
97
|
+
pushed
|
85
98
|
end
|
86
99
|
|
87
|
-
def
|
100
|
+
def dump_model(model)
|
88
101
|
@id_set_string = ''
|
89
102
|
@last_record = []
|
90
103
|
create_hash = ""
|
@@ -97,9 +110,9 @@ module SeedDump
|
|
97
110
|
arr.each_with_index { |r,i|
|
98
111
|
attr_s = [];
|
99
112
|
r.attributes.each do |k,v|
|
100
|
-
if ((model.attr_accessible[:default].include? k) || @opts['without_protection'] || @opts['with_id'])
|
101
|
-
|
102
|
-
@last_record.push k
|
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
|
103
116
|
end
|
104
117
|
end
|
105
118
|
rows.push "#{@indent}{ " << attr_s.join(', ') << " }"
|
@@ -122,7 +135,7 @@ module SeedDump
|
|
122
135
|
|
123
136
|
end
|
124
137
|
|
125
|
-
def
|
138
|
+
def dump_models
|
126
139
|
@seed_rb = ""
|
127
140
|
@models.sort.each do |model|
|
128
141
|
m = model.constantize
|
@@ -135,14 +148,14 @@ module SeedDump
|
|
135
148
|
puts "Callbacks are disabled." if @opts['verbose']
|
136
149
|
end
|
137
150
|
|
138
|
-
@seed_rb <<
|
151
|
+
@seed_rb << dump_model(m) << "\n\n"
|
139
152
|
else
|
140
153
|
puts "Skipping non-ActiveRecord model #{model}..." if @opts['verbose']
|
141
154
|
end
|
142
155
|
end
|
143
156
|
end
|
144
157
|
|
145
|
-
def
|
158
|
+
def write_file
|
146
159
|
File.open(@opts['file'], (@opts['append'] ? "a" : "w")) { |f|
|
147
160
|
f << "# encoding: utf-8\n"
|
148
161
|
f << "# Autogenerated by the db:seed:dump task\n# Do not hesitate to tweak this to your needs\n" unless @opts['append']
|
@@ -163,7 +176,7 @@ module SeedDump
|
|
163
176
|
end
|
164
177
|
end
|
165
178
|
|
166
|
-
def
|
179
|
+
def set_search_path(path, append_public=true)
|
167
180
|
path_parts = [path.to_s, ('public' if append_public)].compact
|
168
181
|
ActiveRecord::Base.connection.schema_search_path = path_parts.join(',')
|
169
182
|
end
|
@@ -174,15 +187,15 @@ module SeedDump
|
|
174
187
|
|
175
188
|
puts "Protection is disabled." if @opts['verbose'] && @opts['without_protection']
|
176
189
|
|
177
|
-
|
190
|
+
set_search_path @opts['schema'] if @opts['schema']
|
178
191
|
|
179
|
-
|
192
|
+
load_models
|
180
193
|
|
181
194
|
puts "Appending seeds to #{@opts['file']}." if @opts['append']
|
182
|
-
|
195
|
+
dump_models
|
183
196
|
|
184
197
|
puts "Writing #{@opts['file']}."
|
185
|
-
|
198
|
+
write_file
|
186
199
|
|
187
200
|
puts "Done."
|
188
201
|
end
|
data/seed_dump.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = "seed_dump"
|
8
|
-
s.version = "0.
|
8
|
+
s.version = "0.6.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-
|
12
|
+
s.date = "2013-08-01"
|
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 = [
|
@@ -38,16 +38,7 @@ Gem::Specification.new do |s|
|
|
38
38
|
]
|
39
39
|
s.homepage = "https://github.com/rroblak/seed_dump"
|
40
40
|
s.require_paths = ["lib"]
|
41
|
-
s.rubygems_version = "
|
41
|
+
s.rubygems_version = "2.0.3"
|
42
42
|
s.summary = "{Seed Dumper for Rails}"
|
43
|
-
|
44
|
-
if s.respond_to? :specification_version then
|
45
|
-
s.specification_version = 3
|
46
|
-
|
47
|
-
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
48
|
-
else
|
49
|
-
end
|
50
|
-
else
|
51
|
-
end
|
52
43
|
end
|
53
44
|
|
data/test/seed_dump_test.rb
CHANGED
@@ -10,21 +10,22 @@ class SeedDumpTest < ActiveSupport::TestCase
|
|
10
10
|
"MODEL_DIR" => 'test/models/**.rb',
|
11
11
|
"FILE" => Dir.pwd + '/test/db/seeds.rb',
|
12
12
|
"VERBOSE" => false,
|
13
|
-
"DEBUG" => false
|
13
|
+
"DEBUG" => false,
|
14
|
+
"RAILS4" => ENV['RAILS4']
|
14
15
|
}
|
15
16
|
end
|
16
17
|
|
17
18
|
test "load sample model" do
|
18
19
|
@env['MODEL_DIR'] = 'test/models/*.rb'
|
19
20
|
@sd.setup @env
|
20
|
-
@sd.
|
21
|
+
@sd.load_models
|
21
22
|
assert_equal ["AbstractSample", "ChildSample", "Sample"], @sd.models
|
22
23
|
end
|
23
24
|
|
24
25
|
test "support nested models" do
|
25
26
|
@env['MODEL_DIR'] = 'test/models/**/*.rb'
|
26
27
|
@sd.setup @env
|
27
|
-
@sd.
|
28
|
+
@sd.load_models
|
28
29
|
assert_equal ["AbstractSample", "ChildSample", "Nested::Sample", "Sample"], @sd.models
|
29
30
|
end
|
30
31
|
|
@@ -32,8 +33,8 @@ class SeedDumpTest < ActiveSupport::TestCase
|
|
32
33
|
@env['MODEL_DIR'] = 'test/models/*.rb'
|
33
34
|
@env['TIMESTAMPS'] = false
|
34
35
|
@sd.setup @env
|
35
|
-
@sd.
|
36
|
-
@sd.
|
36
|
+
@sd.load_models
|
37
|
+
@sd.dump_models
|
37
38
|
assert !@sd.last_record.include?("created_at"), "Should not include created_at if timestamps are off"
|
38
39
|
end
|
39
40
|
|
@@ -41,8 +42,8 @@ class SeedDumpTest < ActiveSupport::TestCase
|
|
41
42
|
@env['MODEL_DIR'] = 'test/models/*.rb'
|
42
43
|
@env['TIMESTAMPS'] = true
|
43
44
|
@sd.setup @env
|
44
|
-
@sd.
|
45
|
-
@sd.
|
45
|
+
@sd.load_models
|
46
|
+
@sd.dump_models
|
46
47
|
assert @sd.last_record.include?("created_at"), "Must include created_at if timestamps are desired"
|
47
48
|
end
|
48
49
|
|
@@ -50,8 +51,8 @@ class SeedDumpTest < ActiveSupport::TestCase
|
|
50
51
|
@env['MODEL_DIR'] = 'test/models/*.rb'
|
51
52
|
@env['WITH_ID'] = true
|
52
53
|
@sd.setup @env
|
53
|
-
@sd.
|
54
|
-
@sd.
|
54
|
+
@sd.load_models
|
55
|
+
@sd.dump_models
|
55
56
|
assert @sd.last_record.include?("id"), "WITH_ID must include id"
|
56
57
|
end
|
57
58
|
|
@@ -60,8 +61,8 @@ class SeedDumpTest < ActiveSupport::TestCase
|
|
60
61
|
@env['MODEL_DIR'] = 'test/models/*.rb'
|
61
62
|
@env['TIMESTAMPS'] = false
|
62
63
|
@sd.setup @env
|
63
|
-
@sd.
|
64
|
-
@sd.
|
64
|
+
@sd.load_models
|
65
|
+
@sd.dump_models
|
65
66
|
assert_equal [], @sd.last_record
|
66
67
|
end
|
67
68
|
|
@@ -69,8 +70,8 @@ class SeedDumpTest < ActiveSupport::TestCase
|
|
69
70
|
@env['MODEL_DIR'] = 'test/models/*.rb'
|
70
71
|
@env['CREATE_METHOD'] = 'create!'
|
71
72
|
@sd.setup @env
|
72
|
-
@sd.
|
73
|
-
@sd.
|
73
|
+
@sd.load_models
|
74
|
+
@sd.dump_models
|
74
75
|
assert @sd.instance_variable_get(:@seed_rb) =~ /create!/, 'CREATE_METHOD must specify the creation method'
|
75
76
|
end
|
76
77
|
end
|
metadata
CHANGED
@@ -1,8 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: seed_dump
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
5
|
-
prerelease:
|
4
|
+
version: 0.6.0
|
6
5
|
platform: ruby
|
7
6
|
authors:
|
8
7
|
- Rob Halff
|
@@ -10,7 +9,7 @@ authors:
|
|
10
9
|
autorequire:
|
11
10
|
bindir: bin
|
12
11
|
cert_chain: []
|
13
|
-
date: 2013-
|
12
|
+
date: 2013-08-01 00:00:00.000000000 Z
|
14
13
|
dependencies: []
|
15
14
|
description: Dump (parts) of your database to db/seeds.rb to get a headstart creating
|
16
15
|
a meaningful seeds.rb file
|
@@ -41,26 +40,25 @@ files:
|
|
41
40
|
- test/test_helper.rb
|
42
41
|
homepage: https://github.com/rroblak/seed_dump
|
43
42
|
licenses: []
|
43
|
+
metadata: {}
|
44
44
|
post_install_message:
|
45
45
|
rdoc_options: []
|
46
46
|
require_paths:
|
47
47
|
- lib
|
48
48
|
required_ruby_version: !ruby/object:Gem::Requirement
|
49
|
-
none: false
|
50
49
|
requirements:
|
51
|
-
- -
|
50
|
+
- - '>='
|
52
51
|
- !ruby/object:Gem::Version
|
53
52
|
version: '0'
|
54
53
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
55
|
-
none: false
|
56
54
|
requirements:
|
57
|
-
- -
|
55
|
+
- - '>='
|
58
56
|
- !ruby/object:Gem::Version
|
59
57
|
version: '0'
|
60
58
|
requirements: []
|
61
59
|
rubyforge_project:
|
62
|
-
rubygems_version:
|
60
|
+
rubygems_version: 2.0.3
|
63
61
|
signing_key:
|
64
|
-
specification_version:
|
65
|
-
summary:
|
62
|
+
specification_version: 4
|
63
|
+
summary: '{Seed Dumper for Rails}'
|
66
64
|
test_files: []
|