add update for existing items
This commit is contained in:
@@ -29,7 +29,7 @@ DATABASE_URL: str = f"postgresql://{DB_USER}:{DB_PASSWORD}@{DB_SERVER}:{DB_PORT}
|
||||
|
||||
|
||||
def cleanup_database(db: Session, log, dry_run: bool):
|
||||
log.info("cleanup_database")
|
||||
log.debug("cleanup_database")
|
||||
# get tables from registry
|
||||
for table in registry:
|
||||
log.info(f"{table}")
|
||||
@@ -41,7 +41,7 @@ def cleanup_database(db: Session, log, dry_run: bool):
|
||||
db.commit()
|
||||
|
||||
def load_data(filename: str, log) -> Dict[AnyStr, Dict[AnyStr, Any]]:
|
||||
log.info("load_data")
|
||||
log.debug("load_data")
|
||||
import_file = Path(filename)
|
||||
if not import_file.exists():
|
||||
log.info(f"File {filename} does not exist. Do nothing.")
|
||||
@@ -57,6 +57,16 @@ def get_ids(items: List[Any]) -> List[AnyStr]:
|
||||
result.append(item.id)
|
||||
return result
|
||||
|
||||
def update_item(db: Session, import_data: Dict[AnyStr, Any], item: Any, dry_run: bool, log):
|
||||
for (key, value) in import_data.items():
|
||||
existing_value = getattr(item, str(key))
|
||||
if existing_value != value:
|
||||
if not dry_run:
|
||||
log.info(f"update {key}({existing_value}) with {value}")
|
||||
setattr(model, str(key), value)
|
||||
db.add(item)
|
||||
db.commit()
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
logger = get_logger(args.verbose, "kontor")
|
||||
@@ -76,9 +86,12 @@ if __name__ == '__main__':
|
||||
import_items = data[tablename]
|
||||
for import_item in import_items:
|
||||
if import_item['id'] in existing_ids:
|
||||
logger.info(f"update {import_item['id']}")
|
||||
logger.debug(f"update {import_item['id']}")
|
||||
existing_item = db.get(model, import_item['id'])
|
||||
update_item(db, import_item, existing_item, args.dry_run, logger)
|
||||
existing_ids.remove(import_item['id'])
|
||||
else:
|
||||
logger.info(f"import {import_item['id']}")
|
||||
|
||||
logger.debug(f"import {import_item['id']}")
|
||||
logger.info(f"remaining items for {tablename}: {len(existing_ids)}")
|
||||
logger.info('kontor.import finished')
|
||||
|
||||
|
||||
Reference in New Issue
Block a user