| 81 | engine.dispose() |
| 82 | |
| 83 | def prepare_models(self, engine): |
| 84 | if not self.prepared: |
| 85 | # SQLAlchemy will check if the items exist before trying to |
| 86 | # create them, which is a race condition. If it raises an error |
| 87 | # in one iteration, the next may pass all the existence checks |
| 88 | # and the call will succeed. |
| 89 | retries = 0 |
| 90 | while True: |
| 91 | try: |
| 92 | ResultModelBase.metadata.create_all(engine) |
| 93 | except DatabaseError: |
| 94 | if retries < PREPARE_MODELS_MAX_RETRIES: |
| 95 | sleep_amount_ms = get_exponential_backoff_interval( |
| 96 | 10, retries, 1000, True |
| 97 | ) |
| 98 | time.sleep(sleep_amount_ms / 1000) |
| 99 | retries += 1 |
| 100 | else: |
| 101 | raise |
| 102 | else: |
| 103 | break |
| 104 | self._migrate_missing_columns(engine) |
| 105 | self.prepared = True |
| 106 | |
| 107 | def _migrate_missing_columns(self, engine): |
| 108 | """Add missing nullable columns to existing tables if needed.""" |