<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def upsert_module_file(module_ident, fileid, filename):
 """Upsert a file associated with ``fileid`` with ``filename`` as a module_files entry associated with content at ``module_ident``. """

    with db_connect() as db_conn:
        with db_conn.cursor() as cursor:
            cursor.execute("SELECT true FROM module_files "
                           "WHERE module_ident = %s "
                           "AND filename = %s",
                           (module_ident, filename,))
            try:
                cursor.fetchone()[0]
            except (IndexError, TypeError):
                cursor.execute("INSERT INTO module_files "
                               "(module_ident, fileid, filename) "
                               "VALUES (%s, %s, %s)",
                               (module_ident, fileid, filename,))
            else:
                cursor.execute("UPDATE module_files "
                               "SET (fileid) = (%s) "
                               "WHERE module_ident = %s AND filename = %s",
                               (fileid, module_ident, filename,))