Skip to content

prefect.server.models.deployments

Functions for interacting with deployment ORM objects. Intended for internal use by the Prefect REST API.

check_work_queues_for_deployment async

Get work queues that can pick up the specified deployment.

Work queues will pick up a deployment when all of the following are met.

  • The deployment has ALL tags that the work queue has (i.e. the work queue's tags must be a subset of the deployment's tags).
  • The work queue's specified deployment IDs match the deployment's ID, or the work queue does NOT have specified deployment IDs.
  • The work queue's specified flow runners match the deployment's flow runner or the work queue does NOT have a specified flow runner.

Notes on the query:

  • Our database currently allows either "null" and empty lists as null values in filters, so we need to catch both cases with "or".
  • json_contains(A, B) should be interpreted as "True if A contains B".

Returns:

Type Description
List[WorkQueue]

List[db.WorkQueue]: WorkQueues

Source code in prefect/server/models/deployments.py
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
@db_injector
async def check_work_queues_for_deployment(
    db: PrefectDBInterface, session: AsyncSession, deployment_id: UUID
) -> List[schemas.core.WorkQueue]:
    """
    Get work queues that can pick up the specified deployment.

    Work queues will pick up a deployment when all of the following are met.

    - The deployment has ALL tags that the work queue has (i.e. the work
    queue's tags must be a subset of the deployment's tags).
    - The work queue's specified deployment IDs match the deployment's ID,
    or the work queue does NOT have specified deployment IDs.
    - The work queue's specified flow runners match the deployment's flow
    runner or the work queue does NOT have a specified flow runner.

    Notes on the query:

    - Our database currently allows either "null" and empty lists as
    null values in filters, so we need to catch both cases with "or".
    - `json_contains(A, B)` should be interpreted as "True if A
    contains B".

    Returns:
        List[db.WorkQueue]: WorkQueues
    """
    deployment = await session.get(db.Deployment, deployment_id)
    if not deployment:
        raise ObjectNotFoundError(f"Deployment with id {deployment_id} not found")

    query = (
        select(db.WorkQueue)
        # work queue tags are a subset of deployment tags
        .filter(
            or_(
                json_contains(deployment.tags, db.WorkQueue.filter["tags"]),
                json_contains([], db.WorkQueue.filter["tags"]),
                json_contains(None, db.WorkQueue.filter["tags"]),
            )
        )
        # deployment_ids is null or contains the deployment's ID
        .filter(
            or_(
                json_contains(
                    db.WorkQueue.filter["deployment_ids"],
                    str(deployment.id),
                ),
                json_contains(None, db.WorkQueue.filter["deployment_ids"]),
                json_contains([], db.WorkQueue.filter["deployment_ids"]),
            )
        )
    )

    result = await session.execute(query)
    return result.scalars().unique().all()

count_deployments async

Count deployments.

Parameters:

Name Type Description Default
session AsyncSession

A database session

required
flow_filter FlowFilter

only count deployments whose flows match these criteria

None
flow_run_filter FlowRunFilter

only count deployments whose flow runs match these criteria

None
task_run_filter TaskRunFilter

only count deployments whose task runs match these criteria

None
deployment_filter DeploymentFilter

only count deployment that match these filters

None
work_pool_filter WorkPoolFilter

only count deployments that match these work pool filters

None
work_queue_filter WorkQueueFilter

only count deployments that match these work pool queue filters

None

Returns:

Name Type Description
int int

the number of deployments matching filters

Source code in prefect/server/models/deployments.py
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
@db_injector
async def count_deployments(
    db: PrefectDBInterface,
    session: AsyncSession,
    flow_filter: schemas.filters.FlowFilter = None,
    flow_run_filter: schemas.filters.FlowRunFilter = None,
    task_run_filter: schemas.filters.TaskRunFilter = None,
    deployment_filter: schemas.filters.DeploymentFilter = None,
    work_pool_filter: schemas.filters.WorkPoolFilter = None,
    work_queue_filter: schemas.filters.WorkQueueFilter = None,
) -> int:
    """
    Count deployments.

    Args:
        session: A database session
        flow_filter: only count deployments whose flows match these criteria
        flow_run_filter: only count deployments whose flow runs match these criteria
        task_run_filter: only count deployments whose task runs match these criteria
        deployment_filter: only count deployment that match these filters
        work_pool_filter: only count deployments that match these work pool filters
        work_queue_filter: only count deployments that match these work pool queue filters

    Returns:
        int: the number of deployments matching filters
    """

    query = select(sa.func.count(sa.text("*"))).select_from(db.Deployment)

    query = await _apply_deployment_filters(
        query=query,
        flow_filter=flow_filter,
        flow_run_filter=flow_run_filter,
        task_run_filter=task_run_filter,
        deployment_filter=deployment_filter,
        work_pool_filter=work_pool_filter,
        work_queue_filter=work_queue_filter,
    )

    result = await session.execute(query)
    return result.scalar()

create_deployment async

Upserts a deployment.

Parameters:

Name Type Description Default
session AsyncSession

a database session

required
deployment Deployment

a deployment model

required

Returns:

Type Description
Optional[ORMDeployment]

db.Deployment: the newly-created or updated deployment

Source code in prefect/server/models/deployments.py
 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
@db_injector
async def create_deployment(
    db: PrefectDBInterface,
    session: AsyncSession,
    deployment: schemas.core.Deployment,
) -> Optional["ORMDeployment"]:
    """Upserts a deployment.

    Args:
        session: a database session
        deployment: a deployment model

    Returns:
        db.Deployment: the newly-created or updated deployment

    """

    # set `updated` manually
    # known limitation of `on_conflict_do_update`, will not use `Column.onupdate`
    # https://docs.sqlalchemy.org/en/14/dialects/sqlite.html#the-set-clause
    deployment.updated = pendulum.now("UTC")

    schedules = deployment.schedules
    insert_values = deployment.dict(
        shallow=True, exclude_unset=True, exclude={"schedules"}
    )

    # The job_variables field in client and server schemas is named
    # infra_overrides in the database.
    job_variables = insert_values.pop("job_variables", None)
    if job_variables:
        insert_values["infra_overrides"] = job_variables

    conflict_update_fields = deployment.dict(
        shallow=True,
        exclude_unset=True,
        exclude={"id", "created", "created_by", "schedules", "job_variables"},
    )
    if job_variables:
        conflict_update_fields["infra_overrides"] = job_variables

    insert_stmt = (
        db.insert(db.Deployment)
        .values(**insert_values)
        .on_conflict_do_update(
            index_elements=db.deployment_unique_upsert_columns,
            set_={**conflict_update_fields},
        )
    )

    await session.execute(insert_stmt)

    # Get the id of the deployment we just created or updated
    result = await session.execute(
        sa.select(db.Deployment.id).where(
            sa.and_(
                db.Deployment.flow_id == deployment.flow_id,
                db.Deployment.name == deployment.name,
            )
        )
    )
    deployment_id = result.scalar_one_or_none()

    if not deployment_id:
        return None

    # Because this was possibly an upsert, we need to delete any existing
    # schedules and any runs from the old deployment.

    await _delete_scheduled_runs(
        session=session, deployment_id=deployment_id, auto_scheduled_only=True
    )

    await delete_schedules_for_deployment(session=session, deployment_id=deployment_id)

    if schedules:
        await create_deployment_schedules(
            session=session,
            deployment_id=deployment_id,
            schedules=[
                schemas.actions.DeploymentScheduleCreate(
                    schedule=schedule.schedule,
                    active=schedule.active,  # type: ignore[call-arg]
                )
                for schedule in schedules
            ],
        )

    query = (
        sa.select(db.Deployment)
        .where(
            sa.and_(
                db.Deployment.flow_id == deployment.flow_id,
                db.Deployment.name == deployment.name,
            )
        )
        .execution_options(populate_existing=True)
    )
    result = await session.execute(query)
    return result.scalar()

create_deployment_schedules async

Creates a deployment's schedules.

Parameters:

Name Type Description Default
session AsyncSession

A database session

required
deployment_id UUID

a deployment id

required
schedules List[DeploymentScheduleCreate]

a list of deployment schedule create actions

required
Source code in prefect/server/models/deployments.py
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
@db_injector
async def create_deployment_schedules(
    db: PrefectDBInterface,
    session: AsyncSession,
    deployment_id: UUID,
    schedules: List[schemas.actions.DeploymentScheduleCreate],
) -> List[schemas.core.DeploymentSchedule]:
    """
    Creates a deployment's schedules.

    Args:
        session: A database session
        deployment_id: a deployment id
        schedules: a list of deployment schedule create actions
    """

    schedules_with_deployment_id = []
    for schedule in schedules:
        data = schedule.dict()
        data["deployment_id"] = deployment_id
        schedules_with_deployment_id.append(data)

    models = [
        db.DeploymentSchedule(**schedule) for schedule in schedules_with_deployment_id
    ]
    session.add_all(models)
    await session.flush()

    return [schemas.core.DeploymentSchedule.from_orm(m) for m in models]

delete_deployment async

Delete a deployment by id.

Parameters:

Name Type Description Default
session AsyncSession

A database session

required
deployment_id UUID

a deployment id

required

Returns:

Name Type Description
bool bool

whether or not the deployment was deleted

Source code in prefect/server/models/deployments.py
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
@db_injector
async def delete_deployment(
    db: PrefectDBInterface, session: AsyncSession, deployment_id: UUID
) -> bool:
    """
    Delete a deployment by id.

    Args:
        session: A database session
        deployment_id: a deployment id

    Returns:
        bool: whether or not the deployment was deleted
    """

    # delete scheduled runs, both auto- and user- created.
    await _delete_scheduled_runs(
        session=session, deployment_id=deployment_id, auto_scheduled_only=False
    )

    result = await session.execute(
        delete(db.Deployment).where(db.Deployment.id == deployment_id)
    )
    return result.rowcount > 0

delete_deployment_schedule async

Deletes a deployment schedule.

Parameters:

Name Type Description Default
session AsyncSession

A database session

required
deployment_schedule_id UUID

a deployment schedule id

required
Source code in prefect/server/models/deployments.py
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
@db_injector
async def delete_deployment_schedule(
    db: PrefectDBInterface,
    session: AsyncSession,
    deployment_id: UUID,
    deployment_schedule_id: UUID,
) -> bool:
    """
    Deletes a deployment schedule.

    Args:
        session: A database session
        deployment_schedule_id: a deployment schedule id
    """

    result = await session.execute(
        sa.delete(db.DeploymentSchedule).where(
            sa.and_(
                db.DeploymentSchedule.id == deployment_schedule_id,
                db.DeploymentSchedule.deployment_id == deployment_id,
            )
        )
    )

    return result.rowcount > 0

delete_schedules_for_deployment async

Deletes a deployment schedule.

Parameters:

Name Type Description Default
session AsyncSession

A database session

required
deployment_id UUID

a deployment id

required
Source code in prefect/server/models/deployments.py
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
@db_injector
async def delete_schedules_for_deployment(
    db: PrefectDBInterface, session: AsyncSession, deployment_id: UUID
) -> bool:
    """
    Deletes a deployment schedule.

    Args:
        session: A database session
        deployment_id: a deployment id
    """

    result = await session.execute(
        sa.delete(db.DeploymentSchedule).where(
            db.DeploymentSchedule.deployment_id == deployment_id
        )
    )

    return result.rowcount > 0

read_deployment async

Reads a deployment by id.

Parameters:

Name Type Description Default
session AsyncSession

A database session

required
deployment_id UUID

a deployment id

required

Returns:

Type Description
Optional[ORMDeployment]

db.Deployment: the deployment

Source code in prefect/server/models/deployments.py
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
@db_injector
async def read_deployment(
    db: PrefectDBInterface, session: AsyncSession, deployment_id: UUID
) -> Optional["ORMDeployment"]:
    """Reads a deployment by id.

    Args:
        session: A database session
        deployment_id: a deployment id

    Returns:
        db.Deployment: the deployment
    """

    return await session.get(db.Deployment, deployment_id)

read_deployment_by_name async

Reads a deployment by name.

Parameters:

Name Type Description Default
session AsyncSession

A database session

required
name str

a deployment name

required
flow_name str

the name of the flow the deployment belongs to

required

Returns:

Type Description
Optional[ORMDeployment]

db.Deployment: the deployment

Source code in prefect/server/models/deployments.py
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
@db_injector
async def read_deployment_by_name(
    db: PrefectDBInterface, session: AsyncSession, name: str, flow_name: str
) -> Optional["ORMDeployment"]:
    """Reads a deployment by name.

    Args:
        session: A database session
        name: a deployment name
        flow_name: the name of the flow the deployment belongs to

    Returns:
        db.Deployment: the deployment
    """

    result = await session.execute(
        select(db.Deployment)
        .join(db.Flow, db.Deployment.flow_id == db.Flow.id)
        .where(
            sa.and_(
                db.Flow.name == flow_name,
                db.Deployment.name == name,
            )
        )
        .limit(1)
    )
    return result.scalar()

read_deployment_schedules async

Reads a deployment's schedules.

Parameters:

Name Type Description Default
session AsyncSession

A database session

required
deployment_id UUID

a deployment id

required

Returns:

Type Description
List[DeploymentSchedule]

list[schemas.core.DeploymentSchedule]: the deployment's schedules

Source code in prefect/server/models/deployments.py
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
@db_injector
async def read_deployment_schedules(
    db: PrefectDBInterface,
    session: AsyncSession,
    deployment_id: UUID,
    deployment_schedule_filter: Optional[
        schemas.filters.DeploymentScheduleFilter
    ] = None,
) -> List[schemas.core.DeploymentSchedule]:
    """
    Reads a deployment's schedules.

    Args:
        session: A database session
        deployment_id: a deployment id

    Returns:
        list[schemas.core.DeploymentSchedule]: the deployment's schedules
    """

    query = (
        sa.select(db.DeploymentSchedule)
        .where(db.DeploymentSchedule.deployment_id == deployment_id)
        .order_by(db.DeploymentSchedule.updated.desc())
    )

    if deployment_schedule_filter:
        query = query.where(deployment_schedule_filter.as_sql_filter(db))

    result = await session.execute(query)

    return [schemas.core.DeploymentSchedule.from_orm(s) for s in result.scalars().all()]

read_deployments async

Read deployments.

Parameters:

Name Type Description Default
session AsyncSession

A database session

required
offset int

Query offset

None
limit int

Query limit

None
flow_filter FlowFilter

only select deployments whose flows match these criteria

None
flow_run_filter FlowRunFilter

only select deployments whose flow runs match these criteria

None
task_run_filter TaskRunFilter

only select deployments whose task runs match these criteria

None
deployment_filter DeploymentFilter

only select deployment that match these filters

None
work_pool_filter WorkPoolFilter

only select deployments whose work pools match these criteria

None
work_queue_filter WorkQueueFilter

only select deployments whose work pool queues match these criteria

None
sort DeploymentSort

the sort criteria for selected deployments. Defaults to name ASC.

NAME_ASC

Returns:

Type Description
Sequence[ORMDeployment]

List[db.Deployment]: deployments

Source code in prefect/server/models/deployments.py
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
@db_injector
async def read_deployments(
    db: PrefectDBInterface,
    session: AsyncSession,
    offset: int = None,
    limit: int = None,
    flow_filter: schemas.filters.FlowFilter = None,
    flow_run_filter: schemas.filters.FlowRunFilter = None,
    task_run_filter: schemas.filters.TaskRunFilter = None,
    deployment_filter: schemas.filters.DeploymentFilter = None,
    work_pool_filter: schemas.filters.WorkPoolFilter = None,
    work_queue_filter: schemas.filters.WorkQueueFilter = None,
    sort: schemas.sorting.DeploymentSort = schemas.sorting.DeploymentSort.NAME_ASC,
) -> Sequence["ORMDeployment"]:
    """
    Read deployments.

    Args:
        session: A database session
        offset: Query offset
        limit: Query limit
        flow_filter: only select deployments whose flows match these criteria
        flow_run_filter: only select deployments whose flow runs match these criteria
        task_run_filter: only select deployments whose task runs match these criteria
        deployment_filter: only select deployment that match these filters
        work_pool_filter: only select deployments whose work pools match these criteria
        work_queue_filter: only select deployments whose work pool queues match these criteria
        sort: the sort criteria for selected deployments. Defaults to `name` ASC.

    Returns:
        List[db.Deployment]: deployments
    """

    query = select(db.Deployment).order_by(sort.as_sql_sort(db=db))

    query = await _apply_deployment_filters(
        query=query,
        flow_filter=flow_filter,
        flow_run_filter=flow_run_filter,
        task_run_filter=task_run_filter,
        deployment_filter=deployment_filter,
        work_pool_filter=work_pool_filter,
        work_queue_filter=work_queue_filter,
    )

    if offset is not None:
        query = query.offset(offset)
    if limit is not None:
        query = query.limit(limit)

    result = await session.execute(query)
    return result.scalars().unique().all()

schedule_runs async

Schedule flow runs for a deployment

Parameters:

Name Type Description Default
session AsyncSession

a database session

required
deployment_id UUID

the id of the deployment to schedule

required
start_time datetime

the time from which to start scheduling runs

None
end_time datetime

runs will be scheduled until at most this time

None
min_time timedelta

runs will be scheduled until at least this far in the future

None
min_runs int

a minimum amount of runs to schedule

None
max_runs int

a maximum amount of runs to schedule

None

This function will generate the minimum number of runs that satisfy the min and max times, and the min and max counts. Specifically, the following order will be respected.

- Runs will be generated starting on or after the `start_time`
- No more than `max_runs` runs will be generated
- No runs will be generated after `end_time` is reached
- At least `min_runs` runs will be generated
- Runs will be generated until at least `start_time` + `min_time` is reached

Returns:

Type Description
List[UUID]

a list of flow run ids scheduled for the deployment

Source code in prefect/server/models/deployments.py
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
async def schedule_runs(
    session: AsyncSession,
    deployment_id: UUID,
    start_time: datetime.datetime = None,
    end_time: datetime.datetime = None,
    min_time: datetime.timedelta = None,
    min_runs: int = None,
    max_runs: int = None,
    auto_scheduled: bool = True,
) -> List[UUID]:
    """
    Schedule flow runs for a deployment

    Args:
        session: a database session
        deployment_id: the id of the deployment to schedule
        start_time: the time from which to start scheduling runs
        end_time: runs will be scheduled until at most this time
        min_time: runs will be scheduled until at least this far in the future
        min_runs: a minimum amount of runs to schedule
        max_runs: a maximum amount of runs to schedule

    This function will generate the minimum number of runs that satisfy the min
    and max times, and the min and max counts. Specifically, the following order
    will be respected.

        - Runs will be generated starting on or after the `start_time`
        - No more than `max_runs` runs will be generated
        - No runs will be generated after `end_time` is reached
        - At least `min_runs` runs will be generated
        - Runs will be generated until at least `start_time` + `min_time` is reached

    Returns:
        a list of flow run ids scheduled for the deployment
    """
    if min_runs is None:
        min_runs = PREFECT_API_SERVICES_SCHEDULER_MIN_RUNS.value()
    if max_runs is None:
        max_runs = PREFECT_API_SERVICES_SCHEDULER_MAX_RUNS.value()
    if start_time is None:
        start_time = pendulum.now("UTC")
    if end_time is None:
        end_time = start_time + (
            PREFECT_API_SERVICES_SCHEDULER_MAX_SCHEDULED_TIME.value()
        )
    if min_time is None:
        min_time = PREFECT_API_SERVICES_SCHEDULER_MIN_SCHEDULED_TIME.value()

    start_time = pendulum.instance(start_time)
    end_time = pendulum.instance(end_time)

    runs = await _generate_scheduled_flow_runs(
        session=session,
        deployment_id=deployment_id,
        start_time=start_time,
        end_time=end_time,
        min_time=min_time,
        min_runs=min_runs,
        max_runs=max_runs,
        auto_scheduled=auto_scheduled,
    )
    return await _insert_scheduled_flow_runs(session=session, runs=runs)

update_deployment async

Updates a deployment.

Parameters:

Name Type Description Default
session AsyncSession

a database session

required
deployment_id UUID

the ID of the deployment to modify

required
deployment DeploymentUpdate

changes to a deployment model

required

Returns:

Name Type Description
bool bool

whether the deployment was updated

Source code in prefect/server/models/deployments.py
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
@db_injector
async def update_deployment(
    db: PrefectDBInterface,
    session: AsyncSession,
    deployment_id: UUID,
    deployment: schemas.actions.DeploymentUpdate,
) -> bool:
    """Updates a deployment.

    Args:
        session: a database session
        deployment_id: the ID of the deployment to modify
        deployment: changes to a deployment model

    Returns:
        bool: whether the deployment was updated

    """

    from prefect.server.api.workers import WorkerLookups

    schedules = deployment.schedules

    # exclude_unset=True allows us to only update values provided by
    # the user, ignoring any defaults on the model
    update_data = deployment.dict(
        shallow=True,
        exclude_unset=True,
        exclude={"work_pool_name"},
    )

    # The job_variables field in client and server schemas is named
    # infra_overrides in the database.
    job_variables = update_data.pop("job_variables", None)
    if job_variables:
        update_data["infra_overrides"] = job_variables

    should_update_schedules = update_data.pop("schedules", None) is not None

    if deployment.work_pool_name and deployment.work_queue_name:
        # If a specific pool name/queue name combination was provided, get the
        # ID for that work pool queue.
        update_data[
            "work_queue_id"
        ] = await WorkerLookups()._get_work_queue_id_from_name(
            session=session,
            work_pool_name=deployment.work_pool_name,
            work_queue_name=deployment.work_queue_name,
            create_queue_if_not_found=True,
        )
    elif deployment.work_pool_name:
        # If just a pool name was provided, get the ID for its default
        # work pool queue.
        update_data[
            "work_queue_id"
        ] = await WorkerLookups()._get_default_work_queue_id_from_work_pool_name(
            session=session,
            work_pool_name=deployment.work_pool_name,
        )
    elif deployment.work_queue_name:
        # If just a queue name was provided, ensure the queue exists and
        # get its ID.
        work_queue = await models.work_queues.ensure_work_queue_exists(
            session=session, name=update_data["work_queue_name"]
        )
        update_data["work_queue_id"] = work_queue.id

    if "is_schedule_active" in update_data:
        update_data["paused"] = not update_data["is_schedule_active"]

    update_stmt = (
        sa.update(db.Deployment)
        .where(db.Deployment.id == deployment_id)
        .values(**update_data)
    )
    result = await session.execute(update_stmt)

    # delete any auto scheduled runs that would have reflected the old deployment config
    await _delete_scheduled_runs(
        session=session, deployment_id=deployment_id, auto_scheduled_only=True
    )

    if should_update_schedules:
        # If schedules were provided, remove the existing schedules and
        # replace them with the new ones.
        await delete_schedules_for_deployment(
            session=session, deployment_id=deployment_id
        )
        await create_deployment_schedules(
            session=session,
            deployment_id=deployment_id,
            schedules=[
                schemas.actions.DeploymentScheduleCreate(
                    schedule=schedule.schedule,
                    active=schedule.active,  # type: ignore[call-arg]
                )
                for schedule in schedules
            ],
        )

    return result.rowcount > 0

update_deployment_schedule async

Updates a deployment's schedules.

Parameters:

Name Type Description Default
session AsyncSession

A database session

required
deployment_schedule_id UUID

a deployment schedule id

required
schedule DeploymentScheduleUpdate

a deployment schedule update action

required
Source code in prefect/server/models/deployments.py
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
@db_injector
async def update_deployment_schedule(
    db: PrefectDBInterface,
    session: AsyncSession,
    deployment_id: UUID,
    deployment_schedule_id: UUID,
    schedule: schemas.actions.DeploymentScheduleUpdate,
) -> bool:
    """
    Updates a deployment's schedules.

    Args:
        session: A database session
        deployment_schedule_id: a deployment schedule id
        schedule: a deployment schedule update action
    """

    result = await session.execute(
        sa.update(db.DeploymentSchedule)
        .where(
            sa.and_(
                db.DeploymentSchedule.id == deployment_schedule_id,
                db.DeploymentSchedule.deployment_id == deployment_id,
            )
        )
        .values(**schedule.dict(exclude_none=True))
    )

    return result.rowcount > 0