Skip to main content

reifydb_transaction/transaction/catalog/
identity_attribute.rs

1// SPDX-License-Identifier: Apache-2.0
2// Copyright (c) 2026 ReifyDB
3
4use reifydb_core::interface::catalog::{
5	change::CatalogTrackIdentityAttributeChangeOperations,
6	identity::{IdentityAttribute, IdentityAttributeId},
7};
8use reifydb_value::Result;
9
10use crate::{
11	change::{
12		Change,
13		OperationType::{Create, Delete},
14		TransactionalIdentityAttributeChanges,
15	},
16	interceptor::identity_attribute::{IdentityAttributePostCreateContext, IdentityAttributePreDeleteContext},
17	transaction::admin::AdminTransaction,
18};
19
20impl CatalogTrackIdentityAttributeChangeOperations for AdminTransaction {
21	fn track_identity_attribute_created(&mut self, attribute: IdentityAttribute) -> Result<()> {
22		self.interceptors
23			.identity_attribute_post_create
24			.execute(IdentityAttributePostCreateContext::new(&attribute))?;
25		let change = Change {
26			pre: None,
27			post: Some(attribute),
28			op: Create,
29		};
30		self.changes.add_identity_attribute_change(change);
31		Ok(())
32	}
33
34	fn track_identity_attribute_deleted(&mut self, attribute: IdentityAttribute) -> Result<()> {
35		self.interceptors
36			.identity_attribute_pre_delete
37			.execute(IdentityAttributePreDeleteContext::new(&attribute))?;
38		let change = Change {
39			pre: Some(attribute),
40			post: None,
41			op: Delete,
42		};
43		self.changes.add_identity_attribute_change(change);
44		Ok(())
45	}
46}
47
48impl TransactionalIdentityAttributeChanges for AdminTransaction {
49	fn find_identity_attribute(&self, id: IdentityAttributeId) -> Option<&IdentityAttribute> {
50		for change in self.changes.identity_attribute.iter().rev() {
51			if let Some(attribute) = &change.post {
52				if attribute.id == id {
53					return Some(attribute);
54				}
55			} else if let Some(attribute) = &change.pre
56				&& attribute.id == id && change.op == Delete
57			{
58				return None;
59			}
60		}
61		None
62	}
63
64	fn find_identity_attribute_by_name(&self, name: &str) -> Option<&IdentityAttribute> {
65		for change in self.changes.identity_attribute.iter().rev() {
66			if let Some(attribute) = &change.post {
67				if attribute.name == name {
68					return Some(attribute);
69				}
70			} else if let Some(attribute) = &change.pre
71				&& attribute.name == name && change.op == Delete
72			{
73				return None;
74			}
75		}
76		None
77	}
78
79	fn is_identity_attribute_deleted(&self, id: IdentityAttributeId) -> bool {
80		self.changes
81			.identity_attribute
82			.iter()
83			.rev()
84			.any(|change| change.op == Delete && change.pre.as_ref().map(|a| a.id) == Some(id))
85	}
86
87	fn is_identity_attribute_deleted_by_name(&self, name: &str) -> bool {
88		self.changes.identity_attribute.iter().rev().any(|change| {
89			change.op == Delete && change.pre.as_ref().map(|a| a.name == name).unwrap_or(false)
90		})
91	}
92}