Skip to content

Commit 11c63ba

Browse files
committed
fix(chat/migrations): fix migrations
- remove buggy soft deletes --> we do not yet differentiate between what has been deleted and what is still used but rolling back now is as easy as just installing an older version --> we can differentiate later (maybe even doing a hard delete later instead of a soft delete) - add and switch to debug logging statements for all control structures
1 parent c51cb94 commit 11c63ba

18 files changed

+130
-588
lines changed

src/askui/chat/migrations/versions/057f82313448_import_json_assistants.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ def _insert_assistants_batch(
3333
) -> None:
3434
"""Insert a batch of assistants into the database, ignoring conflicts."""
3535
if not assistants_batch:
36+
logger.debug("No assistants to insert, skipping batch")
3637
return
3738

3839
connection.execute(
@@ -50,6 +51,10 @@ def upgrade() -> None:
5051

5152
# Skip if directory doesn't exist (e.g., first-time setup)
5253
if not assistants_dir.exists():
54+
logger.debug(
55+
"Assistants directory does not exist, skipping import of assistants",
56+
extra={"assistants_dir": str(assistants_dir)},
57+
)
5358
return
5459

5560
# Get the table from the current database schema
@@ -94,6 +99,9 @@ def downgrade() -> None:
9499
result = connection.execute(assistants_table.select())
95100
rows = result.fetchall()
96101
if not rows:
102+
logger.debug(
103+
"No assistants found in the database, skipping export of rows to json",
104+
)
97105
return
98106

99107
for row in rows:
@@ -103,6 +111,10 @@ def downgrade() -> None:
103111
)
104112
json_path = assistants_dir / f"{assistant.id}.json"
105113
if json_path.exists():
114+
logger.debug(
115+
"Json file for assistant already exists, skipping export of row to json",
116+
extra={"assistant_id": assistant.id, "json_path": str(json_path)},
117+
)
106118
continue
107119
with json_path.open("w", encoding="utf-8") as f:
108120
f.write(assistant.model_dump_json())

src/askui/chat/migrations/versions/1a2b3c4d5e6f_create_threads_table.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"""create_threads_table
22
33
Revision ID: 1a2b3c4d5e6f
4-
Revises: a0f1a2b3c4d5
4+
Revises: 9e0f1a2b3c4d
55
Create Date: 2025-01-27 12:00:00.000000
66
77
"""
@@ -13,7 +13,7 @@
1313

1414
# revision identifiers, used by Alembic.
1515
revision: str = "1a2b3c4d5e6f"
16-
down_revision: Union[str, None] = "a0f1a2b3c4d5"
16+
down_revision: Union[str, None] = "9e0f1a2b3c4d"
1717
branch_labels: Union[str, Sequence[str], None] = None
1818
depends_on: Union[str, Sequence[str], None] = None
1919

src/askui/chat/migrations/versions/2b3c4d5e6f7a_create_messages_table.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"""create_messages_table
22
33
Revision ID: 2b3c4d5e6f7a
4-
Revises: 1a2b3c4d5e6f
4+
Revises: 3c4d5e6f7a8b
55
Create Date: 2025-01-27 12:01:00.000000
66
77
"""
@@ -13,7 +13,7 @@
1313

1414
# revision identifiers, used by Alembic.
1515
revision: str = "2b3c4d5e6f7a"
16-
down_revision: Union[str, None] = "1a2b3c4d5e6f"
16+
down_revision: Union[str, None] = "3c4d5e6f7a8b"
1717
branch_labels: Union[str, Sequence[str], None] = None
1818
depends_on: Union[str, Sequence[str], None] = None
1919

src/askui/chat/migrations/versions/37007a499ca7_soft_delete_assistants_dir.py

Lines changed: 0 additions & 87 deletions
This file was deleted.

src/askui/chat/migrations/versions/3c4d5e6f7a8b_create_runs_table.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"""create_runs_table
22
33
Revision ID: 3c4d5e6f7a8b
4-
Revises: 2b3c4d5e6f7a
4+
Revises: 1a2b3c4d5e6f
55
Create Date: 2025-01-27 12:02:00.000000
66
77
"""
@@ -13,7 +13,7 @@
1313

1414
# revision identifiers, used by Alembic.
1515
revision: str = "3c4d5e6f7a8b"
16-
down_revision: Union[str, None] = "2b3c4d5e6f7a"
16+
down_revision: Union[str, None] = "1a2b3c4d5e6f"
1717
branch_labels: Union[str, Sequence[str], None] = None
1818
depends_on: Union[str, Sequence[str], None] = None
1919

src/askui/chat/migrations/versions/4d5e6f7a8b9c_import_json_threads.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ def _insert_threads_batch(
3333
) -> None:
3434
"""Insert a batch of threads into the database, ignoring conflicts."""
3535
if not threads_batch:
36+
logger.debug("No threads to insert, skipping batch")
3637
return
3738

3839
connection.execute(
@@ -50,6 +51,10 @@ def upgrade() -> None: # noqa: C901
5051

5152
# Skip if workspaces directory doesn't exist (e.g., first-time setup)
5253
if not workspaces_dir.exists():
54+
logger.debug(
55+
"Workspaces directory does not exist, skipping import of threads",
56+
extra={"workspaces_dir": str(workspaces_dir)},
57+
)
5358
return
5459

5560
# Get the table from the current database schema
@@ -62,12 +67,20 @@ def upgrade() -> None: # noqa: C901
6267
# Iterate through all workspace directories
6368
for workspace_dir in workspaces_dir.iterdir():
6469
if not workspace_dir.is_dir():
70+
logger.debug(
71+
"Skipping non-directory in workspaces",
72+
extra={"path": str(workspace_dir)},
73+
)
6574
continue
6675

6776
workspace_id = workspace_dir.name
6877
threads_dir = workspace_dir / "threads"
6978

7079
if not threads_dir.exists():
80+
logger.debug(
81+
"Threads directory does not exist, skipping workspace",
82+
extra={"workspace_id": workspace_id, "threads_dir": str(threads_dir)},
83+
)
7184
continue
7285

7386
# Get all JSON files in the threads directory
@@ -102,6 +115,9 @@ def downgrade() -> None:
102115
result = connection.execute(threads_table.select())
103116
rows = result.fetchall()
104117
if not rows:
118+
logger.debug(
119+
"No threads found in the database, skipping export of rows to json",
120+
)
105121
return
106122

107123
for row in rows:
@@ -111,6 +127,10 @@ def downgrade() -> None:
111127
threads_dir.mkdir(parents=True, exist_ok=True)
112128
json_path = threads_dir / f"{thread_model.id}.json"
113129
if json_path.exists():
130+
logger.debug(
131+
"Json file for thread already exists, skipping export of row to json",
132+
extra={"thread_id": thread_model.id, "json_path": str(json_path)},
133+
)
114134
continue
115135
with json_path.open("w", encoding="utf-8") as f:
116136
f.write(thread_model.model_dump_json())

src/askui/chat/migrations/versions/5a1b2c3d4e5f_create_mcp_configs_table.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"""create_mcp_configs_table
22
33
Revision ID: 5a1b2c3d4e5f
4-
Revises: 37007a499ca7
4+
Revises: c35e88ea9595
55
Create Date: 2025-01-27 10:00:00.000000
66
77
"""
@@ -13,7 +13,7 @@
1313

1414
# revision identifiers, used by Alembic.
1515
revision: str = "5a1b2c3d4e5f"
16-
down_revision: Union[str, None] = "37007a499ca7"
16+
down_revision: Union[str, None] = "c35e88ea9595"
1717
branch_labels: Union[str, Sequence[str], None] = None
1818
depends_on: Union[str, Sequence[str], None] = None
1919

src/askui/chat/migrations/versions/5e6f7a8b9c0d_import_json_messages.py

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"""import_json_messages
22
33
Revision ID: 5e6f7a8b9c0d
4-
Revises: 4d5e6f7a8b9c
4+
Revises: 6f7a8b9c0d1e
55
Create Date: 2025-01-27 12:04:00.000000
66
77
"""
@@ -18,7 +18,7 @@
1818

1919
# revision identifiers, used by Alembic.
2020
revision: str = "5e6f7a8b9c0d"
21-
down_revision: Union[str, None] = "4d5e6f7a8b9c"
21+
down_revision: Union[str, None] = "6f7a8b9c0d1e"
2222
branch_labels: Union[str, Sequence[str], None] = None
2323
depends_on: Union[str, Sequence[str], None] = None
2424

@@ -33,6 +33,7 @@ def _insert_messages_batch(
3333
) -> None:
3434
"""Insert a batch of messages into the database, handling foreign key violations."""
3535
if not messages_batch:
36+
logger.debug("No messages to insert, skipping batch")
3637
return
3738

3839
# Validate and fix foreign key references
@@ -56,6 +57,7 @@ def _validate_and_fix_foreign_keys( # noqa: C901
5657
- If run_id is invalid: set to None
5758
"""
5859
if not messages_batch:
60+
logger.debug("Empty message batch, nothing to validate")
5961
return []
6062

6163
# Extract all foreign key values
@@ -180,6 +182,10 @@ def upgrade() -> None: # noqa: C901
180182
"""Import existing messages from JSON files in workspace directories."""
181183
# Skip if workspaces directory doesn't exist (e.g., first-time setup)
182184
if not workspaces_dir.exists():
185+
logger.debug(
186+
"Workspaces directory does not exist, skipping import of messages",
187+
extra={"workspaces_dir": str(workspaces_dir)},
188+
)
183189
return
184190

185191
# Get the table from the current database schema
@@ -192,17 +198,29 @@ def upgrade() -> None: # noqa: C901
192198
# Iterate through all workspace directories
193199
for workspace_dir in workspaces_dir.iterdir():
194200
if not workspace_dir.is_dir():
201+
logger.debug(
202+
"Skipping non-directory in workspaces",
203+
extra={"path": str(workspace_dir)},
204+
)
195205
continue
196206

197207
workspace_id = workspace_dir.name
198208
messages_dir = workspace_dir / "messages"
199209

200210
if not messages_dir.exists():
211+
logger.debug(
212+
"Messages directory does not exist, skipping workspace",
213+
extra={"workspace_id": workspace_id, "messages_dir": str(messages_dir)},
214+
)
201215
continue
202216

203217
# Iterate through thread directories
204218
for thread_dir in messages_dir.iterdir():
205219
if not thread_dir.is_dir():
220+
logger.debug(
221+
"Skipping non-directory in messages",
222+
extra={"path": str(thread_dir)},
223+
)
206224
continue
207225

208226
# Get all JSON files in the thread directory
@@ -241,6 +259,9 @@ def downgrade() -> None:
241259
result = connection.execute(messages_table.select())
242260
rows = result.fetchall()
243261
if not rows:
262+
logger.debug(
263+
"No messages found in the database, skipping export of rows to json",
264+
)
244265
return
245266

246267
for row in rows:
@@ -257,6 +278,10 @@ def downgrade() -> None:
257278
messages_dir.mkdir(parents=True, exist_ok=True)
258279
json_path = messages_dir / f"{message_model.id}.json"
259280
if json_path.exists():
281+
logger.debug(
282+
"Json file for message already exists, skipping export of row to json",
283+
extra={"message_id": message_model.id, "json_path": str(json_path)},
284+
)
260285
continue
261286
with json_path.open("w", encoding="utf-8") as f:
262287
f.write(message_model.model_dump_json())

src/askui/chat/migrations/versions/6b2c3d4e5f6a_import_json_mcp_configs.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ def _insert_mcp_configs_batch(
3535
) -> None:
3636
"""Insert a batch of MCP configs into the database, ignoring conflicts."""
3737
if not mcp_configs_batch:
38+
logger.debug("No MCP configs to insert, skipping batch")
3839
return
3940

4041
connection.execute(
@@ -52,6 +53,10 @@ def upgrade() -> None:
5253

5354
# Skip if directory doesn't exist (e.g., first-time setup)
5455
if not mcp_configs_dir.exists():
56+
logger.debug(
57+
"MCP configs directory does not exist, skipping import of MCP configs",
58+
extra={"mcp_configs_dir": str(mcp_configs_dir)},
59+
)
5560
return
5661

5762
# Get the table from the current database schema
@@ -97,6 +102,9 @@ def downgrade() -> None:
97102
result = connection.execute(mcp_configs_table.select())
98103
rows = result.fetchall()
99104
if not rows:
105+
logger.debug(
106+
"No MCP configs found in the database, skipping export of rows to json",
107+
)
100108
return
101109

102110
for row in rows:
@@ -106,6 +114,10 @@ def downgrade() -> None:
106114
)
107115
json_path = mcp_configs_dir / f"{mcp_config.id}.json"
108116
if json_path.exists():
117+
logger.debug(
118+
"Json file for mcp config already exists, skipping export of row to json",
119+
extra={"mcp_config_id": mcp_config.id, "json_path": str(json_path)},
120+
)
109121
continue
110122
with json_path.open("w", encoding="utf-8") as f:
111123
f.write(mcp_config.model_dump_json())

0 commit comments

Comments
 (0)