diff --git a/server/migrations/20250912000001-add-multi-tenant-support.js b/server/migrations/20250912000001-add-multi-tenant-support.js index fa566ef..7930f49 100644 --- a/server/migrations/20250912000001-add-multi-tenant-support.js +++ b/server/migrations/20250912000001-add-multi-tenant-support.js @@ -250,24 +250,36 @@ module.exports = { } } - // Create default tenant for backward compatibility - const defaultTenantId = await queryInterface.bulkInsert('tenants', [{ - id: Sequelize.literal('gen_random_uuid()'), - name: 'Default Organization', - slug: 'default', - subscription_type: 'enterprise', - is_active: true, - auth_provider: 'local', - features: JSON.stringify({ - max_devices: -1, - max_users: -1, - api_rate_limit: 50000, - data_retention_days: -1, - features: ['all'] - }), - created_at: new Date(), - updated_at: new Date() - }], { returning: true }); + // Create default tenant for backward compatibility (only if it doesn't exist) + const [existingTenant] = await queryInterface.sequelize.query( + 'SELECT id FROM tenants WHERE slug = \'default\' LIMIT 1', + { type: Sequelize.QueryTypes.SELECT } + ); + + let defaultTenantId; + if (!existingTenant) { + console.log('🏢 Creating default tenant...'); + defaultTenantId = await queryInterface.bulkInsert('tenants', [{ + id: Sequelize.literal('gen_random_uuid()'), + name: 'Default Organization', + slug: 'default', + subscription_type: 'enterprise', + is_active: true, + auth_provider: 'local', + features: JSON.stringify({ + max_devices: -1, + max_users: -1, + api_rate_limit: 50000, + data_retention_days: -1, + features: ['all'] + }), + created_at: new Date(), + updated_at: new Date() + }], { returning: true }); + } else { + console.log('⚠️ Default tenant already exists, skipping...'); + defaultTenantId = existingTenant.id; + } // Associate existing users with default tenant await queryInterface.sequelize.query(`