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 AsyncSession

A database session.

required
block_schema Union[BlockSchema, BlockSchema]

A block schema object.

required

Returns:

Type Description
UUID

The ID of the registered block schema.

Source code in src/prefect/server/models/block_registration.py
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
async def register_block_schema(
    session: AsyncSession,
    block_schema: Union[schemas.core.BlockSchema, "ClientBlockSchema"],
) -> UUID:
    """
    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:
        new_block_schema = await create_block_schema(
            session=session,
            block_schema=block_schema,
        )
        return new_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 AsyncSession

A database session.

required
block_type Union[BlockType, BlockType]

A block type object.

required

Returns:

Type Description
UUID

The ID of the registered block type.

Source code in src/prefect/server/models/block_registration.py
 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
async def register_block_type(
    session: AsyncSession,
    block_type: Union[schemas.core.BlockType, "ClientBlockType"],
) -> UUID:
    """
    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:
        new_block_type = await create_block_type(
            session=session,
            block_type=block_type,
        )
        assert new_block_type is not None, f"Failed to create block type {block_type}"
        return new_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 AsyncSession

A database session.

required
Source code in src/prefect/server/models/block_registration.py
204
205
206
207
208
209
210
211
212
213
214
async def run_block_auto_registration(session: AsyncSession) -> None:
    """
    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)