Context manager to help finding a problematic value.
(self, conn)
| 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): |