[1.7.x] Fixed bug in circular dependency algo for migration dependencies.

Previous algo only worked if the first item was a part of the loop,
and you would get an infinite loop otherwise (see test).

To fix this, it was much easier to do a pre-pass.

A bonus is that you now get an error message that actually helps you debug
the dependency problem.

Backport of ff3d746e8d8e8fbe6de287bd0f4c3a9fa23c18dc from master, with
additional tests from c5def493d0993d65bf7d96f0a204006cbeaa6178
[1.7.x] Release notes for ff3d746e8d8e8fbe6de287bd0f4c3a9fa23c18dc

Backport of 8b15018fd57b7b10df321b61a17601982660ebdc from master
[1.7.x] Fixed #23901 -- Documented how to use SpatiaLite with Homebrew.

Backport of cc870b8ef5e3464c6f051e3ef0a25dfc4b597452 from master
[1.7.x] Fixed #23872 -- Removed sensitivity of migrations tests to CWD.

Backport of 47b7f601eeb93bd7a91f87b77da658212f2f2314 from master.