MCPcopy
hub / github.com/psycopg/psycopg / find_insert_problem

Method find_insert_problem

tests/fix_faker.py:145–167  ·  tests/fix_faker.py::Faker.find_insert_problem

Context manager to help finding a problematic value.

(self, conn)

Source from the content-addressed store, hash-verified

143
144 @contextmanager
145 def find_insert_problem(self, conn):
146 class="st">""class="st">"Context manager to help finding a problematic value."class="st">""
147 try:
148 with conn.transaction():
149 yield
150 except psycopg.DatabaseError:
151 cur = psycopg.Cursor(conn)
152 class="cm"># Repeat insert one field at time, until finding the wrong one
153 cur.execute(self.drop_stmt)
154 cur.execute(self.create_stmt)
155 for i, rec in enumerate(self.records):
156 for j, val in enumerate(rec):
157 try:
158 cur.execute(self._insert_field_stmt(j), (val,))
159 except psycopg.DatabaseError as e:
160 if len(r := repr(val)) > 200:
161 r = fclass="st">"{r[:200]}... ({len(r)} chars)"
162 raise Exception(
163 fclass="st">"value {r!r} at record {i} column0 {j} failed insert: {e}"
164 ) from None
165
166 class="cm"># just in case, but hopefully we should have triggered the problem
167 raise
168
169 @asynccontextmanager
170 async def find_insert_problem_async(self, aconn):

Callers 6

workFunction · 0.80
workFunction · 0.80
workFunction · 0.80
test_randomFunction · 0.80
workFunction · 0.80
test_randomFunction · 0.80

Calls 3

executeMethod · 0.95
_insert_field_stmtMethod · 0.95
transactionMethod · 0.45

Tested by 6

workFunction · 0.64
workFunction · 0.64
workFunction · 0.64
test_randomFunction · 0.64
workFunction · 0.64
test_randomFunction · 0.64