Skip to content

Commit

Permalink
pg didn't like the alembic upgrade sequence
Browse files Browse the repository at this point in the history
  • Loading branch information
romanchyla committed Sep 19, 2018
1 parent 86c3f29 commit e585542
Showing 1 changed file with 11 additions and 7 deletions.
18 changes: 11 additions & 7 deletions alembic/versions/45eb49b7e934_add_ratelimit_to_oauth_clients.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,20 @@ def upgrade():
with op.batch_alter_table('users') as batch_op:
batch_op.alter_column(
column_name = 'ratelimit_level',
type_ = sa.types.Float,
default=2.0)
type_ = sa.types.Float)
batch_op.alter_column(
column_name = 'ratelimit_level',
server_default='2.0')
batch_op.add_column(sa.Column('_allowed_scopes', sa.Text))

# now we are going to migrate data
# if a user previously had high ratelimit, we will copy it to the oauth client

app = create_app()
with app.app_context():
for c in db.session.query(OAuthClient).filter(User.ratelimit_level >= 2).yield_per(50):
current_level = c.user.ratelimit_level
c.ratelimit = current_level
for u in db.session.query(User).filter(User.ratelimit_level >= 2).yield_per(50):
for c in db.session.query(OAuthClient).filter(OAuthClient.user_id == u.id).all():
c.ratelimit = u.ratelimit_level
db.session.commit()


Expand All @@ -55,8 +57,10 @@ def downgrade():
batch_op.drop_column('created')

with op.batch_alter_table('users') as batch_op:
bbatch_op.alter_column(
column_name = 'ratelimit_level',
type_ = sa.types.Integer)
batch_op.alter_column(
column_name = 'ratelimit_level',
type_ = sa.types.Integer,
default=2)
server_default='2')
batch_op.drop_column('_allowed_scopes')

0 comments on commit e585542

Please sign in to comment.