redash/migrations/permissions_migration.py

30 lines
1.2 KiB
Python
Raw Normal View History

2014-05-13 16:36:04 +00:00
import peewee
2014-04-14 17:14:01 +00:00
from playhouse.migrate import Migrator
from redash import models
2014-06-18 17:23:48 +00:00
from redash.models import db
2014-04-14 17:14:01 +00:00
if __name__ == '__main__':
db.connect_db()
migrator = Migrator(db.database)
if not models.Group.table_exists():
print "Creating groups table..."
models.Group.create_table()
with db.database.transaction():
models.Group.insert(name='admin', permissions=['admin'], tables=['*']).execute()
2014-05-08 18:19:23 +00:00
models.Group.insert(name='api', permissions=['view_query'], tables=['*']).execute()
2014-04-14 17:14:01 +00:00
models.Group.insert(name='default', permissions=models.Group.DEFAULT_PERMISSIONS, tables=['*']).execute()
2014-04-14 17:14:01 +00:00
migrator.add_column(models.User, models.User.groups, 'groups')
2014-05-13 16:36:04 +00:00
models.User.update(groups=['admin', 'default']).where(peewee.SQL("is_admin = true")).execute()
models.User.update(groups=['admin', 'default']).where(peewee.SQL("'admin' = any(permissions)")).execute()
2014-05-13 16:36:04 +00:00
models.User.update(groups=['default']).where(peewee.SQL("is_admin = false")).execute()
migrator.drop_column(models.User, 'permissions')
2014-05-09 17:12:34 +00:00
migrator.drop_column(models.User, 'is_admin')
2014-04-14 17:14:01 +00:00
db.close_db(None)