Migrate 'flow_collection' field to 'entity_type'.
480624
def migrate_flow_collection(apps, schema_editor):
    """"""
    Process = apps.get_model('flow', 'Process')
    DescriptorSchema = apps.get_model('flow', 'DescriptorSchema')

    for process in Process.objects.all():
        process.entity_type = process.flow_collection
        process.entity_descriptor_schema = process.flow_collection

        if (process.entity_descriptor_schema is not None and
                not DescriptorSchema.objects.filter(slug=process.entity_descriptor_schema).exists()):
            raise LookupError(
                "Descriptow schema '{}' referenced in 'entity_descriptor_schema' not "
                "found.".format(process.entity_descriptor_schema)
            )

        process.save()
1178353
def as_flow(cls, obj):
        """"""
        if isinstance(obj, cls): return obj
        if is_string(obj):
            return cls.pickle_load(obj)
        elif isinstance(obj, collections.Mapping):
            return cls.from_dict(obj)
        else:
            raise TypeError("Don't know how to convert type %s into a Flow" % type(obj))
1107714
def to_yaml(self):
        """"""
        import yaml

        with self.__reference__.open('w') as fp:
            yaml.dump(self.asjsonld(), fp, default_flow_style=False)
390711
def _MigrateArtifact(artifact):
  """"""
  name = Text(artifact.name)

  try:
    logging.info("Migating %s", name)
    data_store.REL_DB.WriteArtifact(artifact)
    logging.info("  Wrote %s", name)
  except db.DuplicatedArtifactError:
    logging.info("  Skipped %s, because artifact already exists.", name)
1263
def run_migration(data, version_start, version_end):
    """"""
    items = []
    if version_start == 1 and version_end == 2:
        for item in data['accounts']:
            items.append(v2.upgrade(item))

    if version_start == 2 and version_end == 1:
        for item in data:
            items.append(v2.downgrade(item))
        items = {'accounts': items}
    return items
154268
def _migrate_single(self, conn, migration):
        """"""
        with contextlib.ExitStack() as stack:
            for wrapper in self._wrappers:
                stack.enter_context(wrapper(conn))
            migration.func(conn)
1003494
def migrateFileFields(portal):
    
    portal_types = [
        "Attachment",
        "ARImport",
        "Instrument",
        "InstrumentCertification",
        "Method",
        "Multifile",
        "Report",
        "ARReport",
        "SamplePoint"]

    for portal_type in portal_types:
        # Do the migration
        migrate_to_blob(
            portal,
            portal_type=portal_type,
            remove_old_value=True)
1346120
def MigrateArtifacts():
  """"""

  # First, delete all existing artifacts in REL_DB.
  artifacts = data_store.REL_DB.ReadAllArtifacts()
  if artifacts:
    logging.info("Deleting %d artifacts from REL_DB.", len(artifacts))
    for artifact in data_store.REL_DB.ReadAllArtifacts():
      data_store.REL_DB.DeleteArtifact(Text(artifact.name))
  else:
    logging.info("No artifacts found in REL_DB.")

  artifacts = artifact_registry.REGISTRY.GetArtifacts(
      reload_datastore_artifacts=True)

  logging.info("Found %d artifacts in AFF4.", len(artifacts))

  # Only migrate user-created artifacts.
  artifacts = list(filter(_IsCustom, artifacts))

  logging.info("Migrating %d user-created artifacts.", len(artifacts))

  for artifact in artifacts:
    _MigrateArtifact(artifact)
475978
def create_entity(project_id, entity_type_id, entity_value, synonyms):
    """"""
    import dialogflow_v2 as dialogflow
    entity_types_client = dialogflow.EntityTypesClient()

    # Note: synonyms must be exactly [entity_value] if the
    # entity_type's kind is KIND_LIST
    synonyms = synonyms or [entity_value]

    entity_type_path = entity_types_client.entity_type_path(
        project_id, entity_type_id)

    entity = dialogflow.types.EntityType.Entity()
    entity.value = entity_value
    entity.synonyms.extend(synonyms)

    response = entity_types_client.batch_create_entities(
        entity_type_path, [entity])

    print('Entity created: {}'.format(response))
731470
def from_es(self, hit):
        """"""
        doc = hit.copy()
        klass = shallow_class_factory(self.model)

        # We can pass in the entire source, except when we have a non-indexable many-to-many
        for field in self.model._meta.get_fields():

            if not field.auto_created and field.many_to_many:
                if not issubclass(field.rel.to, Indexable):
                    if field.name in doc["_source"]:
                        del doc["_source"][field.name]

            # if field.one_to_many:
            #     if field.name in doc["_source"]:
            #         del doc["_source"][field.name]

        # Now let's go ahead and parse all the fields
        fields = self.mapping.properties.properties
        for key in fields:
            # TODO: What if we've mapped the property to a different name? Will we allow that?
            field = fields[key]

            # if isinstance(field, InnerObject):
            #     import pdb; pdb.set_trace()
            #     continue

            if doc["_source"].get(key):
                attribute_value = doc["_source"][key]

                doc["_source"][key] = field.to_python(attribute_value)

        return klass(**doc["_source"])
230827
def update_dependency_kinds(apps, schema_editor):
    """"""
    DataDependency = apps.get_model('flow', 'DataDependency')
    for dependency in DataDependency.objects.all():
        # Assume dependency is of subprocess kind.
        dependency.kind = 'subprocess'

        # Check child inputs to determine if this is an IO dependency.
        child = dependency.child
        parent = dependency.parent

        for field_schema, fields in iterate_fields(child.input, child.process.input_schema):
            name = field_schema['name']
            value = fields[name]

            if field_schema.get('type', '').startswith('data:'):
                if value == parent.pk:
                    dependency.kind = 'io'
                    break
            elif field_schema.get('type', '').startswith('list:data:'):
                for data in value:
                    if value == parent.pk:
                        dependency.kind = 'io'
                        break

        dependency.save()
147220
def move_to_collection(self, request, *args, **kwargs):
        """"""
        ids = self.get_ids(request.data)
        src_collection_id = self.get_id(request.data, 'source_collection')
        dst_collection_id = self.get_id(request.data, 'destination_collection')

        src_collection = self._get_collection_for_user(src_collection_id, request.user)
        dst_collection = self._get_collection_for_user(dst_collection_id, request.user)

        entity_qs = self._get_entities(request.user, ids)
        entity_qs.move_to_collection(src_collection, dst_collection)

        return Response()
1363911
def update_templatetype(templatetype,**kwargs):
    

    tmpltype_i = db.DBSession.query(TemplateType).filter(TemplateType.id == templatetype.id).one()

    _update_templatetype(templatetype, tmpltype_i)

    db.DBSession.flush()

    return tmpltype_i
491855
def delete_session_entity_type(project_id, session_id,
                               entity_type_display_name):
    """"""
    import dialogflow_v2 as dialogflow
    session_entity_types_client = dialogflow.SessionEntityTypesClient()

    session_entity_type_name = (
        session_entity_types_client.session_entity_type_path(
            project_id, session_id, entity_type_display_name))

    session_entity_types_client.delete_session_entity_type(
        session_entity_type_name)
560887
def replace(
        self,
        name=_void,
        kind=_void,
        annotation=_void,
        default=_void,
        _partial_kwarg=_void,
    ):
        """"""

        if name is _void:
            name = self._name

        if kind is _void:
            kind = self._kind

        if annotation is _void:
            annotation = self._annotation

        if default is _void:
            default = self._default

        if _partial_kwarg is _void:
            _partial_kwarg = self._partial_kwarg

        return type(self)(
            name,
            kind,
            default=default,
            annotation=annotation,
            _partial_kwarg=_partial_kwarg,
        )
255759
def migrate(connection, dsn):
    
    all_migrations = _get_all_migrations()
    logger.debug('Collected migrations: {}'.format(all_migrations))

    for version, modname in all_migrations:
        if _is_missed(connection, version) and version <= SCHEMA_VERSION:
            logger.info('Missed migration: {} migration is missed. Migrating...'.format(version))
            module = __import__(modname, fromlist='dummy')

            # run each migration under its own transaction. This allows us to apply valid migrations
            # and break on invalid.
            trans = connection.begin()
            try:
                module.Migration().migrate(connection)
                _update_version(connection, version)
                trans.commit()
            except:
                trans.rollback()
                logger.error("Failed to migrate '{}'  on {} ".format(version, dsn))
                raise
302163
def migrate(pool, from_connection, to_connection):
    
    f = connect_database(from_connection)
    t = connect_database(to_connection)

    if isinstance(f, ProjectDB):
        for each in f.get_all():
            each = unicode_obj(each)
            logging.info("projectdb: %s", each['name'])
            t.drop(each['name'])
            t.insert(each['name'], each)
    elif isinstance(f, TaskDB):
        pool = Pool(pool)
        pool.map(
            lambda x, f=from_connection, t=to_connection: taskdb_migrating(x, f, t),
            f.projects)
    elif isinstance(f, ResultDB):
        pool = Pool(pool)
        pool.map(
            lambda x, f=from_connection, t=to_connection: resultdb_migrating(x, f, t),
            f.projects)