~homeworkprod/byceps

ref: 3beaa96d910b36c05dfa41af8f22073a10dcab53 byceps/scripts/generate_sql_to_delete_user.py -rwxr-xr-x 1.4 KiB
3beaa96d — Jochen Kupperschmidt PostgreSQL v9.6 is only available as of Ubuntu Trusty, but not yet on Ubuntu Precise 5 years 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
#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""Generate the SQL statements to remove a user and his/her various
traces from the database.

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

import click

from byceps.util.system import get_config_filename_from_env_or_exit

from bootstrap.util import app_context
from bootstrap.validators import validate_user_screen_name


@click.command()
@click.argument('user', callback=validate_user_screen_name)
def execute(user):
    click.echo('Generating SQL statements to delete user "{}" ...'.format(user.screen_name))

    statements = generate_delete_statements(user.id)
    for statement in statements:
        click.secho(statement, fg='red')


def generate_delete_statements(user_id):
    for table, user_id_column in [
        ('authz_user_roles', 'user_id'),
        ('board_categories_lastviews', 'user_id'),
        ('board_topics_lastviews', 'user_id'),
        ('newsletter_subscriptions', 'user_id'),
        ('terms_consents', 'user_id'),
        ('verification_tokens', 'user_id'),
        ('user_details', 'user_id'),
        ('users', 'id'),
    ]:
        yield "DELETE FROM {} WHERE {} = '{}';".format(
            table, user_id_column, user_id)


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