Skip to content

Cli

init_db()

Ensure the tables needed by this plugin exist.

Source code in ckanext/versioned_datastore/cli.py
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
@versioned_datastore.command(name='initdb')
def init_db():
    """
    Ensure the tables needed by this plugin exist.
    """
    tables = [
        import_stats_table,
        datastore_resource_details_table,
        datastore_slugs_table,
        navigational_slugs_table,
        datastore_downloads_core_files_table,
        datastore_downloads_derivative_files_table,
        datastore_downloads_requests_table,
    ]
    # create the tables if they don't exist
    for table in tables:
        if not table.exists():
            table.create()
    click.secho('Finished creating tables', fg='green')

reindex(resource_ids, full=False)

Reindex either a specific resource or all resources.

Source code in ckanext/versioned_datastore/cli.py
 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
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
@versioned_datastore.command()
@click.option('-r', '--resource_id', 'resource_ids', multiple=True)
@click.option(
    '--full',
    is_flag=True,
    show_default=True,
    default=False,
    help='Completely resync all data from MongoDB to Elasticsearch for the resource(s)',
)
def reindex(resource_ids: Tuple[str], full: bool = False):
    """
    Reindex either a specific resource or all resources.
    """
    ids = set()
    context = {'ignore_auth': True}

    if resource_ids:
        # the user has specified some resources to reindex
        ids.update(resource_ids)
    else:
        # the user hasn't specified the resources to reindex, so we should get a list of
        # all resources in the system
        data_dict = {'query': 'name:', 'offset': 0, 'limit': 100}
        while True:
            result = toolkit.get_action('resource_search')(context, data_dict)
            if len(result['results']) > 0:
                ids.update(resource['id'] for resource in result['results'])
                data_dict['offset'] += data_dict['limit']
            else:
                break

    if not ids:
        click.secho('No resources found to reindex', fg='green')
        return

    click.secho(f'Found {len(ids)} resources to reindex', fg='yellow')

    for resource_id in sorted(ids):
        try:
            result = toolkit.get_action('vds_data_sync')(
                context, {'resource_id': resource_id, 'full': full}
            )
            click.secho(
                f'Queued reindex of {resource_id} as job {result["job_id"]}', fg='cyan'
            )
        except toolkit.ValidationError as e:
            click.secho(
                f'Failed to reindex {resource_id} due to validation error: {e}',
                fg='red',
            )

versioned_datastore()

Perform various tasks on the versioned datastore.

Source code in ckanext/versioned_datastore/cli.py
25
26
27
28
29
30
@click.group()
def versioned_datastore():
    """
    Perform various tasks on the versioned datastore.
    """
    pass