-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Feat/Discord Connector #125
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
Changes from all commits
0391c22
4b3c662
158976e
1d67a87
a0f9efd
303856e
5952356
d11b636
6b38129
afc1100
42cc1b8
ebfc2cb
a3c9148
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,112 @@ | ||
| """Add DISCORD_CONNECTOR to SearchSourceConnectorType and DocumentType enums | ||
|
|
||
| Revision ID: 9 | ||
| Revises: 8 | ||
| """ | ||
|
|
||
| from typing import Sequence, Union | ||
|
|
||
| from alembic import op | ||
| import sqlalchemy as sa | ||
|
|
||
|
|
||
| # revision identifiers, used by Alembic. | ||
| revision: str = "9" | ||
| down_revision: Union[str, None] = "8" | ||
| branch_labels: Union[str, Sequence[str], None] = None | ||
| depends_on: Union[str, Sequence[str], None] = None | ||
|
|
||
| # Define the ENUM type name and the new value | ||
| CONNECTOR_ENUM = "searchsourceconnectortype" | ||
| CONNECTOR_NEW_VALUE = "DISCORD_CONNECTOR" | ||
| DOCUMENT_ENUM = "documenttype" | ||
| DOCUMENT_NEW_VALUE = "DISCORD_CONNECTOR" | ||
|
|
||
|
|
||
| def upgrade() -> None: | ||
| """Upgrade schema - add DISCORD_CONNECTOR to connector and document enum.""" | ||
| # Add DISCORD_CONNECTOR to searchsourceconnectortype | ||
| op.execute(f"ALTER TYPE {CONNECTOR_ENUM} ADD VALUE '{CONNECTOR_NEW_VALUE}'") | ||
| # Add DISCORD_CONNECTOR to documenttype | ||
| op.execute(f"ALTER TYPE {DOCUMENT_ENUM} ADD VALUE '{DOCUMENT_NEW_VALUE}'") | ||
|
|
||
|
|
||
| def downgrade() -> None: | ||
| """Downgrade schema - remove DISCORD_CONNECTOR from connector and document enum.""" | ||
|
|
||
| # Old enum name | ||
| old_connector_enum_name = f"{CONNECTOR_ENUM}_old" | ||
| old_document_enum_name = f"{DOCUMENT_ENUM}_old" | ||
|
|
||
| old_connector_values = ( | ||
| "SERPER_API", | ||
| "TAVILY_API", | ||
| "LINKUP_API", | ||
| "SLACK_CONNECTOR", | ||
| "NOTION_CONNECTOR", | ||
| "GITHUB_CONNECTOR", | ||
| "LINEAR_CONNECTOR", | ||
| ) | ||
| old_document_values = ( | ||
| "EXTENSION", | ||
| "CRAWLED_URL", | ||
| "FILE", | ||
| "SLACK_CONNECTOR", | ||
| "NOTION_CONNECTOR", | ||
| "YOUTUBE_VIDEO", | ||
| "GITHUB_CONNECTOR", | ||
| "LINEAR_CONNECTOR", | ||
| ) | ||
|
|
||
| old_connector_values_sql = ", ".join([f"'{v}'" for v in old_connector_values]) | ||
| old_document_values_sql = ", ".join([f"'{v}'" for v in old_document_values]) | ||
|
|
||
| # Table and column names (adjust if different) | ||
| connector_table_name = "search_source_connectors" | ||
| connector_column_name = "connector_type" | ||
| document_table_name = "documents" | ||
| document_column_name = "document_type" | ||
|
|
||
| # Connector Enum Downgrade Steps | ||
| # 1. Rename the current connector enum type | ||
| op.execute(f"ALTER TYPE {CONNECTOR_ENUM} RENAME TO {old_connector_enum_name}") | ||
|
|
||
| # 2. Create the new connector enum type with the old values | ||
| op.execute(f"CREATE TYPE {CONNECTOR_ENUM} AS ENUM({old_connector_values_sql})") | ||
|
|
||
| # 3. Update the connector table: | ||
| op.execute( | ||
| f"ALTER TABLE {connector_table_name} " | ||
| f"ALTER COLUMN {connector_column_name} " | ||
| f"TYPE {CONNECTOR_ENUM} " | ||
| f"USING {connector_column_name}::text::{CONNECTOR_ENUM}" | ||
| ) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unsafe type casting in the ALTER TABLE statement for search_source_connectors. The code attempts to cast connector_type to the new enum type without first verifying that all existing values can be safely cast. If any DISCORD_CONNECTOR entries exist, the cast will fail since the new enum type doesn't include this value. A DELETE operation should be performed before the cast.
|
||
|
|
||
| # 4. Drop the old connector enum type | ||
| op.execute(f"DROP TYPE {old_connector_enum_name}") | ||
|
|
||
|
|
||
| # Document Enum Downgrade Steps | ||
| # 1. Rename the current document enum type | ||
| op.execute(f"ALTER TYPE {DOCUMENT_ENUM} RENAME TO {old_document_enum_name}") | ||
|
|
||
| # 2. Create the new document enum type with the old values | ||
| op.execute(f"CREATE TYPE {DOCUMENT_ENUM} AS ENUM({old_document_values_sql})") | ||
|
|
||
| # 3. Delete rows with the new value from the documents table | ||
| op.execute( | ||
| f"DELETE FROM {document_table_name} WHERE {document_column_name}::text = '{DOCUMENT_NEW_VALUE}'" | ||
| ) | ||
|
|
||
| # 4. Alter the document table to use the new enum type (casting old values) | ||
| op.execute( | ||
| f"ALTER TABLE {document_table_name} " | ||
| f"ALTER COLUMN {document_column_name} " | ||
| f"TYPE {DOCUMENT_ENUM} " | ||
| f"USING {document_column_name}::text::{DOCUMENT_ENUM}" | ||
| ) | ||
|
|
||
| # 5. Drop the old enum types | ||
| op.execute(f"DROP TYPE {old_document_enum_name}") | ||
|
|
||
| # ### end Alembic commands ### | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing DELETE operation for search_source_connectors table in downgrade(). While the code deletes DISCORD_CONNECTOR documents, it doesn't delete the corresponding connector entries. This asymmetry could lead to orphaned data since the SearchSourceConnector table has a unique constraint on connector_type. When downgrading and recreating the enum without DISCORD_CONNECTOR, any existing DISCORD_CONNECTOR entries would violate the type constraint.