def upgrade(migrate_engine): # Upgrade operations go here. Don't create your own engine; bind # migrate_engine to your metadata debug = True session.configure(bind=migrate_engine) migrate_engine.echo=debug engine = migrate_engine r_meta = s.MetaData(migrate_engine, True) Base.metadata.bind = migrate_engine class AuthUserLog(Base): """ event: L - Login R - Register P - Password F - Forgot """ __tablename__ = 'auth_user_log' __table_args__ = {"sqlite_autoincrement": True} id = Column(types.Integer, primary_key=True) user_id = Column(types.Integer, ForeignKey("auth_users.id"), index=True) time = Column(types.DateTime(), default=func.now()) ip_addr = Column(Unicode(39), nullable=False) internal_user = Column(Boolean, nullable=False, default=False) external_user = Column(Boolean, nullable=False, default=False) event = Column(types.Enum(u'L',u'R',u'P',u'F', name=u"event"), default=u'L') t=AuthUserLog.__table__ if not 'internal_user' in r_meta.tables[t.name].c: t.c['internal_user'].create(table=t) if not 'external_user' in r_meta.tables[t.name].c: t.c['external_user'].create(table=t) session.flush()
def upgrade(migrate_engine): class AuthUserLog(Base): """ event: L - Login R - Register P - Password F - Forgot """ __tablename__ = 'auth_user_log' __table_args__ = {"sqlite_autoincrement": True} id = Column(Integer, primary_key=True) user_id = Column(Integer, ForeignKey("auth_users.id", onupdate='CASCADE', ondelete='CASCADE'), index=True) time = Column(DateTime(), default=func.now()) ip_addr = Column(Unicode(39), nullable=False) internal_user = Column(Boolean, nullable=False, default=False) external_user = Column(Boolean, nullable=False, default=False) event = Column(Enum(u'L',u'R',u'P',u'F', name=u"event"), default=u'L') recreate_constraints = [AuthUserLog] # Upgrade operations go here. Don't create your own engine; bind # migrate_engine to your metadata debug = True session.configure(bind=migrate_engine) migrate_engine.echo=debug metadata.bind = migrate_engine metadata.reflect(only=['auth_users']) r_meta = s.MetaData(migrate_engine, True) def commit(): session.commit() r_meta.bind.execute ('COMMIT;') metadata.bind.execute('COMMIT;') # create constraints fks = [] for md in recreate_constraints: t = md.__table__ rt = r_meta.tables[t.name] rt_constraints = [a for a in rt.foreign_keys] for cs in deepcopy(t.foreign_keys): if cs.__class__.__name__ == 'ForeignKey': table, column = cs.target_fullname.split('.') target = [r_meta.tables[table].c[column]] parent = [r_meta.tables[cs.parent.table.name].c[cs.parent.name]] fk = ForeignKeyConstraint(columns=parent,refcolumns=target) fk.use_alter = cs.use_alter fk.ondelete = 'CASCADE' fk.onupdate = 'CASCADE' fk.name = cs.name fks.append(fk) if (cs.name in [a.name for a in rt_constraints] or (cs.target_fullname in [a.target_fullname for a in rt_constraints])): fk.drop(migrate_engine) commit() for fk in fks: fk.create(migrate_engine) commit()
def upgrade(migrate_engine): # Upgrade operations go here. Don't create your own engine; bind # migrate_engine to your metadata debug = True session.configure(bind=migrate_engine) migrate_engine.echo = debug metadata.bind = migrate_engine metadata.reflect(only=["auth_users"]) r_meta = s.MetaData(migrate_engine, True) def commit(): session.commit() r_meta.bind.execute("COMMIT;") metadata.bind.execute("COMMIT;") commit() for p in create: if p.__table__.name in r_meta.tables: p.__table__.drop(migrate_engine) commit() r_meta.reflect() p.__table__.create(migrate_engine) commit() # cleanup old tables inconsistencies for acl in Acl.all(): try: perm = Permission.by_id(acl.permission) except NoResultFound, e: session.delete(acl) session.commit() commit() except Exception, e: pass
def upgrade(migrate_engine): # Upgrade operations go here. Don't create your own engine; bind # migrate_engine to your metadata debug = True session.configure(bind=migrate_engine) migrate_engine.echo=debug metadata.bind = migrate_engine real_perms = Permission.all() for p in real_perms[:]: if not p.name in default_permissions: session.delete(p) session.commit() nreal_perms = [p.name for p in real_perms] for p in default_permissions: if not p in nreal_perms: perm = Permission(name=p, description=default_permissions[p]) session.add(perm) session.commit() for p in default_acls: default_acls try: perm = Permission.by_name(p) except: pass roles = default_acls[p] for role in roles: access = roles[role] orole = Role.by_name(role) if access: if not perm in orole.global_permissions: orole.global_permissions.append(perm) session.add(orole) session.commit() else: if perm in orole.global_permissions: del orole.global_permissions[orole.global_permissions.index(perm)] session.add(orole) session.commit() session.flush()
def upgrade(migrate_engine): """r_meta must be reflected from the current database whereas metadata is the current constructed metadata for the migration purpose""" # Upgrade operations go here. Don't create your own engine; bind # migrate_engine to your metadata class Acl(Base): __tablename__ = 'authentication_acl' role = Column(Integer, ForeignKey("authentication_role.id", name="fk_acl_role", use_alter=True, ondelete="CASCADE", onupdate="CASCADE"), primary_key=True) permission = Column(Integer, ForeignKey("authentication_permission.id", name="fk_acl_permission", use_alter=True, ondelete="CASCADE", onupdate="CASCADE"), primary_key=True) class Role(Base): __tablename__ = 'authentication_role' id = Column(Integer, primary_key=True) name = Column(Unicode(50), unique=True) description = Column(Unicode(2500)) global_permissions = relationship( "Permission", uselist=True, secondary="authentication_acl", secondaryjoin="Acl.permission==Permission.id") def __init__(self, id=None, name=None, description=None, global_permissions=None): self.id = id self.description = description self.name = name if global_permissions is not None: self.global_permissions.extend(global_permissions) class Project(Base): __tablename__ = 'projects' id = Column(Integer, primary_key=True) name = Column(Unicode(50), unique=True) description = Column(Unicode(255)) directory = Column(Unicode(2550)) user_id = Column(Integer, ) class Permission(Base): __tablename__ = 'authentication_permission' id = Column(Integer, primary_key=True) name = Column(Unicode(50), unique=True) description = Column(Unicode(2500)) roles = relationship( "Role", uselist=True, secondary="authentication_acl", secondaryjoin="Acl.permission==Role.id") def __init__(self, id=None, name=None, description=None, roles=None): self.id = id self.name = name self.description=description if roles is not None: self.roles.extends(roles) class Server(Base): __tablename__ = 'servers' id = Column(Integer, primary_key=True) name = Column(Unicode(50), unique=True) url = Column(Unicode(255)) help_mail = Column(Unicode(255)) def __init__(self, name=None, url=None, help_mail=None, ): self.name = name self.url = url self.help_mail = help_mail class Service(Base): __tablename__ = 'services' __table_args__ = ( UniqueConstraint('name', 'server_id', name='unique_server_service'), Index('search_classification', 'classification'), Index('search_package', 'package'), ) id = Column(Integer, primary_key=True) name = Column(Unicode(100), unique=True) server_id = Column(Integer, ForeignKey("servers.id", name="fk_service_server", use_alter=True), nullable=False) package = Column(Unicode(2500), default='not read', nullable=False) classification = Column(Unicode(2500), default='not read', nullable=False) enable = Column(Boolean(), default=True, nullable=False) exportable = Column(Boolean(), default=False, nullable=False) type = Column(Enum('program', 'workflow', 'viewer', name='service_type'), default='program', nullable=False) def __init__(self, name=None, url=None, help_mail=None, ): self.name = name self.url = url self.help_mail = help_mail debug = True session.configure(bind=migrate_engine) migrate_engine.echo=debug metadata.bind = migrate_engine """ Create services table """ r_meta = s.MetaData(migrate_engine, True) Base.metadata.bind = migrate_engine for o in [Service]: t = o.__table__ if not t.name in r_meta.tables: t.create(migrate_engine) r_meta = s.MetaData(migrate_engine, True) for c in ['enable', 'exportable',]: if not c in r_meta.tables[t.name].c: t.c[c].create(table=t) recreate_table_fkeys(o, session) """ Reload all permissions """ real_perms = Permission.all() for p in real_perms[:]: if not p.name in default_permissions: session.delete(p) session.commit() nreal_perms = [p.name for p in real_perms] for p in default_permissions: if not p in nreal_perms: perm = Permission(name=p, description=default_permissions[p]) session.add(perm) session.commit() """ Reload all ACLS """ for p in default_acls: default_acls try: perm = Permission.by_name(p) except: pass roles = default_acls[p] for role in roles: access = roles[role] orole = Role.by_name(role) if access: if not perm in orole.global_permissions: orole.global_permissions.append(perm) session.add(orole) session.commit() else: if perm in orole.global_permissions: del orole.global_permissions[orole.global_permissions.index(perm)] session.add(orole) session.commit() session.flush()
def commit(): session.commit() r_meta.bind.execute ('COMMIT;') metadata.bind.execute('COMMIT;')
def upgrade(migrate_engine): """r_meta must be reflected from the current database whereas metadata is the current constructed metadata for the migration purpose""" class Server(Base): __tablename__ = 'servers' id = Column(Integer, primary_key=True) name = Column(Unicode(50), unique=True) url = Column(Unicode(255)) help_mail = Column(Unicode(255)) def __init__(self, name=None, url=None, help_mail=None, ): self.name = name self.url = url self.help_mail = help_mail class Acl(Base): __tablename__ = 'authentication_acl' role = Column(Integer, ForeignKey("authentication_role.id", name="fk_acl_role", use_alter=True, ondelete="CASCADE", onupdate="CASCADE"), primary_key=True) permission = Column(Integer, ForeignKey("authentication_permission.id", name="fk_acl_permission", use_alter=True, ondelete="CASCADE", onupdate="CASCADE"), primary_key=True) class Role(Base): __tablename__ = 'authentication_role' id = Column(Integer, primary_key=True) name = Column(Unicode(50), unique=True) description = Column(Unicode(2500)) global_permissions = relationship( "Permission", uselist=True, secondary="authentication_acl", secondaryjoin="Acl.permission==Permission.id") def __init__(self, id=None, name=None, description=None, global_permissions=None): self.id = id self.description = description self.name = name if global_permissions is not None: self.global_permissions.extend(global_permissions) class Permission(Base): __tablename__ = 'authentication_permission' id = Column(Integer, primary_key=True) name = Column(Unicode(50), unique=True) description = Column(Unicode(2500)) roles = relationship( "Role", uselist=True, secondary="authentication_acl", secondaryjoin="Acl.permission==Role.id") def __init__(self, id=None, name=None, description=None, roles=None): self.id = id self.name = name self.description=description if roles is not None: self.roles.extends(roles) # Upgrade operations go here. Don't create your own engine; bind # migrate_engine to your metadata debug = True session.configure(bind=migrate_engine) migrate_engine.echo=debug metadata.bind = migrate_engine def commit(): session.commit() r_meta.bind.execute ('COMMIT;') metadata.bind.execute('COMMIT;') """ Reload all permissions """ real_perms = Permission.all() for p in real_perms[:]: if not p.name in default_permissions: session.delete(p) session.commit() nreal_perms = [p.name for p in real_perms] for p in default_permissions: if not p in nreal_perms: perm = Permission(name=p, description=default_permissions[p]) session.add(perm) session.commit() """ Reload all ACLS """ for p in default_acls: default_acls try: perm = Permission.by_name(p) except: pass roles = default_acls[p] for role in roles: access = roles[role] orole = Role.by_name(role) if access: if not perm in orole.global_permissions: orole.global_permissions.append(perm) session.add(orole) session.commit() else: if perm in orole.global_permissions: del orole.global_permissions[orole.global_permissions.index(perm)] session.add(orole) session.commit() session.flush() """ Create remote servers table """ r_meta = s.MetaData(migrate_engine, True) Base.metadata.bind = migrate_engine for t in [Server.__table__]: if not t.name in r_meta.tables: t.create(migrate_engine) """Recreate all authentication_acl foreign keys """ fks = [] commit() for md in [Acl]: t = md.__table__ rt = r_meta.tables[t.name] rt_constraints = [a for a in rt.foreign_keys] for cs in deepcopy(t.foreign_keys): if cs.__class__.__name__ == 'ForeignKey': table, column = cs.target_fullname.split('.') target = [r_meta.tables[table].c[column]] parent = [r_meta.tables[cs.parent.table.name].c[cs.parent.name]] fk = ForeignKeyConstraint(columns=parent,refcolumns=target) fk.use_alter = cs.use_alter fk.ondelete = 'CASCADE' fk.onupdate = 'CASCADE' fk.name = cs.name fks.append(fk) if (cs.name in [a.name for a in rt_constraints] or (cs.target_fullname in [a.target_fullname for a in rt_constraints])): fk.drop(migrate_engine) commit() for fk in fks: fk.create(migrate_engine) commit()
def upgrade(migrate_engine): """r_meta must be reflected from the current database whereas metadata is the current constructed metadata for the migration purpose""" # Upgrade operations go here. Don't create your own engine; bind # migrate_engine to your metadata class Acl(Base): __tablename__ = 'authentication_acl' role = Column(Integer, ForeignKey("authentication_role.id", name="fk_acl_role", use_alter=True, ondelete="CASCADE", onupdate="CASCADE"), primary_key=True) permission = Column(Integer, ForeignKey("authentication_permission.id", name="fk_acl_permission", use_alter=True, ondelete="CASCADE", onupdate="CASCADE"), primary_key=True) class Role(Base): __tablename__ = 'authentication_role' id = Column(Integer, primary_key=True) name = Column(Unicode(50), unique=True) description = Column(Unicode(2500)) global_permissions = relationship( "Permission", uselist=True, secondary="authentication_acl", secondaryjoin="Acl.permission==Permission.id") def __init__(self, id=None, name=None, description=None, global_permissions=None): self.id = id self.description = description self.name = name if global_permissions is not None: self.global_permissions.extend(global_permissions) class Project(Base): __tablename__ = 'projects' id = Column(Integer, primary_key=True) name = Column(Unicode(50), unique=True) description = Column(Unicode(255)) directory = Column(Unicode(2550)) user_id = Column(Integer, ) class Permission(Base): __tablename__ = 'authentication_permission' id = Column(Integer, primary_key=True) name = Column(Unicode(50), unique=True) description = Column(Unicode(2500)) roles = relationship( "Role", uselist=True, secondary="authentication_acl", secondaryjoin="Acl.permission==Role.id") def __init__(self, id=None, name=None, description=None, roles=None): self.id = id self.name = name self.description=description if roles is not None: self.roles.extends(roles) class ProjectServer(Base): __tablename__ = 'projects_servers' project_id = Column(Integer, ForeignKey("projects.id", name="fk_projectserver_project", use_alter=True, ondelete="CASCADE", onupdate="CASCADE"), primary_key=True) server_id = Column(Integer, ForeignKey("servers.id", name="fk_projectserver_server", use_alter=True, ondelete="CASCADE", onupdate="CASCADE"), primary_key=True) class Server(Base): __tablename__ = 'servers' id = Column(Integer, primary_key=True) name = Column(Unicode(50), unique=True) url = Column(Unicode(255)) help_mail = Column(Unicode(255)) def __init__(self, name=None, url=None, help_mail=None, ): self.name = name self.url = url self.help_mail = help_mail class Service(Base): __tablename__ = 'services' __table_args__ = ( UniqueConstraint('name', 'server_id', name='unique_server_service'), Index('search_classification', 'classification'), Index('search_package', 'package'), ) id = Column(Integer, primary_key=True) name = Column(Unicode(100), unique=True) server_id = Column(Integer, ForeignKey("servers.id", name="fk_service_server", use_alter=True), nullable=False) package = Column(Unicode(2500), default='not read', nullable=False) classification = Column(Unicode(2500), default='not read', nullable=False) enable = Column(Boolean(), default=True, nullable=False) exportable = Column(Boolean(), default=False, nullable=False) type = Column(Enum('program', 'workflow', 'viewer', name='service_type'), default='program', nullable=False) project_id = Column(Integer, ForeignKey("projects.id", name="fk_service_project", use_alter=True), nullable=False) description = Column(Unicode(2500),) debug = True session.configure(bind=migrate_engine) migrate_engine.echo=debug metadata.bind = migrate_engine Base.metadata.bind = migrate_engine r_meta = s.MetaData(migrate_engine, True) """ Recreate projet_server foreign keys """ for o in [ProjectServer]: r_meta = s.MetaData(migrate_engine, True) t = o.__table__ recreate_table_fkeys(o, session)