Repository navigation
feat(kb): entity-based permissions with share service layer and exten… #1092
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
2 commits
Select commit
Hold shift + click to select a range
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
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
202 changes: 202 additions & 0 deletions
202
backend/alembic/versions/20260506_d1e2f3a4b5c6_extend_resource_members_for_entity.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,202 @@ | ||
| # SPDX-FileCopyrightText: 2025 Weibo, Inc. | ||
| # | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| """extend_resource_members_for_entity | ||
|
|
||
| Revision ID: d1e2f3a4b5c6 | ||
| Revises: c3d4e5f6a708 | ||
| Create Date: 2026-05-06 | ||
|
|
||
| Add entity_type, entity_id columns to resource_members table. | ||
| Change unique constraint from (resource_type, resource_id, user_id) | ||
| to (resource_type, resource_id, entity_type, entity_id). | ||
| Keep user_id column for backward compatibility (nullable, auto-synced from entity_id | ||
| for entity_type='user' via SQLAlchemy events). | ||
| entity_id is backfilled from user_id then set to NOT NULL. | ||
| """ | ||
|
|
||
| import sqlalchemy as sa | ||
|
|
||
| from alembic import op | ||
|
|
||
| revision = "d1e2f3a4b5c6" | ||
| down_revision = "c3d4e5f6a708" | ||
| branch_labels = None | ||
| depends_on = None | ||
|
|
||
|
|
||
| def upgrade() -> None: | ||
| conn = op.get_bind() | ||
| inspector = sa.inspect(conn) | ||
| columns = [col["name"] for col in inspector.get_columns("resource_members")] | ||
|
|
||
| # Step 1: Add entity_type column | ||
| if "entity_type" not in columns: | ||
| op.add_column( | ||
| "resource_members", | ||
| sa.Column( | ||
| "entity_type", | ||
| sa.String(20), | ||
| nullable=False, | ||
| server_default="user", | ||
| comment="Entity type: user, namespace", | ||
| ), | ||
| ) | ||
|
|
||
| # Step 2: Add entity_id column | ||
| if "entity_id" not in columns: | ||
| op.add_column( | ||
| "resource_members", | ||
| sa.Column( | ||
| "entity_id", | ||
| sa.String(100), | ||
| nullable=True, | ||
| comment="Entity identifier: user_id for 'user', external ID for others", | ||
| ), | ||
| ) | ||
|
|
||
| # Step 3: Backfill existing rows - set entity_id = CAST(user_id AS CHAR), entity_type = 'user' | ||
| dialect = conn.dialect.name | ||
| if dialect == "mysql": | ||
| op.execute( | ||
| """ | ||
| UPDATE resource_members | ||
| SET entity_id = CAST(user_id AS CHAR), entity_type = 'user' | ||
| WHERE entity_id IS NULL | ||
| """ | ||
| ) | ||
| elif dialect == "sqlite": | ||
| op.execute( | ||
| """ | ||
| UPDATE resource_members | ||
| SET entity_id = CAST(user_id AS TEXT), entity_type = 'user' | ||
| WHERE entity_id IS NULL | ||
| """ | ||
| ) | ||
|
|
||
| # Step 3.5: Make entity_id NOT NULL after backfill | ||
| if dialect == "mysql": | ||
| op.alter_column( | ||
| "resource_members", | ||
| "entity_id", | ||
| existing_type=sa.String(100), | ||
| nullable=False, | ||
| ) | ||
| elif dialect == "sqlite": | ||
| with op.batch_alter_table("resource_members") as batch_op: | ||
| batch_op.alter_column( | ||
| "entity_id", | ||
| existing_type=sa.String(100), | ||
| nullable=False, | ||
| ) | ||
|
|
||
| # Step 4: Make user_id nullable (kept for backward compatibility) | ||
| # user_id is auto-synced from entity_id for entity_type='user' via SQLAlchemy events | ||
| if dialect == "mysql": | ||
| op.execute( | ||
| "ALTER TABLE resource_members MODIFY user_id INT NULL COMMENT 'Member user ID (kept for backward compatibility, use entity_type+entity_id for new code)'" | ||
| ) | ||
| elif dialect == "sqlite": | ||
| with op.batch_alter_table("resource_members") as batch_op: | ||
| batch_op.alter_column( | ||
| "user_id", | ||
| existing_type=sa.Integer(), | ||
| nullable=True, | ||
| ) | ||
|
|
||
| # Step 6: Add entity_display_name for entity-type member snapshots | ||
| if "entity_display_name" not in columns: | ||
| op.add_column( | ||
| "resource_members", | ||
| sa.Column( | ||
| "entity_display_name", | ||
| sa.String(100), | ||
| nullable=True, | ||
| comment="Display name snapshot for entity-type members", | ||
| ), | ||
| ) | ||
|
|
||
| # Step 5: Drop old unique constraint and create new one | ||
| if dialect == "mysql": | ||
| # Dynamically find the actual unique constraint/index name from MySQL | ||
| old_uc_name_result = conn.execute( | ||
| sa.text( | ||
| "SELECT CONSTRAINT_NAME FROM information_schema.TABLE_CONSTRAINTS " | ||
| "WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'resource_members' " | ||
| "AND CONSTRAINT_TYPE = 'UNIQUE'" | ||
| ) | ||
| ).fetchone() | ||
| old_uc_name = ( | ||
| old_uc_name_result[0] if old_uc_name_result else "uq_resource_members" | ||
| ) | ||
|
|
||
| op.drop_constraint(old_uc_name, "resource_members", type_="unique") | ||
| op.create_unique_constraint( | ||
| "uq_resource_members_entity", | ||
| "resource_members", | ||
| ["resource_type", "resource_id", "entity_type", "entity_id"], | ||
| ) | ||
| else: | ||
| # SQLite: use batch operations | ||
| with op.batch_alter_table("resource_members") as batch_op: | ||
| batch_op.drop_constraint("uq_resource_members", type_="unique") | ||
| batch_op.create_unique_constraint( | ||
| "uq_resource_members_entity", | ||
| ["resource_type", "resource_id", "entity_type", "entity_id"], | ||
| ) | ||
|
|
||
| # Step 7: Add index for entity-type queries | ||
| op.create_index( | ||
| "idx_resource_members_entity", | ||
| "resource_members", | ||
| ["entity_type", "entity_id", "resource_type", "status"], | ||
| ) | ||
|
|
||
|
|
||
| def downgrade() -> None: | ||
| conn = op.get_bind() | ||
| dialect = conn.dialect.name | ||
|
|
||
| # Drop entity index | ||
| op.drop_index("idx_resource_members_entity", table_name="resource_members") | ||
|
|
||
| # Drop new unique constraint, restore old one | ||
| if dialect == "mysql": | ||
| # Dynamically find the current unique constraint name | ||
| new_uc_name_result = conn.execute( | ||
| sa.text( | ||
| "SELECT CONSTRAINT_NAME FROM information_schema.TABLE_CONSTRAINTS " | ||
| "WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'resource_members' " | ||
| "AND CONSTRAINT_TYPE = 'UNIQUE'" | ||
| ) | ||
| ).fetchone() | ||
| new_uc_name = ( | ||
| new_uc_name_result[0] | ||
| if new_uc_name_result | ||
| else "uq_resource_members_entity" | ||
| ) | ||
|
|
||
| op.drop_constraint(new_uc_name, "resource_members", type_="unique") | ||
| op.create_unique_constraint( | ||
| "uq_resource_members", | ||
| "resource_members", | ||
| ["resource_type", "resource_id", "user_id"], | ||
| ) | ||
| else: | ||
| with op.batch_alter_table("resource_members") as batch_op: | ||
| batch_op.drop_constraint("uq_resource_members_entity", type_="unique") | ||
| batch_op.create_unique_constraint( | ||
| "uq_resource_members", | ||
| ["resource_type", "resource_id", "user_id"], | ||
| ) | ||
|
|
||
| # Drop added columns (check existence to tolerate partial rollback states) | ||
| inspector = sa.inspect(conn) | ||
| columns = [col["name"] for col in inspector.get_columns("resource_members")] | ||
| if "entity_display_name" in columns: | ||
| op.drop_column("resource_members", "entity_display_name") | ||
| if "entity_id" in columns: | ||
| op.drop_column("resource_members", "entity_id") | ||
| if "entity_type" in columns: | ||
| op.drop_column("resource_members", "entity_type") | ||
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.
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.