rails-response-dumper 9.4.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 +4 -4
- data/lib/rails_response_dumper/defined.rb +52 -3
- 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: b3c1d630c5395612c28b6f7635f62134037c41be07a1e02feca0e23ede865442
|
|
4
|
+
data.tar.gz: a69b60c69ad1a517e39ec262b81fd8a7fcbb7dd3d78136eaaf041d654e21663f
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
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 |
|
|
53
|
-
model
|
|
54
|
-
|
|
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
|