seed-do 3.1.0 → 3.2.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 +4 -4
- data/lib/seed-do/active_record_extension.rb +13 -13
- data/lib/seed-do/block_hash.rb +1 -1
- data/lib/seed-do/capistrano.rb +2 -2
- data/lib/seed-do/capistrano3.rb +1 -1
- data/lib/seed-do/railtie.rb +1 -1
- data/lib/seed-do/runner.rb +27 -31
- data/lib/seed-do/seeder.rb +40 -41
- data/lib/seed-do/version.rb +1 -1
- data/lib/seed-do/writer.rb +49 -47
- data/lib/seed-do.rb +4 -8
- data/lib/tasks/seed_do.rake +4 -8
- metadata +3 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: f41a0a75dff7954f90a7ed6cafe60eef73f0c20490ff9e42952b34e2efe896bc
|
|
4
|
+
data.tar.gz: dd04a94f944a7a290d211b16cec2537a3fa34bc2816cee6c1b594d22a91a907c
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: ee3af13c18e0354bc04a378db5d0fc6ef344e2af3c617cc5405153200dd37d316be5e31d07234e077719e875c0daa6098878907146c27924ccb2021c9ba36725
|
|
7
|
+
data.tar.gz: 36367e0c2457b3d18781824ef255095545b7c441c37468c28c2448413877656c5180edca15da8f8d7309cc2bda78d4d9e2e0e7066afdfe607ec0ad71dc89fd89
|
|
@@ -41,25 +41,25 @@ module SeedDo
|
|
|
41
41
|
# Person.seed_once(:id, :id => 1, :name => "Harry") # => Name *not* changed
|
|
42
42
|
def seed_once(*args, &block)
|
|
43
43
|
constraints, data = parse_seed_do_args(args, block)
|
|
44
|
-
SeedDo::Seeder.new(self, constraints, data, :
|
|
44
|
+
SeedDo::Seeder.new(self, constraints, data, insert_only: true).seed
|
|
45
45
|
end
|
|
46
46
|
|
|
47
47
|
private
|
|
48
48
|
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
else
|
|
56
|
-
# Partition the args, assuming the first hash is the start of the data
|
|
57
|
-
args.partition { |arg| !arg.is_a?(Hash) }
|
|
58
|
-
end
|
|
49
|
+
def parse_seed_do_args(args, block)
|
|
50
|
+
if block.nil?
|
|
51
|
+
if args.last.is_a?(Array)
|
|
52
|
+
# Last arg is an array of data, so assume the rest of the args are constraints
|
|
53
|
+
data = args.pop
|
|
54
|
+
[args, data]
|
|
59
55
|
else
|
|
60
|
-
#
|
|
61
|
-
|
|
56
|
+
# Partition the args, assuming the first hash is the start of the data
|
|
57
|
+
args.partition { |arg| !arg.is_a?(Hash) }
|
|
62
58
|
end
|
|
59
|
+
else
|
|
60
|
+
# We have a block, so assume the args are all constraints
|
|
61
|
+
[args, [SeedDo::BlockHash.new(block).to_hash]]
|
|
63
62
|
end
|
|
63
|
+
end
|
|
64
64
|
end
|
|
65
65
|
end
|
data/lib/seed-do/block_hash.rb
CHANGED
data/lib/seed-do/capistrano.rb
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
Capistrano::Configuration.instance.load do
|
|
2
2
|
namespace :db do
|
|
3
|
-
desc
|
|
4
|
-
task :seed_do, :
|
|
3
|
+
desc 'Load seed data into database'
|
|
4
|
+
task :seed_do, roles: :db, only: { primary: true } do
|
|
5
5
|
run "cd #{release_path} && bundle exec rake RAILS_ENV=#{rails_env} db:seed_do"
|
|
6
6
|
end
|
|
7
7
|
end
|
data/lib/seed-do/capistrano3.rb
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
load File.expand_path('
|
|
1
|
+
load File.expand_path('../tasks/seed_do_capistrano3.rake', __dir__)
|
data/lib/seed-do/railtie.rb
CHANGED
data/lib/seed-do/runner.rb
CHANGED
|
@@ -29,45 +29,41 @@ module SeedDo
|
|
|
29
29
|
|
|
30
30
|
private
|
|
31
31
|
|
|
32
|
-
|
|
33
|
-
|
|
32
|
+
def run_file(filename)
|
|
33
|
+
puts "\n== Seed from #{filename}" unless SeedDo.quiet
|
|
34
34
|
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
end
|
|
35
|
+
ActiveRecord::Base.transaction do
|
|
36
|
+
open(filename) do |file|
|
|
37
|
+
chunked_ruby = +''
|
|
38
|
+
file.each_line do |line|
|
|
39
|
+
if line == "# BREAK EVAL\n"
|
|
40
|
+
eval(chunked_ruby)
|
|
41
|
+
chunked_ruby = +''
|
|
42
|
+
else
|
|
43
|
+
chunked_ruby << line
|
|
45
44
|
end
|
|
46
|
-
eval(chunked_ruby) unless chunked_ruby == ''
|
|
47
45
|
end
|
|
46
|
+
eval(chunked_ruby) unless chunked_ruby == ''
|
|
48
47
|
end
|
|
49
48
|
end
|
|
49
|
+
end
|
|
50
50
|
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
else
|
|
57
|
-
File.open(filename) do |file|
|
|
58
|
-
yield file
|
|
59
|
-
end
|
|
60
|
-
end
|
|
51
|
+
def open(filename, &)
|
|
52
|
+
if filename[-3..-1] == '.gz'
|
|
53
|
+
Zlib::GzipReader.open(filename, &)
|
|
54
|
+
else
|
|
55
|
+
File.open(filename, &)
|
|
61
56
|
end
|
|
57
|
+
end
|
|
62
58
|
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
end
|
|
68
|
-
filenames.uniq!
|
|
69
|
-
filenames = filenames.find_all { |filename| filename =~ @filter } if @filter
|
|
70
|
-
filenames
|
|
59
|
+
def filenames
|
|
60
|
+
filenames = []
|
|
61
|
+
@fixture_paths.each do |path|
|
|
62
|
+
filenames += (Dir[File.join(path, '*.rb')] + Dir[File.join(path, '*.rb.gz')]).sort
|
|
71
63
|
end
|
|
64
|
+
filenames.uniq!
|
|
65
|
+
filenames = filenames.find_all { |filename| filename =~ @filter } if @filter
|
|
66
|
+
filenames
|
|
67
|
+
end
|
|
72
68
|
end
|
|
73
69
|
end
|
data/lib/seed-do/seeder.rb
CHANGED
|
@@ -41,62 +41,61 @@ module SeedDo
|
|
|
41
41
|
|
|
42
42
|
private
|
|
43
43
|
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
44
|
+
def validate_constraints!
|
|
45
|
+
unknown_columns = @constraints.map(&:to_s) - @model_class.column_names
|
|
46
|
+
return if unknown_columns.empty?
|
|
47
|
+
|
|
48
|
+
raise(ArgumentError,
|
|
48
49
|
"Your seed constraints contained unknown columns: #{column_list(unknown_columns)}. " +
|
|
49
50
|
"Valid columns are: #{column_list(@model_class.column_names)}.")
|
|
50
|
-
|
|
51
|
-
end
|
|
51
|
+
end
|
|
52
52
|
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
53
|
+
def validate_data!
|
|
54
|
+
raise ArgumentError, 'Seed data missing' if @data.empty?
|
|
55
|
+
end
|
|
56
56
|
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
57
|
+
def column_list(columns)
|
|
58
|
+
'`' + columns.join('`, `') + '`'
|
|
59
|
+
end
|
|
60
60
|
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
61
|
+
def seed_record(data)
|
|
62
|
+
record = find_or_initialize_record(data)
|
|
63
|
+
return if @options[:insert_only] && !record.new_record?
|
|
64
64
|
|
|
65
|
-
|
|
65
|
+
puts " - #{@model_class} #{data.inspect}" unless @options[:quiet]
|
|
66
66
|
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
67
|
+
record.assign_attributes(data)
|
|
68
|
+
record.save(validate: false) || raise(ActiveRecord::RecordNotSaved, 'Record not saved!')
|
|
69
|
+
record
|
|
70
|
+
end
|
|
71
71
|
|
|
72
|
-
|
|
73
|
-
|
|
72
|
+
def find_or_initialize_record(data)
|
|
73
|
+
@model_class.where(constraint_conditions(data)).take ||
|
|
74
74
|
@model_class.new
|
|
75
|
-
|
|
75
|
+
end
|
|
76
76
|
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
77
|
+
def constraint_conditions(data)
|
|
78
|
+
@constraints.to_h { |c| [c, data[c.to_sym]] }
|
|
79
|
+
end
|
|
80
80
|
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
81
|
+
def update_id_sequence
|
|
82
|
+
return unless %w[PostgreSQL PostGIS].include?(@model_class.connection.adapter_name)
|
|
83
|
+
return if @model_class.primary_key.nil? || @model_class.sequence_name.nil?
|
|
84
84
|
|
|
85
|
-
|
|
86
|
-
|
|
85
|
+
quoted_id = @model_class.connection.quote_column_name(@model_class.primary_key)
|
|
86
|
+
sequence = @model_class.sequence_name
|
|
87
87
|
|
|
88
|
-
|
|
89
|
-
|
|
88
|
+
if @model_class.connection.postgresql_version >= 100_000
|
|
89
|
+
sql = <<-EOS
|
|
90
90
|
SELECT setval('#{sequence}', (SELECT GREATEST(MAX(#{quoted_id})+(SELECT seqincrement FROM pg_sequence WHERE seqrelid = '#{sequence}'::regclass), (SELECT seqmin FROM pg_sequence WHERE seqrelid = '#{sequence}'::regclass)) FROM #{@model_class.quoted_table_name}), false)
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
91
|
+
EOS
|
|
92
|
+
else
|
|
93
|
+
sql = <<-EOS
|
|
94
94
|
SELECT setval('#{sequence}', (SELECT GREATEST(MAX(#{quoted_id})+(SELECT increment_by FROM #{sequence}), (SELECT min_value FROM #{sequence})) FROM #{@model_class.quoted_table_name}), false)
|
|
95
|
-
|
|
96
|
-
end
|
|
97
|
-
|
|
98
|
-
@model_class.connection.execute sql
|
|
99
|
-
end
|
|
95
|
+
EOS
|
|
100
96
|
end
|
|
97
|
+
|
|
98
|
+
@model_class.connection.execute sql
|
|
99
|
+
end
|
|
101
100
|
end
|
|
102
101
|
end
|
data/lib/seed-do/version.rb
CHANGED
data/lib/seed-do/writer.rb
CHANGED
|
@@ -18,9 +18,9 @@ module SeedDo
|
|
|
18
18
|
class Writer
|
|
19
19
|
cattr_accessor :default_options
|
|
20
20
|
@@default_options = {
|
|
21
|
-
:
|
|
22
|
-
:
|
|
23
|
-
:
|
|
21
|
+
chunk_size: 100,
|
|
22
|
+
constraints: [:id],
|
|
23
|
+
seed_type: :seed
|
|
24
24
|
}
|
|
25
25
|
|
|
26
26
|
# @param [Hash] options
|
|
@@ -33,13 +33,13 @@ module SeedDo
|
|
|
33
33
|
# @option options [Array<Symbol>] :constraints ([:id]) The constraining attributes for the seeds
|
|
34
34
|
def initialize(options = {})
|
|
35
35
|
@options = self.class.default_options.merge(options)
|
|
36
|
-
raise ArgumentError,
|
|
36
|
+
raise ArgumentError, 'missing option :class_name' unless @options[:class_name]
|
|
37
37
|
end
|
|
38
38
|
|
|
39
39
|
# Creates a new instance of {Writer} with the `options`, and then calls {#write} with the
|
|
40
40
|
# `io_or_filename` and `block`
|
|
41
|
-
def self.write(io_or_filename, options = {}, &
|
|
42
|
-
new(options).write(io_or_filename, &
|
|
41
|
+
def self.write(io_or_filename, options = {}, &)
|
|
42
|
+
new(options).write(io_or_filename, &)
|
|
43
43
|
end
|
|
44
44
|
|
|
45
45
|
# Writes the necessary headers and footers, and yields to a block within which the actual
|
|
@@ -51,7 +51,7 @@ module SeedDo
|
|
|
51
51
|
# closed automatically.)
|
|
52
52
|
# @yield [self] make calls to `#<<` within the block
|
|
53
53
|
def write(io_or_filename, &block)
|
|
54
|
-
raise ArgumentError,
|
|
54
|
+
raise ArgumentError, 'missing block' unless block_given?
|
|
55
55
|
|
|
56
56
|
if io_or_filename.respond_to?(:write)
|
|
57
57
|
write_to_io(io_or_filename, &block)
|
|
@@ -65,7 +65,7 @@ module SeedDo
|
|
|
65
65
|
# Add a seed. Must be called within a block passed to {#write}.
|
|
66
66
|
# @param [Hash] seed The attributes for the seed
|
|
67
67
|
def <<(seed)
|
|
68
|
-
raise
|
|
68
|
+
raise 'You must add seeds inside a SeedDo::Writer#write block' unless @io
|
|
69
69
|
|
|
70
70
|
buffer = +''
|
|
71
71
|
|
|
@@ -76,57 +76,59 @@ module SeedDo
|
|
|
76
76
|
end
|
|
77
77
|
|
|
78
78
|
buffer << ",\n"
|
|
79
|
-
buffer << ' ' + seed.inspect
|
|
79
|
+
buffer << (' ' + seed.inspect)
|
|
80
80
|
|
|
81
81
|
@io.write(buffer)
|
|
82
82
|
|
|
83
83
|
@count += 1
|
|
84
84
|
end
|
|
85
|
-
|
|
85
|
+
alias add <<
|
|
86
86
|
|
|
87
87
|
private
|
|
88
88
|
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
89
|
+
def write_to_io(io)
|
|
90
|
+
@io = io
|
|
91
|
+
@count = 0
|
|
92
|
+
@io.write(file_header)
|
|
93
|
+
@io.write(seed_header)
|
|
94
|
+
yield(self)
|
|
95
|
+
@io.write(seed_footer)
|
|
96
|
+
@io.write(file_footer)
|
|
97
|
+
ensure
|
|
98
|
+
@io = nil
|
|
99
|
+
@count = nil
|
|
100
|
+
end
|
|
99
101
|
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
# DO NOT MODIFY THIS FILE, it was auto-generated.
|
|
103
|
-
#
|
|
104
|
-
# Date: #{Time.now}
|
|
105
|
-
# Seeding #{@options[:class_name]}
|
|
106
|
-
# Written with the command:
|
|
107
|
-
#
|
|
108
|
-
# #{$0} #{$*.join}
|
|
109
|
-
#
|
|
110
|
-
|
|
111
|
-
|
|
102
|
+
def file_header
|
|
103
|
+
<<~END
|
|
104
|
+
# DO NOT MODIFY THIS FILE, it was auto-generated.
|
|
105
|
+
#
|
|
106
|
+
# Date: #{Time.now}
|
|
107
|
+
# Seeding #{@options[:class_name]}
|
|
108
|
+
# Written with the command:
|
|
109
|
+
#
|
|
110
|
+
# #{$0} #{$*.join}
|
|
111
|
+
#
|
|
112
|
+
END
|
|
113
|
+
end
|
|
112
114
|
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
# End auto-generated file.
|
|
116
|
-
|
|
117
|
-
|
|
115
|
+
def file_footer
|
|
116
|
+
<<~END
|
|
117
|
+
# End auto-generated file.
|
|
118
|
+
END
|
|
119
|
+
end
|
|
118
120
|
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
121
|
+
def seed_header
|
|
122
|
+
constraints = @options[:constraints] && @options[:constraints].map(&:inspect).join(', ')
|
|
123
|
+
"#{@options[:class_name]}.#{@options[:seed_type]}(#{constraints}"
|
|
124
|
+
end
|
|
123
125
|
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
126
|
+
def seed_footer
|
|
127
|
+
"\n)\n"
|
|
128
|
+
end
|
|
127
129
|
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
130
|
+
def chunk_this_seed?
|
|
131
|
+
@count != 0 && (@count % @options[:chunk_size]) == 0
|
|
132
|
+
end
|
|
131
133
|
end
|
|
132
134
|
end
|
data/lib/seed-do.rb
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
require 'active_record'
|
|
2
2
|
require 'active_support/core_ext/module/attribute_accessors'
|
|
3
|
-
require 'seed-do/railtie' if defined?(Rails)
|
|
3
|
+
require 'seed-do/railtie' if defined?(Rails)
|
|
4
4
|
|
|
5
5
|
module SeedDo
|
|
6
6
|
autoload :VERSION, 'seed-do/version'
|
|
@@ -10,17 +10,13 @@ module SeedDo
|
|
|
10
10
|
autoload :Runner, 'seed-do/runner'
|
|
11
11
|
autoload :Writer, 'seed-do/writer'
|
|
12
12
|
|
|
13
|
-
mattr_accessor :quiet
|
|
14
|
-
|
|
15
13
|
# Set `SeedDo.quiet = true` to silence all output
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
mattr_accessor :fixture_paths
|
|
14
|
+
mattr_accessor :quiet, default: false
|
|
19
15
|
|
|
20
16
|
# Set this to be an array of paths to directories containing your seed files. If used as a Rails
|
|
21
|
-
# plugin, SeedDo will set
|
|
17
|
+
# plugin, SeedDo will set it to contain `Rails.root/db/fixtures` and
|
|
22
18
|
# `Rails.root/db/fixtures/Rails.env`
|
|
23
|
-
|
|
19
|
+
mattr_accessor :fixture_paths, default: ['db/fixtures']
|
|
24
20
|
|
|
25
21
|
# Load seed data from files
|
|
26
22
|
# @param [Array] fixture_paths The paths to look for seed files in
|
data/lib/tasks/seed_do.rake
CHANGED
|
@@ -24,14 +24,10 @@ namespace :db do
|
|
|
24
24
|
# to load files from RAILS_ROOT/features/fixtures
|
|
25
25
|
rake db:seed_do FIXTURE_PATH=features/fixtures
|
|
26
26
|
EOS
|
|
27
|
-
task :
|
|
28
|
-
if ENV[
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
if ENV["FIXTURE_PATH"]
|
|
33
|
-
fixture_paths = [ENV["FIXTURE_PATH"], ENV["FIXTURE_PATH"] + '/' + Rails.env]
|
|
34
|
-
end
|
|
27
|
+
task seed_do: :environment do
|
|
28
|
+
filter = /#{ENV['FILTER'].tr(',', '|')}/ if ENV['FILTER']
|
|
29
|
+
|
|
30
|
+
fixture_paths = [ENV['FIXTURE_PATH'], ENV['FIXTURE_PATH'] + '/' + Rails.env] if ENV['FIXTURE_PATH']
|
|
35
31
|
|
|
36
32
|
SeedDo.seed(fixture_paths, filter)
|
|
37
33
|
end
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: seed-do
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 3.
|
|
4
|
+
version: 3.2.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Shinichi Maeshima
|
|
@@ -78,7 +78,8 @@ files:
|
|
|
78
78
|
homepage: http://github.com/willnet/seed-do
|
|
79
79
|
licenses:
|
|
80
80
|
- MIT
|
|
81
|
-
metadata:
|
|
81
|
+
metadata:
|
|
82
|
+
rubygems_mfa_required: 'true'
|
|
82
83
|
rdoc_options: []
|
|
83
84
|
require_paths:
|
|
84
85
|
- lib
|