Skip to content

prefect.settings.models.server.database

ServerDatabaseSettings

Bases: PrefectBaseSettings

Settings for controlling server database behavior

Source code in src/prefect/settings/models/server/database.py
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
class ServerDatabaseSettings(PrefectBaseSettings):
    """
    Settings for controlling server database behavior
    """

    model_config = _build_settings_config(("server", "database"))

    connection_url: Optional[SecretStr] = Field(
        default=None,
        description="""
        A database connection URL in a SQLAlchemy-compatible
        format. Prefect currently supports SQLite and Postgres. Note that all
        Prefect database engines must use an async driver - for SQLite, use
        `sqlite+aiosqlite` and for Postgres use `postgresql+asyncpg`.

        SQLite in-memory databases can be used by providing the url
        `sqlite+aiosqlite:///file::memory:?cache=shared&uri=true&check_same_thread=false`,
        which will allow the database to be accessed by multiple threads. Note
        that in-memory databases can not be accessed from multiple processes and
        should only be used for simple tests.
        """,
        validation_alias=AliasChoices(
            AliasPath("connection_url"),
            "prefect_server_database_connection_url",
            "prefect_api_database_connection_url",
        ),
    )

    driver: Optional[Literal["postgresql+asyncpg", "sqlite+aiosqlite"]] = Field(
        default=None,
        description=(
            "The database driver to use when connecting to the database. "
            "If not set, the driver will be inferred from the connection URL."
        ),
        validation_alias=AliasChoices(
            AliasPath("driver"),
            "prefect_server_database_driver",
            "prefect_api_database_driver",
        ),
    )

    host: Optional[str] = Field(
        default=None,
        description="The database server host.",
        validation_alias=AliasChoices(
            AliasPath("host"),
            "prefect_server_database_host",
            "prefect_api_database_host",
        ),
    )

    port: Optional[int] = Field(
        default=None,
        description="The database server port.",
        validation_alias=AliasChoices(
            AliasPath("port"),
            "prefect_server_database_port",
            "prefect_api_database_port",
        ),
    )

    user: Optional[str] = Field(
        default=None,
        description="The user to use when connecting to the database.",
        validation_alias=AliasChoices(
            AliasPath("user"),
            "prefect_server_database_user",
            "prefect_api_database_user",
        ),
    )

    name: Optional[str] = Field(
        default=None,
        description="The name of the Prefect database on the remote server, or the path to the database file for SQLite.",
        validation_alias=AliasChoices(
            AliasPath("name"),
            "prefect_server_database_name",
            "prefect_api_database_name",
        ),
    )

    password: Optional[SecretStr] = Field(
        default=None,
        description="The password to use when connecting to the database. Should be kept secret.",
        validation_alias=AliasChoices(
            AliasPath("password"),
            "prefect_server_database_password",
            "prefect_api_database_password",
        ),
    )

    echo: bool = Field(
        default=False,
        description="If `True`, SQLAlchemy will log all SQL issued to the database. Defaults to `False`.",
        validation_alias=AliasChoices(
            AliasPath("echo"),
            "prefect_server_database_echo",
            "prefect_api_database_echo",
        ),
    )

    migrate_on_start: bool = Field(
        default=True,
        description="If `True`, the database will be migrated on application startup.",
        validation_alias=AliasChoices(
            AliasPath("migrate_on_start"),
            "prefect_server_database_migrate_on_start",
            "prefect_api_database_migrate_on_start",
        ),
    )

    timeout: Optional[float] = Field(
        default=10.0,
        description="A statement timeout, in seconds, applied to all database interactions made by the API. Defaults to 10 seconds.",
        validation_alias=AliasChoices(
            AliasPath("timeout"),
            "prefect_server_database_timeout",
            "prefect_api_database_timeout",
        ),
    )

    connection_timeout: Optional[float] = Field(
        default=5,
        description="A connection timeout, in seconds, applied to database connections. Defaults to `5`.",
        validation_alias=AliasChoices(
            AliasPath("connection_timeout"),
            "prefect_server_database_connection_timeout",
            "prefect_api_database_connection_timeout",
        ),
    )

    sqlalchemy_pool_size: Optional[int] = Field(
        default=None,
        description="Controls connection pool size when using a PostgreSQL database with the Prefect API. If not set, the default SQLAlchemy pool size will be used.",
        validation_alias=AliasChoices(
            AliasPath("sqlalchemy_pool_size"),
            "prefect_server_database_sqlalchemy_pool_size",
            "prefect_sqlalchemy_pool_size",
        ),
    )

    sqlalchemy_max_overflow: Optional[int] = Field(
        default=None,
        description="Controls maximum overflow of the connection pool when using a PostgreSQL database with the Prefect API. If not set, the default SQLAlchemy maximum overflow value will be used.",
        validation_alias=AliasChoices(
            AliasPath("sqlalchemy_max_overflow"),
            "prefect_server_database_sqlalchemy_max_overflow",
            "prefect_sqlalchemy_max_overflow",
        ),
    )

    @model_validator(mode="after")
    def emit_warnings(self) -> Self:  # noqa: F821
        """More post-hoc validation of settings, including warnings for misconfigurations."""
        warn_on_database_password_value_without_usage(self)
        return self

emit_warnings()

More post-hoc validation of settings, including warnings for misconfigurations.

Source code in src/prefect/settings/models/server/database.py
162
163
164
165
166
@model_validator(mode="after")
def emit_warnings(self) -> Self:  # noqa: F821
    """More post-hoc validation of settings, including warnings for misconfigurations."""
    warn_on_database_password_value_without_usage(self)
    return self

warn_on_database_password_value_without_usage(settings)

Validator for settings warning if the database password is set but not used.

Source code in src/prefect/settings/models/server/database.py
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
def warn_on_database_password_value_without_usage(settings: ServerDatabaseSettings):
    """
    Validator for settings warning if the database password is set but not used.
    """
    db_password = (
        settings.password.get_secret_value()
        if isinstance(settings.password, SecretStr)
        else None
    )
    api_db_connection_url = (
        settings.connection_url.get_secret_value()
        if isinstance(settings.connection_url, SecretStr)
        else settings.connection_url
    )

    if (
        db_password
        and api_db_connection_url is not None
        and "PREFECT_API_DATABASE_PASSWORD" not in api_db_connection_url
        and "PREFECT_SERVER_DATABASE_PASSWORD" not in api_db_connection_url
        and db_password not in api_db_connection_url
        and quote_plus(db_password) not in api_db_connection_url
    ):
        warnings.warn(
            "PREFECT_SERVER_DATABASE_PASSWORD is set but not included in the "
            "PREFECT_SERVER_DATABASE_CONNECTION_URL. "
            "The provided password will be ignored."
        )
    return settings