0001-sql-add-created_at-updated_at-columns-on-wcs_meta-ta.patch
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 | ||
1080 |
# reindex flags |
|
1081 |
sql.set_reindex('foo', 'bar', conn=conn, cur=cur) |
|
1082 |
conn.commit() |
|
1083 |
cur.execute('SELECT created_at, updated_at FROM wcs_meta WHERE key = %s', ('reindex_foo',)) |
|
1084 |
row = cur.fetchone() |
|
1085 |
assert row[0] is not None |
|
1086 |
assert row[1] is not None |
|
1087 |
old_created_at = row[0] |
|
1088 |
old_updated_at = row[0] |
|
1089 | ||
1090 |
sql.set_reindex('foo', 'bar', conn=conn, cur=cur) |
|
1091 |
conn.commit() |
|
1092 |
cur.execute('SELECT created_at, updated_at FROM wcs_meta WHERE key = %s', ('reindex_foo',)) |
|
1093 |
row = cur.fetchone() |
|
1094 |
assert row[0] == old_created_at |
|
1095 |
assert row[1] == old_updated_at |
|
1096 | ||
1097 |
sql.set_reindex('foo', 'bar-2', conn=conn, cur=cur) |
|
1098 |
conn.commit() |
|
1099 |
cur.execute('SELECT created_at, updated_at FROM wcs_meta WHERE key = %s', ('reindex_foo',)) |
|
1100 |
row = cur.fetchone() |
|
1101 |
assert row[0] == old_created_at |
|
1102 |
assert row[1] != old_updated_at |
|
1103 | ||
1104 |
# sql_level |
|
1105 |
cur.execute('SELECT created_at, updated_at FROM wcs_meta WHERE key = %s', ('sql_level',)) |
|
1106 |
row = cur.fetchone() |
|
1107 |
assert row[0] is not None |
|
1108 |
assert row[1] is not None |
|
1109 |
old_created_at = row[0] |
|
1110 |
old_updated_at = row[0] |
|
1111 | ||
1112 |
sql.migrate() |
|
1113 |
cur.execute('SELECT created_at, updated_at FROM wcs_meta WHERE key = %s', ('sql_level',)) |
|
1114 |
row = cur.fetchone() |
|
1115 |
assert row[0] == old_created_at |
|
1116 |
assert row[1] == old_updated_at |
|
1117 | ||
1118 |
cur.execute('''UPDATE wcs_meta SET value = %s WHERE key = %s''', (str(1), 'sql_level')) |
|
1119 |
conn.commit() |
|
1120 |
sql.migrate() |
|
1121 |
cur.execute('SELECT created_at, updated_at FROM wcs_meta WHERE key = %s', ('sql_level',)) |
|
1122 |
row = cur.fetchone() |
|
1123 |
assert row[0] == old_created_at |
|
1124 |
assert row[1] != old_updated_at |
|
1125 | ||
1126 | ||
1077 | 1127 |
def test_sql_level(pub): |
1078 | 1128 |
conn, cur = sql.get_connection_and_cursor() |
1079 | 1129 |
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 | ||
1496 | 1501 |
if insert_current_sql_level: |
1497 | 1502 |
sql_level = SQL_LEVEL[0] |
1498 | 1503 |
else: |
... | ... | |
1503 | 1508 |
('sql_level', str(sql_level)), |
1504 | 1509 |
) |
1505 | 1510 | |
1511 |
else: |
|
1512 |
cur.execute( |
|
1513 |
'''SELECT column_name FROM information_schema.columns |
|
1514 |
WHERE table_schema = 'public' |
|
1515 |
AND table_name = %s''', |
|
1516 |
('wcs_meta',), |
|
1517 |
) |
|
1518 |
existing_fields = {x[0] for x in cur.fetchall()} |
|
1519 |
if 'created_at' not in existing_fields: |
|
1520 |
cur.execute('''ALTER TABLE wcs_meta ADD COLUMN created_at timestamptz DEFAULT NOW()''') |
|
1521 |
if 'updated_at' not in existing_fields: |
|
1522 |
cur.execute('''ALTER TABLE wcs_meta ADD COLUMN updated_at timestamptz DEFAULT NOW()''') |
|
1523 | ||
1506 | 1524 |
if own_conn: |
1507 | 1525 |
conn.commit() |
1508 | 1526 |
cur.close() |
... | ... | |
4153 | 4171 |
(key_name, value), |
4154 | 4172 |
) |
4155 | 4173 |
else: |
4156 |
cur.execute('''UPDATE wcs_meta SET value = %s WHERE key = %s''', (value, key_name)) |
|
4174 |
if row[0] != value: |
|
4175 |
cur.execute( |
|
4176 |
'''UPDATE wcs_meta SET value = %s, updated_at = NOW() WHERE key = %s''', (value, key_name) |
|
4177 |
) |
|
4157 | 4178 |
if own_conn: |
4158 | 4179 |
conn.commit() |
4159 | 4180 |
cur.close() |
... | ... | |
4315 | 4336 |
for formdef in FormDef.select(): |
4316 | 4337 |
do_formdef_tables(formdef, rebuild_views=False, rebuild_global_views=False) |
4317 | 4338 | |
4318 |
cur.execute('''UPDATE wcs_meta SET value = %s WHERE key = %s''', (str(SQL_LEVEL[0]), 'sql_level')) |
|
4339 |
if sql_level != SQL_LEVEL[0]: |
|
4340 |
cur.execute( |
|
4341 |
'''UPDATE wcs_meta SET value = %s, updated_at=NOW() WHERE key = %s''', |
|
4342 |
(str(SQL_LEVEL[0]), 'sql_level'), |
|
4343 |
) |
|
4319 | 4344 | |
4320 | 4345 |
conn.commit() |
4321 | 4346 |
cur.close() |
4322 |
- |