MergePolicy

MergePolicy

class save_to_db.core.merge_policy.MergePolicy(db_adapter, policy, defaults=None)[source]

Bases: dict

Merge model policy defines how model conflicts should be resolved when using merge_models() method of AdapterBase class.

There are conflicts of two types:

  • Meging models have x-to-one relationship with other model class, but point to diffent model instances.

    Example:

    • Two merging model instances have many-to-one relationship field x_id pointing to two different model X instances. Merging the model requires them to point to the same instance of X.
  • Meging models have one-to-many relationship with other model class X, but updating related model X instances to point to the merged instance breaks a unique constraint on model X.

    Example:

    • Two merging model instances of class A have one-to-many relationship with model X and point to two different instances of X. Model X has a unique constaint on field a_id pointing to model A, thus making it impossible for two instances of X to point to the same merged instance of A.
Parameters:
  • db_adapter – An instance of AdapterBase subclass.
  • policy

    A dictionary that looks like this:

    merge_policy = {
        OrmModelClass_1: {
            x_to_1_field_1: resolve_x_to_1_field_1,
            x_to_1_field_2: resolve_x_to_1_field_2,
            1_to_many_field_1: resolve_1_to_many_field_1,
            1_to_many_field_2: False,  # merge not allowed
            ...
        },
        ...
    }
    

    Where:

    • OrmModelClass_1 is a reference to an ORM model class.
    • x_to_1_field_1 and x_to_1_field_2 are x-to-one foreign key field names from the class.
    • 1_to_many_field_1 and 1_to_many_field_2 are one-to-may foreign key field names from the class.
    • resolve_x_to_1_field_1 and resolve_x_to_1_field_2 are merge functions for the fields, function arguments:
      • model is model into which other model is merged.
      • merged_model is a model that is in the process of being merged.
      • child_model is a model that model points to with forward_foreign_key.

        Here we refer to the model being pointed to as “child”, although in reality it can be different.

      • merged_child_model is a model that merged_model points to with forward_foreign_key.
      • forward_foreign_key is a name of a parent field that points to child.
      • reverse_foreign_key is a name of a child field that points parent.

      child_model and merged_child_model are not the same, but being pointed to by parent models from models argment using the same key.

      The function must return one child model to be used.

      Note

      merged_child_model is not deleted after the merging, for x-to-one relationships it can be left unchanged.

    • resolve_1_to_many_field_1 is a merge function for the field. Conflicts in one-to-many fields may arise when there is a unique constraint that is going to be broken if two child models start to point to the same parent model. The function accepts arguments the same arguments as resolve_x_to_1_field_1 and a list of unique constaints (lists of field names) that are going to be broken.

      Warning

      merged_child_model is not deleted after the merging. Delete it yourself or make sure it does not brake the unique constraints.

  • defaults

    Default model resolver. A dictionary that looks like this:

    defaults = {
        OrmModelClass_1: {
            'one': resolve_x_to_1_field_1,
            'many': resolve_1_to_many_field_1,
        },
        ...
    }
    

    Where:

    • OrmModelClass_1,
    • resolve_x_to_1_field_1,
    • and ‘resolve_1_to_many_field_1 are same as for the policy parameter.

    If, in the example above, some other ORM model class has a relationship with OrmModelClass_1, but does not define a resolver, default model resolver is used.

REL_MANY = 'many'

Default key for x-to-many resolvers.

REL_ONE = 'one'

Default key for x-to-one resolvers.

add_model_policy(model_cls, model_policy, model_defaults=None)[source]

Adds merge policy for a model.

Parameters:
  • model_cls – An ORM model class to add.
  • model_policy – Merge policy for the class. This has the same structure as policy[model_cls] (where policy is the argument passed to constructor).
  • model_defaults – Default model resolvers for the class. This has the same structure as defaults[model_cls] (where defaults is the argument passed to constructor).