forked from ITISFoundation/osparc-simcore
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into buy-credits
- Loading branch information
Showing
2 changed files
with
51 additions
and
0 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
50 changes: 50 additions & 0 deletions
50
...imcore_postgres_database/migration/versions/392a86f2e446_all_users_must_have_a_product.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,50 @@ | ||
"""all users must have a product | ||
Revision ID: 392a86f2e446 | ||
Revises: 0ad000429e3d | ||
Create Date: 2024-01-09 15:14:11.504329+00:00 | ||
""" | ||
import sqlalchemy as sa | ||
from alembic import op | ||
|
||
# revision identifiers, used by Alembic. | ||
revision = "392a86f2e446" | ||
down_revision = "0ad000429e3d" | ||
branch_labels = None | ||
depends_on = None | ||
|
||
|
||
def upgrade(): | ||
# If a user has NO associated product groups, assign them a default product | ||
migration_query = sa.text( | ||
""" | ||
INSERT INTO user_to_groups (uid, gid) | ||
SELECT u.id, ( | ||
SELECT p.group_id | ||
FROM products p | ||
WHERE p.group_id IS NOT NULL | ||
ORDER BY p.priority ASC | ||
LIMIT 1 | ||
) AS default_group_id | ||
FROM users u | ||
WHERE u.id NOT IN ( | ||
SELECT utg.uid | ||
FROM user_to_groups utg | ||
WHERE utg.gid IN ( | ||
SELECT p.group_id | ||
FROM products p | ||
) | ||
) | ||
AND u.status = 'ACTIVE'; | ||
""" | ||
) | ||
|
||
# Execute the migration query | ||
conn = op.get_bind() | ||
conn.execute(migration_query) | ||
|
||
|
||
def downgrade(): | ||
# Define the downgrade logic if needed | ||
pass |