extend check_kontor.py for comparing json file with db

This commit is contained in:
Thomas Peetz
2025-04-21 00:55:50 +02:00
parent 2bdbef8f8a
commit bb049441b4
2 changed files with 42 additions and 5 deletions
+40 -3
View File
@@ -2,6 +2,7 @@
Checks the database kontor
"""
from enum import Enum, auto
import json
import mariadb
import requests
from pathlib import Path
@@ -12,7 +13,8 @@ from config import get_logger, get_database_cursors
parser = ArgumentParser(formatter_class=ArgumentDefaultsHelpFormatter)
parser.add_argument('--verbose', '-v', action='count', default=0)
parser.add_argument('--config', '-c', default='kontor')
parser.add_argument('--dir', '-d', default='/data/media')
parser.add_argument('--file', '-f')
parser.add_argument('--dir', '-d')
parser.add_argument('--dry-run', '-m', action='store_true')
parser.add_argument('--reset-cloud-link', '-r', action='store_true')
args = parser.parse_args()
@@ -128,13 +130,48 @@ def reset_cloud_link(conn, dry_run, log):
cursor.execute('UPDATE media_file SET cloud_link="" WHERE id is NOT NULL')
conn.commit()
def check_file_with_db(data_file: Path, m_conn, log):
log.info(f"read json file: {data_file}")
cursor = m_conn.cursor()
with open(data_file, 'r') as json_file:
json_load = json.load(json_file)
for table in json_load:
log.info(f"{table}: {len(json_load[table])}")
items = json_load[table]
for item in items:
item_id = item['id']
select_statement = f"SELECT * FROM {table} WHERE id='{item_id}'"
cursor.execute(select_statement)
rows = cursor.fetchall()
count = len(rows)
log.info(f"{count} entries found for {item_id}")
if count == 0:
log.info(f"entry for {item_id} not found")
if count == 1:
log.info(f"check entry {item_id}")
#log.info(f"entry {rows[0]}")
columns = []
values = []
for (key, value) in item.items():
columns.append(key)
values.append(value)
for index, _ in enumerate(columns):
log.info(f"compare {values[index]} with {rows[0][index]}")
if __name__ == '__main__':
log = get_logger(args.verbose, args.config)
log.info("kontor.check_kontor started")
_, m_conn = get_database_cursors(log, args.config)
log.info("kontor.check_kontor.rename_files_to_id")
rename_files_to_id(args.dir, args.dry_run, m_conn, log)
if args.dir:
log.info("kontor.check_kontor.rename_files_to_id")
rename_files_to_id(args.dir, args.dry_run, m_conn, log)
if args.file:
data_file = Path(args.file)
if data_file.exists():
log.info("kontor.check_kontor.check_file_with_db")
check_file_with_db(data_file, m_conn, log)
#logger.info("kontor.check_kontor.update_cloud_link_with_found_files")
#update_cloud_link_with_found_files(data_dir, mariadb_conn, args.dry_run)
#logger.info("kontor.check_kontor.get_ids_from_column_cloud_link")
+2 -2
View File
@@ -4,7 +4,7 @@ copy data from SQLite to MariaDB
from argparse import ArgumentParser, ArgumentDefaultsHelpFormatter
from pathlib import Path
from typing import Dict
from config import get_logger, get_database_cursors, get_meta_data, get_scripts
from config import get_logger, get_database_cursors
import mariadb
import json
@@ -40,7 +40,7 @@ def copy_data(mariadb_conn, data_file: Path, log):
columns.append(key)
values.append(value)
row = tuple(values)
#log.info(f"values: {row}")
log.info(f"values: {row}")
insert_statement = 'INSERT INTO {}({}) VALUES({})'.format(table, ', '.join(columns), ', '.join(['?']*len(columns)))
#log.info(f"statement: {insert_statement}")
mariadb_cursor.execute(insert_statement, row)