-
Notifications
You must be signed in to change notification settings - Fork 34
feat(admin): add system administration module with user and model management #246
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
Changes from all commits
bf88b45
fc09aa9
d045b90
78f7d1e
f690ff1
7e8919b
f90c927
bc56117
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| # SPDX-FileCopyrightText: 2025 Weibo, Inc. | ||
| # | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| """Add role column to users table | ||
|
|
||
| Revision ID: b2c3d4e5f6a7 | ||
| Revises: 2b3c4d5e6f7g | ||
| Create Date: 2025-07-22 10:00:00.000000+08:00 | ||
|
|
||
| """ | ||
| from typing import Sequence, Union | ||
|
|
||
| from alembic import op | ||
| import sqlalchemy as sa | ||
|
|
||
|
|
||
| # revision identifiers, used by Alembic. | ||
| revision: str = 'b2c3d4e5f6a7' | ||
| down_revision: Union[str, Sequence[str], None] = '2b3c4d5e6f7g' | ||
| branch_labels: Union[str, Sequence[str], None] = None | ||
| depends_on: Union[str, Sequence[str], None] = None | ||
|
|
||
|
|
||
| def upgrade() -> None: | ||
| """Add role column to users table. | ||
|
|
||
| Values: 'admin', 'user' | ||
| Default 'user' for existing users. | ||
| Users with user_name='admin' will be set to role='admin'. | ||
| """ | ||
| # Check if column already exists before adding | ||
| op.execute(""" | ||
| SET @column_exists = ( | ||
| SELECT COUNT(*) | ||
| FROM INFORMATION_SCHEMA.COLUMNS | ||
| WHERE TABLE_SCHEMA = DATABASE() | ||
| AND TABLE_NAME = 'users' | ||
| AND COLUMN_NAME = 'role' | ||
| ); | ||
| """) | ||
|
|
||
| op.execute(""" | ||
| SET @query = IF(@column_exists = 0, | ||
| 'ALTER TABLE users ADD COLUMN role VARCHAR(20) NOT NULL DEFAULT ''user'' AFTER is_active', | ||
| 'SELECT 1' | ||
| ); | ||
| """) | ||
|
|
||
| op.execute("PREPARE stmt FROM @query;") | ||
| op.execute("EXECUTE stmt;") | ||
| op.execute("DEALLOCATE PREPARE stmt;") | ||
|
|
||
| # Set admin role for users with user_name='admin' | ||
| op.execute(""" | ||
| UPDATE users SET role = 'admin' WHERE user_name = 'admin'; | ||
| """) | ||
|
Comment on lines
+54
to
+57
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hardcoded username for admin role assignment. Setting Consider documenting this assumption or providing a separate mechanism (e.g., environment variable or config) to specify the initial admin username. 🤖 Prompt for AI Agents |
||
|
|
||
|
|
||
| def downgrade() -> None: | ||
| """Remove role column from users table.""" | ||
| op.drop_column('users', 'role') | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Migration uses MySQL-specific syntax, breaking portability.
The migration relies on MySQL session variables (
SET @variable),PREPARE/EXECUTE/DEALLOCATE, andINFORMATION_SCHEMAqueries. The backend model's__table_args__suggests SQLite support (sqlite_autoincrement), which means this migration will fail on SQLite.Consider using Alembic's dialect-aware operations or the
op.get_bind().dialect.namecheck: