|
| 1 | +"""Normalize object storage URLs to object keys. |
| 2 | +
|
| 3 | +Previously, widget_image.image_url, widget_documents.url (for uploaded files), |
| 4 | +and tenant.hero_image_url stored full S3 URLs |
| 5 | +(e.g. https://host/bucket/my-file.png). Going forward only the object key |
| 6 | +(e.g. my-file.png) is stored; URLs are constructed dynamically at read time |
| 7 | +using the S3_BUCKET / S3_HOST environment variables. |
| 8 | +
|
| 9 | +This migration strips the bucket+host prefix from any rows that still contain |
| 10 | +a full URL, leaving only the object key. The downgrade puts them back using |
| 11 | +the values of those env vars at the time it is run. |
| 12 | +
|
| 13 | +Revision ID: 8e50e47b7d32 |
| 14 | +Revises: d756d0fe942c |
| 15 | +Create Date: 2026-04-29 00:00:00.000000 |
| 16 | +""" |
| 17 | +import os |
| 18 | + |
| 19 | +from alembic import op |
| 20 | +import sqlalchemy as sa |
| 21 | + |
| 22 | +# revision identifiers, used by Alembic. |
| 23 | +revision = '8e50e47b7d32' |
| 24 | +down_revision = 'd756d0fe942c' |
| 25 | +branch_labels = None |
| 26 | +depends_on = None |
| 27 | + |
| 28 | + |
| 29 | +def _url_prefix() -> str | None: |
| 30 | + """Return the full URL prefix that was previously prepended to object keys. |
| 31 | +
|
| 32 | + Returns None when S3_HOST or S3_BUCKET are not configured, in which case |
| 33 | + nothing needs to be migrated (no full URLs could have been stored). |
| 34 | + """ |
| 35 | + host = os.getenv('S3_HOST') |
| 36 | + bucket = os.getenv('S3_BUCKET') |
| 37 | + if not host or not bucket: |
| 38 | + return None |
| 39 | + return f'https://{host}/{bucket}/' |
| 40 | + |
| 41 | + |
| 42 | +def upgrade(): |
| 43 | + prefix = _url_prefix() |
| 44 | + if not prefix: |
| 45 | + return |
| 46 | + |
| 47 | + conn = op.get_bind() |
| 48 | + |
| 49 | + # ---- widget_image.image_url ---------------------------------------- |
| 50 | + conn.execute( |
| 51 | + sa.text( |
| 52 | + """ |
| 53 | + UPDATE widget_image |
| 54 | + SET image_url = SUBSTRING(image_url FROM :prefix_len) |
| 55 | + WHERE image_url LIKE :prefix_pattern |
| 56 | + """ |
| 57 | + ), |
| 58 | + { |
| 59 | + 'prefix_len': len(prefix) + 1, # 1-based SUBSTRING index |
| 60 | + 'prefix_pattern': prefix + '%', |
| 61 | + }, |
| 62 | + ) |
| 63 | + |
| 64 | + # ---- widget_documents.url (uploaded files only) -------------------- |
| 65 | + conn.execute( |
| 66 | + sa.text( |
| 67 | + """ |
| 68 | + UPDATE widget_documents |
| 69 | + SET url = SUBSTRING(url FROM :prefix_len) |
| 70 | + WHERE is_uploaded = TRUE |
| 71 | + AND url LIKE :prefix_pattern |
| 72 | + """ |
| 73 | + ), |
| 74 | + { |
| 75 | + 'prefix_len': len(prefix) + 1, |
| 76 | + 'prefix_pattern': prefix + '%', |
| 77 | + }, |
| 78 | + ) |
| 79 | + |
| 80 | + # ---- tenant.hero_image_url ----------------------------------------- |
| 81 | + conn.execute( |
| 82 | + sa.text( |
| 83 | + """ |
| 84 | + UPDATE tenant |
| 85 | + SET hero_image_url = SUBSTRING(hero_image_url FROM :prefix_len) |
| 86 | + WHERE hero_image_url LIKE :prefix_pattern |
| 87 | + """ |
| 88 | + ), |
| 89 | + { |
| 90 | + 'prefix_len': len(prefix) + 1, |
| 91 | + 'prefix_pattern': prefix + '%', |
| 92 | + }, |
| 93 | + ) |
| 94 | + |
| 95 | + |
| 96 | +def downgrade(): |
| 97 | + prefix = _url_prefix() |
| 98 | + if not prefix: |
| 99 | + return |
| 100 | + |
| 101 | + conn = op.get_bind() |
| 102 | + |
| 103 | + # Prepend the prefix back to any rows that do NOT already start with https:// |
| 104 | + conn.execute( |
| 105 | + sa.text( |
| 106 | + """ |
| 107 | + UPDATE widget_image |
| 108 | + SET image_url = :prefix || image_url |
| 109 | + WHERE image_url IS NOT NULL |
| 110 | + AND image_url NOT LIKE 'https://%' |
| 111 | + AND image_url <> '' |
| 112 | + """ |
| 113 | + ), |
| 114 | + {'prefix': prefix}, |
| 115 | + ) |
| 116 | + |
| 117 | + conn.execute( |
| 118 | + sa.text( |
| 119 | + """ |
| 120 | + UPDATE widget_documents |
| 121 | + SET url = :prefix || url |
| 122 | + WHERE is_uploaded = TRUE |
| 123 | + AND url IS NOT NULL |
| 124 | + AND url NOT LIKE 'https://%' |
| 125 | + AND url <> '' |
| 126 | + """ |
| 127 | + ), |
| 128 | + {'prefix': prefix}, |
| 129 | + ) |
| 130 | + |
| 131 | + conn.execute( |
| 132 | + sa.text( |
| 133 | + """ |
| 134 | + UPDATE tenant |
| 135 | + SET hero_image_url = :prefix || hero_image_url |
| 136 | + WHERE hero_image_url IS NOT NULL |
| 137 | + AND hero_image_url NOT LIKE 'https://%' |
| 138 | + AND hero_image_url <> '' |
| 139 | + """ |
| 140 | + ), |
| 141 | + {'prefix': prefix}, |
| 142 | + ) |
0 commit comments