def _calculate_diff()

in core/lib/sqlparse/diff.py [0:0]


    def _calculate_diff(self):
        diffs = {
            "removed": [],
            "added": [],
            # Customized messages
            "msgs": [],
            # Any attributes that were modified
            "attrs_modified": [],
        }
        # We are copying here since we want to change the col list.
        # Shallow copy should be enough here
        col_left_copy = copy.copy(self.left.column_list)
        col_right_copy = copy.copy(self.right.column_list)
        col_left_dict = {col.name: col for col in col_left_copy}
        col_right_dict = {col.name: col for col in col_right_copy}
        for col in self.get_dropped_cols():
            diffs["removed"].append(col)
            col_left_copy.remove(col)

        for col in self.get_added_cols():
            diffs["added"].append(col)
            col_right_copy.remove(col)

        column_changes = []
        for col in set(col_left_dict.keys()) & set(col_right_dict.keys()):
            if col_left_dict[col] != col_right_dict[col]:
                column_changes.append(
                    f"Previous column: {col_left_dict[col]}, "
                    f"desired column: {col_right_dict[col]}"
                )
        if column_changes:
            diffs["msgs"].append("Column attrs changes detected:")
            diffs["msgs"].append("\n".join(column_changes))

        # Two tables have different col order
        if sorted(col_left_copy, key=lambda col: col.name) == sorted(
            col_right_copy, key=lambda col: col.name
        ):
            old_order = []
            new_order = []
            for col1, col2 in zip(col_left_copy, col_right_copy):
                if col1 != col2:
                    old_order.append(col1.name)
                    new_order.append(col2.name)
            if old_order:
                diffs["msgs"].append("Column order mismatch was detected:")
                diffs["msgs"].append("- " + ", ".join(old_order))
                diffs["msgs"].append("+ " + ", ".join(new_order))

        for idx in self.left.indexes:
            if idx not in self.right.indexes:
                diffs["removed"].append(idx)
        for idx in self.right.indexes:
            if idx not in self.left.indexes:
                diffs["added"].append(idx)

        if self.left.primary_key != self.right.primary_key:
            if self.left.primary_key.column_list:
                diffs["removed"].append(self.left.primary_key)
            if self.right.primary_key.column_list:
                diffs["added"].append(self.right.primary_key)

        for attr in self.attrs_to_check:
            tbl_option_old = getattr(self.left, attr)
            tbl_option_new = getattr(self.right, attr)
            if not is_equal(tbl_option_old, tbl_option_new):
                diffs["removed"].append(TableOptionDiff(attr, tbl_option_old))
                diffs["added"].append(TableOptionDiff(attr, tbl_option_new))
                diffs["attrs_modified"].append(attr)

        return diffs