|
43 | 43 | Metadata, |
44 | 44 | Model, |
45 | 45 | TextField, |
46 | | - PrimaryKeyField, |
47 | 46 | ) |
48 | 47 | from playhouse.migrate import MySQLMigrator, PostgresqlMigrator, migrate |
49 | 48 | from playhouse.pool import PooledMySQLDatabase, PooledPostgresqlDatabase |
@@ -1525,6 +1524,43 @@ def alter_db_rename_column(migrator, table_name, old_column_name, new_column_nam |
1525 | 1524 | pass |
1526 | 1525 |
|
1527 | 1526 |
|
| 1527 | +def ensure_model_indexes(migrator): |
| 1528 | + """Create indexes declared by the Peewee models when they are missing.""" |
| 1529 | + members = inspect.getmembers(sys.modules[__name__], inspect.isclass) |
| 1530 | + for name, model in members: |
| 1531 | + if model == DataBaseModel or not issubclass(model, DataBaseModel): |
| 1532 | + continue |
| 1533 | + |
| 1534 | + table_name = model._meta.table_name |
| 1535 | + expected = {} |
| 1536 | + for field in model._meta.fields.values(): |
| 1537 | + if field.primary_key: |
| 1538 | + continue |
| 1539 | + if field.index or field.unique: |
| 1540 | + expected[(field.name,)] = bool(field.unique) |
| 1541 | + |
| 1542 | + for columns, unique in model._meta.indexes: |
| 1543 | + expected[tuple(columns)] = bool(unique) |
| 1544 | + |
| 1545 | + if not expected: |
| 1546 | + continue |
| 1547 | + |
| 1548 | + try: |
| 1549 | + existing = {tuple(index.columns): bool(index.unique) for index in DB.get_indexes(table_name)} |
| 1550 | + except Exception as ex: |
| 1551 | + logging.error(f"Failed to inspect indexes on {table_name}: {ex}") |
| 1552 | + continue |
| 1553 | + |
| 1554 | + for columns, unique in expected.items(): |
| 1555 | + if columns in existing and (not unique or existing[columns]): |
| 1556 | + continue |
| 1557 | + try: |
| 1558 | + migrate(migrator.add_index(table_name, columns, unique=unique)) |
| 1559 | + logging.info(f"Created {'unique ' if unique else ''}index on {table_name} ({', '.join(columns)})") |
| 1560 | + except Exception as ex: |
| 1561 | + logging.error(f"Failed to create {'unique ' if unique else ''}index on {table_name} ({', '.join(columns)}): {ex}") |
| 1562 | + |
| 1563 | + |
1528 | 1564 | def migrate_add_unique_email(migrator): |
1529 | 1565 | """Deduplicates user emails and add UNIQUE constraint to email column (idempotent)""" |
1530 | 1566 | # step 0: check existing index state on user.email and prepare for unique constraint |
@@ -1780,6 +1816,7 @@ def migrate_db(): |
1780 | 1816 | alter_db_add_column(migrator, "knowledgebase", "artifact_task_finish_at", DateTimeField(null=True)) |
1781 | 1817 | alter_db_add_column(migrator, "knowledgebase", "skill_task_id", CharField(max_length=32, null=True, help_text="Skill generation task ID", index=True)) |
1782 | 1818 | alter_db_add_column(migrator, "knowledgebase", "skill_task_finish_at", DateTimeField(null=True)) |
| 1819 | + alter_db_add_column(migrator, "evaluation_runs", "dialog_id", CharField(max_length=32, null=False, default="", help_text="dialog configuration being evaluated", index=True)) |
1783 | 1820 | alter_db_column_type(migrator, "tenant_llm", "api_key", TextField(null=True, help_text="API KEY")) |
1784 | 1821 | alter_db_add_column(migrator, "tenant_llm", "status", CharField(max_length=1, null=False, help_text="is it validate(0: wasted, 1: validate)", default="1", index=True)) |
1785 | 1822 | alter_db_add_column(migrator, "connector2kb", "auto_parse", CharField(max_length=1, null=False, default="1", index=False)) |
@@ -1833,3 +1870,4 @@ def migrate_db(): |
1833 | 1870 | logging.disable(logging.NOTSET) |
1834 | 1871 | # this is after re-enabling logging to allow logging changed user emails |
1835 | 1872 | migrate_add_unique_email(migrator) |
| 1873 | + ensure_model_indexes(migrator) |
0 commit comments