-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow the user to hard ignore certain tables from soft-deletion (#23)
* Update README.md with usage examples for ignored_tables= * Change deprecated stmt.froms to stmt.get_final_froms() * Updated isort in .pre-commit-config.yaml * Implemented and fixed tests.
- Loading branch information
Showing
14 changed files
with
145 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
[bumpversion] | ||
current_version = 0.8.2 | ||
current_version = 0.8.3 | ||
commit = True | ||
tag = False | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
[tool] | ||
[tool.poetry] | ||
name = "sqlalchemy-easy-softdelete" | ||
version = "0.8.2" | ||
version = "0.8.3" | ||
homepage = "https://github.com/flipbit03/sqlalchemy-easy-softdelete" | ||
description = "Easily add soft-deletion to your SQLAlchemy Models." | ||
authors = ["Cadu <[email protected]>"] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,4 +2,4 @@ | |
|
||
__author__ = """Cadu""" | ||
__email__ = '[email protected]' | ||
__version__ = '0.8.2' | ||
__version__ = '0.8.3' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 20 additions & 7 deletions
27
sqlalchemy_easy_softdelete/handler/sqlalchemy_easy_softdelete.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,36 @@ | ||
"""This module is responsible for activating the query rewriter.""" | ||
|
||
from functools import cache | ||
from typing import List, Optional | ||
|
||
from sqlalchemy.event import listens_for | ||
from sqlalchemy.orm import ORMExecuteState, Session | ||
|
||
from sqlalchemy_easy_softdelete.handler.rewriter import SoftDeleteQueryRewriter | ||
from sqlalchemy_easy_softdelete.hook import IgnoredTable | ||
|
||
global_rewriter: Optional[SoftDeleteQueryRewriter] = None | ||
|
||
@cache | ||
def activate_soft_delete_hook(deleted_field_name: str, disable_soft_delete_option_name: str): | ||
|
||
def activate_soft_delete_hook( | ||
deleted_field_name: str, disable_soft_delete_option_name: str, ignored_tables: List[IgnoredTable] | ||
): | ||
"""Activate an event hook to rewrite the queries.""" | ||
|
||
global global_rewriter | ||
global_rewriter = SoftDeleteQueryRewriter( | ||
deleted_field_name=deleted_field_name, | ||
disable_soft_delete_option_name=disable_soft_delete_option_name, | ||
ignored_tables=ignored_tables, | ||
) | ||
|
||
# Enable Soft Delete on all Relationship Loads which implement SoftDeleteMixin | ||
@listens_for(Session, "do_orm_execute") | ||
@listens_for(Session, identifier="do_orm_execute") | ||
def soft_delete_execute(state: ORMExecuteState): | ||
if not state.is_select: | ||
return | ||
|
||
adapted = SoftDeleteQueryRewriter(deleted_field_name, disable_soft_delete_option_name).rewrite_statement( | ||
state.statement | ||
) | ||
# Rewrite the statement | ||
adapted = global_rewriter.rewrite_statement(state.statement) | ||
|
||
# Replace the statement | ||
state.statement = adapted |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
from dataclasses import dataclass | ||
from typing import Optional | ||
|
||
from sqlalchemy import Table | ||
|
||
|
||
@dataclass | ||
class IgnoredTable: | ||
name: str | ||
table_schema: Optional[str] = None | ||
|
||
def match_name(self, table: Table): | ||
# Table matches if the name and schema match | ||
return self.name == table.name and self.table_schema == table.schema |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters