Projet

Général

Profil

0001-logged-errors-add-deprecated-usages-56613.patch

Lauréline Guérin, 06 septembre 2021 16:23

Télécharger (8,63 ko)

Voir les différences:

Subject: [PATCH 1/2] logged-errors: add deprecated usages (#56613)

 tests/test_logged_errors.py | 31 +++++++++++++++++++++++++
 wcs/logged_errors.py        |  7 +++++-
 wcs/publisher.py            | 22 ++++++++++++++++--
 wcs/sql.py                  | 45 +++++++++++++++++++++----------------
 4 files changed, 83 insertions(+), 22 deletions(-)
 create mode 100644 tests/test_logged_errors.py
tests/test_logged_errors.py
1
import pytest
2

  
3
from .utilities import clean_temporary_pub, create_temporary_pub
4

  
5

  
6
@pytest.fixture
7
def pub(request):
8
    return create_temporary_pub(sql_mode=True)
9

  
10

  
11
def teardown_module(module):
12
    clean_temporary_pub()
13

  
14

  
15
def test_deprecated_error(pub):
16
    pub.loggederror_class.wipe()
17

  
18
    pub.record_deprecated_usage('foo bar')
19
    assert pub.loggederror_class.count() == 1
20
    error = pub.loggederror_class.select()[0]
21
    assert error.summary == '[DEPRECATED] foo bar'
22
    assert error.kind == 'deprecated_usage'
23
    assert error.occurences_count == 1
24

  
25
    pub.record_deprecated_usage('foo bar')
26
    pub.record_deprecated_usage('foo bar')
27
    assert pub.loggederror_class.count() == 1
28
    error = pub.loggederror_class.select()[0]
29
    assert error.summary == '[DEPRECATED] foo bar'
30
    assert error.kind == 'deprecated_usage'
31
    assert error.occurences_count == 3
wcs/logged_errors.py
28 28
    _names = 'logged-errors'
29 29

  
30 30
    id = None
31
    kind = None
31 32
    tech_id = None
32 33
    summary = None
33 34
    formdef_class = None
......
58 59
        expression=None,
59 60
        expression_type=None,
60 61
        exception=None,
62
        kind=None,
61 63
    ):
62 64
        # noqa pylint: disable=too-many-arguments
63 65
        error = cls()
66
        error.kind = kind
64 67
        error.summary = str(error_summary)
65 68
        error.traceback = plain_error_msg
66 69
        error.expression = expression
......
98 101
        if not self.id:
99 102
            return
100 103
        self.occurences_count += 1
104
        self.kind = error.kind
101 105
        self.latest_occurence_timestamp = now()
102 106
        # update with new error context
103 107
        self.formdata_id = error.formdata_id
......
111 115
        self.store()
112 116

  
113 117
    @classmethod
114
    def record_error(cls, error_summary, plain_error_msg, publisher, *args, **kwargs):
118
    def record_error(cls, error_summary, plain_error_msg, publisher, kind=None, *args, **kwargs):
115 119
        formdef = kwargs.pop('formdef', None)
116 120
        formdata = kwargs.pop('formdata', None)
117 121
        workflow = kwargs.pop('workflow', None)
......
137 141
            formdata=formdata,
138 142
            formdef=formdef,
139 143
            workflow=workflow,
144
            kind=kind,
140 145
            *args,
141 146
            **kwargs,
142 147
        )
wcs/publisher.py
358 358
        conn.commit()
359 359
        cur.close()
360 360

  
361
    def record_deprecated_usage(self, *args, **kwargs):
362
        return self.record_error(context='[DEPRECATED]', deprecated_usage=True, *args, **kwargs)
363

  
361 364
    def record_error(
362
        self, error_summary=None, context=None, exception=None, record=True, notify=False, *args, **kwargs
365
        self,
366
        error_summary=None,
367
        context=None,
368
        exception=None,
369
        record=True,
370
        notify=False,
371
        deprecated_usage=False,
372
        *args,
373
        **kwargs,
363 374
    ):
364 375
        if not record and not notify:
365 376
            return
......
391 402

  
392 403
        logged_exception = None
393 404
        if record and self.loggederror_class:
405
            kind = 'deprecated_usage' if deprecated_usage else None
394 406
            logged_exception = self.loggederror_class.record_error(
395
                error_summary, plain_error_msg, publisher=self, exception=exception, *args, **kwargs
407
                error_summary,
408
                plain_error_msg,
409
                publisher=self,
410
                exception=exception,
411
                kind=kind,
412
                *args,
413
                **kwargs,
396 414
            )
397 415
        if not notify or logged_exception and logged_exception.occurences_count > 1:
398 416
            # notify only first occurence
wcs/sql.py
1092 1092
    if cur.fetchone()[0] == 0:
1093 1093
        cur.execute(
1094 1094
            '''CREATE TABLE %s (id SERIAL PRIMARY KEY,
1095
                                        tech_id VARCHAR UNIQUE,
1096
                                        summary VARCHAR,
1097
                                        formdef_class VARCHAR,
1098
                                        formdata_id VARCHAR,
1099
                                        formdef_id VARCHAR,
1100
                                        workflow_id VARCHAR,
1101
                                        status_id VARCHAR,
1102
                                        status_item_id VARCHAR,
1103
                                        expression VARCHAR,
1104
                                        expression_type VARCHAR,
1105
                                        traceback TEXT,
1106
                                        exception_class VARCHAR,
1107
                                        exception_message VARCHAR,
1108
                                        occurences_count INTEGER,
1109
                                        first_occurence_timestamp TIMESTAMP WITH TIME ZONE,
1110
                                        latest_occurence_timestamp TIMESTAMP WITH TIME ZONE
1111
                                        )'''
1095
                                kind VARCHAR,
1096
                                tech_id VARCHAR UNIQUE,
1097
                                summary VARCHAR,
1098
                                formdef_class VARCHAR,
1099
                                formdata_id VARCHAR,
1100
                                formdef_id VARCHAR,
1101
                                workflow_id VARCHAR,
1102
                                status_id VARCHAR,
1103
                                status_item_id VARCHAR,
1104
                                expression VARCHAR,
1105
                                expression_type VARCHAR,
1106
                                traceback TEXT,
1107
                                exception_class VARCHAR,
1108
                                exception_message VARCHAR,
1109
                                occurences_count INTEGER,
1110
                                first_occurence_timestamp TIMESTAMP WITH TIME ZONE,
1111
                                latest_occurence_timestamp TIMESTAMP WITH TIME ZONE
1112
                                )'''
1112 1113
            % table_name
1113 1114
        )
1114 1115
    cur.execute(
......
1121 1122

  
1122 1123
    needed_fields = {x[0] for x in LoggedError._table_static_fields}
1123 1124

  
1125
    # migrations
1126
    if 'kind' not in existing_fields:
1127
        cur.execute('''ALTER TABLE %s ADD COLUMN kind VARCHAR''' % table_name)
1128

  
1124 1129
    # delete obsolete fields
1125 1130
    for field in existing_fields - needed_fields:
1126 1131
        cur.execute('''ALTER TABLE %s DROP COLUMN %s''' % (table_name, field))
......
3033 3038
    _table_name = 'loggederrors'
3034 3039
    _table_static_fields = [
3035 3040
        ('id', 'serial'),
3041
        ('kind', 'varchar'),
3036 3042
        ('tech_id', 'varchar'),
3037 3043
        ('summary', 'varchar'),
3038 3044
        ('formdef_class', 'varchar'),
......
3435 3441
# latest migration, number + description (description is not used
3436 3442
# programmaticaly but will make sure git conflicts if two migrations are
3437 3443
# separately added with the same number)
3438
SQL_LEVEL = (52, 'store digests on formdata and carddata')
3444
SQL_LEVEL = (53, 'add kind column on logged_errors table')
3439 3445

  
3440 3446

  
3441 3447
def migrate_global_views(conn, cur):
......
3614 3620
    if sql_level < 42:
3615 3621
        # 42: create snapshots table
3616 3622
        do_snapshots_table()
3617
    if sql_level < 48:
3623
    if sql_level < 53:
3618 3624
        # 47: store LoggedErrors in SQL
3619 3625
        # 48: remove acked attribute from LoggedError
3626
        # 53: add kind column to logged_errors table
3620 3627
        do_loggederrors_table()
3621 3628
    if sql_level < 50:
3622 3629
        # 49: store Role in SQL
3623
-