# Path: psqlextra/fields/hstore_field.py
# class HStoreField(DjangoHStoreField):
#     """Improved version of Django's :see:HStoreField that adds support for
#     database-level constraints.
# 
#     Notes:
#         - For the implementation of uniqueness, see the
#           custom database back-end.
#     """
# 
#     def __init__(
#         self,
#         *args,
#         uniqueness: Optional[List[Union[str, Tuple[str, ...]]]] = None,
#         required: Optional[List[str]] = None,
#         **kwargs
#     ):
#         """Initializes a new instance of :see:HStoreField.
# 
#         Arguments:
#             uniqueness:
#                 List of keys to enforce as unique. Use tuples
#                 to enforce multiple keys together to be unique.
# 
#             required:
#                 List of keys that should be enforced as required.
#         """
# 
#         super(HStoreField, self).__init__(*args, **kwargs)
# 
#         self.uniqueness = uniqueness
#         self.required = required
# 
#     def get_prep_value(self, value):
#         """Override the base class so it doesn't cast all values to strings.
# 
#         psqlextra supports expressions in hstore fields, so casting all
#         values to strings is a bad idea.
#         """
# 
#         value = Field.get_prep_value(self, value)
# 
#         if isinstance(value, dict):
#             prep_value = {}
#             for key, val in value.items():
#                 if isinstance(val, Expression):
#                     prep_value[key] = val
#                 elif val is not None:
#                     prep_value[key] = str(val)
#                 else:
#                     prep_value[key] = val
# 
#             value = prep_value
# 
#         if isinstance(value, list):
#             value = [str(item) for item in value]
# 
#         return value
# 
#     def deconstruct(self):
#         """Gets the values to pass to :see:__init__ when re-creating this
#         object."""
# 
#         name, path, args, kwargs = super(HStoreField, self).deconstruct()
# 
#         if self.uniqueness is not None:
#             kwargs["uniqueness"] = self.uniqueness
# 
#         if self.required is not None:
#             kwargs["required"] = self.required
# 
#         return name, path, args, kwargs


from psqlextra.fields import HStoreField

        # create new indexes for keys that have been added
        for keys in new_uniqueness:
            if keys not in old_uniqueness:
                self._create_hstore_unique(
                    model, new_field, self._compose_keys(keys)
                )

    def _create_hstore_unique(self, model, field, keys):
        """Creates a UNIQUE constraint for the specified hstore keys."""

        name = self._unique_constraint_name(model._meta.db_table, field, keys)
        columns = ["(%s->'%s')" % (field.column, key) for key in keys]
        sql = self.sql_hstore_unique_create.format(
            name=self.quote_name(name),
            table=self.quote_name(model._meta.db_table),
            columns=",".join(columns),
        )
        self.execute(sql)

    def _rename_hstore_unique(
        self, old_table_name, new_table_name, old_field, new_field, keys
    ):
        """Renames an existing UNIQUE constraint for the specified hstore
        keys."""

        old_name = self._unique_constraint_name(old_table_name, old_field, keys)
        new_name = self._unique_constraint_name(new_table_name, new_field, keys)

        sql = self.sql_hstore_unique_rename.format(

# Path: psqlextra/fields/hstore_field.py
# class HStoreField(DjangoHStoreField):
#     """Improved version of Django's :see:HStoreField that adds support for
#     database-level constraints.
# 
#     Notes:
#         - For the implementation of uniqueness, see the
#           custom database back-end.
#     """
# 
#     def __init__(
#         self,
#         *args,
#         uniqueness: Optional[List[Union[str, Tuple[str, ...]]]] = None,
#         required: Optional[List[str]] = None,
#         **kwargs
#     ):
#         """Initializes a new instance of :see:HStoreField.
# 
#         Arguments:
#             uniqueness:
#                 List of keys to enforce as unique. Use tuples
#                 to enforce multiple keys together to be unique.
# 
#             required:
#                 List of keys that should be enforced as required.
#         """
# 
#         super(HStoreField, self).__init__(*args, **kwargs)
# 
#         self.uniqueness = uniqueness
#         self.required = required
# 
#     def get_prep_value(self, value):
#         """Override the base class so it doesn't cast all values to strings.
# 
#         psqlextra supports expressions in hstore fields, so casting all
#         values to strings is a bad idea.
#         """
# 
#         value = Field.get_prep_value(self, value)
# 
#         if isinstance(value, dict):
#             prep_value = {}
#             for key, val in value.items():
#                 if isinstance(val, Expression):
#                     prep_value[key] = val
#                 elif val is not None:
#                     prep_value[key] = str(val)
#                 else:
#                     prep_value[key] = val
# 
#             value = prep_value
# 
#         if isinstance(value, list):
#             value = [str(item) for item in value]
# 
#         return value
# 
#     def deconstruct(self):
#         """Gets the values to pass to :see:__init__ when re-creating this
#         object."""
# 
#         name, path, args, kwargs = super(HStoreField, self).deconstruct()
# 
#         if self.uniqueness is not None:
#             kwargs["uniqueness"] = self.uniqueness
# 
#         if self.required is not None:
#             kwargs["required"] = self.required
# 
#         return name, path, args, kwargs


# Path: psqlextra/backend/side_effects/hstore_unique.py
from psqlextra.fields import HStoreField


        # create new indexes for keys that have been added
        for keys in new_uniqueness:
            if keys not in old_uniqueness:
                self._create_hstore_unique(
                    model, new_field, self._compose_keys(keys)
                )

    def _create_hstore_unique(self, model, field, keys):
        """Creates a UNIQUE constraint for the specified hstore keys."""

        name = self._unique_constraint_name(model._meta.db_table, field, keys)
        columns = ["(%s->'%s')" % (field.column, key) for key in keys]
        sql = self.sql_hstore_unique_create.format(
            name=self.quote_name(name),
            table=self.quote_name(model._meta.db_table),
            columns=",".join(columns),
        )
        self.execute(sql)

    def _rename_hstore_unique(
        self, old_table_name, new_table_name, old_field, new_field, keys
    ):
        """Renames an existing UNIQUE constraint for the specified hstore
        keys."""

        old_name = self._unique_constraint_name(old_table_name, old_field, keys)
        new_name = self._unique_constraint_name(new_table_name, new_field, keys)

        sql = self.sql_hstore_unique_rename.format(

            old_name=self.quote_name(old_name),