Repository navigation
fix(workspaces): reject non-existent file IDs in add_files_to_workspace #281
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
877dcef
fix(workspaces): reject non-existent file IDs in add_files_to_workspace
EnjoyBacon7 484e23e
fix(models): add FK from workspace_files.file_id to files.file_id
EnjoyBacon7 31859f6
test(workspaces): add unit tests for get_existing_file_ids validation
EnjoyBacon7 1b154df
fix(workspaces): use files.id (int) FK in workspace_files instead of …
EnjoyBacon7 8a41e71
fix(workspaces): fix delete_workspace orphan logic, migration ambigui…
EnjoyBacon7 f6d6456
fix(workspaces): fix integration tests and remaining review issues
EnjoyBacon7 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
openrag/scripts/migrations/alembic/versions/f1a2b3c4d5e6_add_workspace_files_file_id_fk.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| """add FK from workspace_files.file_id (int) to files.id | ||
|
|
||
| Revision ID: f1a2b3c4d5e6 | ||
| Revises: e7f8a9b0c1d2 | ||
| Create Date: 2026-03-12 10:00:00.000000 | ||
|
|
||
| """ | ||
|
|
||
| from collections.abc import Sequence | ||
|
|
||
| import sqlalchemy as sa | ||
| from alembic import op | ||
|
|
||
| # revision identifiers, used by Alembic. | ||
| revision: str = "f1a2b3c4d5e6" | ||
| down_revision: str | Sequence[str] | None = "e7f8a9b0c1d2" | ||
| branch_labels: str | Sequence[str] | None = None | ||
| depends_on: str | Sequence[str] | None = None | ||
|
|
||
|
|
||
| def upgrade() -> None: | ||
| """Migrate workspace_files.file_id from string to integer FK referencing files.id.""" | ||
| # 1. Purge rows that have no matching file (no valid files.file_id to JOIN against). | ||
| op.execute("DELETE FROM workspace_files WHERE file_id NOT IN (SELECT file_id FROM files)") | ||
|
|
||
| # 2. Add a temporary integer column to hold the resolved files.id value. | ||
| op.add_column("workspace_files", sa.Column("file_fk", sa.Integer(), nullable=True)) | ||
|
|
||
| # 3. Populate it by joining on the string file_id, scoped to the workspace's partition | ||
| # to resolve ambiguity when the same filename exists in multiple partitions. | ||
| op.execute( | ||
| "UPDATE workspace_files wf " | ||
| "SET file_fk = f.id " | ||
| "FROM files f " | ||
| "JOIN workspaces w ON w.workspace_id = wf.workspace_id " | ||
| "WHERE f.file_id = wf.file_id AND f.partition_name = w.partition_name" | ||
| ) | ||
|
|
||
| # 3b. Drop any rows that couldn't be resolved (file_fk still NULL). | ||
| op.execute("DELETE FROM workspace_files WHERE file_fk IS NULL") | ||
|
|
||
| # 4. Drop the old string column and its index. | ||
| op.drop_index("ix_workspace_files_file_id", table_name="workspace_files") | ||
| op.drop_column("workspace_files", "file_id") | ||
|
|
||
| # 5. Rename file_fk → file_id, make it NOT NULL. | ||
| op.alter_column("workspace_files", "file_fk", new_column_name="file_id", nullable=False) | ||
|
|
||
| # 6. Recreate the index, unique constraint, and FK. | ||
| op.create_index("ix_workspace_files_file_id", "workspace_files", ["file_id"]) | ||
| op.create_unique_constraint("uix_workspace_file", "workspace_files", ["workspace_id", "file_id"]) | ||
| op.create_foreign_key( | ||
| "fk_workspace_files_file_id", | ||
| "workspace_files", | ||
| "files", | ||
| ["file_id"], | ||
| ["id"], | ||
| ondelete="CASCADE", | ||
| ) | ||
|
coderabbitai[bot] marked this conversation as resolved.
Resolved
|
||
|
|
||
|
|
||
| def downgrade() -> None: | ||
| """Revert workspace_files.file_id back to a string column.""" | ||
| op.drop_constraint("fk_workspace_files_file_id", "workspace_files", type_="foreignkey") | ||
| op.drop_index("ix_workspace_files_file_id", table_name="workspace_files") | ||
|
|
||
| # Re-add a string column and repopulate from files.file_id via JOIN. | ||
| op.add_column("workspace_files", sa.Column("file_str", sa.String(), nullable=True)) | ||
| op.execute("UPDATE workspace_files wf SET file_str = f.file_id FROM files f WHERE f.id = wf.file_id") | ||
| op.drop_column("workspace_files", "file_id") | ||
| op.alter_column("workspace_files", "file_str", new_column_name="file_id", nullable=False) | ||
| op.create_index("ix_workspace_files_file_id", "workspace_files", ["file_id"]) | ||
| op.create_unique_constraint("uix_workspace_file", "workspace_files", ["workspace_id", "file_id"]) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.