Skip to content

prefect.server.models.block_registration

register_block_schema(session, block_schema) async

Stores the provided block schema in the Prefect REST API database.

If a block schema with a matching checksum and version is already saved, then the ID of the existing block schema will be returned.

Parameters:

Name Type Description Default
session Session

A database session.

required
block_schema BlockSchema

A block schema object.

required

Returns:

Type Description

The ID of the registered block schema.

Source code in src/prefect/server/models/block_registration.py
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
async def register_block_schema(
    session: sa.orm.Session,
    block_schema: schemas.core.BlockSchema,
):
    """
    Stores the provided block schema in the Prefect REST API database.

    If a block schema with a matching checksum and version is already saved,
    then the ID of the existing block schema will be returned.

    Args:
        session: A database session.
        block_schema: A block schema object.

    Returns:
        The ID of the registered block schema.
    """

    from prefect.server.models.block_schemas import (
        create_block_schema,
        read_block_schema_by_checksum,
    )

    existing_block_schema = await read_block_schema_by_checksum(
        session=session, checksum=block_schema.checksum, version=block_schema.version
    )
    if existing_block_schema is None:
        block_schema = await create_block_schema(
            session=session,
            block_schema=block_schema,
        )
        return block_schema.id
    else:
        return existing_block_schema.id

register_block_type(session, block_type) async

Stores the provided block type in the Prefect REST API database.

If a block type with a matching slug is already saved, then the block type will be updated to match the passed in block type.

Parameters:

Name Type Description Default
session Session

A database session.

required
block_type BlockType

A block type object.

required

Returns:

Type Description

The ID of the registered block type.

Source code in src/prefect/server/models/block_registration.py
 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
async def register_block_type(
    session: sa.orm.Session,
    block_type: schemas.core.BlockType,
):
    """
    Stores the provided block type in the Prefect REST API database.

    If a block type with a matching slug is already saved, then the block type
    will be updated to match the passed in block type.

    Args:
        session: A database session.
        block_type: A block type object.

    Returns:
        The ID of the registered block type.
    """
    from prefect.server.models.block_types import (
        create_block_type,
        read_block_type_by_slug,
        update_block_type,
    )

    existing_block_type = await read_block_type_by_slug(
        session=session,
        block_type_slug=block_type.slug,
    )
    if existing_block_type is None:
        block_type = await create_block_type(
            session=session,
            block_type=block_type,
        )
        return block_type.id
    else:
        await update_block_type(
            session=session,
            block_type_id=existing_block_type.id,
            block_type=block_type,
        )
        return existing_block_type.id

run_block_auto_registration(session) async

Registers all blocks in the client block registry and any blocks from Prefect Collections that are configured for auto-registration.

Parameters:

Name Type Description Default
session Session

A database session.

required
Source code in src/prefect/server/models/block_registration.py
198
199
200
201
202
203
204
205
206
207
208
async def run_block_auto_registration(session: sa.orm.Session):
    """
    Registers all blocks in the client block registry and any blocks from Prefect
    Collections that are configured for auto-registration.

    Args:
        session: A database session.
    """
    await _install_protected_system_blocks(session)
    await _register_registry_blocks(session)
    await _register_collection_blocks(session=session)