Skip to content

Details

create_details(resource_id, version, columns, file_hash=None)

Helper for creating a DatastoreResourceDetails object.

Parameters:

Name Type Description Default
resource_id str

the resource id of the ingested resource

required
version int

the version being ingested

required
columns List[str]

the columns present in the ingested resource

required
file_hash Optional[str]

the computed file hash of the uploaded data

None

Returns:

Type Description
int

the id of the created row

Source code in ckanext/versioned_datastore/lib/importing/details.py
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
def create_details(
    resource_id: str,
    version: int,
    columns: List[str],
    file_hash: Optional[str] = None,
) -> int:
    """
    Helper for creating a DatastoreResourceDetails object.

    :param resource_id: the resource id of the ingested resource
    :param version: the version being ingested
    :param columns: the columns present in the ingested resource
    :param file_hash: the computed file hash of the uploaded data
    :returns: the id of the created row
    """
    columns_str = json.dumps(columns)
    details = DatastoreResourceDetails(
        resource_id=resource_id,
        version=version,
        columns=columns_str,
        file_hash=file_hash,
    )
    details.add()
    details.commit()
    return details.id

get_all_details(resource_id, up_to_version=None)

Retrieve all the details for a resource and return them as an OrderedDict using the versions as the keys. If the up_to_version parameter is passed then any versions beyond it are ignored and not returned in the resulting OrderedDict.

Parameters:

Name Type Description Default
resource_id

the resource id

required
up_to_version

the maximum version to include in the resulting OrderedDict (inclusive)

None

Returns:

Type Description
Dict[int, DatastoreResourceDetails]

None or an OrderedDict of version: DatastoreResourceDetails objects in ascending order

Source code in ckanext/versioned_datastore/lib/importing/details.py
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
def get_all_details(
    resource_id, up_to_version=None
) -> Dict[int, DatastoreResourceDetails]:
    """
    Retrieve all the details for a resource and return them as an OrderedDict using the
    versions as the keys. If the up_to_version parameter is passed then any versions
    beyond it are ignored and not returned in the resulting OrderedDict.

    :param resource_id: the resource id
    :param up_to_version: the maximum version to include in the resulting OrderedDict
        (inclusive)
    :returns: None or an OrderedDict of version: DatastoreResourceDetails objects in
        ascending order
    """
    query = (
        model.Session.query(DatastoreResourceDetails)
        .filter(DatastoreResourceDetails.resource_id == resource_id)
        .order_by(DatastoreResourceDetails.version.asc())
    )

    all_details = OrderedDict()
    for details in query:
        if up_to_version is not None and details.version > up_to_version:
            break
        else:
            all_details[details.version] = details

    return all_details

get_details(resource_id, version)

Retrieve the details for a resource at a given version.

Parameters:

Name Type Description Default
resource_id

the resource id

required
version

the version to get the details at

required

Returns:

Type Description

None or a DatastoreResourceDetails object

Source code in ckanext/versioned_datastore/lib/importing/details.py
37
38
39
40
41
42
43
44
45
46
47
48
49
50
def get_details(resource_id, version):
    """
    Retrieve the details for a resource at a given version.

    :param resource_id: the resource id
    :param version: the version to get the details at
    :returns: None or a DatastoreResourceDetails object
    """
    return (
        model.Session.query(DatastoreResourceDetails)
        .filter(DatastoreResourceDetails.resource_id == resource_id)
        .filter(DatastoreResourceDetails.version == version)
        .first()
    )

get_details_at(resource_id, version=None)

Retrieve the details that apply at the given version. If the version is None (the default) then the latest details are returned.

Parameters:

Name Type Description Default
resource_id str

the resource ID to get the details for

required
version Optional[int]

the version of the details to get (or None for latest)

None

Returns:

Type Description
Optional[DatastoreResourceDetails]

None if no details were found, or a DatastoreResourceDetails object

Source code in ckanext/versioned_datastore/lib/importing/details.py
 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
def get_details_at(
    resource_id: str, version: Optional[int] = None
) -> Optional[DatastoreResourceDetails]:
    """
    Retrieve the details that apply at the given version. If the version is None (the
    default) then the latest details are returned.

    :param resource_id: the resource ID to get the details for
    :param version: the version of the details to get (or None for latest)
    :returns: None if no details were found, or a DatastoreResourceDetails object
    """
    all_details = query = (
        model.Session.query(DatastoreResourceDetails)
        .filter(DatastoreResourceDetails.resource_id == resource_id)
        .order_by(DatastoreResourceDetails.version.asc())
    ).all()

    if not all_details:
        return None

    if version is None:
        return all_details[-1]

    candidate = None
    for details in query:
        if version >= details.version:
            candidate = details
        else:
            break
    return candidate

get_last_file_hash(resource_id)

Retrieves the most recent file hash and returns it. If there is no most recent file hash (either because there isn't a file hash present or because there aren't any details rows available) then this function returns None.

Parameters:

Name Type Description Default
resource_id

the resource id

required

Returns:

Type Description

None or the most recent file hash

Source code in ckanext/versioned_datastore/lib/importing/details.py
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
def get_last_file_hash(resource_id):
    """
    Retrieves the most recent file hash and returns it. If there is no most recent file
    hash (either because there isn't a file hash present or because there aren't any
    details rows available) then this function returns None.

    :param resource_id: the resource id
    :returns: None or the most recent file hash
    """
    last_details = (
        model.Session.query(DatastoreResourceDetails)
        .filter(DatastoreResourceDetails.resource_id == resource_id)
        .order_by(DatastoreResourceDetails.version.desc())
        .first()
    )

    return last_details.file_hash if last_details is not None else None