-
Notifications
You must be signed in to change notification settings - Fork 390
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
change: delete on cascade responses when associated user is deleted (#…
…5126) # Description This PR adds the following changes: * Change `Responses` SqlAlchemy ORM model to delete responses on cascade when a user is deleted. * Add a migration for `responses` doing the following: * Removing all the responses rows with `user_id` equal to `NULL` so the foreign key constraint can be modified. * Change `user_id` constraint to delete on cascade responses when a user is deleted. * Change `user_id` to be non nullable. Closes #5109 **Type of change** - Improvement (change adding some improvement to an existing functionality) **How Has This Been Tested** - [x] Manually tested on PostgreSQL and SQLite. - [x] It should be tested on an environment with responses with `user_id` equal to `NULL`. **Checklist** - [ ] I added relevant documentation - [ ] follows the style guidelines of this project - [ ] I did a self-review of my code - [ ] I made corresponding changes to the documentation - [ ] I confirm My changes generate no new warnings - [ ] I have added tests that prove my fix is effective or that my feature works - [ ] I have added relevant notes to the CHANGELOG.md file (See https://keepachangelog.com/) --------- Co-authored-by: Paco Aranda <[email protected]>
- Loading branch information
1 parent
e91bcb9
commit 60d9340
Showing
7 changed files
with
66 additions
and
37 deletions.
There are no files selected for viewing
This file contains 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 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
51 changes: 51 additions & 0 deletions
51
.../src/argilla_server/alembic/versions/d00f819ccc67_update_responses_user_id_foreign_key.py
This file contains 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,51 @@ | ||
# Copyright 2021-present, the Recognai S.L. team. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
"""update responses user_id foreign key | ||
Revision ID: d00f819ccc67 | ||
Revises: ca7293c38970 | ||
Create Date: 2024-06-27 18:04:46.080762 | ||
""" | ||
|
||
from alembic import op | ||
import sqlalchemy as sa | ||
|
||
|
||
# revision identifiers, used by Alembic. | ||
revision = "d00f819ccc67" | ||
down_revision = "ca7293c38970" | ||
branch_labels = None | ||
depends_on = None | ||
|
||
|
||
CONSTRAINT_NAME = "responses_user_id_fkey" | ||
NAMING_CONVENTION = {"fk": "%(table_name)s_%(column_0_name)s_fkey"} | ||
|
||
|
||
def upgrade() -> None: | ||
op.execute("DELETE FROM responses WHERE user_id IS NULL") | ||
|
||
with op.batch_alter_table("responses", naming_convention=NAMING_CONVENTION) as batch_op: | ||
batch_op.alter_column("user_id", existing_type=sa.Uuid(), nullable=False) | ||
batch_op.drop_constraint(CONSTRAINT_NAME, type_="foreignkey") | ||
batch_op.create_foreign_key(CONSTRAINT_NAME, "users", ["user_id"], ["id"], ondelete="CASCADE") | ||
|
||
|
||
def downgrade() -> None: | ||
with op.batch_alter_table("responses", naming_convention=NAMING_CONVENTION) as batch_op: | ||
batch_op.alter_column("user_id", existing_type=sa.Uuid(), nullable=True) | ||
batch_op.drop_constraint(CONSTRAINT_NAME, type_="foreignkey") | ||
batch_op.create_foreign_key(CONSTRAINT_NAME, "users", ["user_id"], ["id"], ondelete="SET NULL") |
This file contains 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 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 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 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