Fix jwt-token

This commit is contained in:
2025-09-13 15:16:52 +02:00
parent d188da7812
commit 718a077ea3

View File

@@ -7,7 +7,8 @@
module.exports = {
async up(queryInterface, Sequelize) {
// Create tenants table
// Create tenants table (idempotent)
try {
await queryInterface.createTable('tenants', {
id: {
type: Sequelize.UUID,
@@ -109,17 +110,67 @@ module.exports = {
defaultValue: Sequelize.NOW
}
});
console.log('✅ Created tenants table');
} catch (error) {
if (error.parent?.code === '42P07') { // Table already exists
console.log('⚠️ Tenants table already exists, skipping...');
} else {
throw error;
}
}
// Add indexes to tenants table
// Add indexes to tenants table (idempotent)
try {
await queryInterface.addIndex('tenants', ['slug'], { unique: true });
console.log('✅ Added unique index on tenants.slug');
} catch (error) {
if (error.parent?.code === '42P07') { // Index already exists
console.log('⚠️ Index tenants_slug already exists, skipping...');
} else {
throw error;
}
}
try {
await queryInterface.addIndex('tenants', ['domain'], {
unique: true,
where: { domain: { [Sequelize.Op.ne]: null } }
});
await queryInterface.addIndex('tenants', ['is_active']);
await queryInterface.addIndex('tenants', ['auth_provider']);
console.log('✅ Added unique index on tenants.domain');
} catch (error) {
if (error.parent?.code === '42P07') { // Index already exists
console.log('⚠️ Index tenants_domain already exists, skipping...');
} else {
throw error;
}
}
// Add tenant-related columns to users table
try {
await queryInterface.addIndex('tenants', ['is_active']);
console.log('✅ Added index on tenants.is_active');
} catch (error) {
if (error.parent?.code === '42P07') { // Index already exists
console.log('⚠️ Index tenants_is_active already exists, skipping...');
} else {
throw error;
}
}
try {
await queryInterface.addIndex('tenants', ['auth_provider']);
console.log('✅ Added index on tenants.auth_provider');
} catch (error) {
if (error.parent?.code === '42P07') { // Index already exists
console.log('⚠️ Index tenants_auth_provider already exists, skipping...');
} else {
throw error;
}
}
// Add tenant-related columns to users table (idempotent)
const usersTableDescription = await queryInterface.describeTable('users');
if (!usersTableDescription.tenant_id) {
await queryInterface.addColumn('users', 'tenant_id', {
type: Sequelize.UUID,
allowNull: true,
@@ -129,27 +180,70 @@ module.exports = {
},
comment: 'Tenant this user belongs to'
});
console.log('✅ Added tenant_id column to users table');
} else {
console.log('⚠️ Column tenant_id already exists in users table, skipping...');
}
if (!usersTableDescription.external_provider) {
await queryInterface.addColumn('users', 'external_provider', {
type: Sequelize.ENUM('local', 'saml', 'oauth', 'ldap', 'custom_sso'),
defaultValue: 'local',
comment: 'Authentication provider used for this user'
});
console.log('✅ Added external_provider column to users table');
} else {
console.log('⚠️ Column external_provider already exists in users table, skipping...');
}
if (!usersTableDescription.external_id) {
await queryInterface.addColumn('users', 'external_id', {
type: Sequelize.STRING,
allowNull: true,
comment: 'User ID from external authentication provider'
});
console.log('✅ Added external_id column to users table');
} else {
console.log('⚠️ Column external_id already exists in users table, skipping...');
}
// Add indexes to users table
// Add indexes to users table (idempotent)
try {
await queryInterface.addIndex('users', ['tenant_id']);
console.log('✅ Added index on users.tenant_id');
} catch (error) {
if (error.parent?.code === '42P07') { // Index already exists
console.log('⚠️ Index users_tenant_id already exists, skipping...');
} else {
throw error;
}
}
try {
await queryInterface.addIndex('users', ['external_provider']);
console.log('✅ Added index on users.external_provider');
} catch (error) {
if (error.parent?.code === '42P07') { // Index already exists
console.log('⚠️ Index users_external_provider already exists, skipping...');
} else {
throw error;
}
}
try {
await queryInterface.addIndex('users', ['external_id', 'tenant_id'], {
unique: true,
name: 'users_external_id_tenant_unique',
where: { external_id: { [Sequelize.Op.ne]: null } }
});
console.log('✅ Added unique index on users.external_id + tenant_id');
} catch (error) {
if (error.parent?.code === '42P07') { // Index already exists
console.log('⚠️ Index users_external_id_tenant_unique already exists, skipping...');
} else {
throw error;
}
}
// Create default tenant for backward compatibility
const defaultTenantId = await queryInterface.bulkInsert('tenants', [{