def init(): try: # Try to create session to check if DB is awake db_session.execute("SELECT 1") except Exception as e: logger.error(e) raise e
def remove_filing(db_session, filing_id): res = db_session.execute( select(FilingSubType).filter( FilingSubtype.filing_id == filing_id)).unique().scalars().all() for subtype in res: db_session.delete(res) res = db_session.execute( select(FilingRaw)\ .filter(FilingRaw.filing_id == filing_id) ).unique().scalars().all() for raw in res: db_session.delete(res) res = db_session.execute( select(Filing)\ .filter(Filing.filing_id == filing_id) ).unique().scalars().all() for filing in res: db_session.delete(res) db_session.commit()
def init(): try: # Try to create session to check if DB is awake db_session.execute("SELECT 1") # Wait for API to be awake, run one simple tests to authenticate except Exception as e: logger.error(e) raise e
def db_healthcheck() -> Union[bool, Optional[Exception]]: try: db_session.execute("SELECT 1") logger.success("DB Connection Successful") return True, None except Exception as e: logger.error(e) return False, e
def _check_db(log_fn: tp.Callable) -> None: """See if the database is alive.""" try: log_fn('Executing SELECT', depth=1) db_session.execute("SELECT 1") except Exception as ex: log_fn(ex, level=logging.WARN) raise ex return True
def init(): try: # Try to create session to check if DB is awake logger.debug(db_session.__dict__) db_session.execute("SELECT 1") except Exception as e: logger.error(e) raise e finally: db_session.close()
def clear_tables(): """ This fixture will be executed after every test, clearing the given table. You can remove the autouse, and specify when it executed whenever you want. """ yield # db_session.autocommit = True db_session.execute("TRUNCATE TABLE item CASCADE") db_session.commit()
def init(): try: # Attempt to create a DB session, # this will serve to check if DB is awake os.environ["TESTING"] = True from app.db.session import db_session db_session.execute("SELECT 1") except Exception as e: logger.error(e) raise e
def sync_members(): try: print('Starting to sync members from CCB into members table...') req = { 'username': os.getenv("CCB_USER"), 'password': os.getenv("CCB_PASS"), 'keep_me_logged_in': True } session = requests.Session() login_res = session.post('https://thirdave.ccbchurch.com/api/login', json=req) if login_res.status_code == 200: pages_res = session.get( 'https://thirdave.ccbchurch.com/api/individuals?page=1&per_page=100' ) if pages_res.status_code == 200: pages = pages_res.headers['X-Total-Pages'] db_session.execute('''TRUNCATE TABLE members''') db_session.commit() for page in range(1, int(pages) + 1): res = session.get( 'https://thirdave.ccbchurch.com/api/individuals?page=%s&per_page=100' % page) members = json.loads(res.text) objects = [] for member in members: if member['family_position'] != 'CHILD' and member[ 'last_name'] != '.Org' and member[ 'last_name'] != 'Fellowship': objects.append( Members( ccbid=member.get("id"), name=member.get("name"), email=member.get("email"), phone=member.get("phone").get("mobile"), member_since=member.get("created"), image_url=get_image(member.get("images")))) else: continue else: print('Bulk saving CCB members to members table') db_session.bulk_save_objects(objects) db_session.commit() continue db_session.close() else: db_session.close() return 200 else: print('Failed retrieving members from CCB') return 400 else: print('Failed authenticating with CCB while syncing members') return 404 except Exception as e: db_session.close() print(e) return 500 finally: db_session.close()