Repository: PrefectHQ/prefect

Issue:
default_database_connection_url tries to create default database instead of using path specified in environment
### 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

The changed code in default_database_connection_url attempts to create a new database using the default url even when the PREFECT_API_DATABASE_CONNECTION_URL environment variable specifies a different location

![image](https://github.com/PrefectHQ/prefect/assets/47480785/caa1eb3d-eb0a-4932-9c49-1a61babf766d)


### Reproduction

```python3
specify a valid location for PREFECT_HOME and PREFECT_API_DATABASE_CONNECTION_URL and run in an environment where the default path would be unwritable.
```


### Error

```python3
Traceback (most recent call last):

File "/opt/venv/lib/python3.9/site-packages/prefect/cli/_utilities.py", line 46, in wrapper

return fn(*args, **kwargs)

File "/opt/venv/lib/python3.9/site-packages/prefect/cli/config.py", line 188, in view

default_settings = default_settings.with_obfuscated_secrets()

File "/opt/venv/lib/python3.9/site-packages/prefect/settings.py", line 1795, in with_obfuscated_secrets

update={

File "/opt/venv/lib/python3.9/site-packages/prefect/settings.py", line 1796, in <dictcomp>

setting.name: obfuscate(self.value_of(setting))

File "/opt/venv/lib/python3.9/site-packages/prefect/settings.py", line 1736, in value_of

value = setting.value_callback(self, value)

File "/opt/venv/lib/python3.9/site-packages/prefect/settings.py", line 390, in default_database_connection_url

new_default.touch(mode=0o600)

File "/usr/lib/python3.9/pathlib.py", line 1304, in touch

fd = self._raw_open(flags, mode)

File "/usr/lib/python3.9/pathlib.py", line 1116, in _raw_open

return self._accessor.open(self, flags, mode)

FileNotFoundError: [Errno 2] No such file or directory: '/var/lib/postgresql/.prefect/prefect.db'

An exception occurred.
```


### Versions

```Text
Prefect version info

Version: 2.10.12

API version: 0.8.4

Python version: 3.9.2

Git commit: ec55abdf

Built: Thu, Jun 1, 2023 3:51 PM

OS/Arch: linux/x86_64

Profile: default

Server type: server
```


### Additional context

The configuration was working on Prefect 2.10.11


Discussion:
Thanks for the report!

We definitely should not be touching the database while loading settings. Instead, we should ensure the database exists in `PrefectDBInterface.create_db`
@bunchesofdonald - this is a pretty high priority item for us. Any chance that you might be able to pick this up sometime this week?

Resolving patch:
diff --git a/src/prefect/settings.py b/src/prefect/settings.py
--- a/src/prefect/settings.py
+++ b/src/prefect/settings.py
@@ -386,9 +386,6 @@ def default_database_connection_url(settings, value):
         if old_default.exists():
             return "sqlite+aiosqlite:///" + str(old_default)
 
-        # Create the new default database with 0600 permissions if it does not exist
-        new_default.touch(mode=0o600)
-
     # Otherwise, return the new default
     return "sqlite+aiosqlite:///" + str(new_default)
 
@@ -485,7 +482,7 @@ def default_cloud_ui_url(settings, value):
     Optional[bool],
     default=None,
 )
-"""If `True`, use interactive prompts in CLI commands. If `False`, no interactive 
+"""If `True`, use interactive prompts in CLI commands. If `False`, no interactive
 prompts will be used. If `None`, the value will be dynamically determined based on
 the presence of an interactive-enabled terminal.
 """
@@ -529,7 +526,7 @@ def default_cloud_ui_url(settings, value):
     bool,
     default=False,
 )
-"""If `True`, disables SSL checking to allow insecure requests. 
+"""If `True`, disables SSL checking to allow insecure requests.
 This is recommended only during development, e.g. when using self-signed certificates.
 """
 
@@ -554,7 +551,7 @@ def default_cloud_ui_url(settings, value):
 """
 If true, enable support for HTTP/2 for communicating with an API.
 
-If the API does not support HTTP/2, this will have no effect and connections will be 
+If the API does not support HTTP/2, this will have no effect and connections will be
 made via HTTP/1.1.
 """
 
@@ -566,7 +563,7 @@ def default_cloud_ui_url(settings, value):
 Defaults to 5.
 Set to 0 to disable retries.
 
-See `PREFECT_CLIENT_RETRY_EXTRA_CODES` for details on which HTTP status codes are 
+See `PREFECT_CLIENT_RETRY_EXTRA_CODES` for details on which HTTP status codes are
 retried.
 """
 
@@ -684,7 +681,7 @@ def default_cloud_ui_url(settings, value):
 
 PREFECT_FLOW_DEFAULT_RETRIES = Setting(int, default=0)
 """
-This value sets the default number of retries for all flows. 
+This value sets the default number of retries for all flows.
 This value does not overwrite individually set retries values on a flow
 """
 
@@ -721,7 +718,7 @@ def default_cloud_ui_url(settings, value):
     default=True,
 )
 """
-Controls whether or not block auto-registration on start 
+Controls whether or not block auto-registration on start
 up should be memoized. Setting to False may result in slower server start
 up times.
 """
@@ -760,7 +757,7 @@ def default_cloud_ui_url(settings, value):
 )
 """
 The path to a custom YAML logging configuration file. If
-no file is found, the default `logging.yml` is used. 
+no file is found, the default `logging.yml` is used.
 Defaults to a logging.yml in the Prefect home directory.
 """
 
@@ -817,7 +814,7 @@ def default_cloud_ui_url(settings, value):
 Controls the behavior when loggers attempt to send logs to the API handler from outside
 of a flow.
 
-All logs sent to the API must be associated with a flow run. The API log handler can 
+All logs sent to the API must be associated with a flow run. The API log handler can
 only be used outside of a flow by manually providing a flow run identifier. Logs
 that are not associated with a flow run will not be sent to the API. This setting can
 be used to determine if a warning or error is displayed when the identifier is missing.
@@ -853,7 +850,7 @@ def default_cloud_ui_url(settings, value):
     default=15,
 )
 """
-The agent loop interval, in seconds. Agents will check for new runs this often. 
+The agent loop interval, in seconds. Agents will check for new runs this often.
 Defaults to `15`.
 """
 
@@ -874,7 +871,7 @@ def default_cloud_ui_url(settings, value):
 Determines whether `State.result()` fetches results automatically or not.
 In Prefect 2.6.0, the `State.result()` method was updated to be async
 to facilitate automatic retrieval of results from storage which means when
-writing async code you must `await` the call. For backwards compatibility, 
+writing async code you must `await` the call. For backwards compatibility,
 the result is not retrieved by default for async users. You may opt into this
 per call by passing  `fetch=True` or toggle this setting to change the behavior
 globally.
@@ -888,8 +885,8 @@ def default_cloud_ui_url(settings, value):
     default=True,
 )
 """
-If set, any block types that have been imported will be registered with the 
-backend on application startup. If not set, block types must be manually 
+If set, any block types that have been imported will be registered with the
+backend on application startup. If not set, block types must be manually
 registered.
 """
 
@@ -1031,7 +1028,7 @@ def default_cloud_ui_url(settings, value):
 )
 """The number of flow runs the scheduler will attempt to insert
 in one batch across all deployments. If the number of flow runs to
-schedule exceeds this amount, the runs will be inserted in batches of this size. 
+schedule exceeds this amount, the runs will be inserted in batches of this size.
 Defaults to `500`.
 """
 
@@ -1133,7 +1130,7 @@ def default_cloud_ui_url(settings, value):
     bool,
     default=True,
 )
-"""Whether or not to start the scheduling service in the server application. 
+"""Whether or not to start the scheduling service in the server application.
 If disabled, you will need to run this service separately to schedule runs for deployments.
 """
 
@@ -1141,8 +1138,8 @@ def default_cloud_ui_url(settings, value):
     bool,
     default=True,
 )
-"""Whether or not to start the late runs service in the server application. 
-If disabled, you will need to run this service separately to have runs past their 
+"""Whether or not to start the late runs service in the server application.
+If disabled, you will need to run this service separately to have runs past their
 scheduled start time marked as late.
 """
 
@@ -1150,7 +1147,7 @@ def default_cloud_ui_url(settings, value):
     bool,
     default=True,
 )
-"""Whether or not to start the flow run notifications service in the server application. 
+"""Whether or not to start the flow run notifications service in the server application.
 If disabled, you will need to run this service separately to send flow run notifications.
 """
 


Test patch: