-
Notifications
You must be signed in to change notification settings - Fork 7
Migrate User Model ID from UUID to Integer #219
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
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
8b3d4cc
database migration for user_id to int
avirajsingh7 7f93f62
changes in api and crud
avirajsingh7 cf30151
fix test cases
avirajsingh7 5c6d144
fix migration
avirajsingh7 494533f
run precommit
avirajsingh7 0827b8a
fix migration head
avirajsingh7 73b1453
pre commit run
avirajsingh7 39e88d5
drop unnecessary migration
avirajsingh7 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
156 changes: 156 additions & 0 deletions
156
backend/app/alembic/versions/66abc97f3782_user_id_from_uuid_to_int.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,156 @@ | ||
| """user_id from uuid to int | ||
|
|
||
| Revision ID: 66abc97f3782 | ||
| Revises: 60b6c511a485 | ||
| Create Date: 2025-06-16 11:05:36.196795 | ||
|
|
||
| """ | ||
| from alembic import op | ||
| import sqlalchemy as sa | ||
| import sqlmodel.sql.sqltypes | ||
|
|
||
|
|
||
| # revision identifiers, used by Alembic. | ||
| revision = "66abc97f3782" | ||
| down_revision = "60b6c511a485" | ||
| branch_labels = None | ||
| depends_on = None | ||
|
|
||
|
|
||
| def upgrade(): | ||
| conn = op.get_bind() | ||
|
|
||
| # Drop foreign key constraints | ||
| conn.execute( | ||
| sa.text("ALTER TABLE document DROP CONSTRAINT document_owner_id_fkey;") | ||
| ) | ||
| conn.execute( | ||
| sa.text("ALTER TABLE collection DROP CONSTRAINT collection_owner_id_fkey;") | ||
| ) | ||
| conn.execute( | ||
| sa.text("ALTER TABLE projectuser DROP CONSTRAINT projectuser_user_id_fkey;") | ||
| ) | ||
| conn.execute(sa.text("ALTER TABLE apikey DROP CONSTRAINT apikey_user_id_fkey;")) | ||
|
|
||
| # Drop primary key constraint on "user" table | ||
| conn.execute(sa.text('ALTER TABLE "user" DROP CONSTRAINT user_pkey;')) | ||
|
|
||
| # Create mapping table from UUID to INT | ||
| conn.execute( | ||
| sa.text( | ||
| """ | ||
| CREATE TABLE uuid_to_int_map ( | ||
| user_id_uuid UUID PRIMARY KEY, | ||
| user_id_int INT GENERATED ALWAYS AS IDENTITY | ||
| ); | ||
| """ | ||
| ) | ||
| ) | ||
|
|
||
| # Populate mapping table | ||
| conn.execute( | ||
| sa.text('INSERT INTO uuid_to_int_map (user_id_uuid) SELECT id FROM "user";') | ||
| ) | ||
|
|
||
| # Add new_id to user table and populate it | ||
| conn.execute(sa.text('ALTER TABLE "user" ADD COLUMN new_id INT;')) | ||
| conn.execute( | ||
| sa.text( | ||
| """ | ||
| UPDATE "user" SET new_id = uuid_map.user_id_int | ||
| FROM uuid_to_int_map uuid_map | ||
| WHERE "user".id = uuid_map.user_id_uuid; | ||
| """ | ||
| ) | ||
| ) | ||
|
|
||
| # document | ||
| conn.execute(sa.text("ALTER TABLE document ADD COLUMN new_owner_id INT;")) | ||
| conn.execute( | ||
| sa.text( | ||
| """ | ||
| UPDATE document SET new_owner_id = uuid_map.user_id_int | ||
| FROM uuid_to_int_map uuid_map | ||
| WHERE document.owner_id = uuid_map.user_id_uuid; | ||
| """ | ||
| ) | ||
| ) | ||
|
|
||
| # collection | ||
| conn.execute(sa.text("ALTER TABLE collection ADD COLUMN new_owner_id INT;")) | ||
| conn.execute( | ||
| sa.text( | ||
| """ | ||
| UPDATE collection SET new_owner_id = uuid_map.user_id_int | ||
| FROM uuid_to_int_map uuid_map | ||
| WHERE collection.owner_id = uuid_map.user_id_uuid; | ||
| """ | ||
| ) | ||
| ) | ||
|
|
||
| # projectuser | ||
| conn.execute(sa.text("ALTER TABLE projectuser ADD COLUMN new_user_id INT;")) | ||
| conn.execute( | ||
| sa.text( | ||
| """ | ||
| UPDATE projectuser SET new_user_id = uuid_map.user_id_int | ||
| FROM uuid_to_int_map uuid_map | ||
| WHERE projectuser.user_id = uuid_map.user_id_uuid; | ||
| """ | ||
| ) | ||
| ) | ||
|
|
||
| # apikey | ||
| conn.execute(sa.text("ALTER TABLE apikey ADD COLUMN new_user_id INT;")) | ||
| conn.execute( | ||
| sa.text( | ||
| """ | ||
| UPDATE apikey SET new_user_id = uuid_map.user_id_int | ||
| FROM uuid_to_int_map uuid_map | ||
| WHERE apikey.user_id = uuid_map.user_id_uuid; | ||
| """ | ||
| ) | ||
| ) | ||
|
|
||
| # Drop old columns and rename new ones | ||
| conn.execute(sa.text("ALTER TABLE document DROP COLUMN owner_id;")) | ||
| conn.execute( | ||
| sa.text("ALTER TABLE document RENAME COLUMN new_owner_id TO owner_id;") | ||
| ) | ||
|
|
||
| conn.execute(sa.text("ALTER TABLE collection DROP COLUMN owner_id;")) | ||
| conn.execute( | ||
| sa.text("ALTER TABLE collection RENAME COLUMN new_owner_id TO owner_id;") | ||
| ) | ||
|
|
||
| conn.execute(sa.text("ALTER TABLE projectuser DROP COLUMN user_id;")) | ||
| conn.execute( | ||
| sa.text("ALTER TABLE projectuser RENAME COLUMN new_user_id TO user_id;") | ||
| ) | ||
|
|
||
| conn.execute(sa.text("ALTER TABLE apikey DROP COLUMN user_id;")) | ||
| conn.execute(sa.text("ALTER TABLE apikey RENAME COLUMN new_user_id TO user_id;")) | ||
|
|
||
| conn.execute(sa.text('ALTER TABLE "user" DROP COLUMN id;')) | ||
| conn.execute(sa.text('ALTER TABLE "user" RENAME COLUMN new_id TO id;')) | ||
|
|
||
| # Re-add primary key | ||
| conn.execute(sa.text('ALTER TABLE "user" ADD PRIMARY KEY (id);')) | ||
|
|
||
| # Create sequence for new integer IDs | ||
| conn.execute(sa.text('CREATE SEQUENCE user_id_seq START 1 OWNED BY "user".id;')) | ||
| conn.execute( | ||
| sa.text( | ||
| "ALTER TABLE \"user\" ALTER COLUMN id SET DEFAULT nextval('user_id_seq');" | ||
| ) | ||
| ) | ||
| conn.execute( | ||
| sa.text("SELECT setval('user_id_seq', (SELECT MAX(id) FROM \"user\"));") | ||
| ) | ||
|
|
||
| # Drop mapping table | ||
| conn.execute(sa.text("DROP TABLE uuid_to_int_map;")) | ||
|
|
||
|
|
||
| def downgrade(): | ||
| pass |
55 changes: 55 additions & 0 deletions
55
backend/app/alembic/versions/8e7dc5eab0b0_add_fk_constraint_to_user_id_columns.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| """add fk constraint to user id columns | ||
|
|
||
| Revision ID: 8e7dc5eab0b0 | ||
| Revises: 66abc97f3782 | ||
| Create Date: 2025-06-16 11:08:46.893642 | ||
|
|
||
| """ | ||
| from alembic import op | ||
| import sqlalchemy as sa | ||
| import sqlmodel.sql.sqltypes | ||
|
|
||
|
|
||
| # revision identifiers, used by Alembic. | ||
| revision = "8e7dc5eab0b0" | ||
| down_revision = "66abc97f3782" | ||
| branch_labels = None | ||
| depends_on = None | ||
|
|
||
|
|
||
| def upgrade(): | ||
| # ### commands auto generated by Alembic - please adjust! ### | ||
| op.alter_column("apikey", "user_id", existing_type=sa.INTEGER(), nullable=False) | ||
| op.create_foreign_key( | ||
| None, "apikey", "user", ["user_id"], ["id"], ondelete="CASCADE" | ||
| ) | ||
| op.alter_column( | ||
| "collection", "owner_id", existing_type=sa.INTEGER(), nullable=False | ||
| ) | ||
| op.create_foreign_key( | ||
| None, "collection", "user", ["owner_id"], ["id"], ondelete="CASCADE" | ||
| ) | ||
| op.alter_column("document", "owner_id", existing_type=sa.INTEGER(), nullable=False) | ||
| op.create_foreign_key( | ||
| None, "document", "user", ["owner_id"], ["id"], ondelete="CASCADE" | ||
| ) | ||
| op.alter_column( | ||
| "projectuser", "user_id", existing_type=sa.INTEGER(), nullable=False | ||
| ) | ||
| op.create_foreign_key( | ||
| None, "projectuser", "user", ["user_id"], ["id"], ondelete="CASCADE" | ||
| ) | ||
| # ### end Alembic commands ### | ||
|
|
||
|
|
||
| def downgrade(): | ||
| # ### commands auto generated by Alembic - please adjust! ### | ||
| op.drop_constraint(None, "projectuser", type_="foreignkey") | ||
| op.alter_column("projectuser", "user_id", existing_type=sa.INTEGER(), nullable=True) | ||
| op.drop_constraint(None, "document", type_="foreignkey") | ||
| op.alter_column("document", "owner_id", existing_type=sa.INTEGER(), nullable=True) | ||
| op.drop_constraint(None, "collection", type_="foreignkey") | ||
| op.alter_column("collection", "owner_id", existing_type=sa.INTEGER(), nullable=True) | ||
| op.drop_constraint(None, "apikey", type_="foreignkey") | ||
| op.alter_column("apikey", "user_id", existing_type=sa.INTEGER(), nullable=True) | ||
| # ### end Alembic commands ### |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.