### 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_
// Cache the first key time
    stdoptional<int64_t> time_first_key;
    for (auto spk_man  walInstance->GetAllScriptPubKeyMans()) {
        int64_t time = spk_man->GetTimeFirstKey();
        if (!time_first_key || time < *time_first_key) time_first_key = time;
    }
    if (time_first_key) walInstance->m_birth_time = *time_first_key;
    if (chain && !AttachChain(walInstance, *chain, rescan_required, error, warnings)) {
        return nullptr;
    }
    {
        LOCK(walInstance->cs_wal);
        walInstance->SetBroadcastTransactions(args.GetBoolArg("-walbroadcast", DEFAULT_WALLETBROADCAST));
        walInstance->WalLogPrintf("setKeyPool.size() = %u\n",      walInstance->GetKeyPoolSize());
        walInstance->WalLogPrintf("mapWal.size() = %u\n",       walInstance->mapWal.size());
        walInstance->WalLogPrintf("m_address_book.size() = %u\n",  walInstance->m_address_book.size());
    }
    return walInstance;
}
bool CWalAttachChain(const stdshared_ptr<CWal>& walInstance, interfacesChain& chain, const bool rescan_required, bilingual_str& error, stdvector<bilingual_str>& warnings)
{
    LOCK(walInstance->cs_wal);
    // allow setting the chain if it hasn't been set already but prevent changing it
    assert(!walInstance->m_chain || walInstance->m_chain == &chain);
    walInstance->m_chain = &chain;
    // Unless allowed, ensur files are not reused across chains
    if (!gArgs.GetBoolArg("-walcrosschain", DEFAULT_WALLETCROSSCHAIN)) {
        WalBatch batch(walInstance->GetDatabase());
        CBlockLocator locator;
        if (batch.ReadBestBlock(locator) && locator.vHave.size() > 0 && chain.getHeight()) {