(
module_name: str,
log_prefix: str = "[自动补列]",
)
| 82 | |
| 83 | |
| 84 | async def auto_add_missing_columns( |
| 85 | module_name: str, |
| 86 | log_prefix: str = "[自动补列]", |
| 87 | ) -> None: |
| 88 | tables = _plugin_tables(module_name) |
| 89 | if not tables: |
| 90 | return |
| 91 | |
| 92 | def _existing_columns(sync_conn, table_name): |
| 93 | insp = sa_inspect(sync_conn) |
| 94 | if not insp.has_table(table_name): |
| 95 | return None |
| 96 | return {col["name"] for col in insp.get_columns(table_name)} |
| 97 | |
| 98 | pending: List[str] = [] |
| 99 | try: |
| 100 | async with engine.connect() as conn: |
| 101 | dialect = conn.dialect |
| 102 | for table in tables: |
| 103 | existing = await conn.run_sync(_existing_columns, table.name) |
| 104 | if existing is None: |
| 105 | continue |
| 106 | for column in table.columns: |
| 107 | if column.name not in existing: |
| 108 | pending.append(_add_column_sql(table.name, column, dialect)) |
| 109 | except Exception as e: |
| 110 | logger.warning(f"{log_prefix} 读取表结构失败: {e}") |
| 111 | return |
| 112 | |
| 113 | for sql in pending: |
| 114 | try: |
| 115 | async with engine.begin() as conn: |
| 116 | await conn.execute(text(sql)) |
| 117 | logger.info(f"{log_prefix} {sql}") |
| 118 | except Exception as e: |
| 119 | logger.warning(f"{log_prefix} 执行失败: {sql} -> {e}") |
no test coverage detected