Project

General

Profile

0001-sql-add-evo.who-on-evolution-update-31472.patch

Benjamin Dauvergne, 19 Apr 2019 06:27 PM

Download (4.55 KB)

View differences:

Subject: [PATCH] sql: add evo.who on evolution update (#31472)

 tests/test_sql.py       | 45 +++++++++++++++++++++++++++++++++++++++++
 tests/test_workflows.py |  3 ++-
 wcs/sql.py              | 12 ++++++++++-
 3 files changed, 58 insertions(+), 2 deletions(-)
tests/test_sql.py
1786 1786
    formdatas = sql.AnyFormData.select(criterias, order_by='receipt_time', limit=20, offset=0)
1787 1787
    print time.time() - t0
1788 1788
    assert (time.time() - t0) < 0.5
1789

  
1790

  
1791
@postgresql
1792
def test_migration_30_anonymize_evo_who():
1793
    formdef = FormDef()
1794
    formdef.name = 'tests migration 24'
1795
    formdef.fields = []
1796
    formdef.store()
1797

  
1798
    user = sql.SqlUser()
1799
    user.name = 'JohnDoe'
1800
    user.store()
1801

  
1802
    klass = formdef.data_class()
1803
    formdata = klass()
1804
    formdata.evolution = []
1805
    formdata.anonymised = datetime.datetime.now()
1806
    evo = Evolution(formdata)
1807
    evo.who = user.id
1808
    evo.time = time.localtime()
1809
    formdata.evolution.append(evo)
1810
    formdata.store()
1811

  
1812
    conn, cur = sql.get_connection_and_cursor()
1813
    cur.execute('UPDATE wcs_meta SET value = 29 WHERE key = %s', ('sql_level',))
1814
    conn.commit()
1815
    cur.close()
1816

  
1817
    conn, cur = sql.get_connection_and_cursor()
1818
    cur.execute('SELECT COUNT(*) FROM %s_evolutions WHERE who IS NULL' % formdef.table_name)
1819
    assert cur.fetchone() == (0,)
1820
    cur.execute('SELECT COUNT(*) FROM wcs_meta WHERE key = %s AND value::integer > 29', ('sql_level',))
1821
    assert cur.fetchone() == (0,)
1822
    conn.commit()
1823
    cur.close()
1824

  
1825
    sql.migrate()
1826

  
1827
    conn, cur = sql.get_connection_and_cursor()
1828
    cur.execute('SELECT COUNT(*) FROM %s_evolutions WHERE who IS NULL' % formdef.table_name)
1829
    assert cur.fetchone() == (1,)
1830
    cur.execute('SELECT COUNT(*) FROM wcs_meta WHERE key = %s AND value::integer > 29', ('sql_level',))
1831
    assert cur.fetchone() == (1,)
1832
    conn.commit()
1833
    cur.close()
tests/test_workflows.py
675 675
        item2.perform(formdata)
676 676
        assert pub.user_class.get(user.id).roles == []
677 677

  
678
def test_anonymise(pub):
678
def test_anonymise(two_pubs):
679 679
    # build a backoffice field
680 680
    Workflow.wipe()
681 681
    wf = Workflow(name='wf with backoffice field')
......
718 718
                                                          'bo1': None,
719 719
                                                          'bo2': 'foo', 'bo2_display': 'foo'}
720 720
    assert formdef.data_class().get(formdata.id).workflow_data is None
721
    assert formdef.data_class().get(formdata.id).evolution[0].who is None
721 722

  
722 723
def test_remove(pub):
723 724
    formdef = FormDef()
wcs/sql.py
1377 1377
                if hasattr(evo, '_sql_id'):
1378 1378
                    sql_dict.update({'id': evo._sql_id})
1379 1379
                    sql_statement = '''UPDATE %s_evolutions SET
1380
                                        who = %%(who)s,
1380 1381
                                        time = %%(time)s,
1381 1382
                                        last_jump_datetime = %%(last_jump_datetime)s,
1382 1383
                                        status = %%(status)s,
......
2167 2168
    return result
2168 2169

  
2169 2170

  
2170
SQL_LEVEL = 29
2171
SQL_LEVEL = 30
2171 2172

  
2172 2173
def migrate_global_views(conn, cur):
2173 2174
    cur.execute('''SELECT COUNT(*) FROM information_schema.tables
......
2284 2285
            do_formdef_indexes(formdef, created=False, conn=conn, cur=cur)
2285 2286
    if sql_level < 25:
2286 2287
        do_session_table()
2288
    if sql_level < 30:
2289
        # 30: actually remove evo.who on anonymised formdatas
2290
        from wcs.formdef import FormDef
2291
        for formdef in FormDef.select():
2292
            for formdata in formdef.data_class().select_iterator(clause=[NotNull('anonymised')]):
2293
                if formdata.evolution:
2294
                    for evo in formdata.evolution:
2295
                        evo.who = None
2296
                    formdata.store()
2287 2297

  
2288 2298
    cur.execute('''UPDATE wcs_meta SET value = %s WHERE key = %s''', (
2289 2299
        str(SQL_LEVEL), 'sql_level'))
2290
-