rails-response-dumper 9.3.0 → 10.0.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: fc45f1c178874b8d00152b502c2526c07132c3a85769817e4d40ab18ae48c4dc
4
- data.tar.gz: f18df66c267e443f446ec1e0f0b11c48a782a35c9a4427969055be8d2fb91b05
3
+ metadata.gz: b3c1d630c5395612c28b6f7635f62134037c41be07a1e02feca0e23ede865442
4
+ data.tar.gz: a69b60c69ad1a517e39ec262b81fd8a7fcbb7dd3d78136eaaf041d654e21663f
5
5
  SHA512:
6
- metadata.gz: 29ad069e567e06f6b7f59a1089ed99c4d11ec0531c5a37d8bc3f836c3bd6ecd33498da2cd7459a8c84b1d14618d8000ca22692042bc3a7c33d91964350e114c3
7
- data.tar.gz: 47482181b53a35fa1449b77e989cea80dbeb6e2346565f80a0d8f756e0b7fbc937b38c2a59daf82e59a9a2b83acde6e63692adfd9e541b715c10796bf1974464
6
+ metadata.gz: 9f726fc24bccb8b3cea82ce74fce2f1210b005e628eebae6f2681b60708583547374a2337fd796d5279a5f39ddce8b4b77f809df209c2966584d80dce28e96cd
7
+ data.tar.gz: 940550d72e911f84c478ba55ab9155a44d8b0a7f303cb378b7d58b3d4dde8e568b59b691ac0001ab48fb89c1adff6f8b865f8df4549affd6dd443d2ff82c6f8d
@@ -48,10 +48,59 @@ module RailsResponseDumper
48
48
  @reset_models += models
49
49
  end
50
50
 
51
+ # Set the sequences to their start values. The sequences are the same as
52
+ # the sequences that TRUNCATE ... RESTART IDENTITY CASCADE restarts, but
53
+ # the rows stay in the tables. Each dump runs in a transaction, and so the
54
+ # rollback removes the rows, but a rollback does not reset the sequences.
55
+ #
56
+ # For each connection, one query does these steps:
57
+ #
58
+ # 1. Find the tables of the models and the tables that inherit from them.
59
+ # TRUNCATE uses inheritance only for the tables that you name. It does
60
+ # not use inheritance for the tables that it finds through CASCADE.
61
+ # 2. Find all tables that refer to these tables through a foreign key.
62
+ # Do this step again for each table that the query finds.
63
+ # 3. Find the sequences that these tables own. A serial column owns its
64
+ # sequence with deptype 'a'. An identity column owns its sequence with
65
+ # deptype 'i'.
66
+ # 4. Use setval to set each sequence to its start value. The next value
67
+ # from the sequence is then the start value.
68
+ #
69
+ # The tables must be empty before each dump. If a table has rows, a new
70
+ # row can get the same ID as an old row.
71
+ #
72
+ # Refer to:
73
+ # - https://www.postgresql.org/docs/current/sql-truncate.html
74
+ # - https://www.postgresql.org/docs/current/queries-with.html
75
+ # - https://www.postgresql.org/docs/current/catalog-pg-inherits.html
76
+ # - https://www.postgresql.org/docs/current/catalog-pg-constraint.html
77
+ # - https://www.postgresql.org/docs/current/catalog-pg-depend.html
78
+ # - https://www.postgresql.org/docs/current/catalog-pg-sequence.html
79
+ # - https://www.postgresql.org/docs/current/functions-sequence.html
51
80
  def reset_models!
52
- reset_models.each do |model|
53
- model.connection.exec_query <<~SQL.squish
54
- TRUNCATE #{model.quoted_table_name} RESTART IDENTITY CASCADE
81
+ reset_models.group_by(&:connection).each do |connection, models|
82
+ tables = models.map { |model| connection.quote(model.quoted_table_name) }
83
+ connection.exec_query <<~SQL.squish
84
+ WITH RECURSIVE named(oid) AS (
85
+ SELECT unnest(ARRAY[#{tables.join(', ')}]::regclass[])
86
+ UNION
87
+ SELECT pg_inherits.inhrelid FROM pg_inherits JOIN named ON pg_inherits.inhparent = named.oid
88
+ ), tables(oid) AS (
89
+ SELECT oid FROM named
90
+ UNION
91
+ SELECT pg_constraint.conrelid
92
+ FROM pg_constraint
93
+ JOIN tables ON pg_constraint.confrelid = tables.oid
94
+ WHERE pg_constraint.contype = 'f'
95
+ )
96
+ SELECT setval(pg_sequence.seqrelid, pg_sequence.seqstart, false)
97
+ FROM tables
98
+ JOIN pg_depend
99
+ ON pg_depend.refclassid = 'pg_class'::regclass
100
+ AND pg_depend.refobjid = tables.oid
101
+ AND pg_depend.classid = 'pg_class'::regclass
102
+ AND pg_depend.deptype IN ('a', 'i')
103
+ JOIN pg_sequence ON pg_sequence.seqrelid = pg_depend.objid
55
104
  SQL
56
105
  end
57
106
  end
@@ -21,6 +21,10 @@ module RailsResponseDumper
21
21
  options['dumps-dir'] = v
22
22
  end
23
23
 
24
+ opts.on('--skip-delete', 'Do not delete existing dumps before running.') do |v|
25
+ options[:skip_delete] = v
26
+ end
27
+
24
28
  opts.on('--fail-fast', 'Abort the run after first failure.') do |v|
25
29
  options[:fail_fast] = v
26
30
  end
@@ -18,7 +18,7 @@ module RailsResponseDumper
18
18
  globs = options[:filenames]
19
19
  else
20
20
  globs = ['dumpers/**/*_dumper.rb']
21
- FileUtils.rm_rf dumps_dir
21
+ FileUtils.rm_rf dumps_dir unless options[:skip_delete]
22
22
  end
23
23
 
24
24
  FileUtils.mkdir_p dumps_dir
@@ -30,7 +30,7 @@ module RailsResponseDumper
30
30
  errors = []
31
31
 
32
32
  dumper_blocks = RailsResponseDumper::Defined.dumpers.flat_map do |defined|
33
- if options[:filenames].present?
33
+ if options[:filenames].present? && !options[:skip_delete]
34
34
  # clear previous dumps for that file in case removed from updated dump file
35
35
  FileUtils.rm_rf "#{dumps_dir}/#{defined.name.underscore}/"
36
36
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rails-response-dumper
3
3
  version: !ruby/object:Gem::Version
4
- version: 9.3.0
4
+ version: 10.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Pioneer Valley Books
@@ -73,7 +73,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
73
73
  - !ruby/object:Gem::Version
74
74
  version: '0'
75
75
  requirements: []
76
- rubygems_version: 4.0.16
76
+ rubygems_version: 4.0.20
77
77
  specification_version: 4
78
78
  summary: Dump HTTP responses from a Rails application to the file system
79
79
  test_files: []