Skip to content

Commit 018eb7e

Browse files
committed
Fix SaaS multi-tenancy: backfill owner_id for existing data
Added migration 20241223_02 to set owner_id and created_by_id for existing databases and users that were created before the SaaS multi-tenancy migration. This fixes the issue where existing bill groups and users were not visible in the admin panel. Bump version to 3.2.9
1 parent d0a6cea commit 018eb7e

4 files changed

Lines changed: 43 additions & 4 deletions

File tree

client/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "client",
33
"private": true,
4-
"version": "3.2.8",
4+
"version": "3.2.9",
55
"type": "module",
66
"scripts": {
77
"dev": "vite",

client/src/App.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,7 @@ function App() {
309309
/>
310310
<Divider />
311311
<Text size="xs" c="dimmed" ta="center">
312-
BillManager v3.2.8 - Licensed under{' '}
312+
BillManager v3.2.9 - Licensed under{' '}
313313
<Anchor href="https://osaasy.dev/" target="_blank" size="xs">
314314
O'Saasy
315315
</Anchor>

server/app.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -765,7 +765,7 @@ def process_auto_payments():
765765

766766
@api_bp.route('/api/version', methods=['GET'])
767767
def get_version():
768-
return jsonify({'version': '3.2.8', 'license': "O'Saasy", 'license_url': 'https://osaasy.dev/', 'features': ['enhanced_frequencies', 'auto_payments', 'postgresql_saas', 'row_tenancy']})
768+
return jsonify({'version': '3.2.9', 'license': "O'Saasy", 'license_url': 'https://osaasy.dev/', 'features': ['enhanced_frequencies', 'auto_payments', 'postgresql_saas', 'row_tenancy']})
769769

770770
@api_bp.route('/ping')
771771
def ping(): return jsonify({'status': 'ok'})
@@ -1926,7 +1926,7 @@ def jwt_get_version():
19261926
return jsonify({
19271927
'success': True,
19281928
'data': {
1929-
'version': '3.2.8',
1929+
'version': '3.2.9',
19301930
'api_version': 'v2',
19311931
'license': "O'Saasy",
19321932
'license_url': 'https://osaasy.dev/',

server/db_migrations.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,13 +129,52 @@ def migrate_20241223_01_add_saas_tenancy_columns(db):
129129
db.session.commit()
130130

131131

132+
def migrate_20241223_02_backfill_tenancy_ownership(db):
133+
"""Backfill owner_id and created_by_id for existing data.
134+
135+
For existing installations upgrading to SaaS multi-tenancy:
136+
- Set databases.owner_id to the first admin user for databases with NULL owner
137+
- Set users.created_by_id to the first admin user for users with NULL creator
138+
"""
139+
# Find the first admin user (usually the original admin)
140+
result = db.session.execute(text('''
141+
SELECT id FROM users WHERE role = 'admin' ORDER BY id LIMIT 1
142+
'''))
143+
row = result.fetchone()
144+
145+
if not row:
146+
logger.warning("No admin user found, skipping tenancy backfill")
147+
return
148+
149+
first_admin_id = row[0]
150+
logger.info(f"Using admin user ID {first_admin_id} for backfill")
151+
152+
# Backfill databases.owner_id
153+
result = db.session.execute(text('''
154+
UPDATE databases SET owner_id = :admin_id WHERE owner_id IS NULL
155+
'''), {'admin_id': first_admin_id})
156+
db_count = result.rowcount
157+
logger.info(f"Set owner_id for {db_count} database(s)")
158+
159+
# Backfill users.created_by_id (except for the admin themselves)
160+
result = db.session.execute(text('''
161+
UPDATE users SET created_by_id = :admin_id
162+
WHERE created_by_id IS NULL AND id != :admin_id
163+
'''), {'admin_id': first_admin_id})
164+
user_count = result.rowcount
165+
logger.info(f"Set created_by_id for {user_count} user(s)")
166+
167+
db.session.commit()
168+
169+
132170
# List of all migrations in order
133171
# Format: (version, description, function)
134172
MIGRATIONS = [
135173
('20241221_01', 'Increase password_hash column to 256 chars', migrate_20241221_01_password_hash_length),
136174
('20241221_02', 'Add migrations tracking index', migrate_20241221_02_add_migrations_index),
137175
('20241222_01', 'Add subscription tier and billing_interval columns', migrate_20241222_01_add_subscription_tier),
138176
('20241223_01', 'Add SaaS multi-tenancy columns (owner_id, created_by_id)', migrate_20241223_01_add_saas_tenancy_columns),
177+
('20241223_02', 'Backfill owner_id and created_by_id for existing data', migrate_20241223_02_backfill_tenancy_ownership),
139178
]
140179

141180

0 commit comments

Comments
 (0)