Projet

Général

Profil

0001-sql-add-created_at-updated_at-columns-on-wcs_meta-ta.patch

Lauréline Guérin, 19 mai 2022 10:37

Télécharger (3,74 ko)

Voir les différences:

Subject: [PATCH] sql: add created_at & updated_at columns on wcs_meta table
 (#63466)

 tests/test_sql.py | 26 ++++++++++++++++++++++++++
 wcs/sql.py        | 42 ++++++++++++++++++++++++++++++++++++++++--
 2 files changed, 66 insertions(+), 2 deletions(-)
tests/test_sql.py
1074 1074
    return bool(cur.fetchone()[0] == 1)
1075 1075

  
1076 1076

  
1077
def test_wcs_meta_dates(pub):
1078
    conn, cur = sql.get_connection_and_cursor()
1079
    cur.execute('INSERT INTO wcs_meta(key, value) VALUES (%s, %s)', ('foo', 'bar'))
1080
    cur.execute('SELECT created_at, updated_at FROM wcs_meta WHERE key = %s', ('foo',))
1081
    conn.commit()
1082
    row = cur.fetchone()
1083
    assert row[0] is not None
1084
    assert row[1] is not None
1085
    old_created_at = row[0]
1086
    old_updated_at = row[0]
1087

  
1088
    cur.execute('UPDATE wcs_meta SET value = %s WHERE key = %s', ('bar', 'foo'))
1089
    cur.execute('SELECT created_at, updated_at FROM wcs_meta WHERE key = %s', ('foo',))
1090
    conn.commit()
1091
    row = cur.fetchone()
1092
    assert row[0] == old_created_at
1093
    assert row[1] == old_updated_at
1094

  
1095
    cur.execute('UPDATE wcs_meta SET value = %s WHERE key = %s', ('bar-2', 'foo'))
1096
    cur.execute('SELECT created_at, updated_at FROM wcs_meta WHERE key = %s', ('foo',))
1097
    conn.commit()
1098
    row = cur.fetchone()
1099
    assert row[0] == old_created_at
1100
    assert row[1] != old_updated_at
1101

  
1102

  
1077 1103
def test_sql_level(pub):
1078 1104
    conn, cur = sql.get_connection_and_cursor()
1079 1105
    cur.execute('DROP TABLE wcs_meta')
wcs/sql.py
1487 1487
                      AND table_name = %s''',
1488 1488
        ('wcs_meta',),
1489 1489
    )
1490
    if cur.fetchone()[0] == 0:
1490
    table_exists = cur.fetchone()[0] > 0
1491

  
1492
    if not table_exists:
1491 1493
        cur.execute(
1492 1494
            '''CREATE TABLE wcs_meta (id serial PRIMARY KEY,
1493 1495
                                    key varchar,
1494
                                    value varchar)'''
1496
                                    value varchar,
1497
                                    created_at timestamptz DEFAULT NOW(),
1498
                                    updated_at timestamptz DEFAULT NOW())'''
1495 1499
        )
1500
    else:
1501
        cur.execute(
1502
            '''SELECT column_name FROM information_schema.columns
1503
                        WHERE table_schema = 'public'
1504
                          AND table_name = %s''',
1505
            ('wcs_meta',),
1506
        )
1507
        existing_fields = {x[0] for x in cur.fetchall()}
1508
        if 'created_at' not in existing_fields:
1509
            cur.execute('''ALTER TABLE wcs_meta ADD COLUMN created_at timestamptz DEFAULT NOW()''')
1510
        if 'updated_at' not in existing_fields:
1511
            cur.execute('''ALTER TABLE wcs_meta ADD COLUMN updated_at timestamptz DEFAULT NOW()''')
1512

  
1513
    cur.execute(
1514
        '''
1515
CREATE OR REPLACE FUNCTION meta_table_dates_fn()
1516
RETURNS trigger
1517
LANGUAGE plpgsql
1518
AS $$
1519
BEGIN
1520
  NEW.updated_at = NOW();
1521
  RETURN NEW;
1522
END;
1523
$$;'''
1524
    )
1525
    cur.execute('DROP TRIGGER IF EXISTS meta_table_dates_trg ON wcs_meta;')
1526
    cur.execute(
1527
        '''CREATE TRIGGER meta_table_dates_trg BEFORE UPDATE ON wcs_meta
1528
            FOR EACH ROW
1529
            WHEN (OLD.value IS DISTINCT FROM NEW.value)
1530
            EXECUTE PROCEDURE meta_table_dates_fn();'''
1531
    )
1532

  
1533
    if not table_exists:
1496 1534
        if insert_current_sql_level:
1497 1535
            sql_level = SQL_LEVEL[0]
1498 1536
        else:
1499
-