Skip to content
Snippets Groups Projects
Commit 3282d60d authored by Megan Henning's avatar Megan Henning
Browse files

Ensure subject age is int

parent 61120f9a
No related branches found
No related tags found
No related merge requests found
......@@ -19,7 +19,7 @@ from api.jobs.jobs import Job
from api.jobs import gears
from api.types import Origin
CURRENT_DATABASE_VERSION = 27 # An int that is bumped when a new schema change is made
CURRENT_DATABASE_VERSION = 28 # An int that is bumped when a new schema change is made
def get_db_version():
......@@ -93,6 +93,7 @@ def process_cursor(cursor, closure):
# pool.close()
# pool.join()
logging.info('Proccessing {} items in cursor ...'.format(cursor.count()))
failed = False
for document in cursor:
......@@ -990,6 +991,21 @@ def upgrade_to_27():
config.db.projects.update_one({'_id': p['_id']}, {'$set': {'template': template}})
storage.recalc_sessions_compliance(project_id=str(p['_id']))
def upgrade_to_28():
"""
Fixes session.subject.age sometimes being a floating-point rather than integer.
"""
sessions = config.db.sessions.find({'subject.age': {'$type': 'double'}})
logging.info('Fixing {} subjects with age stored as double ...'.format(sessions.count()))
for x in sessions:
try:
int_age = int(x['subject']['age'])
except:
int_age = None
config.db.sessions.update({'_id': x['_id']}, {'$set': {'subject.age': int_age}})
def upgrade_schema():
"""
......@@ -1003,7 +1019,9 @@ def upgrade_schema():
while db_version < CURRENT_DATABASE_VERSION:
db_version += 1
upgrade_script = 'upgrade_to_'+str(db_version)
logging.info('Upgrading to version {} ...'.format(db_version))
globals()[upgrade_script]()
logging.info('Upgrade to version {} complete.'.format(db_version))
except KeyError as e:
logging.exception('Attempted to upgrade using script that does not exist: {}'.format(e))
sys.exit(1)
......
......@@ -7,7 +7,7 @@
"firstname_hash": { "type": ["string", "null"] },
"lastname_hash": { "type": ["string", "null"] },
"age": { "type": ["number", "null"] },
"age": { "type": ["integer", "null"] },
"sex": { "enum": ["male", "female", "other", "unknown", null] },
"race": { "enum": ["American Indian or Alaska Native", "Asian", "Native Hawaiian or Other Pacific Islander", "Black or African American", "White", "More Than One Race", "Unknown or Not Reported", null] },
"ethnicity": { "enum": ["Not Hispanic or Latino", "Hispanic or Latino", "Unknown or Not Reported", null] },
......
......@@ -285,3 +285,32 @@ def test_put_container(data_builder, as_admin):
'subject': {'_id': '000000000000000000000000'}
})
assert r.ok
def test_subject_age_must_be_int(data_builder, as_admin):
# Ensure subject age can only be set as int (and None)
# Found old data that had subject age stored as float
session = data_builder.create_session()
subject_age = 123.2
# Attempt to send age as float
r = as_admin.put('/sessions/' + session, json={
'subject': {
'age': subject_age
}
})
assert r.status_code == 400
subject_age = 123
# Ensure subject age set as int works correctly
r = as_admin.put('/sessions/' + session, json={
'subject': {
'age': subject_age
}
})
assert r.ok
r = as_admin.get('/sessions/' + session)
assert subject_age == r.json['subject']['age']
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment