Commit d24a7770 by Don Mitchell

Remove package_id and remove lower_ stuff from migrator

STUD-1603
parent a69e42dd
...@@ -100,9 +100,7 @@ class LocMapperStore(object): ...@@ -100,9 +100,7 @@ class LocMapperStore(object):
self.location_map.insert({ self.location_map.insert({
'_id': course_son, '_id': course_son,
'org': org, 'org': org,
'lower_org': org.lower(),
'offering': offering, 'offering': offering,
'lower_offering': offering.lower(),
'draft_branch': draft_branch, 'draft_branch': draft_branch,
'prod_branch': prod_branch, 'prod_branch': prod_branch,
'block_map': block_map or {}, 'block_map': block_map or {},
...@@ -197,7 +195,7 @@ class LocMapperStore(object): ...@@ -197,7 +195,7 @@ class LocMapperStore(object):
self._cache_location_map_entry(location, published_usage, draft_usage) self._cache_location_map_entry(location, published_usage, draft_usage)
return result return result
def translate_locator_to_location(self, locator, get_course=False, lower_only=False): def translate_locator_to_location(self, locator, get_course=False):
""" """
Returns an old style Location for the given Locator if there's an appropriate entry in the Returns an old style Location for the given Locator if there's an appropriate entry in the
mapping collection. Note, it requires that the course was previously mapped (a side effect of mapping collection. Note, it requires that the course was previously mapped (a side effect of
...@@ -211,53 +209,31 @@ class LocMapperStore(object): ...@@ -211,53 +209,31 @@ class LocMapperStore(object):
locator: a BlockUsageLocator to translate locator: a BlockUsageLocator to translate
get_course: rather than finding the map for this locator, returns the CourseKey get_course: rather than finding the map for this locator, returns the CourseKey
for the mapped course. for the mapped course.
lower_only: (obsolete?) the locator's fields are lowercased and not the actual case
for the identifier (e.g., came from a sql db which lowercases all ids). Find the actual
case Location for the desired object
""" """
if get_course: if get_course:
cached_value = self._get_course_location_from_cache( cached_value = self._get_course_location_from_cache(
# if locator is already a course_key it won't have course_key attr # if locator is already a course_key it won't have course_key attr
getattr(locator, 'course_key', locator), getattr(locator, 'course_key', locator)
lower_only
) )
else: else:
cached_value = self._get_location_from_cache(locator) cached_value = self._get_location_from_cache(locator)
if cached_value: if cached_value:
return cached_value return cached_value
# This does not require that the course exist in any modulestore # migrate any records which don't have the org and offering fields as
# only that it has a mapping entry. # this won't be able to find what it wants. (only needs to be run once ever per db,
if lower_only: # I'm not sure how to control that, but I'm putting some check here for once per launch)
# migrate any records which don't have the lower_org and lower_offering fields as if not getattr(self, 'offering_migrated', False):
# this won't be able to find what it wants. (only needs to be run once ever per db, obsolete = self.location_map.find(
# I'm not sure how to control that, but I'm putting some check here for once per launch) {'org': {"$exists": False}, "offering": {"$exists": False}, }
if not getattr(self, 'lower_offering_migrated', False): )
obsolete = self.location_map.find( self._migrate_if_necessary(obsolete)
{'lower_org': {"$exists": False}, "lower_offering": {"$exists": False}, } setattr(self, 'offering_migrated', True)
)
self._migrate_if_necessary(obsolete)
setattr(self, 'lower_offering_migrated', True)
entry = self.location_map.find_one(bson.son.SON([
('lower_org', locator.org.lower()),
('lower_offering', locator.offering.lower()),
]))
else:
# migrate any records which don't have the lower_org and lower_offering fields as
# this won't be able to find what it wants. (only needs to be run once ever per db,
# I'm not sure how to control that, but I'm putting some check here for once per launch)
if not getattr(self, 'offering_migrated', False):
obsolete = self.location_map.find(
{'org': {"$exists": False}, "offering": {"$exists": False}, }
)
self._migrate_if_necessary(obsolete)
setattr(self, 'offering_migrated', True)
entry = self.location_map.find_one(bson.son.SON([ entry = self.location_map.find_one(bson.son.SON([
('org', locator.org), ('org', locator.org),
('lower_offering', locator.offering), ('offering', locator.offering),
])) ]))
# look for one which maps to this block block_id # look for one which maps to this block block_id
if entry is None: if entry is None:
...@@ -277,12 +253,8 @@ class LocMapperStore(object): ...@@ -277,12 +253,8 @@ class LocMapperStore(object):
self.decode_key_from_mongo(old_name) self.decode_key_from_mongo(old_name)
) )
if lower_only: entry_org = "org"
entry_org = "lower_org" entry_offering = "offering"
entry_offering = "lower_offering"
else:
entry_org = "org"
entry_offering = "offering"
published_locator = BlockUsageLocator( published_locator = BlockUsageLocator(
CourseLocator( CourseLocator(
...@@ -454,18 +426,20 @@ class LocMapperStore(object): ...@@ -454,18 +426,20 @@ class LocMapperStore(object):
""" """
return self.cache.get(unicode(locator)) return self.cache.get(unicode(locator))
def _get_course_location_from_cache(self, locator_package_id, lower_only=False): def _get_course_location_from_cache(self, course_key):
""" """
See if the package_id is in the cache. If so, return the mapped location to the See if the course_key is in the cache. If so, return the mapped location to the
course root. course root.
""" """
if lower_only: cache_key = self._course_key_cache_string(course_key)
cache_key = u'courseIdLower+{}'.format(locator_package_id.lower())
else:
cache_key = u'courseId+{}'.format(locator_package_id)
return self.cache.get(cache_key) return self.cache.get(cache_key)
def _course_key_cache_string(self, course_key):
"""
Return the string used to cache the course key
"""
return u'{0.org}+{0.offering}'.format(course_key)
def _cache_course_locator(self, old_course_id, published_course_locator, draft_course_locator): def _cache_course_locator(self, old_course_id, published_course_locator, draft_course_locator):
""" """
For quick lookup of courses For quick lookup of courses
...@@ -482,8 +456,7 @@ class LocMapperStore(object): ...@@ -482,8 +456,7 @@ class LocMapperStore(object):
""" """
setmany = {} setmany = {}
if location.category == 'course': if location.category == 'course':
setmany[u'courseId+{}'.format(published_usage.package_id)] = location setmany[self._course_key_cache_string(published_usage)] = location.course_key
setmany[u'courseIdLower+{}'.format(published_usage.package_id.lower())] = location
setmany[unicode(published_usage)] = location setmany[unicode(published_usage)] = location
setmany[unicode(draft_usage)] = location setmany[unicode(draft_usage)] = location
setmany[unicode(location)] = (published_usage, draft_usage) setmany[unicode(location)] = (published_usage, draft_usage)
...@@ -504,8 +477,7 @@ class LocMapperStore(object): ...@@ -504,8 +477,7 @@ class LocMapperStore(object):
delete_keys = [] delete_keys = []
published_locator = unicode(cached_key[0].course_key) published_locator = unicode(cached_key[0].course_key)
course_location = self._course_location_from_cache(published_locator) course_location = self._course_location_from_cache(published_locator)
delete_keys.append(u'courseId+{}'.format(published_locator)) delete_keys.append(self._course_key_cache_string(course_key))
delete_keys.append(u'courseIdLower+{}'.format(unicode(cached_key[0].course_key).lower()))
delete_keys.append(published_locator) delete_keys.append(published_locator)
delete_keys.append(unicode(cached_key[1].course_key)) delete_keys.append(unicode(cached_key[1].course_key))
delete_keys.append(unicode(course_location)) delete_keys.append(unicode(course_location))
...@@ -535,8 +507,7 @@ class LocMapperStore(object): ...@@ -535,8 +507,7 @@ class LocMapperStore(object):
""" """
delete_keys = [] delete_keys = []
if location.category == 'course': if location.category == 'course':
delete_keys.append(u'courseId+{}'.format(published_usage.package_id)) delete_keys.append(self._course_key_cache_string(published_usage.course_key))
delete_keys.append(u'courseIdLower+{}'.format(published_usage.package_id.lower()))
delete_keys.append(unicode(published_usage)) delete_keys.append(unicode(published_usage))
delete_keys.append(unicode(draft_usage)) delete_keys.append(unicode(draft_usage))
...@@ -560,7 +531,7 @@ class LocMapperStore(object): ...@@ -560,7 +531,7 @@ class LocMapperStore(object):
""" """
If entry had an '_id' without a run, remove the whole record. If entry had an '_id' without a run, remove the whole record.
Add fields: schema, org, offering, lower_org, and lower_offering Add fields: schema, org, offering
Remove: course_id, lower_course_id Remove: course_id, lower_course_id
:param entry: :param entry:
""" """
...@@ -579,9 +550,7 @@ class LocMapperStore(object): ...@@ -579,9 +550,7 @@ class LocMapperStore(object):
entry.pop('lower_course_id', None) entry.pop('lower_course_id', None)
old_course_id = SlashSeparatedCourseKey(entry['_id']['org'], entry['_id']['course'], entry['_id']['name']) old_course_id = SlashSeparatedCourseKey(entry['_id']['org'], entry['_id']['course'], entry['_id']['name'])
entry['org'] = old_course_id.org entry['org'] = old_course_id.org
entry['lower_org'] = old_course_id.org.lower()
entry['offering'] = old_course_id.offering.replace('/', '+') entry['offering'] = old_course_id.offering.replace('/', '+')
entry['lower_offering'] = entry['offering'].lower()
return self._migrate_1(entry, True) return self._migrate_1(entry, True)
# insert new migrations just before _migrate_top. _migrate_top sets the schema version and # insert new migrations just before _migrate_top. _migrate_top sets the schema version and
......
...@@ -111,13 +111,6 @@ class BlockLocatorBase(Locator): ...@@ -111,13 +111,6 @@ class BlockLocatorBase(Locator):
raise InvalidKeyError(cls, string) raise InvalidKeyError(cls, string)
return match.groupdict() return match.groupdict()
@property
def package_id(self):
if self.org and self.offering:
return u'{}{}{}'.format(self.org, self.ORG_SEPARATOR, self.offering)
else:
return None
class CourseLocator(BlockLocatorBase, CourseKey): class CourseLocator(BlockLocatorBase, CourseKey):
""" """
...@@ -267,7 +260,7 @@ class CourseLocator(BlockLocatorBase, CourseKey): ...@@ -267,7 +260,7 @@ class CourseLocator(BlockLocatorBase, CourseKey):
""" """
parts = [] parts = []
if self.offering: if self.offering:
parts.append(unicode(self.package_id)) parts.extend([self.org, self.offering])
if self.branch: if self.branch:
parts.append(u"{prefix}+{branch}".format(prefix=self.BRANCH_PREFIX, branch=self.branch)) parts.append(u"{prefix}+{branch}".format(prefix=self.BRANCH_PREFIX, branch=self.branch))
if self.version_guid: if self.version_guid:
...@@ -391,10 +384,6 @@ class BlockUsageLocator(BlockLocatorBase, UsageKey): ...@@ -391,10 +384,6 @@ class BlockUsageLocator(BlockLocatorBase, UsageKey):
return self.course_key.offering return self.course_key.offering
@property @property
def package_id(self):
return self.course_key.package_id
@property
def branch(self): def branch(self):
return self.course_key.branch return self.course_key.branch
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment