~homeworkprod/byceps

ref: 4237b3ec9496efe95dcce82bea3207ab9de4d520 byceps/scripts/generate_sql_to_delete_user.py -rwxr-xr-x 2.6 KiB
4237b3ec — Jochen Kupperschmidt Move ticketing blueprint into `site` subpackage 1 year, 10 months ago
                                                                                
1
2
3
4
5
6
7
8
9
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
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
#!/usr/bin/env python

"""Generate the SQL statements to remove one or more users and their
various (but not all) traces from the database.

Might fail for example if a user posted in a discussion board.

:Copyright: 2006-2020 Jochen Kupperschmidt
:License: Modified BSD, see LICENSE for details.
"""

from typing import Iterable, Iterator, List

import click

from byceps.services.user import service as user_service
from byceps.typing import UserID
from byceps.util.system import get_config_filename_from_env_or_exit

from _util import app_context
from _validators import validate_user_id_format


def validate_user_ids(ctx, param, user_ids: Iterable[UserID]) -> List[UserID]:
    def _validate():
        for user_id in user_ids:
            yield validate_user_id_format(ctx, param, user_id)

    return list(_validate())


@click.command()
@click.argument('user_ids', callback=validate_user_ids, nargs=-1, required=True)
def execute(user_ids):
    statements = generate_delete_statements_for_users(user_ids)
    for statement in statements:
        print(statement)


def generate_delete_statements_for_users(
    user_ids: Iterable[UserID],
) -> Iterator[str]:
    users = user_service.find_users(user_ids)
    existing_user_ids = {u.id for u in users}

    for user_id in user_ids:
        if user_id not in existing_user_ids:
            click.secho(
                # Mask as SQL comment in case STDERR output, is copied and
                # pasted/piped with the actual SQL statements into a RDBMS.
                f'-- Skipping unknown user ID "{user_id}".\n',
                fg='yellow',
                err=True,
            )
            continue

        yield from generate_delete_statements_for_user(user_id)
        yield ''  # empty line


def generate_delete_statements_for_user(user_id: UserID) -> Iterator[str]:
    for table, user_id_column in [
        ('authn_credentials', 'user_id'),
        ('authn_session_tokens', 'user_id'),
        ('authz_user_roles', 'user_id'),
        ('board_categories_lastviews', 'user_id'),
        ('board_topics_lastviews', 'user_id'),
        ('newsletter_subscription_updates', 'user_id'),
        ('consents', 'user_id'),
        ('verification_tokens', 'user_id'),
        ('user_avatar_selections', 'user_id'),
        ('user_avatars', 'creator_id'),
        ('user_details', 'user_id'),
        ('user_events', 'user_id'),
        ('users', 'id'),
    ]:
        yield f"DELETE FROM {table} WHERE {user_id_column} = '{user_id}';"


if __name__ == '__main__':
    config_filename = get_config_filename_from_env_or_exit()
    with app_context(config_filename):
        execute()