|
28 | 28 | """ |
29 | 29 |
|
30 | 30 | __author__ = 'Linuxfabrik GmbH, Zurich/Switzerland' |
31 | | -__version__ = '2026060201' |
| 31 | +__version__ = '2026061701' |
32 | 32 |
|
33 | 33 | import csv |
34 | 34 | import hashlib |
@@ -355,7 +355,8 @@ def create_index( |
355 | 355 | If `True`, creates a unique index. |
356 | 356 | If `False`, creates a standard (non-unique) index. Defaults to `False`. |
357 | 357 | - **delete_db_on_operational_error** (`bool`, optional): |
358 | | - If `True`, deletes the database file when an `OperationalError` occurs. |
| 358 | + If `True`, deletes the database file when the on-disk database turns out |
| 359 | + to be unusable (e.g. a schema mismatch between releases). |
359 | 360 | Defaults to `True`. |
360 | 361 |
|
361 | 362 | ### Returns |
@@ -467,7 +468,8 @@ def cut(conn, table='perfdata', _max=5, delete_db_on_operational_error=True): |
467 | 468 | - **_max** (`int`, optional): |
468 | 469 | Number of most recent records to keep. Defaults to `5`. |
469 | 470 | - **delete_db_on_operational_error** (`bool`, optional): |
470 | | - If `True`, deletes the database file when an `OperationalError` occurs. |
| 471 | + If `True`, deletes the database file when the on-disk database turns out |
| 472 | + to be unusable (e.g. a schema mismatch between releases). |
471 | 473 | Defaults to `True`. |
472 | 474 |
|
473 | 475 | ### Returns |
@@ -530,7 +532,8 @@ def delete(conn, sql, data=None, delete_db_on_operational_error=True): |
530 | 532 | Dictionary of parameters to bind to the SQL statement. |
531 | 533 | Defaults to an empty dict (no parameters). |
532 | 534 | - **delete_db_on_operational_error** (`bool`, optional): |
533 | | - If `True`, deletes the database file when an `OperationalError` occurs. |
| 535 | + If `True`, deletes the database file when the on-disk database turns out |
| 536 | + to be unusable (e.g. a schema mismatch between releases). |
534 | 537 | Defaults to `True`. |
535 | 538 |
|
536 | 539 | ### Returns |
@@ -563,6 +566,14 @@ def delete(conn, sql, data=None, delete_db_on_operational_error=True): |
563 | 566 | if delete_db_on_operational_error: |
564 | 567 | rm_db(conn) |
565 | 568 | return False, f'Operational Error: {e}, Query: {sql}, Data: {data}' |
| 569 | + except (sqlite3.IntegrityError, sqlite3.DataError) as e: |
| 570 | + # A constraint or data error (e.g. a NOT NULL column that a newer or |
| 571 | + # older release no longer writes) means the on-disk schema no |
| 572 | + # longer matches the data being written. Deleting the database lets the |
| 573 | + # next run rebuild a valid cache from scratch. |
| 574 | + if delete_db_on_operational_error: |
| 575 | + rm_db(conn) |
| 576 | + return False, f'Integrity Error: {e}, Query: {sql}, Data: {data}' |
566 | 577 | except Exception as e: |
567 | 578 | return False, f'Query failed: {sql}, Error: {e}, Data: {data}' |
568 | 579 |
|
@@ -906,7 +917,8 @@ def insert(conn, data, table='perfdata', delete_db_on_operational_error=True): |
906 | 917 | Name of the table to insert into. |
907 | 918 | Defaults to `'perfdata'`. |
908 | 919 | - **delete_db_on_operational_error** (`bool`, optional): |
909 | | - If `True`, deletes the database file when an `OperationalError` occurs. |
| 920 | + If `True`, deletes the database file when the on-disk database turns out |
| 921 | + to be unusable (e.g. a schema mismatch between releases). |
910 | 922 | Defaults to `True`. |
911 | 923 |
|
912 | 924 | ### Returns |
@@ -946,6 +958,14 @@ def insert(conn, data, table='perfdata', delete_db_on_operational_error=True): |
946 | 958 | if delete_db_on_operational_error: |
947 | 959 | rm_db(conn) |
948 | 960 | return False, f'Operational Error: {e}, Query: {sql}, Data: {data}' |
| 961 | + except (sqlite3.IntegrityError, sqlite3.DataError) as e: |
| 962 | + # A constraint or data error (e.g. a NOT NULL column that a newer or |
| 963 | + # older release no longer writes) means the on-disk schema no |
| 964 | + # longer matches the data being written. Deleting the database lets the |
| 965 | + # next run rebuild a valid cache from scratch. |
| 966 | + if delete_db_on_operational_error: |
| 967 | + rm_db(conn) |
| 968 | + return False, f'Integrity Error: {e}, Query: {sql}, Data: {data}' |
949 | 969 | except Exception as e: |
950 | 970 | return False, f'Query failed: {sql}, Error: {e}, Data: {data}' |
951 | 971 |
|
@@ -1030,7 +1050,7 @@ def per_second_deltas(filename, name, counters): |
1030 | 1050 | # subsequent drop_table() with "Cannot operate on a closed database". |
1031 | 1051 | ok, _ = insert(conn, row, delete_db_on_operational_error=False) |
1032 | 1052 | if not ok: |
1033 | | - # Schema mismatch from a previous plugin version (different counter |
| 1053 | + # Schema mismatch from a previous release (different counter |
1034 | 1054 | # columns or NOT NULL constraints). Rebuild the table from the |
1035 | 1055 | # current schema; we lose the previous baseline but auto-recover on |
1036 | 1056 | # the next run. |
@@ -1130,7 +1150,8 @@ def replace(conn, data, table='perfdata', delete_db_on_operational_error=True): |
1130 | 1150 | Name of the table to operate on. |
1131 | 1151 | Defaults to `'perfdata'`. |
1132 | 1152 | - **delete_db_on_operational_error** (`bool`, optional): |
1133 | | - If `True`, deletes the database file when an `OperationalError` occurs. |
| 1153 | + If `True`, deletes the database file when the on-disk database turns out |
| 1154 | + to be unusable (e.g. a schema mismatch between releases). |
1134 | 1155 | Defaults to `True`. |
1135 | 1156 |
|
1136 | 1157 | ### Returns |
@@ -1169,6 +1190,14 @@ def replace(conn, data, table='perfdata', delete_db_on_operational_error=True): |
1169 | 1190 | if delete_db_on_operational_error: |
1170 | 1191 | rm_db(conn) |
1171 | 1192 | return False, f'Operational Error: {e}, Query: {sql}, Data: {data}' |
| 1193 | + except (sqlite3.IntegrityError, sqlite3.DataError) as e: |
| 1194 | + # A constraint or data error (e.g. a NOT NULL column that a newer or |
| 1195 | + # older release no longer writes) means the on-disk schema no |
| 1196 | + # longer matches the data being written. Deleting the database lets the |
| 1197 | + # next run rebuild a valid cache from scratch. |
| 1198 | + if delete_db_on_operational_error: |
| 1199 | + rm_db(conn) |
| 1200 | + return False, f'Integrity Error: {e}, Query: {sql}, Data: {data}' |
1172 | 1201 | except Exception as e: |
1173 | 1202 | return False, f'Query failed: {sql}, Error: {e}, Data: {data}' |
1174 | 1203 |
|
@@ -1237,7 +1266,8 @@ def select( |
1237 | 1266 | If `True`, return results as a list of dictionaries. |
1238 | 1267 | If `False`, return raw SQLite row objects. Defaults to `True`. |
1239 | 1268 | - **delete_db_on_operational_error** (`bool`, optional): |
1240 | | - If `True`, deletes the database file when an `OperationalError` occurs. |
| 1269 | + If `True`, deletes the database file when the on-disk database turns out |
| 1270 | + to be unusable (e.g. a schema mismatch between releases). |
1241 | 1271 | Defaults to `True`. |
1242 | 1272 |
|
1243 | 1273 | ### Returns |
|
0 commit comments