Skip to content

Commit

Permalink
Prevent potential db upgrading problem (#628)
Browse files Browse the repository at this point in the history
* fix general db upgrade problem

* add comments
  • Loading branch information
yxjames authored and mistercrunch committed Jun 17, 2016
1 parent 4400c70 commit 4c8523e
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,28 @@
from alembic import op
import sqlalchemy as sa
from caravel import db
from caravel import models


from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import (
Column, Integer, String, ForeignKey)

Base = declarative_base()

class Slice(Base):
"""Declarative class to do query in upgrade"""
__tablename__ = 'slices'
id = Column(Integer, primary_key=True)
slice_name = Column(String(250))
druid_datasource_id = Column(Integer, ForeignKey('datasources.id'))
table_id = Column(Integer, ForeignKey('tables.id'))
perm = Column(String(2000))

def upgrade():
bind = op.get_bind()
op.add_column('slices', sa.Column('perm', sa.String(length=2000), nullable=True))
session = db.Session(bind=bind)

for slc in session.query(models.Slice).all():
# Use Slice class defined here instead of models.Slice
for slc in session.query(Slice).all():
if slc.datasource:
slc.perm = slc.datasource.perm
session.merge(slc)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,37 @@
from alembic import op
import sqlalchemy as sa
from caravel import db
from caravel import models


from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import (
Column, Integer, Boolean)

Base = declarative_base()

class DruidMetric(Base):
"""Declarative class used to do query in upgrade"""
__tablename__ = 'metrics'
id = Column(Integer, primary_key=True)
is_restricted = Column(Boolean, default=False, nullable=True)

class SqlMetric(Base):
"""Declarative class used to do query in upgrade"""
__tablename__ = 'sql_metrics'
id = Column(Integer, primary_key=True)
is_restricted = Column(Boolean, default=False, nullable=True)

def upgrade():
op.add_column('metrics', sa.Column('is_restricted', sa.Boolean(), nullable=True))
op.add_column('sql_metrics', sa.Column('is_restricted', sa.Boolean(), nullable=True))

bind = op.get_bind()
session = db.Session(bind=bind)

for obj in session.query(models.DruidMetric).all():
# don't use models.DruidMetric
# because it assumes the context is consistent with the application
for obj in session.query(DruidMetric).all():
obj.is_restricted = False

for obj in session.query(models.SqlMetric).all():
for obj in session.query(SqlMetric).all():
obj.is_restricted = False

session.commit()
Expand Down

0 comments on commit 4c8523e

Please sign in to comment.