Fix jwt-token
This commit is contained in:
@@ -7,7 +7,8 @@
|
|||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
async up(queryInterface, Sequelize) {
|
async up(queryInterface, Sequelize) {
|
||||||
// Create tenants table
|
// Create tenants table (idempotent)
|
||||||
|
try {
|
||||||
await queryInterface.createTable('tenants', {
|
await queryInterface.createTable('tenants', {
|
||||||
id: {
|
id: {
|
||||||
type: Sequelize.UUID,
|
type: Sequelize.UUID,
|
||||||
@@ -109,17 +110,67 @@ module.exports = {
|
|||||||
defaultValue: Sequelize.NOW
|
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 });
|
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'], {
|
await queryInterface.addIndex('tenants', ['domain'], {
|
||||||
unique: true,
|
unique: true,
|
||||||
where: { domain: { [Sequelize.Op.ne]: null } }
|
where: { domain: { [Sequelize.Op.ne]: null } }
|
||||||
});
|
});
|
||||||
await queryInterface.addIndex('tenants', ['is_active']);
|
console.log('✅ Added unique index on tenants.domain');
|
||||||
await queryInterface.addIndex('tenants', ['auth_provider']);
|
} 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', {
|
await queryInterface.addColumn('users', 'tenant_id', {
|
||||||
type: Sequelize.UUID,
|
type: Sequelize.UUID,
|
||||||
allowNull: true,
|
allowNull: true,
|
||||||
@@ -129,27 +180,70 @@ module.exports = {
|
|||||||
},
|
},
|
||||||
comment: 'Tenant this user belongs to'
|
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', {
|
await queryInterface.addColumn('users', 'external_provider', {
|
||||||
type: Sequelize.ENUM('local', 'saml', 'oauth', 'ldap', 'custom_sso'),
|
type: Sequelize.ENUM('local', 'saml', 'oauth', 'ldap', 'custom_sso'),
|
||||||
defaultValue: 'local',
|
defaultValue: 'local',
|
||||||
comment: 'Authentication provider used for this user'
|
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', {
|
await queryInterface.addColumn('users', 'external_id', {
|
||||||
type: Sequelize.STRING,
|
type: Sequelize.STRING,
|
||||||
allowNull: true,
|
allowNull: true,
|
||||||
comment: 'User ID from external authentication provider'
|
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']);
|
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']);
|
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'], {
|
await queryInterface.addIndex('users', ['external_id', 'tenant_id'], {
|
||||||
unique: true,
|
unique: true,
|
||||||
name: 'users_external_id_tenant_unique',
|
name: 'users_external_id_tenant_unique',
|
||||||
where: { external_id: { [Sequelize.Op.ne]: null } }
|
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
|
// Create default tenant for backward compatibility
|
||||||
const defaultTenantId = await queryInterface.bulkInsert('tenants', [{
|
const defaultTenantId = await queryInterface.bulkInsert('tenants', [{
|
||||||
|
|||||||
Reference in New Issue
Block a user