|
| 1 | +"""user_id from uuid to int |
| 2 | +
|
| 3 | +Revision ID: 66abc97f3782 |
| 4 | +Revises: 60b6c511a485 |
| 5 | +Create Date: 2025-06-16 11:05:36.196795 |
| 6 | +
|
| 7 | +""" |
| 8 | +from alembic import op |
| 9 | +import sqlalchemy as sa |
| 10 | +import sqlmodel.sql.sqltypes |
| 11 | + |
| 12 | + |
| 13 | +# revision identifiers, used by Alembic. |
| 14 | +revision = "66abc97f3782" |
| 15 | +down_revision = "60b6c511a485" |
| 16 | +branch_labels = None |
| 17 | +depends_on = None |
| 18 | + |
| 19 | + |
| 20 | +def upgrade(): |
| 21 | + conn = op.get_bind() |
| 22 | + |
| 23 | + # Drop foreign key constraints |
| 24 | + conn.execute( |
| 25 | + sa.text("ALTER TABLE document DROP CONSTRAINT document_owner_id_fkey;") |
| 26 | + ) |
| 27 | + conn.execute( |
| 28 | + sa.text("ALTER TABLE collection DROP CONSTRAINT collection_owner_id_fkey;") |
| 29 | + ) |
| 30 | + conn.execute( |
| 31 | + sa.text("ALTER TABLE projectuser DROP CONSTRAINT projectuser_user_id_fkey;") |
| 32 | + ) |
| 33 | + conn.execute(sa.text("ALTER TABLE apikey DROP CONSTRAINT apikey_user_id_fkey;")) |
| 34 | + |
| 35 | + # Drop primary key constraint on "user" table |
| 36 | + conn.execute(sa.text('ALTER TABLE "user" DROP CONSTRAINT user_pkey;')) |
| 37 | + |
| 38 | + # Create mapping table from UUID to INT |
| 39 | + conn.execute( |
| 40 | + sa.text( |
| 41 | + """ |
| 42 | + CREATE TABLE uuid_to_int_map ( |
| 43 | + user_id_uuid UUID PRIMARY KEY, |
| 44 | + user_id_int INT GENERATED ALWAYS AS IDENTITY |
| 45 | + ); |
| 46 | + """ |
| 47 | + ) |
| 48 | + ) |
| 49 | + |
| 50 | + # Populate mapping table |
| 51 | + conn.execute( |
| 52 | + sa.text('INSERT INTO uuid_to_int_map (user_id_uuid) SELECT id FROM "user";') |
| 53 | + ) |
| 54 | + |
| 55 | + # Add new_id to user table and populate it |
| 56 | + conn.execute(sa.text('ALTER TABLE "user" ADD COLUMN new_id INT;')) |
| 57 | + conn.execute( |
| 58 | + sa.text( |
| 59 | + """ |
| 60 | + UPDATE "user" SET new_id = uuid_map.user_id_int |
| 61 | + FROM uuid_to_int_map uuid_map |
| 62 | + WHERE "user".id = uuid_map.user_id_uuid; |
| 63 | + """ |
| 64 | + ) |
| 65 | + ) |
| 66 | + |
| 67 | + # document |
| 68 | + conn.execute(sa.text("ALTER TABLE document ADD COLUMN new_owner_id INT;")) |
| 69 | + conn.execute( |
| 70 | + sa.text( |
| 71 | + """ |
| 72 | + UPDATE document SET new_owner_id = uuid_map.user_id_int |
| 73 | + FROM uuid_to_int_map uuid_map |
| 74 | + WHERE document.owner_id = uuid_map.user_id_uuid; |
| 75 | + """ |
| 76 | + ) |
| 77 | + ) |
| 78 | + |
| 79 | + # collection |
| 80 | + conn.execute(sa.text("ALTER TABLE collection ADD COLUMN new_owner_id INT;")) |
| 81 | + conn.execute( |
| 82 | + sa.text( |
| 83 | + """ |
| 84 | + UPDATE collection SET new_owner_id = uuid_map.user_id_int |
| 85 | + FROM uuid_to_int_map uuid_map |
| 86 | + WHERE collection.owner_id = uuid_map.user_id_uuid; |
| 87 | + """ |
| 88 | + ) |
| 89 | + ) |
| 90 | + |
| 91 | + # projectuser |
| 92 | + conn.execute(sa.text("ALTER TABLE projectuser ADD COLUMN new_user_id INT;")) |
| 93 | + conn.execute( |
| 94 | + sa.text( |
| 95 | + """ |
| 96 | + UPDATE projectuser SET new_user_id = uuid_map.user_id_int |
| 97 | + FROM uuid_to_int_map uuid_map |
| 98 | + WHERE projectuser.user_id = uuid_map.user_id_uuid; |
| 99 | + """ |
| 100 | + ) |
| 101 | + ) |
| 102 | + |
| 103 | + # apikey |
| 104 | + conn.execute(sa.text("ALTER TABLE apikey ADD COLUMN new_user_id INT;")) |
| 105 | + conn.execute( |
| 106 | + sa.text( |
| 107 | + """ |
| 108 | + UPDATE apikey SET new_user_id = uuid_map.user_id_int |
| 109 | + FROM uuid_to_int_map uuid_map |
| 110 | + WHERE apikey.user_id = uuid_map.user_id_uuid; |
| 111 | + """ |
| 112 | + ) |
| 113 | + ) |
| 114 | + |
| 115 | + # Drop old columns and rename new ones |
| 116 | + conn.execute(sa.text("ALTER TABLE document DROP COLUMN owner_id;")) |
| 117 | + conn.execute( |
| 118 | + sa.text("ALTER TABLE document RENAME COLUMN new_owner_id TO owner_id;") |
| 119 | + ) |
| 120 | + |
| 121 | + conn.execute(sa.text("ALTER TABLE collection DROP COLUMN owner_id;")) |
| 122 | + conn.execute( |
| 123 | + sa.text("ALTER TABLE collection RENAME COLUMN new_owner_id TO owner_id;") |
| 124 | + ) |
| 125 | + |
| 126 | + conn.execute(sa.text("ALTER TABLE projectuser DROP COLUMN user_id;")) |
| 127 | + conn.execute( |
| 128 | + sa.text("ALTER TABLE projectuser RENAME COLUMN new_user_id TO user_id;") |
| 129 | + ) |
| 130 | + |
| 131 | + conn.execute(sa.text("ALTER TABLE apikey DROP COLUMN user_id;")) |
| 132 | + conn.execute(sa.text("ALTER TABLE apikey RENAME COLUMN new_user_id TO user_id;")) |
| 133 | + |
| 134 | + conn.execute(sa.text('ALTER TABLE "user" DROP COLUMN id;')) |
| 135 | + conn.execute(sa.text('ALTER TABLE "user" RENAME COLUMN new_id TO id;')) |
| 136 | + |
| 137 | + # Re-add primary key |
| 138 | + conn.execute(sa.text('ALTER TABLE "user" ADD PRIMARY KEY (id);')) |
| 139 | + |
| 140 | + # Create sequence for new integer IDs |
| 141 | + conn.execute(sa.text('CREATE SEQUENCE user_id_seq START 1 OWNED BY "user".id;')) |
| 142 | + conn.execute( |
| 143 | + sa.text( |
| 144 | + "ALTER TABLE \"user\" ALTER COLUMN id SET DEFAULT nextval('user_id_seq');" |
| 145 | + ) |
| 146 | + ) |
| 147 | + conn.execute( |
| 148 | + sa.text("SELECT setval('user_id_seq', (SELECT MAX(id) FROM \"user\"));") |
| 149 | + ) |
| 150 | + |
| 151 | + # Drop mapping table |
| 152 | + conn.execute(sa.text("DROP TABLE uuid_to_int_map;")) |
| 153 | + |
| 154 | + |
| 155 | +def downgrade(): |
| 156 | + pass |
0 commit comments