Skip to main content

reifydb_transaction/transaction/catalog/
migration.rs

1// SPDX-License-Identifier: Apache-2.0
2// Copyright (c) 2025 ReifyDB
3
4use reifydb_core::interface::catalog::{
5	change::{CatalogTrackMigrationChangeOperations, CatalogTrackMigrationEventChangeOperations},
6	id::MigrationId,
7	migration::{Migration, MigrationEvent},
8};
9use reifydb_type::Result;
10
11use crate::{
12	change::{
13		Change,
14		OperationType::{Create, Delete},
15		TransactionalMigrationChanges,
16	},
17	transaction::admin::AdminTransaction,
18};
19
20impl CatalogTrackMigrationChangeOperations for AdminTransaction {
21	fn track_migration_created(&mut self, migration: Migration) -> Result<()> {
22		let change = Change {
23			pre: None,
24			post: Some(migration),
25			op: Create,
26		};
27		self.changes.add_migration_change(change);
28		Ok(())
29	}
30
31	fn track_migration_deleted(&mut self, migration: Migration) -> Result<()> {
32		let change = Change {
33			pre: Some(migration),
34			post: None,
35			op: Delete,
36		};
37		self.changes.add_migration_change(change);
38		Ok(())
39	}
40}
41
42impl CatalogTrackMigrationEventChangeOperations for AdminTransaction {
43	fn track_migration_event_created(&mut self, event: MigrationEvent) -> Result<()> {
44		let change = Change {
45			pre: None,
46			post: Some(event),
47			op: Create,
48		};
49		self.changes.add_migration_event_change(change);
50		Ok(())
51	}
52}
53
54impl TransactionalMigrationChanges for AdminTransaction {
55	fn find_migration(&self, id: MigrationId) -> Option<&Migration> {
56		for change in self.changes.migration.iter().rev() {
57			if let Some(migration) = &change.post {
58				if migration.id == id {
59					return Some(migration);
60				}
61			} else if let Some(migration) = &change.pre
62				&& migration.id == id && change.op == Delete
63			{
64				return None;
65			}
66		}
67		None
68	}
69
70	fn find_migration_by_name(&self, name: &str) -> Option<&Migration> {
71		self.changes.migration.iter().rev().find_map(|change| change.post.as_ref().filter(|m| m.name == name))
72	}
73
74	fn is_migration_deleted(&self, id: MigrationId) -> bool {
75		self.changes
76			.migration
77			.iter()
78			.rev()
79			.any(|change| change.op == Delete && change.pre.as_ref().map(|m| m.id == id).unwrap_or(false))
80	}
81
82	fn is_migration_deleted_by_name(&self, name: &str) -> bool {
83		self.changes.migration.iter().rev().any(|change| {
84			change.op == Delete && change.pre.as_ref().map(|m| m.name == name).unwrap_or(false)
85		})
86	}
87}