Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Oct 11, 2025

Overview

This PR adds soft deletion support to the async-sqlalchemy-adapter, matching the functionality available in the synchronous sqlalchemy-adapter. This resolves the feature request in issue #XX.

What is Soft Deletion?

Instead of physically deleting policy records from the database, soft deletion marks them as deleted using a boolean flag. This preserves data history and enables auditing while maintaining the same Casbin API.

Implementation

Core Changes

  • Added optional db_class_softdelete_attribute parameter to Adapter.__init__()
  • Implemented _softdelete_query() helper method to filter out soft-deleted records automatically
  • Updated all query operations (load_policy, load_filtered_policy, save_policy, etc.) to respect the soft delete flag
  • Modified removal operations (remove_policy, remove_policies, remove_filtered_policy) to mark records as deleted instead of removing them

Usage

To use soft deletion, define a custom database model with a boolean is_deleted column:

from sqlalchemy import Column, Boolean
from casbin_async_sqlalchemy_adapter import Adapter, Base

class CasbinRuleSoftDelete(Base):
    __tablename__ = "casbin_rule"
    
    id = Column(Integer, primary_key=True)
    ptype = Column(String(255))
    v0 = Column(String(255))
    # ... other v columns ...
    
    # Add the soft delete column
    is_deleted = Column(Boolean, default=False, index=True, nullable=False)

# Create adapter with soft delete support
adapter = Adapter(
    'sqlite+aiosqlite:///test.db',
    db_class=CasbinRuleSoftDelete,
    db_class_softdelete_attribute=CasbinRuleSoftDelete.is_deleted
)

e = casbin.AsyncEnforcer('model.conf', adapter)

# Now when you remove policies, they will be soft-deleted
await e.remove_policy("alice", "data1", "read")
# The record remains in the database with is_deleted=True

Key Features

  • Optional: Soft deletion is opt-in; fully backward compatible with existing code
  • Type Safety: Validates that the soft delete column is of type Boolean
  • Automatic Filtering: All queries automatically exclude soft-deleted records
  • Data Preservation: Deleted records remain in database for audit purposes
  • Transparent API: No changes to the Casbin API - works seamlessly
  • Well Tested: Comprehensive test suite with 7 new tests covering all scenarios

Testing

All 26 tests pass:

  • 15 existing tests (backward compatibility verified)
  • 7 new soft deletion tests
  • 4 external session tests

Documentation

Added complete documentation section in README.md with usage examples and feature description.

Breaking Changes

None. This is a fully backward-compatible addition. Existing code continues to work without any modifications.

Original prompt

This section details on the original issue you should resolve

<issue_title>Is there a plan to support soft deletion?</issue_title>
<issue_description>Is there a plan to support soft deletion? Similar to the soft deletion feature in sqlalchemy-adapter?</issue_description>

Comments on the Issue (you are @copilot in this section)

Fixes #14


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot AI and others added 2 commits October 11, 2025 13:02
Co-authored-by: hsluoyz <3787410+hsluoyz@users.noreply.github.com>
Co-authored-by: hsluoyz <3787410+hsluoyz@users.noreply.github.com>
Copilot AI changed the title [WIP] Add support for soft deletion feature Add soft deletion support to async-sqlalchemy-adapter Oct 11, 2025
Copilot AI requested a review from hsluoyz October 11, 2025 13:06
@hsluoyz hsluoyz marked this pull request as ready for review November 6, 2025 11:29
@hsluoyz hsluoyz closed this Nov 6, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Is there a plan to support soft deletion?

2 participants