fix: auto-activate credential when provider record exists without active credential

When a plugin is uninstalled, the Provider record remains in the database
with credential_id set to None while ProviderCredential rows are deleted.
Upon reinstalling the plugin and adding a new API key, the existing Provider
record causes the code to skip credential activation, leaving the new key
in an unusable state.

Now when creating a credential and the Provider record already exists with
credential_id=None, the new credential is automatically activated.

Made-with: Cursor
This commit is contained in:
Yansong Zhang
2026-03-16 13:32:46 +08:00
parent dd39fcd9bc
commit f0155138ac

View File

@ -473,9 +473,21 @@ class ProviderConfiguration(BaseModel):
self.switch_preferred_provider_type(provider_type=ProviderType.CUSTOM, session=session)
else:
# some historical data may have a provider record but not be set as valid
provider_record.is_valid = True
if provider_record.credential_id is None:
provider_record.credential_id = new_record.id
provider_record.updated_at = naive_utc_now()
provider_model_credentials_cache = ProviderCredentialsCache(
tenant_id=self.tenant_id,
identity_id=provider_record.id,
cache_type=ProviderCredentialsCacheType.PROVIDER,
)
provider_model_credentials_cache.delete()
self.switch_preferred_provider_type(provider_type=ProviderType.CUSTOM, session=session)
session.commit()
except Exception:
session.rollback()