|
| 1 | +"""Add HTML Status Info table |
| 2 | +
|
| 3 | +Revision ID: 99eceed6e614 |
| 4 | +Revises: 637de6eaa3ab |
| 5 | +Create Date: 2025-07-31 15:36:40.966605 |
| 6 | +
|
| 7 | +""" |
| 8 | +from typing import Sequence, Union |
| 9 | + |
| 10 | +from alembic import op |
| 11 | +import sqlalchemy as sa |
| 12 | + |
| 13 | +from src.util.alembic_helpers import id_column, created_at_column, updated_at_column, url_id_column, switch_enum_type |
| 14 | + |
| 15 | +# revision identifiers, used by Alembic. |
| 16 | +revision: str = '99eceed6e614' |
| 17 | +down_revision: Union[str, None] = '637de6eaa3ab' |
| 18 | +branch_labels: Union[str, Sequence[str], None] = None |
| 19 | +depends_on: Union[str, Sequence[str], None] = None |
| 20 | + |
| 21 | +WEB_STATUS_ENUM = sa.Enum( |
| 22 | + "not_attempted", |
| 23 | + "success", |
| 24 | + "error", |
| 25 | + "404_not_found", |
| 26 | + name="web_status" |
| 27 | +) |
| 28 | +SCRAPE_STATUS_ENUM = sa.Enum( |
| 29 | + "success", |
| 30 | + "error", |
| 31 | + name="scrape_status", |
| 32 | +) |
| 33 | + |
| 34 | +URL_WEB_METADATA_TABLE_NAME = 'url_web_metadata' |
| 35 | +URL_SCRAPE_INFO = 'url_scrape_info' |
| 36 | + |
| 37 | + |
| 38 | + |
| 39 | + |
| 40 | + |
| 41 | +def upgrade() -> None: |
| 42 | + _create_url_html_info_table() |
| 43 | + _add_url_probe_task_type_enum() |
| 44 | + _set_up_scrape_info_table() |
| 45 | + _use_existing_html_data_to_add_scrape_info() |
| 46 | + |
| 47 | +def _use_existing_html_data_to_add_scrape_info(): |
| 48 | + op.execute( |
| 49 | + f""" |
| 50 | + INSERT INTO {URL_SCRAPE_INFO} (url_id, status) |
| 51 | + SELECT url_id, 'success'::scrape_status |
| 52 | + FROM url_compressed_html |
| 53 | + """ |
| 54 | + ) |
| 55 | + op.execute( |
| 56 | + f""" |
| 57 | + INSERT INTO {URL_SCRAPE_INFO} (url_id, status) |
| 58 | + SELECT distinct(url_id), 'success'::scrape_status |
| 59 | + FROM url_html_content |
| 60 | + LEFT JOIN URL_COMPRESSED_HTML USING (url_id) |
| 61 | + WHERE URL_COMPRESSED_HTML.url_id IS NULL |
| 62 | + """ |
| 63 | + ) |
| 64 | + |
| 65 | +def downgrade() -> None: |
| 66 | + _drop_scrape_info_table() |
| 67 | + # Drop Enums |
| 68 | + WEB_STATUS_ENUM.drop(op.get_bind(), checkfirst=True) |
| 69 | + _drop_url_probe_task_type_enum() |
| 70 | + _tear_down_scrape_info_table() |
| 71 | + |
| 72 | + |
| 73 | +def _set_up_scrape_info_table(): |
| 74 | + op.create_table( |
| 75 | + URL_SCRAPE_INFO, |
| 76 | + id_column(), |
| 77 | + url_id_column(), |
| 78 | + sa.Column( |
| 79 | + 'status', |
| 80 | + SCRAPE_STATUS_ENUM, |
| 81 | + nullable=False, |
| 82 | + comment='The status of the most recent scrape attempt.' |
| 83 | + ), |
| 84 | + created_at_column(), |
| 85 | + updated_at_column(), |
| 86 | + sa.UniqueConstraint('url_id', name='uq_url_scrape_info_url_id') |
| 87 | + ) |
| 88 | + |
| 89 | + |
| 90 | + |
| 91 | + |
| 92 | +def _tear_down_scrape_info_table(): |
| 93 | + op.drop_table(URL_SCRAPE_INFO) |
| 94 | + # Drop enum |
| 95 | + SCRAPE_STATUS_ENUM.drop(op.get_bind(), checkfirst=True) |
| 96 | + |
| 97 | + |
| 98 | +def _add_url_probe_task_type_enum() -> None: |
| 99 | + switch_enum_type( |
| 100 | + table_name='tasks', |
| 101 | + column_name='task_type', |
| 102 | + enum_name='task_type', |
| 103 | + new_enum_values=[ |
| 104 | + 'HTML', |
| 105 | + 'Relevancy', |
| 106 | + 'Record Type', |
| 107 | + 'Agency Identification', |
| 108 | + 'Misc Metadata', |
| 109 | + 'Submit Approved URLs', |
| 110 | + 'Duplicate Detection', |
| 111 | + '404 Probe', |
| 112 | + 'Sync Agencies', |
| 113 | + 'Sync Data Sources', |
| 114 | + 'Push to Hugging Face', |
| 115 | + 'URL Probe' |
| 116 | + ] |
| 117 | + ) |
| 118 | + |
| 119 | +def _drop_url_probe_task_type_enum() -> None: |
| 120 | + switch_enum_type( |
| 121 | + table_name='tasks', |
| 122 | + column_name='task_type', |
| 123 | + enum_name='task_type', |
| 124 | + new_enum_values=[ |
| 125 | + 'HTML', |
| 126 | + 'Relevancy', |
| 127 | + 'Record Type', |
| 128 | + 'Agency Identification', |
| 129 | + 'Misc Metadata', |
| 130 | + 'Submit Approved URLs', |
| 131 | + 'Duplicate Detection', |
| 132 | + '404 Probe', |
| 133 | + 'Sync Agencies', |
| 134 | + 'Sync Data Sources', |
| 135 | + 'Push to Hugging Face' |
| 136 | + ] |
| 137 | + ) |
| 138 | + |
| 139 | +def _create_url_html_info_table() -> None: |
| 140 | + op.create_table( |
| 141 | + URL_WEB_METADATA_TABLE_NAME, |
| 142 | + id_column(), |
| 143 | + url_id_column(), |
| 144 | + sa.Column('accessed', sa.Boolean(), nullable=False), |
| 145 | + sa.Column('status_code', sa.Integer(), nullable=True), |
| 146 | + sa.Column('content_type', sa.Text(), nullable=True), |
| 147 | + sa.Column('error_message', sa.Text(), nullable=True), |
| 148 | + created_at_column(), |
| 149 | + updated_at_column(), |
| 150 | + sa.UniqueConstraint('url_id', name='uq_url_web_status_info_url_id'), |
| 151 | + sa.CheckConstraint('status_code >= 100', name='ck_url_web_status_info_status_code_min'), |
| 152 | + sa.CheckConstraint('status_code <= 999', name='ck_url_web_status_info_status_code_max'), |
| 153 | + ) |
| 154 | + |
| 155 | +def _drop_scrape_info_table() -> None: |
| 156 | + op.drop_table(URL_WEB_METADATA_TABLE_NAME) |
0 commit comments