|
| 1 | +"""Add DISCORD_CONNECTOR to SearchSourceConnectorType and DocumentType enums |
| 2 | +
|
| 3 | +Revision ID: 9 |
| 4 | +Revises: 8 |
| 5 | +""" |
| 6 | + |
| 7 | +from typing import Sequence, Union |
| 8 | + |
| 9 | +from alembic import op |
| 10 | +import sqlalchemy as sa |
| 11 | + |
| 12 | + |
| 13 | +# revision identifiers, used by Alembic. |
| 14 | +revision: str = "9" |
| 15 | +down_revision: Union[str, None] = "8" |
| 16 | +branch_labels: Union[str, Sequence[str], None] = None |
| 17 | +depends_on: Union[str, Sequence[str], None] = None |
| 18 | + |
| 19 | +# Define the ENUM type name and the new value |
| 20 | +CONNECTOR_ENUM = "searchsourceconnectortype" |
| 21 | +CONNECTOR_NEW_VALUE = "DISCORD_CONNECTOR" |
| 22 | +DOCUMENT_ENUM = "documenttype" |
| 23 | +DOCUMENT_NEW_VALUE = "DISCORD_CONNECTOR" |
| 24 | + |
| 25 | + |
| 26 | +def upgrade() -> None: |
| 27 | + """Upgrade schema - add DISCORD_CONNECTOR to connector and document enum.""" |
| 28 | + # Add DISCORD_CONNECTOR to searchsourceconnectortype |
| 29 | + op.execute(f"ALTER TYPE {CONNECTOR_ENUM} ADD VALUE '{CONNECTOR_NEW_VALUE}'") |
| 30 | + # Add DISCORD_CONNECTOR to documenttype |
| 31 | + op.execute(f"ALTER TYPE {DOCUMENT_ENUM} ADD VALUE '{DOCUMENT_NEW_VALUE}'") |
| 32 | + |
| 33 | + |
| 34 | +def downgrade() -> None: |
| 35 | + """Downgrade schema - remove DISCORD_CONNECTOR from connector and document enum.""" |
| 36 | + |
| 37 | + # Old enum name |
| 38 | + old_connector_enum_name = f"{CONNECTOR_ENUM}_old" |
| 39 | + old_document_enum_name = f"{DOCUMENT_ENUM}_old" |
| 40 | + |
| 41 | + old_connector_values = ( |
| 42 | + "SERPER_API", |
| 43 | + "TAVILY_API", |
| 44 | + "LINKUP_API", |
| 45 | + "SLACK_CONNECTOR", |
| 46 | + "NOTION_CONNECTOR", |
| 47 | + "GITHUB_CONNECTOR", |
| 48 | + "LINEAR_CONNECTOR", |
| 49 | + ) |
| 50 | + old_document_values = ( |
| 51 | + "EXTENSION", |
| 52 | + "CRAWLED_URL", |
| 53 | + "FILE", |
| 54 | + "SLACK_CONNECTOR", |
| 55 | + "NOTION_CONNECTOR", |
| 56 | + "YOUTUBE_VIDEO", |
| 57 | + "GITHUB_CONNECTOR", |
| 58 | + "LINEAR_CONNECTOR", |
| 59 | + ) |
| 60 | + |
| 61 | + old_connector_values_sql = ", ".join([f"'{v}'" for v in old_connector_values]) |
| 62 | + old_document_values_sql = ", ".join([f"'{v}'" for v in old_document_values]) |
| 63 | + |
| 64 | + # Table and column names (adjust if different) |
| 65 | + connector_table_name = "search_source_connectors" |
| 66 | + connector_column_name = "connector_type" |
| 67 | + document_table_name = "documents" |
| 68 | + document_column_name = "document_type" |
| 69 | + |
| 70 | + # Connector Enum Downgrade Steps |
| 71 | + # 1. Rename the current connector enum type |
| 72 | + op.execute(f"ALTER TYPE {CONNECTOR_ENUM} RENAME TO {old_connector_enum_name}") |
| 73 | + |
| 74 | + # 2. Create the new connector enum type with the old values |
| 75 | + op.execute(f"CREATE TYPE {CONNECTOR_ENUM} AS ENUM({old_connector_values_sql})") |
| 76 | + |
| 77 | + # 3. Update the connector table: |
| 78 | + op.execute( |
| 79 | + f"ALTER TABLE {connector_table_name} " |
| 80 | + f"ALTER COLUMN {connector_column_name} " |
| 81 | + f"TYPE {CONNECTOR_ENUM} " |
| 82 | + f"USING {connector_column_name}::text::{CONNECTOR_ENUM}" |
| 83 | + ) |
| 84 | + |
| 85 | + # 4. Drop the old connector enum type |
| 86 | + op.execute(f"DROP TYPE {old_connector_enum_name}") |
| 87 | + |
| 88 | + |
| 89 | + # Document Enum Downgrade Steps |
| 90 | + # 1. Rename the current document enum type |
| 91 | + op.execute(f"ALTER TYPE {DOCUMENT_ENUM} RENAME TO {old_document_enum_name}") |
| 92 | + |
| 93 | + # 2. Create the new document enum type with the old values |
| 94 | + op.execute(f"CREATE TYPE {DOCUMENT_ENUM} AS ENUM({old_document_values_sql})") |
| 95 | + |
| 96 | + # 3. Delete rows with the new value from the documents table |
| 97 | + op.execute( |
| 98 | + f"DELETE FROM {document_table_name} WHERE {document_column_name}::text = '{DOCUMENT_NEW_VALUE}'" |
| 99 | + ) |
| 100 | + |
| 101 | + # 4. Alter the document table to use the new enum type (casting old values) |
| 102 | + op.execute( |
| 103 | + f"ALTER TABLE {document_table_name} " |
| 104 | + f"ALTER COLUMN {document_column_name} " |
| 105 | + f"TYPE {DOCUMENT_ENUM} " |
| 106 | + f"USING {document_column_name}::text::{DOCUMENT_ENUM}" |
| 107 | + ) |
| 108 | + |
| 109 | + # 5. Drop the old enum types |
| 110 | + op.execute(f"DROP TYPE {old_document_enum_name}") |
| 111 | + |
| 112 | + # ### end Alembic commands ### |
0 commit comments