prefect_sqlalchemy.credentials
¶
Credential classes used to perform authenticated interactions with SQLAlchemy
AsyncDriver
¶
Bases: Enum
Known dialects with their corresponding async drivers.
Attributes:
Name | Type | Description |
---|---|---|
POSTGRESQL_ASYNCPG |
Enum
|
|
SQLITE_AIOSQLITE |
Enum
|
|
MYSQL_ASYNCMY |
Enum
|
|
MYSQL_AIOMYSQL |
Enum
|
Source code in prefect_sqlalchemy/credentials.py
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
|
ConnectionComponents
¶
Bases: BaseModel
Parameters to use to create a SQLAlchemy engine URL.
Attributes:
Name | Type | Description |
---|---|---|
driver |
Union[AsyncDriver, SyncDriver, str]
|
The driver name to use. |
database |
str
|
The name of the database to use. |
username |
Optional[str]
|
The user name used to authenticate. |
password |
Optional[SecretStr]
|
The password used to authenticate. |
host |
Optional[str]
|
The host address of the database. |
port |
Optional[str]
|
The port to connect to the database. |
query |
Optional[Dict[str, str]]
|
A dictionary of string keys to string values to be passed to the dialect and/or the DBAPI upon connect. |
Source code in prefect_sqlalchemy/credentials.py
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 |
|
create_url
¶
Create a fully formed connection URL.
Returns:
Type | Description |
---|---|
URL
|
The SQLAlchemy engine URL. |
Source code in prefect_sqlalchemy/credentials.py
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 |
|
DatabaseCredentials
¶
Bases: Block
Block used to manage authentication with a database.
Attributes:
Name | Type | Description |
---|---|---|
driver |
Optional[Union[AsyncDriver, SyncDriver, str]]
|
The driver name, e.g. "postgresql+asyncpg" |
database |
Optional[str]
|
The name of the database to use. |
username |
Optional[str]
|
The user name used to authenticate. |
password |
Optional[SecretStr]
|
The password used to authenticate. |
host |
Optional[str]
|
The host address of the database. |
port |
Optional[str]
|
The port to connect to the database. |
query |
Optional[Dict[str, str]]
|
A dictionary of string keys to string values to be passed to the dialect and/or the DBAPI upon connect. To specify non-string parameters to a Python DBAPI directly, use connect_args. |
url |
Optional[AnyUrl]
|
Manually create and provide a URL to create the engine,
this is useful for external dialects, e.g. Snowflake, because some
of the params, such as "warehouse", is not directly supported in
the vanilla |
connect_args |
Optional[Dict[str, Any]]
|
The options which will be passed directly to the DBAPI's connect() method as additional keyword arguments. |
Example
Load stored database credentials:
from prefect_sqlalchemy import DatabaseCredentials
database_block = DatabaseCredentials.load("BLOCK_NAME")
Source code in prefect_sqlalchemy/credentials.py
162 163 164 165 166 167 168 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 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 |
|
Config
¶
Configuration of pydantic.
Source code in prefect_sqlalchemy/credentials.py
349 350 351 352 353 354 |
|
block_initialization
¶
Initializes the engine.
Source code in prefect_sqlalchemy/credentials.py
240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 |
|
dict
¶
Convert to a dictionary.
Source code in prefect_sqlalchemy/credentials.py
356 357 358 359 360 361 362 363 364 365 |
|
get_engine
¶
Returns an authenticated engine that can be used to query from databases.
Returns:
Type | Description |
---|---|
Union[Connection, AsyncConnection]
|
The authenticated SQLAlchemy Connection / AsyncConnection. |
Examples:
Create an asynchronous engine to PostgreSQL using URL params.
from prefect import flow
from prefect_sqlalchemy import DatabaseCredentials, AsyncDriver
@flow
def sqlalchemy_credentials_flow():
sqlalchemy_credentials = DatabaseCredentials(
driver=AsyncDriver.POSTGRESQL_ASYNCPG,
username="prefect",
password="prefect_password",
database="postgres"
)
print(sqlalchemy_credentials.get_engine())
sqlalchemy_credentials_flow()
Create a synchronous engine to Snowflake using the url
kwarg.
from prefect import flow
from prefect_sqlalchemy import DatabaseCredentials, AsyncDriver
@flow
def sqlalchemy_credentials_flow():
url = (
"snowflake://<user_login_name>:<password>"
"@<account_identifier>/<database_name>"
"?warehouse=<warehouse_name>"
)
sqlalchemy_credentials = DatabaseCredentials(url=url)
print(sqlalchemy_credentials.get_engine())
sqlalchemy_credentials_flow()
Source code in prefect_sqlalchemy/credentials.py
293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 |
|
SyncDriver
¶
Bases: Enum
Known dialects with their corresponding sync drivers.
Attributes:
Name | Type | Description |
---|---|---|
POSTGRESQL_PSYCOPG2 |
Enum
|
|
POSTGRESQL_PG8000 |
Enum
|
|
POSTGRESQL_PSYCOPG2CFFI |
Enum
|
|
POSTGRESQL_PYPOSTGRESQL |
Enum
|
|
POSTGRESQL_PYGRESQL |
Enum
|
|
MYSQL_MYSQLDB |
Enum
|
|
MYSQL_PYMYSQL |
Enum
|
|
MYSQL_MYSQLCONNECTOR |
Enum
|
|
MYSQL_CYMYSQL |
Enum
|
|
MYSQL_OURSQL |
Enum
|
|
MYSQL_PYODBC |
Enum
|
|
SQLITE_PYSQLITE |
Enum
|
|
SQLITE_PYSQLCIPHER |
Enum
|
|
ORACLE_CX_ORACLE |
Enum
|
|
MSSQL_PYODBC |
Enum
|
|
MSSQL_MXODBC |
Enum
|
|
MSSQL_PYMSSQL |
Enum
|
Source code in prefect_sqlalchemy/credentials.py
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 |
|