mirror of
https://github.com/jupyterhub/jupyterhub.git
synced 2025-10-17 06:52:59 +00:00
get upgrade working on sqlite with foreign key naming convention
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
"""RBAC
|
||||
"""
|
||||
rbac changes for jupyterhub 2.0
|
||||
|
||||
Revision ID: 833da8570507
|
||||
Revises: 4dc2d5a8c53c
|
||||
@@ -14,31 +15,40 @@ depends_on = None
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
|
||||
from jupyterhub import orm
|
||||
|
||||
|
||||
naming_convention = orm.meta.naming_convention
|
||||
|
||||
|
||||
def upgrade():
|
||||
# associate spawners and services with their oauth clients
|
||||
op.add_column(
|
||||
'services', sa.Column('oauth_client_id', sa.Unicode(length=255), nullable=True)
|
||||
)
|
||||
op.create_foreign_key(
|
||||
None,
|
||||
'services',
|
||||
'oauth_clients',
|
||||
['oauth_client_id'],
|
||||
['identifier'],
|
||||
ondelete='SET NULL',
|
||||
)
|
||||
op.add_column(
|
||||
'spawners', sa.Column('oauth_client_id', sa.Unicode(length=255), nullable=True)
|
||||
)
|
||||
op.create_foreign_key(
|
||||
None,
|
||||
'spawners',
|
||||
'oauth_clients',
|
||||
['oauth_client_id'],
|
||||
['identifier'],
|
||||
ondelete='SET NULL',
|
||||
)
|
||||
# op.add_column(
|
||||
# 'services', sa.Column('oauth_client_id', sa.Unicode(length=255), nullable=True)
|
||||
# )
|
||||
for table_name in ('services', 'spawners'):
|
||||
column_name = "oauth_client_id"
|
||||
target_table = "oauth_clients"
|
||||
target_column = "identifier"
|
||||
with op.batch_alter_table(
|
||||
table_name,
|
||||
schema=None,
|
||||
) as batch_op:
|
||||
batch_op.add_column(
|
||||
sa.Column('oauth_client_id', sa.Unicode(length=255), nullable=True),
|
||||
)
|
||||
batch_op.create_foreign_key(
|
||||
naming_convention["fk"]
|
||||
% dict(
|
||||
table_name=table_name,
|
||||
column_0_name=column_name,
|
||||
referred_table_name=target_table,
|
||||
),
|
||||
target_table,
|
||||
[column_name],
|
||||
[target_column],
|
||||
ondelete='SET NULL',
|
||||
)
|
||||
|
||||
# FIXME, maybe: currently drops all api tokens and forces recreation!
|
||||
# this ensures a consistent database, but requires:
|
||||
@@ -57,17 +67,32 @@ def upgrade():
|
||||
|
||||
|
||||
def downgrade():
|
||||
for table_name in ('services', 'spawners'):
|
||||
column_name = "oauth_client_id"
|
||||
target_table = "oauth_clients"
|
||||
target_column = "identifier"
|
||||
|
||||
op.drop_constraint(None, 'spawners', type_='foreignkey')
|
||||
op.drop_column('spawners', 'oauth_client_id')
|
||||
op.drop_constraint(None, 'services', type_='foreignkey')
|
||||
op.drop_column('services', 'oauth_client_id')
|
||||
with op.batch_alter_table(
|
||||
table_name,
|
||||
schema=None,
|
||||
naming_convention=orm.meta.naming_convention,
|
||||
) as batch_op:
|
||||
batch_op.drop_constraint(
|
||||
naming_convention["fk"]
|
||||
% dict(
|
||||
table_name=table_name,
|
||||
column_0_name=column_name,
|
||||
referred_table_name=target_table,
|
||||
),
|
||||
type_='foreignkey',
|
||||
)
|
||||
batch_op.drop_column(column_name)
|
||||
|
||||
# delete OAuth tokens for non-jupyterhub clients
|
||||
# drop new columns from api tokens
|
||||
op.drop_constraint(None, 'api_tokens', type_='foreignkey')
|
||||
op.drop_column('api_tokens', 'session_id')
|
||||
op.drop_column('api_tokens', 'client_id')
|
||||
# op.drop_constraint(None, 'api_tokens', type_='foreignkey')
|
||||
# op.drop_column('api_tokens', 'session_id')
|
||||
# op.drop_column('api_tokens', 'client_id')
|
||||
|
||||
# FIXME: only drop tokens whose client id is not 'jupyterhub'
|
||||
# until then, drop all tokens
|
||||
|
@@ -16,12 +16,12 @@ from sqlalchemy import Boolean
|
||||
from sqlalchemy import Column
|
||||
from sqlalchemy import create_engine
|
||||
from sqlalchemy import DateTime
|
||||
from sqlalchemy import Enum
|
||||
from sqlalchemy import event
|
||||
from sqlalchemy import exc
|
||||
from sqlalchemy import ForeignKey
|
||||
from sqlalchemy import inspect
|
||||
from sqlalchemy import Integer
|
||||
from sqlalchemy import MetaData
|
||||
from sqlalchemy import or_
|
||||
from sqlalchemy import select
|
||||
from sqlalchemy import Table
|
||||
@@ -115,7 +115,17 @@ class JSONList(JSONDict):
|
||||
return value
|
||||
|
||||
|
||||
Base = declarative_base()
|
||||
meta = MetaData(
|
||||
naming_convention={
|
||||
"ix": "ix_%(column_0_label)s",
|
||||
"uq": "uq_%(table_name)s_%(column_0_name)s",
|
||||
"ck": "ck_%(table_name)s_%(constraint_name)s",
|
||||
"fk": "fk_%(table_name)s_%(column_0_name)s_%(referred_table_name)s",
|
||||
"pk": "pk_%(table_name)s",
|
||||
}
|
||||
)
|
||||
|
||||
Base = declarative_base(metadata=meta)
|
||||
Base.log = app_log
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user