planter 0.4.1 → 0.4.2
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/README.md +5 -1
- data/lib/generators/planter/adapter_generator.rb +10 -0
- data/lib/planter/adapters/active_record.rb +11 -0
- data/lib/planter/seeder.rb +37 -0
- data/lib/planter/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 65e5880dfef8ccce0bf8fec627a74ab7ee76c9d0f84379079056d934b9210e58
|
|
4
|
+
data.tar.gz: 2bc7f2d1e88618db03c882291e8dc48b3aa453199f96e235eeab49ec554d0273
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 011cec287611791d0585a80c83e51bd602eafb9bba60755c5079b1eff6f5218ca3a05e21f1651098acda55055590ae4d9ca649f50e3f4559ca7cd971acf647c4
|
|
7
|
+
data.tar.gz: 5a11daf5df589cda533596d9039820c0da140f2c2f44eafb40d33a110cbd3aa7fef0a18c78c8c2f7f8d7a4a6dd8ab1f2e3a4d1a0a7a9f3dee3665fece5ff4fd6
|
data/README.md
CHANGED
|
@@ -23,7 +23,7 @@ currently a pre-release version, it's recommended to lock it to a specific
|
|
|
23
23
|
version, as breaking changes may occur, even at the minor level.
|
|
24
24
|
|
|
25
25
|
```ruby
|
|
26
|
-
gem 'planter', '0.4.
|
|
26
|
+
gem 'planter', '0.4.2'
|
|
27
27
|
```
|
|
28
28
|
|
|
29
29
|
And then execute:
|
|
@@ -352,6 +352,10 @@ class MyAdapter
|
|
|
352
352
|
# Return the attribute used to attach a parent id to the seeded record.
|
|
353
353
|
end
|
|
354
354
|
|
|
355
|
+
def table_columns(model_name:)
|
|
356
|
+
# Return native columns or fields for model_name.
|
|
357
|
+
end
|
|
358
|
+
|
|
355
359
|
def table_names
|
|
356
360
|
# Return table or collection names used by `rails generate planter:seeder ALL`.
|
|
357
361
|
end
|
|
@@ -53,6 +53,16 @@ module Planter
|
|
|
53
53
|
raise NotImplementedError, "\#{self.class} must implement #foreign_key"
|
|
54
54
|
end
|
|
55
55
|
|
|
56
|
+
##
|
|
57
|
+
# Return native columns or fields for the model being seeded.
|
|
58
|
+
#
|
|
59
|
+
# @param [String] model_name the model being seeded
|
|
60
|
+
#
|
|
61
|
+
# @return [Array<String>]
|
|
62
|
+
def table_columns(model_name:)
|
|
63
|
+
raise NotImplementedError, "\#{self.class} must implement #table_columns"
|
|
64
|
+
end
|
|
65
|
+
|
|
56
66
|
##
|
|
57
67
|
# Return table or collection names that can have seeders generated.
|
|
58
68
|
#
|
|
@@ -9,6 +9,7 @@ module Planter
|
|
|
9
9
|
# - +create_record(model_name:, lookup_attributes:, create_attributes:)+
|
|
10
10
|
# - +parent_ids(model_name:, parent:)+
|
|
11
11
|
# - +foreign_key(model_name:, parent:)+
|
|
12
|
+
# - +table_columns(model_name:)+
|
|
12
13
|
# - +table_names+
|
|
13
14
|
#
|
|
14
15
|
# +model_name+ is the configured seeder model name. +parent+ is the
|
|
@@ -56,6 +57,16 @@ module Planter
|
|
|
56
57
|
association_options(model_name, parent).fetch(:foreign_key, "#{parent}_id")
|
|
57
58
|
end
|
|
58
59
|
|
|
60
|
+
##
|
|
61
|
+
# Return native table columns for the model being seeded.
|
|
62
|
+
#
|
|
63
|
+
# @param [String] model_name the model being seeded
|
|
64
|
+
#
|
|
65
|
+
# @return [Array<String>]
|
|
66
|
+
def table_columns(model_name:)
|
|
67
|
+
model_name.constantize.column_names
|
|
68
|
+
end
|
|
69
|
+
|
|
59
70
|
##
|
|
60
71
|
# Return application table names that can have seeders generated.
|
|
61
72
|
#
|
data/lib/planter/seeder.rb
CHANGED
|
@@ -268,6 +268,7 @@ module Planter
|
|
|
268
268
|
def create_record(record, parent_id: nil)
|
|
269
269
|
unique, attrs = split_record(apply_transformations(record))
|
|
270
270
|
unique = unique.merge(foreign_key => parent_id) if parent_id
|
|
271
|
+
unique, attrs = filter_lookup_attributes(unique, attrs)
|
|
271
272
|
adapter.create_record(
|
|
272
273
|
model_name: model,
|
|
273
274
|
lookup_attributes: unique,
|
|
@@ -334,6 +335,42 @@ module Planter
|
|
|
334
335
|
[u, rec.except(*unique_columns)]
|
|
335
336
|
end
|
|
336
337
|
|
|
338
|
+
def filter_lookup_attributes(lookup_attributes, create_attributes)
|
|
339
|
+
return [lookup_attributes, create_attributes] unless adapter.respond_to?(:table_columns)
|
|
340
|
+
|
|
341
|
+
table_columns = adapter.table_columns(model_name: model).map(&:to_s)
|
|
342
|
+
native_lookup_attributes = lookup_attributes.select do |field, _value|
|
|
343
|
+
table_columns.include?(field.to_s)
|
|
344
|
+
end
|
|
345
|
+
non_column_lookup_attributes = lookup_attributes.except(*native_lookup_attributes.keys)
|
|
346
|
+
|
|
347
|
+
if non_column_lookup_attributes.any?
|
|
348
|
+
warn_non_column_lookup_attributes(non_column_lookup_attributes.keys)
|
|
349
|
+
end
|
|
350
|
+
|
|
351
|
+
if native_lookup_attributes.empty?
|
|
352
|
+
raise "No native lookup columns found for #{model}. " \
|
|
353
|
+
"Add a native table column to the seed data or unique_columns."
|
|
354
|
+
end
|
|
355
|
+
|
|
356
|
+
[
|
|
357
|
+
native_lookup_attributes,
|
|
358
|
+
non_column_lookup_attributes.merge(create_attributes)
|
|
359
|
+
]
|
|
360
|
+
end
|
|
361
|
+
|
|
362
|
+
def warn_non_column_lookup_attributes(fields)
|
|
363
|
+
warning_key = [model, fields.map(&:to_s).sort]
|
|
364
|
+
@warned_non_column_lookup_attributes ||= []
|
|
365
|
+
return if @warned_non_column_lookup_attributes.include?(warning_key)
|
|
366
|
+
|
|
367
|
+
@warned_non_column_lookup_attributes << warning_key
|
|
368
|
+
warn(
|
|
369
|
+
"WARNING: Planter moved non-column lookup attributes for #{model} " \
|
|
370
|
+
"into create attributes: #{warning_key.last.join(", ")}"
|
|
371
|
+
)
|
|
372
|
+
end
|
|
373
|
+
|
|
337
374
|
def foreign_key
|
|
338
375
|
adapter.foreign_key(model_name: model, parent: parent)
|
|
339
376
|
end
|
data/lib/planter/version.rb
CHANGED