Migrate request_logs table from old schema to new schema
(self, db)
| 861 | await db.commit() |
| 862 | |
| 863 | async def _migrate_request_logs(self, db): |
| 864 | """Migrate request_logs table from old schema to new schema""" |
| 865 | try: |
| 866 | has_model = await self._column_exists(db, "request_logs", "model") |
| 867 | has_operation = await self._column_exists(db, "request_logs", "operation") |
| 868 | |
| 869 | if has_model and not has_operation: |
| 870 | print("?? ?????request_logs???,????...") |
| 871 | await db.execute("ALTER TABLE request_logs RENAME TO request_logs_old") |
| 872 | await db.execute(""" |
| 873 | CREATE TABLE request_logs ( |
| 874 | id INTEGER PRIMARY KEY AUTOINCREMENT, |
| 875 | token_id INTEGER, |
| 876 | operation TEXT NOT NULL, |
| 877 | request_body TEXT, |
| 878 | response_body TEXT, |
| 879 | status_code INTEGER NOT NULL, |
| 880 | duration FLOAT NOT NULL, |
| 881 | status_text TEXT DEFAULT '', |
| 882 | progress INTEGER DEFAULT 0, |
| 883 | created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, |
| 884 | updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, |
| 885 | FOREIGN KEY (token_id) REFERENCES tokens(id) |
| 886 | ) |
| 887 | """) |
| 888 | await db.execute(""" |
| 889 | INSERT INTO request_logs (token_id, operation, request_body, status_code, duration, status_text, progress, created_at, updated_at) |
| 890 | SELECT |
| 891 | token_id, |
| 892 | model as operation, |
| 893 | json_object('model', model, 'prompt', substr(prompt, 1, 100)) as request_body, |
| 894 | CASE |
| 895 | WHEN status = 'completed' THEN 200 |
| 896 | WHEN status = 'failed' THEN 500 |
| 897 | ELSE 102 |
| 898 | END as status_code, |
| 899 | response_time as duration, |
| 900 | CASE |
| 901 | WHEN status = 'completed' THEN 'completed' |
| 902 | WHEN status = 'failed' THEN 'failed' |
| 903 | ELSE 'processing' |
| 904 | END as status_text, |
| 905 | CASE |
| 906 | WHEN status = 'completed' THEN 100 |
| 907 | WHEN status = 'failed' THEN 0 |
| 908 | ELSE 0 |
| 909 | END as progress, |
| 910 | created_at, |
| 911 | created_at |
| 912 | FROM request_logs_old |
| 913 | """) |
| 914 | await db.execute("DROP TABLE request_logs_old") |
| 915 | print("? request_logs?????") |
| 916 | |
| 917 | if not await self._column_exists(db, "request_logs", "status_text"): |
| 918 | await db.execute("ALTER TABLE request_logs ADD COLUMN status_text TEXT DEFAULT ''") |
| 919 | if not await self._column_exists(db, "request_logs", "progress"): |
| 920 | await db.execute("ALTER TABLE request_logs ADD COLUMN progress INTEGER DEFAULT 0") |