Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a general lms user id column to the student table #1036

Merged
merged 11 commits into from
May 31, 2019

Conversation

sigurdurb
Copy link
Contributor

@sigurdurb sigurdurb commented Oct 29, 2018

Adds a lms_user_id to the ngrader db:

See issue: #1035

How to use:

nbgrader db student add <some student.id> --lms-user-id="'123'"
Notice the extra "" if your lms user id contains only numbers.

Additions that can be made:

  • Not yet added to nbgrader/plugins/export.py : CSV export plugin, just putting it here for reference on things this could be added to:
    score['max_score'] = assignment.max_score

Possible issues:

  • I had a very unhelpful error with the nbgrader db upgrade oldgradebook.db but it still put in the column. Maybe someone else could also test it.

@sigurdurb sigurdurb changed the title Adds a general lms user id column to the student table Add a general lms user id column to the student table Oct 29, 2018
@sigurdurb
Copy link
Contributor Author

So here is the error msg when I upgrade the gradebook.db, yet it still adds the column and I can run nbgrader db student add <some student.id> --lms-user-id="'123'" successfully

$ nbgrader db upgrade gradebook.db
[DbUpgradeApp | WARNING] No nbgrader_config.py file found (rerun with --debug to see where nbgrader is looking)
[DbUpgradeApp | INFO] Backing up /home/reir20173/reir20173/gradebook.db => /home/reir20173/reir20173/gradebook.db.2018-10-30-200724.921960
[DbUpgradeApp | INFO] Upgrading sqlite:////home/reir20173/reir20173/gradebook.db
INFO  [alembic.runtime.migration] Context impl SQLiteImpl.
INFO  [alembic.runtime.migration] Will assume non-transactional DDL.
INFO  [alembic.runtime.migration] Running upgrade 724cde206c17 -> 282d30a0218e, adds lms_user_id column to student table
Traceback (most recent call last):
  File "/opt/conda/bin/alembic", line 11, in <module>
    sys.exit(main())
  File "/opt/conda/lib/python3.5/site-packages/alembic/config.py", line 486, in main
    CommandLine(prog=prog).main(argv=argv)
  File "/opt/conda/lib/python3.5/site-packages/alembic/config.py", line 480, in main
    self.run_cmd(cfg, options)
  File "/opt/conda/lib/python3.5/site-packages/alembic/config.py", line 463, in run_cmd
    **dict((k, getattr(options, k, None)) for k in kwarg)
  File "/opt/conda/lib/python3.5/site-packages/alembic/command.py", line 254, in upgrade
    script.run_env()
  File "/opt/conda/lib/python3.5/site-packages/alembic/script/base.py", line 427, in run_env
    util.load_python_file(self.dir, 'env.py')
  File "/opt/conda/lib/python3.5/site-packages/alembic/util/pyfiles.py", line 81, in load_python_file
    module = load_module_py(module_id, path)
  File "/opt/conda/lib/python3.5/site-packages/alembic/util/compat.py", line 83, in load_module_py
    spec.loader.exec_module(module)
  File "<frozen importlib._bootstrap_external>", line 665, in exec_module
  File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed
  File "/etc/jupyterhub/nbgrader_stuff/lms_octave_nbgrader/nbgrader/nbgrader/alembic/env.py", line 69, in <module>
    run_migrations_online()
  File "/etc/jupyterhub/nbgrader_stuff/lms_octave_nbgrader/nbgrader/nbgrader/alembic/env.py", line 64, in run_migrations_online
    context.run_migrations()
  File "/opt/conda/lib/python3.5/contextlib.py", line 77, in __exit__
    self.gen.throw(type, value, traceback)
  File "/opt/conda/lib/python3.5/site-packages/alembic/runtime/migration.py", line 186, in do_nothing
    yield
  File "/etc/jupyterhub/nbgrader_stuff/lms_octave_nbgrader/nbgrader/nbgrader/alembic/env.py", line 64, in run_migrations_online
    context.run_migrations()
  File "<string>", line 8, in run_migrations
  File "/opt/conda/lib/python3.5/site-packages/alembic/runtime/environment.py", line 836, in run_migrations
    self.get_context().run_migrations(**kw)
  File "/opt/conda/lib/python3.5/contextlib.py", line 77, in __exit__
    self.gen.throw(type, value, traceback)
  File "/opt/conda/lib/python3.5/site-packages/alembic/operations/base.py", line 161, in context
    yield op
  File "/opt/conda/lib/python3.5/site-packages/alembic/runtime/environment.py", line 836, in run_migrations
    self.get_context().run_migrations(**kw)
  File "/opt/conda/lib/python3.5/site-packages/alembic/runtime/migration.py", line 342, in run_migrations
    run_args=kw)
  File "/opt/conda/lib/python3.5/contextlib.py", line 77, in __exit__
    self.gen.throw(type, value, traceback)
  File "/opt/conda/lib/python3.5/site-packages/alembic/runtime/migration.py", line 180, in do_nothing
    yield
  File "/opt/conda/lib/python3.5/site-packages/alembic/runtime/migration.py", line 330, in run_migrations
    step.migration_fn(**kw)
  File "/etc/jupyterhub/nbgrader_stuff/lms_octave_nbgrader/nbgrader/nbgrader/alembic/versions/282d30a0218e_adds_lms_user_id_column_to_student_table.py", line 20, in upgrade
    op.add_column('student', sa.Column('lms_user_id', sa.String(128), unique=True, nullable=True))
  File "<string>", line 8, in add_column
  File "<string>", line 3, in add_column
  File "/opt/conda/lib/python3.5/site-packages/alembic/operations/ops.py", line 1565, in add_column
    return operations.invoke(op)
  File "/opt/conda/lib/python3.5/site-packages/alembic/operations/base.py", line 319, in invoke
    return fn(self, operation)
  File "/opt/conda/lib/python3.5/site-packages/alembic/operations/toimpl.py", line 127, in add_column
    operations.impl.add_constraint(constraint)
  File "/opt/conda/lib/python3.5/site-packages/alembic/ddl/sqlite.py", line 34, in add_constraint
    "No support for ALTER of constraints in SQLite dialect")
NotImplementedError: No support for ALTER of constraints in SQLite dialect
Traceback (most recent call last):
  File "/opt/conda/bin/nbgrader", line 11, in <module>
    load_entry_point('nbgrader', 'console_scripts', 'nbgrader')()
  File "/etc/jupyterhub/nbgrader_stuff/lms_octave_nbgrader/nbgrader/nbgrader/apps/nbgraderapp.py", line 301, in main
    NbGraderApp.launch_instance()
  File "/opt/conda/lib/python3.5/site-packages/jupyter_core/application.py", line 266, in launch_instance
    return super(JupyterApp, cls).launch_instance(argv=argv, **kwargs)
  File "/opt/conda/lib/python3.5/site-packages/traitlets/config/application.py", line 658, in launch_instance
    app.start()
  File "/etc/jupyterhub/nbgrader_stuff/lms_octave_nbgrader/nbgrader/nbgrader/apps/nbgraderapp.py", line 294, in start
    super(NbGraderApp, self).start()
  File "/opt/conda/lib/python3.5/site-packages/jupyter_core/application.py", line 255, in start
    self.subapp.start()
  File "/etc/jupyterhub/nbgrader_stuff/lms_octave_nbgrader/nbgrader/nbgrader/apps/dbapp.py", line 558, in start
    super(DbApp, self).start()
  File "/opt/conda/lib/python3.5/site-packages/jupyter_core/application.py", line 255, in start
    self.subapp.start()
  File "/etc/jupyterhub/nbgrader_stuff/lms_octave_nbgrader/nbgrader/nbgrader/apps/dbapp.py", line 505, in start
    dbutil.upgrade(self.coursedir.db_url)
  File "/etc/jupyterhub/nbgrader_stuff/lms_octave_nbgrader/nbgrader/nbgrader/dbutil.py", line 72, in upgrade
    ['alembic', '-c', alembic_ini, 'upgrade', revision]
  File "/opt/conda/lib/python3.5/subprocess.py", line 581, in check_call
    raise CalledProcessError(retcode, cmd)
subprocess.CalledProcessError: Command '['alembic', '-c', '/tmp/tmpmjnnblz2/alembic.ini', 'upgrade', 'head']' returned non-zero exit status 1

Running it again I get a

sqlite3.OperationalError: duplicate column name: lms_user_id

# nbgrader db upgrade gradebook.db
[DbUpgradeApp | WARNING] No nbgrader_config.py file found (rerun with --debug to see where nbgrader is looking)
[DbUpgradeApp | INFO] Backing up /home/reir20173/reir20173/gradebook.db => /home/reir20173/reir20173/gradebook.db.2018-10-30-200748.769119
[DbUpgradeApp | INFO] Upgrading sqlite:////home/reir20173/reir20173/gradebook.db
INFO  [alembic.runtime.migration] Context impl SQLiteImpl.
INFO  [alembic.runtime.migration] Will assume non-transactional DDL.
INFO  [alembic.runtime.migration] Running upgrade 724cde206c17 -> 282d30a0218e, adds lms_user_id column to student table
Traceback (most recent call last):
  File "/opt/conda/lib/python3.5/site-packages/sqlalchemy/engine/base.py", line 1193, in _execute_context
    context)
  File "/opt/conda/lib/python3.5/site-packages/sqlalchemy/engine/default.py", line 507, in do_execute
    cursor.execute(statement, parameters)
sqlite3.OperationalError: duplicate column name: lms_user_id

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/opt/conda/bin/alembic", line 11, in <module>
    sys.exit(main())
  File "/opt/conda/lib/python3.5/site-packages/alembic/config.py", line 486, in main
    CommandLine(prog=prog).main(argv=argv)
  File "/opt/conda/lib/python3.5/site-packages/alembic/config.py", line 480, in main
    self.run_cmd(cfg, options)
  File "/opt/conda/lib/python3.5/site-packages/alembic/config.py", line 463, in run_cmd
    **dict((k, getattr(options, k, None)) for k in kwarg)
  File "/opt/conda/lib/python3.5/site-packages/alembic/command.py", line 254, in upgrade
    script.run_env()
  File "/opt/conda/lib/python3.5/site-packages/alembic/script/base.py", line 427, in run_env
    util.load_python_file(self.dir, 'env.py')
  File "/opt/conda/lib/python3.5/site-packages/alembic/util/pyfiles.py", line 81, in load_python_file
    module = load_module_py(module_id, path)
  File "/opt/conda/lib/python3.5/site-packages/alembic/util/compat.py", line 83, in load_module_py
    spec.loader.exec_module(module)
  File "<frozen importlib._bootstrap_external>", line 665, in exec_module
  File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed
  File "/etc/jupyterhub/nbgrader_stuff/lms_octave_nbgrader/nbgrader/nbgrader/alembic/env.py", line 69, in <module>
    run_migrations_online()
  File "/etc/jupyterhub/nbgrader_stuff/lms_octave_nbgrader/nbgrader/nbgrader/alembic/env.py", line 64, in run_migrations_online
    context.run_migrations()
  File "<string>", line 8, in run_migrations
  File "/opt/conda/lib/python3.5/site-packages/alembic/runtime/environment.py", line 836, in run_migrations
    self.get_context().run_migrations(**kw)
  File "/opt/conda/lib/python3.5/site-packages/alembic/runtime/migration.py", line 330, in run_migrations
    step.migration_fn(**kw)
  File "/etc/jupyterhub/nbgrader_stuff/lms_octave_nbgrader/nbgrader/nbgrader/alembic/versions/282d30a0218e_adds_lms_user_id_column_to_student_table.py", line 20, in upgrade
    op.add_column('student', sa.Column('lms_user_id', sa.String(128), unique=True, nullable=True))
  File "<string>", line 8, in add_column
  File "<string>", line 3, in add_column
  File "/opt/conda/lib/python3.5/site-packages/alembic/operations/ops.py", line 1565, in add_column
    return operations.invoke(op)
  File "/opt/conda/lib/python3.5/site-packages/alembic/operations/base.py", line 319, in invoke
    return fn(self, operation)
  File "/opt/conda/lib/python3.5/site-packages/alembic/operations/toimpl.py", line 123, in add_column
    schema=schema
  File "/opt/conda/lib/python3.5/site-packages/alembic/ddl/impl.py", line 172, in add_column
    self._exec(base.AddColumn(table_name, column, schema=schema))
  File "/opt/conda/lib/python3.5/site-packages/alembic/ddl/impl.py", line 118, in _exec
    return conn.execute(construct, *multiparams, **params)
  File "/opt/conda/lib/python3.5/site-packages/sqlalchemy/engine/base.py", line 948, in execute
    return meth(self, multiparams, params)
  File "/opt/conda/lib/python3.5/site-packages/sqlalchemy/sql/ddl.py", line 68, in _execute_on_connection
    return connection._execute_ddl(self, multiparams, params)
  File "/opt/conda/lib/python3.5/site-packages/sqlalchemy/engine/base.py", line 1009, in _execute_ddl
    compiled
  File "/opt/conda/lib/python3.5/site-packages/sqlalchemy/engine/base.py", line 1200, in _execute_context
    context)
  File "/opt/conda/lib/python3.5/site-packages/sqlalchemy/engine/base.py", line 1413, in _handle_dbapi_exception
    exc_info
  File "/opt/conda/lib/python3.5/site-packages/sqlalchemy/util/compat.py", line 203, in raise_from_cause
    reraise(type(exception), exception, tb=exc_tb, cause=cause)
  File "/opt/conda/lib/python3.5/site-packages/sqlalchemy/util/compat.py", line 186, in reraise
    raise value.with_traceback(tb)
  File "/opt/conda/lib/python3.5/site-packages/sqlalchemy/engine/base.py", line 1193, in _execute_context
    context)
  File "/opt/conda/lib/python3.5/site-packages/sqlalchemy/engine/default.py", line 507, in do_execute
    cursor.execute(statement, parameters)
sqlalchemy.exc.OperationalError: (sqlite3.OperationalError) duplicate column name: lms_user_id [SQL: 'ALTER TABLE student ADD COLUMN lms_user_id VARCHAR(128)'] (Background on this error at: http://sqlalche.me/e/e3q8)
Traceback (most recent call last):
  File "/opt/conda/bin/nbgrader", line 11, in <module>
    load_entry_point('nbgrader', 'console_scripts', 'nbgrader')()
  File "/etc/jupyterhub/nbgrader_stuff/lms_octave_nbgrader/nbgrader/nbgrader/apps/nbgraderapp.py", line 301, in main
    NbGraderApp.launch_instance()
  File "/opt/conda/lib/python3.5/site-packages/jupyter_core/application.py", line 266, in launch_instance
    return super(JupyterApp, cls).launch_instance(argv=argv, **kwargs)
  File "/opt/conda/lib/python3.5/site-packages/traitlets/config/application.py", line 658, in launch_instance
    app.start()
  File "/etc/jupyterhub/nbgrader_stuff/lms_octave_nbgrader/nbgrader/nbgrader/apps/nbgraderapp.py", line 294, in start
    super(NbGraderApp, self).start()
  File "/opt/conda/lib/python3.5/site-packages/jupyter_core/application.py", line 255, in start
    self.subapp.start()
  File "/etc/jupyterhub/nbgrader_stuff/lms_octave_nbgrader/nbgrader/nbgrader/apps/dbapp.py", line 558, in start
    super(DbApp, self).start()
  File "/opt/conda/lib/python3.5/site-packages/jupyter_core/application.py", line 255, in start
    self.subapp.start()
  File "/etc/jupyterhub/nbgrader_stuff/lms_octave_nbgrader/nbgrader/nbgrader/apps/dbapp.py", line 505, in start
    dbutil.upgrade(self.coursedir.db_url)
  File "/etc/jupyterhub/nbgrader_stuff/lms_octave_nbgrader/nbgrader/nbgrader/dbutil.py", line 72, in upgrade
    ['alembic', '-c', alembic_ini, 'upgrade', revision]
  File "/opt/conda/lib/python3.5/subprocess.py", line 581, in check_call
    raise CalledProcessError(retcode, cmd)
subprocess.CalledProcessError: Command '['alembic', '-c', '/tmp/tmpn286gwz5/alembic.ini', 'upgrade', 'head']' returned non-zero exit status 1


@jhamrick jhamrick added this to the 0.6.0 milestone Dec 15, 2018
@jhamrick
Copy link
Member

Looks good, thanks!

I'll try to have a look and see why the alembic update fails.

Copy link
Member

@jhamrick jhamrick left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So the error appears to be because you enforce the column to be unique (a form of constraint), but SQLite doesn't allow you to add new constraints after the table has already been created. It seems like it would be possible to work around this using alembic batch operations (https://alembic.sqlalchemy.org/en/latest/batch.html), but it would be messy and hard to implement. Thus, I would suggest just dropping the unique requirement, and then everything should work as expected.

Additionally, can you update the API docs to reflect the addition of the new column name?

@sigurdurb
Copy link
Contributor Author

sigurdurb commented Jan 2, 2019

I removed the unique constraint. I'm not sure why the documentation is not being generated. I ran the python tasks.py docs command but nothing seems to have changed. https://nbgrader.readthedocs.io/en/latest/command_line_tools/nbgrader-db-student-add.html This page and some other pages also give a 404 when I click edit on github.

@jhamrick
Copy link
Member

jhamrick commented Jan 3, 2019

You have to manually edit the rst file here to add the column name: https://github.com/jupyter/nbgrader/blob/master/nbgrader/docs/source/api/models.rst

Copy link
Member

@jhamrick jhamrick left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good! I guess the last thing is just to update the tests so that they check for the lms_user_id as well.

nbgrader/api.py Outdated Show resolved Hide resolved
@jhamrick
Copy link
Member

I am not sure why the tests are failing as they seem to pass for me locally. Can you rebase (or merge master into your branch) to bring this up-to-date with the master branch of the repo? I think this time you shouldn't require too many changes, probably the main thing is you'll need to change the down_revision of your alembic file to 167914646830 (which is the task cells one).

@jhamrick jhamrick merged commit 39ea9d4 into jupyter:master May 31, 2019
@jhamrick jhamrick mentioned this pull request May 31, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants