def upgrade(migrate_engine): metadata = sa.MetaData() metadata.bind = migrate_engine builders = sautils.Table('builders', metadata, autoload=True) masters = sautils.Table('masters', metadata, autoload=True) workers = sautils.Table('workers', metadata, autoload=True) builder_masters = sautils.Table('builder_masters', metadata, autoload=True) configured_workers = sautils.Table('configured_workers', metadata, autoload=True) fks_to_change = [] # we need to parse the reflected model in order to find the automatic fk name that was put # mysql and pgsql have different naming convention so this is not very easy to have generic code working. for table, keys in [(builder_masters, (builders.c.id, masters.c.id)), (configured_workers, (builder_masters.c.id, workers.c.id))]: for fk in table.constraints: if not isinstance(fk, sa.ForeignKeyConstraint): continue for c in fk.elements: if c.column in keys: # migrate.xx.ForeignKeyConstraint is changing the model so initializing here # would break the iteration (Set changed size during iteration) fks_to_change.append(( table, (fk.columns, [c.column]), dict(name=fk.name, ondelete='CASCADE'))) for table, args, kwargs in fks_to_change: fk = ForeignKeyConstraint(*args, **kwargs) table.append_constraint(fk) try: fk.drop() except NotSupportedError: pass # some versions of sqlite do not support drop, but will still update the fk fk.create()
def downgrade(migrate_engine): # Operations to reverse the above upgrade go here. meta = MetaData(bind=migrate_engine) mt = Table('meas_MeasurementTable', meta, autoload=True) et = Table('meas_ExtractionTable', meta, autoload=True) st = Table('meas_ScriptTable', meta, autoload=True) fk = ForeignKeyConstraint([mt.c.script_id], [st.c.id]) fk.drop() fk = ForeignKeyConstraint([et.c.script_id], [st.c.id]) fk.drop() st.drop() c = Column('script_blob', BLOB) c.create(mt) c = Column('hash', String(32)) c.create(mt) c = Column('script_name', String(80)) c.create(mt) # # c = Column('script_blob', BLOB) c.create(et) c = Column('hash', String(32)) c.create(et) c = Column('script_name', BLOB) c.create(et) mt.c.script_id.drop() et.c.script_id.drop()
def drop_constraints_and_alter_types(): # 1 drop all foreign key constraints dropped_fk_constraints = [] revision_table = Table('revision', metadata, autoload=True) foreign_tables = ['package', 'package_tag', 'package_revision', 'package_tag_revision', 'package_extra', 'package_extra_revision', ] for table_name in foreign_tables: table = Table(table_name, metadata, autoload=True) for constraint in table.constraints.copy(): if isinstance(constraint, sqlalchemy.schema.ForeignKeyConstraint): foreign_key_cols = [key.column for key in constraint.elements] fk_col = foreign_key_cols[0] if fk_col.table == revision_table: orig_fk = ForeignKeyConstraint(constraint.columns, foreign_key_cols, name=constraint.name, table=table) orig_fk.drop() dropped_fk_constraints.append((constraint.columns, foreign_key_cols, constraint.name, table.name)) # 2 alter type of revision id and foreign keys id_col = constraint.table.columns[constraint.columns[0]] id_col.alter(type=UnicodeText) revision_table = Table('revision', metadata, autoload=True) id_col = revision_table.c['id'] id_col.alter(type=UnicodeText, ) return dropped_fk_constraints
def drop_constraints_and_alter_types(primary_table_name, foreign_tables, revision_table_name): # 1 drop all foreign key constraints dropped_fk_constraints = [] primary_table = Table(primary_table_name, metadata, autoload=True) for table_name in foreign_tables: table = Table(table_name, metadata, autoload=True) for constraint in table.constraints.copy(): if isinstance(constraint, sqlalchemy.schema.ForeignKeyConstraint): foreign_key_cols = [key.column for key in constraint.elements] fk_col = foreign_key_cols[0] if fk_col.table == primary_table: orig_fk = ForeignKeyConstraint(constraint.columns, foreign_key_cols, name=constraint.name, table=table) orig_fk.drop() dropped_fk_constraints.append((constraint.columns, foreign_key_cols, constraint.name, table.name)) #print 'CON', dropped_fk_constraints[-1] # 2 alter type of primary table id and foreign keys id_col = constraint.table.columns[constraint.columns[0]] id_col.alter(type=UnicodeText) primary_table = Table(primary_table_name, metadata, autoload=True) id_col = primary_table.c['id'] id_col.alter(type=UnicodeText) if revision_table_name: # Revision table id column type changed as well revision_table = Table(revision_table_name, metadata, autoload=True) id_col = revision_table.c['id'] id_col.alter(type=UnicodeText) return dropped_fk_constraints
def upgrade(migrate_engine): # Upgrade operations go here. Don't create your own engine; bind # migrate_engine to your metadata debug = False migrate_engine.echo=debug meta.bind = migrate_engine real_meta = MetaData() real_meta.bind = migrate_engine real_meta.reflect() rt = real_meta.tables['authentication_userrole'] for ctraint in deepcopy(rt.foreign_keys): if 'fk_userrole_user' in ctraint.name: column = ctraint.column parent = ctraint.parent fk = ForeignKeyConstraint([parent], [column], **{'table': rt}) fk.name = ctraint.name fk.drop() fkp = [a for a in UserRole.__table__.foreign_keys if a.name == 'fk_userrole_users'][0] fk = ForeignKeyConstraint([fkp.parent], [fkp.column], **{'table': fkp.parent.table}) fk.name = fkp.name fk.use_alter = fkp.use_alter fk.ondelete = fkp.ondelete fk.onupdate = fkp.onupdate fk.create() new_meta = MetaData(bind=migrate_engine) new_meta.reflect()
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 downgrade(migrate_engine): meta = MetaData(bind=migrate_engine) pci_devices = Table("pci_devices", meta, autoload=True) compute_nodes = Table("compute_nodes", meta, autoload=True) fkey = ForeignKeyConstraint(columns=[pci_devices.c.compute_node_id], refcolumns=[compute_nodes.c.id]) fkey.drop()
def downgrade(migrate_engine): meta.bind = migrate_engine user = Table("user", meta, autoload=True) site = Table("site", meta, autoload=True) fk_site = ForeignKeyConstraint([user.c.site_id], [site.c.id]) fk_site.drop()
def downgrade(migrate_engine): meta = MetaData(bind=migrate_engine) pci_devices = Table('pci_devices', meta, autoload=True) compute_nodes = Table('compute_nodes', meta, autoload=True) fkey = ForeignKeyConstraint(columns=[pci_devices.c.compute_node_id], refcolumns=[compute_nodes.c.id]) fkey.drop()
def downgrade(migrate_engine): meta.bind = migrate_engine account = Table('account', meta, autoload=True) cons = ForeignKeyConstraint([account.c.rooms], [room.c.id]) account = Table('account', meta, autoload=True) cons.drop() account.c.rooms.drop() room.drop()
def drop_foreign_key_constraints(constraint_names, columns, ref_columns): """Drop the foreign key constraints that match the given criteria. :param constraint_names: List of foreign key constraint names :param columns: List of the foreign key columns. :param ref_columns: List of the referenced columns. """ for constraint_name in constraint_names: fkey_constraint = ForeignKeyConstraint(columns=columns, refcolumns=ref_columns, name=constraint_name) fkey_constraint.drop()
def upgrade(migrate_engine): # Upgrade operations go here. Don't create your own engine; bind # migrate_engine to your metadata debug = False migrate_engine.echo=debug meta.bind = migrate_engine real_meta = MetaData() real_meta.bind = migrate_engine real_meta.reflect() # finally i decided to go to a separate permission table if 'authentication_permission' not in real_meta.tables: permission.create() for acl, item in ((aclusers, 'users'), (aclprojects, 'projects')): rt = real_meta.tables[acl.name] for ctraint in deepcopy(rt.foreign_keys): if ('perm' in ctraint.name) or ('perm' in ctraint.parent.name): column = ctraint.column parent = ctraint.parent fk = ForeignKeyConstraint([parent], [column], **{'table': rt}) fk.name = ctraint.name fk.drop() if 'permission' in rt.c: if len(rt.c["permission"].foreign_keys) > 0: rt.c["permission"].drop() if 'permission' in rt.c: ctype = rt.c['permission'].type.__class__.__name__ drop = False if 'CHAR' in ctype: drop = True if 'INTEGER' in ctype: drop = True if drop: rt.c["permission"].drop() if not ('permission' in rt.c): acl.c["permission"].create() # refresh metA fkp = {"users":ForeignKey("authentication_permission.id", name="fk_userssacl_permission", use_alter=True, ondelete="CASCADE", onupdate="CASCADE"), "projects":ForeignKey("authentication_permission.id", name="fk_projectsacl_permission", use_alter=True, ondelete="CASCADE", onupdate="CASCADE"), }.get(item) fk = ForeignKeyConstraint([acl.c.permission], [permission.c.id], **{'table': acl}) fk.name = fkp.name fk.use_alter = fkp.use_alter fk.ondelete = fkp.ondelete fk.onupdate = fkp.onupdate fk.create() new_meta = MetaData(bind=migrate_engine) new_meta.reflect() nt = new_meta.tables[acl.name] columns = [] if 'project' in item: columns.append(nt.c['rid']) columns.extend([nt.c['role'], nt.c['permission']]) pk = PrimaryKeyConstraint(*columns) pk.create()
def upgrade(migrate_engine): metadata = sa.MetaData() metadata.bind = migrate_engine table_names = set(TABLES_FKEYS_SET_NULL.keys()) table_names.update(TABLES_COLUMNS_NOT_NULL.keys()) tables = {} for t in table_names: tables[t] = sautils.Table(t, metadata, autoload=True) fks_to_change = [] # We need to parse the reflected model in order to find the automatic # fk name that was put. # Mysql and postgres have different naming convention so this is not very # easy to have generic code working. for t, keys in TABLES_FKEYS_SET_NULL.items(): table = tables[t] for fk in table.constraints: if not isinstance(fk, sa.ForeignKeyConstraint): continue for c in fk.elements: if str(c.column) in keys: # migrate.xx.ForeignKeyConstraint is changing the model # so initializing here would break the iteration # (Set changed size during iteration) fks_to_change.append((table, (fk.columns, [c.column]), dict(name=fk.name, ondelete='SET NULL'))) for table, args, kwargs in fks_to_change: fk = ForeignKeyConstraint(*args, **kwargs) table.append_constraint(fk) try: fk.drop() except NotSupportedError: # some versions of sqlite do not support drop, # but will still update the fk pass fk.create() for t, cols in TABLES_COLUMNS_NOT_NULL.items(): table = tables[t] if table.dialect_options.get('mysql', {}).get('engine') == 'InnoDB': migrate_engine.execute('SET FOREIGN_KEY_CHECKS = 0;') try: for c in table.columns: if c.name in cols: c.alter(nullable=False) finally: if table.dialect_options.get('mysql', {}).get('engine') == 'InnoDB': migrate_engine.execute('SET FOREIGN_KEY_CHECKS = 1;')
def downgrade(migrate_engine): meta.bind = migrate_engine roles_users = Table("roles_users", meta, autoload=True) role = Table("role", meta, autoload=True) user = Table("user", meta, autoload=True) fk_role = ForeignKeyConstraint([roles_users.c.role_id], [role.c.id]) fk_role.drop() fk_user = ForeignKeyConstraint([roles_users.c.user_id], [user.c.id]) fk_user.drop()
def downgrade(migrate_engine): meta.bind = migrate_engine upload = Table("upload", meta, autoload=True) study = Table("study", meta, autoload=True) user = Table("user", meta, autoload=True) fk_study = ForeignKeyConstraint([upload.c.study_id], [study.c.id]) fk_study.drop() fk_user = ForeignKeyConstraint([upload.c.uploader_id], [user.c.id]) fk_user.drop()
def upgrade(migrate_engine): metadata = sa.MetaData() metadata.bind = migrate_engine table_names = set(TABLES_FKEYS_SET_NULL.keys()) table_names.update(TABLES_COLUMNS_NOT_NULL.keys()) tables = {} for t in table_names: tables[t] = sautils.Table(t, metadata, autoload=True) fks_to_change = [] # We need to parse the reflected model in order to find the automatic # fk name that was put. # Mysql and postgres have different naming convention so this is not very # easy to have generic code working. for t, keys in TABLES_FKEYS_SET_NULL.items(): table = tables[t] for fk in table.constraints: if not isinstance(fk, sa.ForeignKeyConstraint): continue for c in fk.elements: if str(c.column) in keys: # migrate.xx.ForeignKeyConstraint is changing the model # so initializing here would break the iteration # (Set changed size during iteration) fks_to_change.append(( table, (fk.columns, [c.column]), dict(name=fk.name, ondelete='SET NULL'))) for table, args, kwargs in fks_to_change: fk = ForeignKeyConstraint(*args, **kwargs) table.append_constraint(fk) try: fk.drop() except NotSupportedError: # some versions of sqlite do not support drop, # but will still update the fk pass fk.create() for t, cols in TABLES_COLUMNS_NOT_NULL.items(): table = tables[t] if table.dialect_options.get('mysql', {}).get('engine') == 'InnoDB': migrate_engine.execute('SET FOREIGN_KEY_CHECKS = 0;') try: for c in table.columns: if c.name in cols: c.alter(nullable=False) finally: if table.dialect_options.get('mysql', {}).get('engine') == 'InnoDB': migrate_engine.execute('SET FOREIGN_KEY_CHECKS = 1;')
def downgrade(migrate_engine): meta.bind = migrate_engine studies_collaborators = Table("studies_collaborators", meta, autoload=True) study = Table("study", meta, autoload=True) user = Table("user", meta, autoload=True) fk_study = ForeignKeyConstraint([studies_collaborators.c.study_id], [study.c.id]) fk_study.drop() fk_user = ForeignKeyConstraint([studies_collaborators.c.user_id], [user.c.id]) fk_user.drop()
def downgrade(migrate_engine): # Operations to reverse the above upgrade go here. meta = MetaData(bind=migrate_engine) tt = Table('meas_IsotopeTable', meta, autoload=True) t = Table('proc_FitTable', meta, autoload=True) cons = ForeignKeyConstraint([t.c.isotope_id], [tt.c.id]) cons.drop() try: t.c.isotope_id.drop() except: pass
def downgrade(migrate_engine): # Operations to reverse the above upgrade go here. meta.bind = migrate_engine puppy = Table('puppy', meta, autoload=True) adoptors = Table('adoptors', meta, autoload=True) puppy_adoptors = Table('puppy_adoptors', meta, autoload=True) cons1 = ForeignKeyConstraint([puppy_adoptors.c.puppy_id], [puppy.c.id]) cons1.drop() cons2 = ForeignKeyConstraint([puppy_adoptors.c.adoptor_id], [adoptors.c.id]) cons2.drop() puppy_adoptors.drop() pass
def downgrade(migrate_engine): # Operations to reverse the above upgrade go here. meta.bind = migrate_engine shelter = Table('shelter', meta, autoload=True) puppy = Table('puppy', meta, autoload=True) shelter_puppies = Table('shelter_puppies', meta, autoload=True) cons1 = ForeignKeyConstraint([shelter_puppies.c.shelter_id], [shelter.c.id]) cons1.drop() cons2 = ForeignKeyConstraint([shelter_puppies.c.puppy_id], [puppy.c.id]) cons2.drop() shelter_puppies.drop() pass
def downgrade(migrate_engine): # Operations to reverse the above upgrade go here. meta = MetaData(bind=migrate_engine) mt = Table('meas_MeasurementTable', meta, autoload=True) sp = Table('meas_SpectrometerParametersTable', meta, autoload=True) c = Column('measurement_id', Integer) c.create(sp) fk = ForeignKeyConstraint([sp.c.measurement_id], [mt.c.id]) fk.create() fk = ForeignKeyConstraint([mt.c.spectrometer_parameters_id], [sp.c.id]) fk.drop() mt.c.spectrometer_parameters_id.drop() sp.c.hash.drop()
def downgrade(migrate_engine): meta.bind = migrate_engine domains_table = Table('domains', meta, autoload=True) domainmetadata_table = Table('domainmetadata', meta, autoload=True) records_table = Table('records', meta, autoload=True) records_fk = ForeignKeyConstraint([records_table.c.domain_id], [domains_table.c.id], ondelete="CASCADE") records_fk.drop() domainmetadata_fk = ForeignKeyConstraint( [domainmetadata_table.c.domain_id], [domains_table.c.id], ondelete="CASCADE") domainmetadata_fk.drop()
def downgrade(migrate_engine): # Operations to reverse the above upgrade go here. meta.bind = migrate_engine mst = Table('gen_MassSpectrometerTable', meta, autoload=True) tt = Table('meas_ExtractionTable', meta, autoload=True) cons = ForeignKeyConstraint([t.c.mass_spectrometer_id], [mst.c.id]) cons.drop() cons = ForeignKeyConstraint([tt.c.sensitivity_id], [t.c.id]) cons.drop() tt.c.sensitivity_multiplier.drop() tt.c.sensitivity_id.drop() t.drop()
def upgrade(migrate_engine): # Upgrade operations go here. Don't create your own engine; bind # migrate_engine to your metadata meta = MetaData(bind=migrate_engine) mt = Table('meas_MeasurementTable', meta, autoload=True) c = Column('spectrometer_parameters_id', Integer) c.create(mt) sp = Table('meas_SpectrometerParametersTable', meta, autoload=True) fk = ForeignKeyConstraint([mt.c.spectrometer_parameters_id], [sp.c.id]) fk.create() fk = ForeignKeyConstraint([sp.c.measurement_id], [mt.c.id]) fk.drop() sp.c.measurement_id.drop() c = Column('hash', String(32)) c.create(sp)
def downgrade(migrate_engine): meta.bind = migrate_engine domains_table = Table('domains', meta, autoload=True) domainmetadata_table = Table('domainmetadata', meta, autoload=True) records_table = Table('records', meta, autoload=True) records_fk = ForeignKeyConstraint( [records_table.c.domain_id], [domains_table.c.id], ondelete="CASCADE") records_fk.drop() domainmetadata_fk = ForeignKeyConstraint( [domainmetadata_table.c.domain_id], [domains_table.c.id], ondelete="CASCADE") domainmetadata_fk.drop()
def drop_foreign_key(fk_def): table = fk_def[0]._get_table() col = fk_def[0] ref_col = fk_def[1] # Use .copy() to avoid the set changing during the for operation for fk in table.foreign_keys.copy(): # Check if the fk is the one we want if fk.column == col and fk.parent == ref_col: fkc = ForeignKeyConstraint([fk.column], [fk.parent], name=fk.constraint.name) fkc.drop() # Check if the fk is the one we want (sometimes it seems the parent # / col is switched if fk.parent == col and fk.column == ref_col: fkc = ForeignKeyConstraint([fk.parent], [fk.column], name=fk.constraint.name) fkc.drop()
def downgrade(migrate_engine): meta.bind = migrate_engine pool_attributes_table = Table('pool_attributes', meta, autoload=True) # pools = Table('pools', meta, autoload=True) constraint = UniqueConstraint('pool_id', 'key', 'value', name='unique_pool_attribute', table=pool_attributes_table) fk_constraint = ForeignKeyConstraint(columns=['pool_id'], refcolumns=['pools.id'], ondelete='CASCADE', table=pool_attributes_table) # First have to drop the ForeignKeyConstraint fk_constraint.drop() # Then drop the UniqueConstraint constraint.drop() # Then recreate the ForeignKeyConstraint fk_constraint.create()
def downgrade(migrate_engine): metadata.bind = migrate_engine metadata.bind.echo = True setup_all() try: # constraints = ExtraData.table.constraints # for c in constraints: # if 'partnerLink_id' in c.columns: fkc = ForeignKeyConstraint([ExtraData.table.c.partnerLink_id], [PartnerLink.table.c.id]) fkc.drop() indexes = ExtraData.table.indexes for l in indexes: if l.name == "ix_extradata_partnerLink_id": l.drop() except: print traceback.format_exc() try: col = ExtraData.table.columns.get('partnerLink_id') col.drop(ExtraData.table) except Exception, e: print str(e) print traceback.format_exc()
def recreate_table_fkeys(table, session,): """Recreate all foreign keys in the table or declarative object""" if not isinstance(table, Table): table = table.__table__ migrate_engine = session.bind metadata = table.metadata r_meta = s.MetaData(migrate_engine, True) def commit(): session.commit() r_meta.bind.execute ('COMMIT;') metadata.bind.execute('COMMIT;') fks = [] commit() t = 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_table = r_meta.tables[cs.parent.table.name] parent = [parent_table.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])): try: fk.drop(migrate_engine) commit() except:pass for fk in fks: fk.create(migrate_engine) commit()
def upgrade(migrate_engine): metadata = sa.MetaData() metadata.bind = migrate_engine tables = {} for t in TABLES_FKEYS: tables[t] = sautils.Table(t, metadata, autoload=True) fks_to_change = [] # We need to parse the reflected model in order to find the automatic # fk name that was put. # Mysql and postgres have different naming convention so this is not very # easy to have generic code working. for t, keys in iteritems(TABLES_FKEYS): table = tables[t] for fk in table.constraints: if not isinstance(fk, sa.ForeignKeyConstraint): continue for c in fk.elements: if str(c.column) in keys: # migrate.xx.ForeignKeyConstraint is changing the model # so initializing here would break the iteration # (Set changed size during iteration) fks_to_change.append(( table, (fk.columns, [c.column]), dict(name=fk.name, ondelete='CASCADE'))) for table, args, kwargs in fks_to_change: fk = ForeignKeyConstraint(*args, **kwargs) table.append_constraint(fk) try: fk.drop() except NotSupportedError: # some versions of sqlite do not support drop, # but will still update the fk pass fk.create()
def downgrade(migrate_engine): # Operations to reverse the above upgrade go here. meta.bind = migrate_engine tt = Table('gen_ProjectTable', meta, autoload=True) cons = ForeignKeyConstraint([t.c.project_id], [tt.c.id]) cons.drop() tt2 = Table('meas_AnalysisTable', meta, autoload=True) cons = ForeignKeyConstraint([t2.c.analysis_id], [tt2.c.id]) cons.drop() cons = ForeignKeyConstraint([t2.c.figure_id], [tt2.c.id]) cons.drop() t.drop() t2.drop()
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()