Project

General

Profile

0001-user-add-a-deleted-flag-24430.patch

Benjamin Dauvergne, 19 Dec 2018 04:20 PM

Download (5.21 KB)

View differences:

Subject: [PATCH 1/5] user: add a deleted flag (#24430)

 tests/test_sql.py | 23 +++++++++++++++++++++++
 wcs/sql.py        | 17 ++++++++++++-----
 wcs/users.py      |  1 +
 3 files changed, 36 insertions(+), 5 deletions(-)
tests/test_sql.py
1179 1179
    conn.commit()
1180 1180
    cur.close()
1181 1181

  
1182
@postgresql
1183
def test_migration_29_user_deleted():
1184
    conn, cur = sql.get_connection_and_cursor()
1185
    cur.execute('UPDATE wcs_meta SET value = 28 WHERE key = %s', ('sql_level',))
1186
    conn.commit()
1187
    cur.close()
1188

  
1189
    sql.SqlUser.wipe()
1190
    user = sql.SqlUser()
1191
    user.name = 'Jean Sénisme'
1192
    user.store()
1193
    assert sql.SqlUser.count() == 1
1194

  
1195
    conn, cur = sql.get_connection_and_cursor()
1196
    cur.execute('ALTER TABLE users DROP COLUMN deleted')
1197
    assert not column_exists_in_table(cur, 'users', 'deleted')
1198
    sql.migrate()
1199
    assert column_exists_in_table(cur, 'users', 'ascii_name')
1200
    assert migration_level(cur) >= 28
1201

  
1202
    assert sql.SqlUser.count() == 1
1203
    assert not sql.SqlUser.get(id=user.id).deleted
1204

  
1182 1205
def drop_formdef_tables():
1183 1206
    conn, cur = sql.get_connection_and_cursor()
1184 1207
    cur.execute('''SELECT table_name FROM information_schema.tables''')
wcs/sql.py
540 540
                                    verified_fields text[],
541 541
                                    name_identifiers text[],
542 542
                                    lasso_dump text,
543
                                    last_seen timestamp)''' % table_name)
543
                                    last_seen timestamp,
544
                                    deleted boolean DEFAULT(FALSE))''' % table_name)
544 545
    cur.execute('''SELECT column_name FROM information_schema.columns
545 546
                    WHERE table_schema = 'public'
546 547
                      AND table_name = %s''', (table_name,))
......
548 549

  
549 550
    needed_fields = set(['id', 'name', 'email', 'roles', 'is_admin',
550 551
        'anonymous', 'name_identifiers', 'verified_fields',
551
        'lasso_dump', 'last_seen', 'fts', 'ascii_name'])
552
        'lasso_dump', 'last_seen', 'fts', 'ascii_name', 'deleted'])
552 553

  
553 554
    from admin.settings import UserFieldsFormDef
554 555
    formdef = UserFieldsFormDef()
......
587 588
    if not 'ascii_name' in existing_fields:
588 589
        cur.execute('ALTER TABLE %s ADD COLUMN ascii_name varchar' % table_name)
589 590

  
591
    if not 'deleted' in existing_fields:
592
        cur.execute('ALTER TABLE %s ADD COLUMN deleted DEFAULT(FALSE)' % table_name)
593

  
590 594
    # delete obsolete fields
591 595
    for field in (existing_fields - needed_fields):
592 596
        cur.execute('''ALTER TABLE %s DROP COLUMN %s''' % (table_name, field))
......
1605 1609
        ('lasso_dump', 'text'),
1606 1610
        ('last_seen', 'timestamp'),
1607 1611
        ('ascii_name', 'varchar'),
1612
        ('deleted', 'boolean'),
1608 1613
    ]
1609 1614

  
1610 1615
    id = None
......
1629 1634
                'verified_fields': self.verified_fields,
1630 1635
                'lasso_dump': self.lasso_dump,
1631 1636
                'last_seen': None,
1637
                'deleted': self.deleted,
1632 1638
        }
1633 1639
        if self.last_seen:
1634 1640
            sql_dict['last_seen'] = datetime.datetime.fromtimestamp(self.last_seen),
......
1696 1702
        o = cls()
1697 1703
        (o.id, o.name, o.email, o.roles, o.is_admin, o.anonymous,
1698 1704
         o.name_identifiers, o.verified_fields, o.lasso_dump,
1699
         o.last_seen, ascii_name) = [str_encode(x) for x in tuple(row[:11])]
1705
         o.last_seen, ascii_name, o.deleted) = [str_encode(x) for x in tuple(row[:12])]
1700 1706
        if o.last_seen:
1701 1707
            o.last_seen = time.mktime(o.last_seen.timetuple())
1702 1708
        if o.roles:
......
2151 2157
    return result
2152 2158

  
2153 2159

  
2154
SQL_LEVEL = 28
2160
SQL_LEVEL = 29
2155 2161

  
2156 2162
def migrate_global_views(conn, cur):
2157 2163
    cur.execute('''SELECT COUNT(*) FROM information_schema.tables
......
2235 2241
        # 26: add digest to formdata
2236 2242
        # 27: add last_jump_datetime in evolutions tables
2237 2243
        migrate_views(conn, cur)
2238
    if sql_level < 21:
2244
    if sql_level < 29:
2239 2245
        # 3: introduction of _structured for user fields
2240 2246
        # 4: removal of identification_token
2241 2247
        # 12: (first part) add fts to users
2242 2248
        # 16: add verified_fields to users
2243 2249
        # 21: (first part) add ascii_name to users
2250
        # 29: add deleted
2244 2251
        do_user_table()
2245 2252
    if sql_level < 6:
2246 2253
        # 6: add actions_roles_array to tables and views
wcs/users.py
35 35
    verified_fields = None
36 36
    name_identifiers = None
37 37
    lasso_dump = None
38
    deleted = False
38 39

  
39 40
    last_seen = None
40 41

  
41
-