live_fixtures 2.0.0 → 2.1.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 4555eba9da64e35ef61d763132a1d2e5a280c785d0b2583fcb9055e6800eae74
4
- data.tar.gz: fb54c037ff212fecb9bdbe2fd0c961df6d0acba9a0ed6dab7579c5e487656b0f
3
+ metadata.gz: 9414f9b5a408acaff1c2749f0f3127412f23f6d364988fcf1f75ce9a15b0cbe4
4
+ data.tar.gz: f6304bcef06ba8b4e153d7ff19744e9af837592ea0aa72a758a5446e3933055c
5
5
  SHA512:
6
- metadata.gz: 6f0af7f78d0638a22f2dc607b6a21b0cf24b95497fe974c4f0c1b1d7c4b895048deac44ec55576391ccf67bc297e8aa318e841ed1ef1669eb286b8cdeba3f932
7
- data.tar.gz: 12a8488d4e93c45462e7362926f057571f54f4afe2640ff94dc422b8c3bbc9a4ff0a6b4a064962e15ca13b75c2abb60e1f016495189a28e3d14b627504152c32
6
+ metadata.gz: 348ebda33ef0e11062c823ab14d64dc5b5c5947686f91d2a094836b7a1499061ff7e306a1e943824a27e958dad4e8ed4581b5626b835c191a5409a2d735f557b
7
+ data.tar.gz: 03eb2d921c1de9d75f144c99dd5166298d2ed67d79844bb5b2aac1b44acb31a00a2076ef4ec27e2a50a72b5f41de68d6168bb517786fb1fef7aadec8d610199f
@@ -2,6 +2,10 @@
2
2
  All notable changes to this project will be documented in this file.
3
3
  This project adheres to [Semantic Versioning](http://semver.org/).
4
4
 
5
+ ## [2.1.0] - 2020-12-09
6
+ ### Added
7
+ support for selectively import tables with custom callbacks per table (#34)
8
+
5
9
  ## [2.0.0] - 2020-11-11
6
10
  ### Breaking changes
7
11
  This is a breaking change because LiveFixtures::Import.new now needs to receive the class_names Hash to be able to correctly compute the insert_order in case there are some unconventional class names associations. And the class_names argument is removed from import_all. But this is the only change.
@@ -55,10 +55,15 @@ module LiveFixtures::Export
55
55
  # @yieldparam model [ActiveRecord::Base] each successive model.
56
56
  # @yieldreturn [Hash{String => Object}] a hash of attributes to be merged and saved with the model's attributes.
57
57
  def export_fixtures(models, with_references = [])
58
- return unless models.present?
58
+ return [] unless models.present?
59
59
 
60
- table_name = models.first.class.table_name
61
- File.open(File.join(@dir, table_name + '.yml'), 'w') do |file|
60
+ model_class = models.first.class
61
+
62
+ File.open(File.join(@dir, model_class.table_name + '.yml'), 'w') do |file|
63
+ file.write <<~PRELUDE
64
+ _fixture:
65
+ model_class: #{model_class.name}
66
+ PRELUDE
62
67
 
63
68
  iterator = export_options[:show_progress] ? ProgressBarIterator : SimpleIterator
64
69
 
@@ -66,7 +71,6 @@ module LiveFixtures::Export
66
71
  more_attributes = block_given? ? yield(model) : {}
67
72
  file.write Fixture.to_yaml(model, with_references, more_attributes)
68
73
  end
69
-
70
74
  end
71
75
  end
72
76
 
@@ -1,3 +1,5 @@
1
+ require 'benchmark'
2
+
1
3
  # An object that facilitates the import of fixtures into a database.
2
4
  class LiveFixtures::Import
3
5
  NO_LABEL = nil
@@ -6,6 +8,13 @@ class LiveFixtures::Import
6
8
  # the inferred one if none was specified.
7
9
  attr_reader :insert_order
8
10
 
11
+ # Map of table_name to import routine
12
+ # @return [Hash<String => Proc>]
13
+ attr_reader :alternate_imports
14
+
15
+ # Accessor for string label for a given fixture mapping to it's db id
16
+ attr_reader :label_to_id
17
+
9
18
  # Instantiate a new Import with the directory containing your fixtures, and
10
19
  # the order in which to import them. The order should ensure fixtures
11
20
  # containing references to another fixture are imported AFTER the referenced
@@ -18,14 +27,25 @@ class LiveFixtures::Import
18
27
  # @option opts [Boolean] show_progress whether or not to show the progress bar
19
28
  # @option opts [Boolean] skip_missing_tables when false, an error will be raised if a yaml file isn't found for each table in insert_order
20
29
  # @option opts [Boolean] skip_missing_refs when false, an error will be raised if an ID isn't found for a label.
30
+ # @option opts [Boolean] use_insert_order_as_table_names when true, table names will be those passed in insert_order, not read from yaml files
21
31
  # @return [LiveFixtures::Import] an importer
22
32
  # @see LiveFixtures::Export::Reference
23
33
  def initialize(root_path, insert_order = nil, class_names = {}, **opts)
24
- defaut_options = { show_progress: true, skip_missing_tables: false, skip_missing_refs: false }
34
+ defaut_options = {
35
+ show_progress: true,
36
+ skip_missing_tables: false,
37
+ skip_missing_refs: false,
38
+ use_insert_order_as_table_names: false,
39
+ }
25
40
  @options = defaut_options.merge(opts)
26
41
  @root_path = root_path
27
- @table_names = Dir.glob(File.join(@root_path, '{*,**}/*.yml')).map do |filepath|
28
- File.basename filepath, ".yml"
42
+
43
+ if insert_order && @options[:use_insert_order_as_table_names]
44
+ @table_names = insert_order
45
+ else
46
+ @table_names = Dir.glob(File.join(@root_path, '{*,**}/*.yml')).map do |filepath|
47
+ File.basename filepath, ".yml"
48
+ end
29
49
  end
30
50
 
31
51
  @class_names = class_names
@@ -40,7 +60,9 @@ class LiveFixtures::Import
40
60
  if @table_names.size < @insert_order.size && !@options[:skip_missing_tables]
41
61
  raise ArgumentError, "table(s) mentioned in `insert_order` which has no yml file to import: #{@insert_order - @table_names}"
42
62
  end
63
+
43
64
  @label_to_id = {}
65
+ @alternate_imports = {}
44
66
  end
45
67
 
46
68
  # Within a transaction, import all the fixtures into the database.
@@ -54,13 +76,22 @@ class LiveFixtures::Import
54
76
  # @see https://github.com/rails/rails/blob/4-2-stable/activerecord/lib/active_record/fixtures.rb#L496
55
77
  def import_all
56
78
  connection = ActiveRecord::Base.connection
79
+ show_progress = @options[:show_progress]
57
80
 
81
+ # TODO: should be additive with alternate_imports so we can delete the fixture file
58
82
  files_to_read = @table_names
59
83
 
60
- unless files_to_read.empty?
61
- connection.transaction(requires_new: true) do
62
- files_to_read.each do |path|
63
- table_name = path.tr '/', '_'
84
+ return if files_to_read.empty?
85
+
86
+ connection.transaction(requires_new: true) do
87
+ files_to_read.each do |path|
88
+ table_name = path.tr '/', '_'
89
+ if alternate = @alternate_imports[table_name]
90
+ time = Benchmark.ms do
91
+ alternate.call(@label_to_id)
92
+ end
93
+ puts "Imported %s in %.0fms" % [table_name, time] if show_progress
94
+ else
64
95
  class_name = @class_names[table_name.to_sym] || table_name.classify
65
96
 
66
97
  ff = Fixtures.new(connection,
@@ -71,16 +102,25 @@ class LiveFixtures::Import
71
102
  skip_missing_refs: @options[:skip_missing_refs])
72
103
 
73
104
  conn = ff.model_connection || connection
74
- iterator = @options[:show_progress] ? ProgressBarIterator : SimpleIterator
75
- iterator.new(ff).each do |table_name, label, row|
76
- conn.insert_fixture(row, table_name)
77
- @label_to_id[label] = conn.send(:last_inserted_id, table_name) unless label == NO_LABEL
105
+
106
+ iterator = show_progress ? ProgressBarIterator : SimpleIterator
107
+ iterator.new(ff).each do |tname, label, row|
108
+ conn.insert_fixture(row, tname)
109
+ @label_to_id[label] = conn.send(:last_inserted_id, tname) unless label == NO_LABEL
78
110
  end
79
111
  end
80
112
  end
81
113
  end
82
114
  end
83
115
 
116
+ # Override import of table using a callable object
117
+ # @param table_name [String] table to use callable instead of fixture file
118
+ # @param callable [Proc] Proc/lambda that will be called with @label_to_id
119
+ def override(table_name, callable)
120
+ @alternate_imports[table_name] = callable
121
+ self
122
+ end
123
+
84
124
  private
85
125
 
86
126
  # Here we go through each of the fixture YAML files to see what polymorphic
@@ -131,7 +171,7 @@ class LiveFixtures::Import
131
171
  def initialize(ff)
132
172
  @ff = ff
133
173
  @bar = LiveFixtures.get_progress_bar(
134
- total:ff.fixtures.size,
174
+ total: ff.fixtures.size,
135
175
  title: ff.model_class.name
136
176
  )
137
177
  end
@@ -1,3 +1,3 @@
1
1
  module LiveFixtures
2
- VERSION = "2.0.0"
2
+ VERSION = "2.1.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: live_fixtures
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.0
4
+ version: 2.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - jleven
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-11-11 00:00:00.000000000 Z
11
+ date: 1970-01-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -214,7 +214,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
214
214
  - !ruby/object:Gem::Version
215
215
  version: '0'
216
216
  requirements: []
217
- rubygems_version: 3.0.3
217
+ rubygems_version: 3.1.4
218
218
  signing_key:
219
219
  specification_version: 4
220
220
  summary: Tools for exporting and importing between databases managed by ActiveRecord.