Repository: PrefectHQ/prefect

Issue:
Block auto-registration is skipped when switching databases
### First check

- [X] I added a descriptive title to this issue.
- [X] I used the GitHub search to find a similar issue and didn't find it.
- [X] I searched the Prefect documentation for this issue.
- [X] I checked that this issue is related to Prefect and not one of its dependencies.

### Bug summary

A community user reported that changing the database that backed their local Orion instance resulted in block disappearing from their UI. The user resolved the issue by deleting their .prefect folder. 

Thread: https://prefect-community.slack.com/archives/CL09KU1K7/p1666812855264589

### Reproduction

```python3
- Start up an Orion server with `prefect orion start`
- View blocks in UI
- Change database with `prefect config set PREFECT_ORION_DATABASE_CONNECTION_URL="new_url"
- View lack of blocks in the UI
```


### Error

_No response_

### Versions

```Text
Version:             2.6.4
API version:         0.8.2
Python version:      3.10.6
Git commit:          51e92dda
Built:               Thu, Oct 20, 2022 3:11 PM
OS/Arch:             darwin/x86_64
Profile:             local
Server type:         ephemeral
Server:
  Database:          sqlite
  SQLite version:    3.39.3
```


### Additional context

This is likely due to block auto-registration memoization. Memoization should be updated so that the block auto-registration memostore key is no longer valid when the orion database URL is changed.


Discussion:

Resolving patch:
diff --git a/src/prefect/orion/api/server.py b/src/prefect/orion/api/server.py
--- a/src/prefect/orion/api/server.py
+++ b/src/prefect/orion/api/server.py
@@ -31,6 +31,7 @@
     PREFECT_DEBUG_MODE,
     PREFECT_MEMO_STORE_PATH,
     PREFECT_MEMOIZE_BLOCK_AUTO_REGISTRATION,
+    PREFECT_ORION_DATABASE_CONNECTION_URL,
 )
 from prefect.utilities.hashing import hash_objects
 
@@ -263,7 +264,10 @@ async def wrapper(*args, **kwargs):
         blocks_registry = get_registry_for_type(Block)
         collection_blocks_data = await _load_collection_blocks_data()
         current_blocks_loading_hash = hash_objects(
-            blocks_registry, collection_blocks_data, hash_algo=sha256
+            blocks_registry,
+            collection_blocks_data,
+            PREFECT_ORION_DATABASE_CONNECTION_URL.value(),
+            hash_algo=sha256,
         )
 
         memo_store_path = PREFECT_MEMO_STORE_PATH.value()


Test patch:
diff --git a/tests/orion/api/test_server.py b/tests/orion/api/test_server.py
--- a/tests/orion/api/test_server.py
+++ b/tests/orion/api/test_server.py
@@ -14,6 +14,7 @@
 from prefect.settings import (
     PREFECT_MEMO_STORE_PATH,
     PREFECT_MEMOIZE_BLOCK_AUTO_REGISTRATION,
+    PREFECT_ORION_DATABASE_CONNECTION_URL,
     temporary_settings,
 )
 from prefect.testing.utilities import AsyncMock
@@ -322,3 +323,19 @@ async def test_does_not_fail_on_read_only_filesystem(self, enable_memoization):
             assert not PREFECT_MEMO_STORE_PATH.value().exists()
         finally:
             PREFECT_MEMO_STORE_PATH.value().parent.chmod(777)
+
+    async def test_changing_database_breaks_cache(self, enable_memoization):
+        test_func = AsyncMock()
+
+        await _memoize_block_auto_registration(test_func)()
+
+        assert test_func.call_count == 1
+
+        with temporary_settings(
+            {
+                PREFECT_ORION_DATABASE_CONNECTION_URL: "something else",
+            }
+        ):
+            await _memoize_block_auto_registration(test_func)()
+
+        assert test_func.call_count == 2