### Is there an existing issue for this?

- [X] I have searched the existing issues

### Current behaviour

crash

### Expected behaviour

no crash

### Steps to reproduce

* Compile 79e8247ddb166f9b980f40249b7372a502402a4d
* Type `migratewallet`

### Relevant log output

```
wallet/scriptpubkeyman.cpp:1915: std::optional<MigrationData> wallet::LegacyScriptPubKeyMan::MigrateToDescriptor(): Assertion `IsMine(desc_spk) != ISMINE_NO' failed.
Aborted (core dumped)
```


### How did you obtain Bitcoin Core

Compiled from source

### What version of Bitcoin Core are you using?

current master

### Operating system and version

Linux

### Machine specifications

_No response_
void CWalDeactivateScriptPubKeyMan(uint256 id, OutputType type, bool internal)
{
    auto spk_man = GetScriptPubKeyMan(type, internal);
    if (spk_man != nullptr && spk_man->GetID() == id) {
        WalLogPrintf("Deactivate spkMan id = %s, type = %s, internal = %s\n", id.ToString(), FormatOutputType(type), internal ? "true"  "false");
        WalBatch batch(GetDatabase());
        if (!batch.EraseActiveScriptPubKeyMan(static_cast<uint8_t>(type), internal)) {
            throw stdruntime_error(stdstring(__func__) + " erasing active ScriptPubKeyMan id failed");
        }
        auto& spk_mans = internal ? m_internal_spk_managers  m_external_spk_managers;
        spk_mans.erase(type);
    }
    NotifyCanGetAddressesChanged();
}
bool CWalIsLegacy() const
{
    if (m_internal_spk_managers.count(OutputTypeLEGACY) == 0) {
        return false;
    }
    auto spk_man = dynamic_cast<LegacyScriptPubKeyMan*>(m_internal_spk_managers.at(OutputTypeLEGACY));
    return spk_man != nullptr;
}
DescriptorScriptPubKeyMan* CWalGetDescriptorScriptPubKeyMan(const WalDescriptor& desc) const
{
    for (auto& spk_man_pair  m_spk_managers) {
        // Try to downcast to DescriptorScriptPubKeyMan then check if the s match
        DescriptorScriptPubKeyMan* spk_manager = dynamic_cast<DescriptorScriptPubKeyMan*>(spk_man_pair.second.get());
        if (spk_manager != nullptr && spk_manager->HasWalDescriptor(desc)) {
            return spk_manager;
        }