Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def init_db(self):
"""Create the database connection"""
self.log.debug("Connecting to db: %s", self.db_url)
try:
self.session_factory = orm.new_session_factory(
self.db_url,
reset=self.reset_db,
echo=self.debug_db,
**self.db_kwargs
)
# trigger constructing thread local db property
_ = self.db
except OperationalError as e:
self.log.error("Failed to connect to db: %s", self.db_url)
self.log.debug("Database error was:", exc_info=True)
if self.db_url.startswith('sqlite:///'):
self._check_db_path(self.db_url.split(':///', 1)[1])
self.log.critical('\n'.join([
"If you recently upgraded JupyterHub, try running",
" jupyterhub upgrade-db",
"to upgrade your JupyterHub database schema",
if urlinfo.password:
# avoid logging the database password
urlinfo = urlinfo._replace(
netloc='{}:[redacted]@{}:{}'.format(
urlinfo.username, urlinfo.hostname, urlinfo.port
)
)
db_log_url = urlinfo.geturl()
else:
db_log_url = self.db_url
self.log.debug("Connecting to db: %s", db_log_url)
if self.upgrade_db:
dbutil.upgrade_if_needed(self.db_url, log=self.log)
try:
self.session_factory = orm.new_session_factory(
self.db_url, reset=self.reset_db, echo=self.debug_db, **self.db_kwargs
)
self.db = self.session_factory()
except OperationalError as e:
self.log.error("Failed to connect to db: %s", db_log_url)
self.log.debug("Database error was:", exc_info=True)
if self.db_url.startswith('sqlite:///'):
self._check_db_path(self.db_url.split(':///', 1)[1])
self.log.critical(
'\n'.join(
[
"If you recently upgraded JupyterHub, try running",
" jupyterhub upgrade-db",
"to upgrade your JupyterHub database schema",
]
)
def shell(args=None):
"""Start an IPython shell hooked up to the jupyerhub database"""
from .app import JupyterHub
hub = JupyterHub()
hub.load_config_file(hub.config_file)
db_url = hub.db_url
db = orm.new_session_factory(db_url, **hub.db_kwargs)()
ns = {'db': db, 'db_url': db_url, 'orm': orm}
import IPython
IPython.start_ipython(args, user_ns=ns)