def is_db_alive(): try: db_session.execute('SELECT NOW()').first() return True # pylint: disable=W0703 except Exception as e: logger.exception(e) return False
def add_favorite(cls, user, office): """ Add a favorite to a user. Avoid as much as possible replication errors by ignoring duplicates. """ statement = cls.__table__.insert().prefix_with("IGNORE").values( user_id=user.id, office_siret=office.siret, ) db_session.execute(statement) db_session.commit()
def cbs_delete_records(): try: print('> Deleting CBS records...') sql = text(""" delete from labonneboite.etablissements_third_party_update where reason='%s'; """ % (REASON_KEY)) db_session.execute(sql) db_session.commit() print('> Done') except Exception as err: print('> error executing request', err)
def cbs_insert_records(): try: print('> Inserting CBS records...', file) sql = text(""" LOAD DATA LOCAL INFILE '%s' into table etablissements_third_party_update FIELDS ENCLOSED BY '\"' TERMINATED BY ',' LINES TERMINATED BY '\\n' IGNORE 1 ROWS (@score,@siret) SET score_alternance=@score, sirets=@siret, reason='%s', date_created=NOW(); """ % (file, REASON_KEY)) db_session.execute(sql) db_session.commit() print('> Done') except Exception as err: print('> error executing request', err, '\n> Did you forget to set the env var `ENABLE_DB_INFILE=1`?')
def get_date_of_last_data_deploy(cls): """ Get date of when the 'etablissements' table was (re)created by the deploy_data process. Returns None if the information is not available. """ query = """ SELECT CREATE_TIME FROM information_schema.tables WHERE TABLE_SCHEMA = '%s' AND TABLE_NAME = '%s'; """ % (DATABASE['NAME'], settings.OFFICE_TABLE) last_data_deploy_date = db_session.execute(query).first() if last_data_deploy_date is None: return None last_data_deploy_date = last_data_deploy_date[0] # Formatting date in french format using locale.setlocale is strongly discouraged. # Using babel instead is the recommended way. # See https://stackoverflow.com/questions/985505/locale-date-formatting-in-python last_data_deploy_date_formated_as_french = format_date( last_data_deploy_date, locale='fr', format='long') return last_data_deploy_date_formated_as_french
def cbs_count_records(): sql = text(""" SELECT COUNT(*) FROM labonneboite.etablissements_third_party_update where reason='%s'; """ % (REASON_KEY)) try: res = db_session.execute(sql) print('> CBS records count: ', res.first()[0]) except Exception as err: print('> error executing request', err)
def is_db_alive(): try: db_session.execute('SELECT NOW()').first() return True except: return False