fix: purge rule version history after reset import
This commit is contained in:
@@ -482,7 +482,66 @@ async def _backup_rule_domain(session) -> Path:
|
||||
return backup_path
|
||||
|
||||
|
||||
async def reset_and_import_rules(root: Path, *, dry_run: bool, prune_oss: bool) -> None:
|
||||
async def _purge_rule_history() -> int:
|
||||
async with GetAsyncSession() as session:
|
||||
await session.execute(
|
||||
text(
|
||||
"""
|
||||
UPDATE leaudit_audit_runs ar
|
||||
SET rule_version_id = rs.current_version_id,
|
||||
rule_source_oss_url = COALESCE(current_rv.oss_url, ar.rule_source_oss_url),
|
||||
rule_source_sha256 = COALESCE(current_rv.file_sha256, ar.rule_source_sha256),
|
||||
rule_type_id = COALESCE(current_rv.metadata_type_id, ar.rule_type_id)
|
||||
FROM leaudit_rule_sets rs
|
||||
LEFT JOIN leaudit_rule_versions current_rv ON current_rv.id = rs.current_version_id
|
||||
CROSS JOIN leaudit_rule_versions old_rv
|
||||
WHERE old_rv.id = ar.rule_version_id
|
||||
AND ar.rule_set_id = rs.id
|
||||
AND old_rv.id <> rs.current_version_id
|
||||
AND rs.current_version_id IS NOT NULL
|
||||
"""
|
||||
)
|
||||
)
|
||||
await session.execute(
|
||||
text(
|
||||
"""
|
||||
UPDATE leaudit_rule_results rr
|
||||
SET rule_version_id = ar.rule_version_id
|
||||
FROM leaudit_audit_runs ar
|
||||
CROSS JOIN leaudit_rule_versions old_rv
|
||||
WHERE old_rv.id = rr.rule_version_id
|
||||
AND rr.run_id = ar.id
|
||||
AND old_rv.id <> ar.rule_version_id
|
||||
"""
|
||||
)
|
||||
)
|
||||
result = await session.execute(
|
||||
text(
|
||||
"""
|
||||
DELETE FROM leaudit_rule_versions rv
|
||||
WHERE NOT EXISTS (
|
||||
SELECT 1
|
||||
FROM leaudit_rule_sets rs
|
||||
WHERE rs.current_version_id = rv.id
|
||||
)
|
||||
AND NOT EXISTS (
|
||||
SELECT 1
|
||||
FROM leaudit_audit_runs ar
|
||||
WHERE ar.rule_version_id = rv.id
|
||||
)
|
||||
AND NOT EXISTS (
|
||||
SELECT 1
|
||||
FROM leaudit_rule_results rr
|
||||
WHERE rr.rule_version_id = rv.id
|
||||
)
|
||||
"""
|
||||
)
|
||||
)
|
||||
await session.commit()
|
||||
return int(result.rowcount or 0)
|
||||
|
||||
|
||||
async def reset_and_import_rules(root: Path, *, dry_run: bool, prune_oss: bool, purge_rule_history: bool) -> None:
|
||||
local_rules = load_local_rules(root)
|
||||
canonical_keys = {
|
||||
OssPathUtils.BuildRuleYamlKey(local.rule_type, local.version_no)
|
||||
@@ -572,6 +631,9 @@ async def reset_and_import_rules(root: Path, *, dry_run: bool, prune_oss: bool)
|
||||
print("oss_deleted=0")
|
||||
|
||||
await import_rules(root, dry_run=False)
|
||||
if purge_rule_history:
|
||||
deleted = await _purge_rule_history()
|
||||
print(f"purged_rule_versions={deleted}")
|
||||
|
||||
|
||||
def main() -> None:
|
||||
@@ -580,9 +642,17 @@ def main() -> None:
|
||||
parser.add_argument("--execute", action="store_true")
|
||||
parser.add_argument("--reset-rule-domain", action="store_true")
|
||||
parser.add_argument("--prune-oss", action="store_true")
|
||||
parser.add_argument("--purge-rule-history", action="store_true")
|
||||
args = parser.parse_args()
|
||||
if args.reset_rule_domain:
|
||||
asyncio.run(reset_and_import_rules(Path(args.root), dry_run=not args.execute, prune_oss=args.prune_oss))
|
||||
asyncio.run(
|
||||
reset_and_import_rules(
|
||||
Path(args.root),
|
||||
dry_run=not args.execute,
|
||||
prune_oss=args.prune_oss,
|
||||
purge_rule_history=args.purge_rule_history,
|
||||
)
|
||||
)
|
||||
else:
|
||||
asyncio.run(import_rules(Path(args.root), dry_run=not args.execute))
|
||||
|
||||
|
||||
Reference in New Issue
Block a user