From e321f911712d8bd96838ed28b120262b34c94c09 Mon Sep 17 00:00:00 2001 From: kelle Date: Wed, 24 Jul 2024 16:24:44 -0400 Subject: [PATCH] check only one adopted in ingest script --- simple/utils/spectral_types.py | 56 +++++++++++++++++++++++++--------- 1 file changed, 42 insertions(+), 14 deletions(-) diff --git a/simple/utils/spectral_types.py b/simple/utils/spectral_types.py index 88c40d7af..7bdf83131 100644 --- a/simple/utils/spectral_types.py +++ b/simple/utils/spectral_types.py @@ -176,20 +176,6 @@ def ingest_spectral_type( .values(adopted=False) ) conn.commit() - # check that adopted flag is successfully changed - old_adopted_data = ( - db.query(db.SpectralTypes) - .filter( - and_( - db.SpectralTypes.c.source == old_adopted["source"][0], - db.SpectralTypes.c.regime == old_adopted["regime"][0], - db.SpectralTypes.c.reference == old_adopted["reference"][0], - ) - ) - .table() - ) - logger.debug("Old adopted measurement unset") - logger.debug(f"Old adopted data:\n {old_adopted_data}") except sqlalchemy.exc.IntegrityError as e: if ( db.query(db.Publications) @@ -213,6 +199,48 @@ def ingest_spectral_type( else: logger.warning(msg) + # check that adopted flag is successfully changed + if old_adopted is not None: + old_adopted_data = ( + db.query(db.SpectralTypes) + .filter( + and_( + db.SpectralTypes.c.source == old_adopted["source"][0], + db.SpectralTypes.c.regime == old_adopted["regime"][0], + db.SpectralTypes.c.reference == old_adopted["reference"][0], + ) + ) + .table() + ) + logger.debug("Old adopted measurement unset") + logger.debug(f"Old adopted data:\n {old_adopted_data}") + + # check that there is only one adopted measurement + results = ( + db.query(db.SpectralTypes) + .filter( + and_(db.SpectralTypes.c.source == db_name, db.SpectralTypes.c.adopted == 1) + ) + .table() + ) + logger.debug(f"Adopted measurements for {db_name}: {results}") + if len(results) > 1: + msg = f"Multiple adopted measurements for {db_name}" + if raise_error: + logger.error(msg) + raise AstroDBError(msg) + else: + logger.warning(msg) + elif len(results) == 0: + msg = f"No adopted measurements for {db_name}" + if raise_error: + logger.error(msg) + raise AstroDBError(msg) + else: + logger.warning(msg) + else: + logger.debug(f"Adopted measurement for {db_name}: {results}") + def convert_spt_string_to_code(spectral_type_string): """