Projet

Général

Profil

0001-sql-add-user-phone-field-migration-69838.patch

Paul Marillonnet, 25 octobre 2022 10:30

Télécharger (3,09 ko)

Voir les différences:

Subject: [PATCH 1/6] sql: add user phone field migration (#69838)

 tests/test_sql.py | 28 ++++++++++++++++++++++++++++
 wcs/sql.py        | 24 +++++++++++++++++++++++-
 2 files changed, 51 insertions(+), 1 deletion(-)
tests/test_sql.py
2239 2239
    assert len(objects) == 2
2240 2240

  
2241 2241

  
2242
def test_migration_68_add_user_phone_field(pub):
2243
    conn, cur = sql.get_connection_and_cursor()
2244
    cur.execute('UPDATE wcs_meta SET value = 67 WHERE key = %s', ('sql_level',))
2245
    conn.commit()
2246
    cur.close()
2247

  
2248
    sql.SqlUser.wipe()
2249
    user = sql.SqlUser()
2250
    user.name = 'Jean Sénisme'
2251
    user.store()
2252

  
2253
    user2 = sql.SqlUser()
2254
    user2.name = 'Jean II'
2255
    user2.store()
2256
    assert sql.SqlUser.count() == 2
2257

  
2258
    conn, cur = sql.get_connection_and_cursor()
2259
    cur.execute('ALTER TABLE users DROP COLUMN phone')
2260
    assert not column_exists_in_table(cur, 'users', 'phone')
2261
    sql.migrate()
2262
    assert column_exists_in_table(cur, 'users', 'phone')
2263
    assert migration_level(cur) >= 68
2264

  
2265
    assert sql.SqlUser.count() == 2
2266
    assert sql.SqlUser.get(id=user.id).phone is None
2267
    assert sql.SqlUser.get(id=user2.id).phone is None
2268

  
2269

  
2242 2270
def test_logged_error_store_without_integrity_error(pub, sql_queries):
2243 2271
    sql.LoggedError.record('there was an error')
2244 2272

  
wcs/sql.py
1577 1577
        token.store()
1578 1578

  
1579 1579

  
1580
def add_user_phone_field():
1581
    conn, cur = get_connection_and_cursor()
1582
    table_name = 'users'
1583

  
1584
    cur.execute(
1585
        '''SELECT column_name FROM information_schema.columns
1586
                    WHERE table_schema = 'public'
1587
                      AND table_name = %s''',
1588
        (table_name,),
1589
    )
1590
    existing_fields = {x[0] for x in cur.fetchall()}
1591

  
1592
    # migrations
1593
    if 'phone' not in existing_fields:
1594
        cur.execute('''ALTER TABLE %s ADD COLUMN phone VARCHAR''' % table_name)
1595

  
1596
    conn.commit()
1597
    cur.close()
1598

  
1599

  
1580 1600
@guard_postgres
1581 1601
def do_meta_table(conn=None, cur=None, insert_current_sql_level=True):
1582 1602
    own_conn = False
......
4418 4438
# latest migration, number + description (description is not used
4419 4439
# programmaticaly but will make sure git conflicts if two migrations are
4420 4440
# separately added with the same number)
4421
SQL_LEVEL = (67, 're-migrate legacy tokens')
4441
SQL_LEVEL = (68, 'add user phone field')
4422 4442

  
4423 4443

  
4424 4444
def migrate_global_views(conn, cur):
......
4579 4599
        # 67: re-migrate legacy tokens
4580 4600
        do_tokens_table()
4581 4601
        migrate_legacy_tokens()
4602
    if sql_level < 68:
4603
        add_user_phone_field()
4582 4604
    if sql_level < 52:
4583 4605
        # 2: introduction of formdef_id in views
4584 4606
        # 5: add concerned_roles_array, is_at_endpoint and fts to views
4585
-