54 lines
1.9 KiB
Python
54 lines
1.9 KiB
Python
#!/usr/bin/env python3
|
|
"""
|
|
Migrate existing group user_ids from string to integer format.
|
|
Run this to fix existing groups that have string-stored member IDs.
|
|
"""
|
|
import os
|
|
import sys
|
|
|
|
# Add the project root to the path
|
|
sys.path.insert(0, '/Users/le0an/Developer/Personal/circle-flexoffice')
|
|
|
|
# Read credentials from environment variables
|
|
FLASK_SECRET_KEY = os.environ.get('FLASK_SECRET_KEY', 'dev')
|
|
DB_PASSWORD = os.environ.get('DB_PASSWORD', 'password')
|
|
|
|
os.environ['FLASK_SECRET_KEY'] = FLASK_SECRET_KEY
|
|
os.environ['DB_PASSWORD'] = DB_PASSWORD
|
|
|
|
from app import app, db, Group, json
|
|
|
|
app.config['TESTING'] = True
|
|
|
|
def migrate_group_ids():
|
|
with app.app_context():
|
|
groups = Group.query.filter(Group.user_ids != None).all()
|
|
fixed = 0
|
|
for group in groups:
|
|
if group.user_ids:
|
|
try:
|
|
user_ids = json.loads(group.user_ids)
|
|
# Try converting to integers
|
|
int_user_ids = []
|
|
for uid in user_ids:
|
|
try:
|
|
int_uid = int(uid)
|
|
int_user_ids.append(int_uid)
|
|
except (ValueError, TypeError):
|
|
# If it's already an int or can't be converted, keep as is
|
|
int_user_ids.append(uid)
|
|
|
|
# Only update if there was a change
|
|
if user_ids != int_user_ids:
|
|
group.user_ids = json.dumps(int_user_ids)
|
|
db.session.commit()
|
|
fixed += 1
|
|
print(f'Fixed group {group.id} ({group.name}): {user_ids} -> {int_user_ids}')
|
|
except Exception as e:
|
|
print(f'Error on group {group.id}: {e}')
|
|
print(f'\nMigrated {fixed} groups')
|
|
return fixed
|
|
|
|
if __name__ == '__main__':
|
|
count = migrate_group_ids()
|
|
print(f'\nDone! Migrated {count} groups.') |