Changeset View
Changeset View
Standalone View
Standalone View
swh/storage/storage.py
Show First 20 Lines • Show All 1,143 Lines • ▼ Show 20 Lines | def snapshot_get_branches(self, snapshot_id, branches_from=b'', | ||||
'id': snapshot_id, | 'id': snapshot_id, | ||||
'branches': branches, | 'branches': branches, | ||||
'next_branch': next_branch, | 'next_branch': next_branch, | ||||
} | } | ||||
return None | return None | ||||
@db_transaction() | @db_transaction() | ||||
def origin_visit_add(self, origin, date=None, db=None, cur=None, *, | def origin_visit_add(self, origin, date=None, type=None, | ||||
ts=None): | db=None, cur=None, *, ts=None): | ||||
"""Add an origin_visit for the origin at ts with status 'ongoing'. | """Add an origin_visit for the origin at ts with status 'ongoing'. | ||||
For backward compatibility, `type` is optional and defaults to | |||||
the origin's type. | |||||
Args: | Args: | ||||
origin: Visited Origin id | origin: Visited Origin id | ||||
date: timestamp of such visit | date: timestamp of such visit | ||||
type (str): the type of loader used for the visit (hg, git, ...) | |||||
Returns: | Returns: | ||||
dict: dictionary with keys origin and visit where: | dict: dictionary with keys origin and visit where: | ||||
- origin: origin identifier | - origin: origin identifier | ||||
- visit: the visit identifier for the new visit occurrence | - visit: the visit identifier for the new visit occurrence | ||||
""" | """ | ||||
if ts is None: | if ts is None: | ||||
if date is None: | if date is None: | ||||
raise TypeError('origin_visit_add expected 2 arguments.') | raise TypeError('origin_visit_add expected 2 arguments.') | ||||
else: | else: | ||||
assert date is None | assert date is None | ||||
warnings.warn("argument 'ts' of origin_visit_add was renamed " | warnings.warn("argument 'ts' of origin_visit_add was renamed " | ||||
"to 'date' in v0.0.109.", | "to 'date' in v0.0.109.", | ||||
DeprecationWarning) | DeprecationWarning) | ||||
date = ts | date = ts | ||||
origin_id = origin # TODO: rename the argument | origin_id = origin # TODO: rename the argument | ||||
if isinstance(date, str): | if isinstance(date, str): | ||||
date = dateutil.parser.parse(date) | date = dateutil.parser.parse(date) | ||||
visit_id = db.origin_visit_add(origin_id, date, cur) | if type is None: | ||||
ardumont: I missed db=db, cur=cur here as well. | |||||
origin = self.origin_get({'id': origin}, db=db, cur=cur) | |||||
type = origin['type'] | |||||
visit_id = db.origin_visit_add(origin_id, date, type, cur) | |||||
if self.journal_writer: | if self.journal_writer: | ||||
# We can write to the journal only after inserting to the | # We can write to the journal only after inserting to the | ||||
# DB, because we want the id of the visit | # DB, because we want the id of the visit | ||||
origin = self.origin_get([{'id': origin_id}], db=db, cur=cur)[0] | origin = self.origin_get([{'id': origin_id}], db=db, cur=cur)[0] | ||||
del origin['id'] | del origin['id'] | ||||
self.journal_writer.write_addition('origin_visit', { | self.journal_writer.write_addition('origin_visit', { | ||||
'origin': origin, 'date': date, 'visit': visit_id, | 'origin': origin, 'date': date, 'type': type, | ||||
'visit': visit_id, | |||||
'status': 'ongoing', 'metadata': None, 'snapshot': None}) | 'status': 'ongoing', 'metadata': None, 'snapshot': None}) | ||||
return { | return { | ||||
'origin': origin_id, | 'origin': origin_id, | ||||
'visit': visit_id, | 'visit': visit_id, | ||||
} | } | ||||
@db_transaction() | @db_transaction() | ||||
▲ Show 20 Lines • Show All 61 Lines • ▼ Show 20 Lines | def origin_visit_upsert(self, visits, db=None, cur=None): | ||||
visits = copy.deepcopy(visits) | visits = copy.deepcopy(visits) | ||||
for visit in visits: | for visit in visits: | ||||
if isinstance(visit['date'], str): | if isinstance(visit['date'], str): | ||||
visit['date'] = dateutil.parser.parse(visit['date']) | visit['date'] = dateutil.parser.parse(visit['date']) | ||||
if self.journal_writer: | if self.journal_writer: | ||||
for visit in visits: | for visit in visits: | ||||
visit = visit.copy() | visit = visit.copy() | ||||
visit['origin'] = self.origin_get( | origin = self.origin_get( | ||||
[{'id': visit['origin']}], db=db, cur=cur)[0] | [{'id': visit['origin']}], db=db, cur=cur)[0] | ||||
visit['origin'] = origin | |||||
if visit.get('type') is None: | |||||
visit['type'] = origin['type'] | |||||
Done Inline Actionswhy did you drop the db and cur parameter? My understanding was to stay within the same transaction, we need to pass those along. ardumont: why did you drop the db and cur parameter?
My understanding was to stay within the same… | |||||
Done Inline Actionsmy mistake vlorentz: my mistake | |||||
del visit['origin']['id'] | del visit['origin']['id'] | ||||
self.journal_writer.write_addition('origin_visit', visit) | self.journal_writer.write_addition('origin_visit', visit) | ||||
for visit in visits: | for visit in visits: | ||||
# TODO: upsert them all in a single query | # TODO: upsert them all in a single query | ||||
db.origin_visit_upsert(**visit, cur=cur) | db.origin_visit_upsert(**visit, cur=cur) | ||||
@db_transaction_generator(statement_timeout=500) | @db_transaction_generator(statement_timeout=500) | ||||
▲ Show 20 Lines • Show All 526 Lines • Show Last 20 Lines |
I missed db=db, cur=cur here as well.