Skip to content

Commit

Permalink
Migrate ODS datasets urls (#2559)
Browse files Browse the repository at this point in the history
  • Loading branch information
abulte authored Oct 16, 2020
1 parent 9ca1ba3 commit c81945e
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
- Datetime fields `password_rotation_demanded` and `password_rotation_performed` added to user model.
- Override Flask-Security's login and reset password forms to implement the password rotation checks.
- Password complexity settings hardening [#2554](https://github.com/opendatateam/udata/pull/2554)
- Migrate ODS datasets urls [#2559](https://github.com/opendatateam/udata/pull/2559)

## 2.3.0 (2020-09-29)

Expand Down
38 changes: 38 additions & 0 deletions udata/migrations/2020-10-16-migrate-ods-resources.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
'''
Migrate ODS harvested resources URLS from
use_labels_for_header=true to use_labels_for_header=false
cf https://github.com/opendatateam/udata-ods/pull/169
'''
import logging

from mongoengine.errors import ValidationError
from udata.models import Dataset

log = logging.getLogger(__name__)

MIX_MATCH = [
'use_labels_for_header=true',
'use_labels_for_header=false'
]


def migrate(db):
log.info('Migrating ODS harvest datasets...')

count = 0
# datasets from ODS
datasets = Dataset.objects.filter(**{'extras__ods:url__ne': None})
for d in datasets:
touched = False
for r in d.resources:
if r.url.endswith(MIX_MATCH[0]):
r.url = r.url.replace(MIX_MATCH[0], MIX_MATCH[1])
touched = True
count += 1
if touched:
try:
d.save()
except ValidationError as e:
log.warning(f'Error while saving dataset {d.id}: {str(e)}')

log.info(f'Completed, {count} resources migrated.')

0 comments on commit c81945e

Please sign in to comment.