def upsert_prelude_project(self,path=None,filename=None,uuid=None,version=None,nclips=None):
cursor=self.conn.cursor()
self.conn.commit()
#if uuid is None:
# raise DataError("You need to pass a valid uuid")
try:
sqlcmd = """insert into prelude_projects (filepath,filename,uuid,version,clips,lastseen)
values (%s,%s,%s,%s,%s,now()) returning id"""
cursor.execute(sqlcmd,(path,filename,uuid,version,nclips))
except psycopg2.IntegrityError as e: #if we violate unique keys, try to update on filename
self.conn.rollback()
try:
sqlcmd = """update prelude_projects set filepath=%s, filename=%s, uuid=%s, version=%s, clips=%s, lastseen=now()
where filepath=%s and filename=%s returning id"""
cursor.execute(sqlcmd,(path,filename,uuid,version,nclips,path,filename))
except psycopg2.IntegrityError as e: #if that causes a violation, try to update on uuid
self.conn.rollback()
sqlcmd = """update prelude_projects set filepath=%s, filename=%s, uuid=%s, version=%s, clips=%s, lastseen=now()
where uuid=%s returning id"""
cursor.execute(sqlcmd,(path,filename,uuid,version,nclips,uuid))
self.conn.commit()
result=cursor.fetchone()
return result[0] #return id of inserted row
评论列表
文章目录