good_ormning/pg/graph/
mod.rs

1use std::collections::HashSet;
2use enum_dispatch::enum_dispatch;
3use samevariant::samevariant;
4use crate::graphmigrate::Comparison;
5use self::{
6    table::NodeTable_,
7    field::NodeField_,
8    constraint::NodeConstraint_,
9    index::NodeIndex_,
10    utils::{
11        PgMigrateCtx,
12        NodeDataDispatch,
13        NodeData,
14    },
15};
16use super::schema::{
17    table::SchemaTableId,
18    field::SchemaFieldId,
19    constraint::SchemaConstraintId,
20    index::SchemaIndexId,
21};
22
23pub mod table;
24pub mod field;
25pub mod constraint;
26pub mod index;
27pub mod utils;
28
29#[derive(Clone, Eq, PartialEq, Hash, Debug, PartialOrd, Ord)]
30pub enum GraphId {
31    Table(SchemaTableId),
32    Field(SchemaTableId, SchemaFieldId),
33    Constraint(SchemaTableId, SchemaConstraintId),
34    Index(SchemaTableId, SchemaIndexId),
35}
36
37#[derive(Clone)]
38#[enum_dispatch(NodeDataDispatch)]
39#[samevariant(PairwiseNode)]
40pub(crate) enum Node {
41    Table(NodeTable_),
42    Field(NodeField_),
43    Constraint(NodeConstraint_),
44    Index(NodeIndex_),
45}
46
47impl Node {
48    pub(crate) fn table(t: NodeTable_) -> Self {
49        Node::Table(t)
50    }
51
52    pub(crate) fn field(t: NodeField_) -> Self {
53        Node::Field(t)
54    }
55
56    pub(crate) fn table_constraint(t: NodeConstraint_) -> Self {
57        Node::Constraint(t)
58    }
59
60    pub(crate) fn table_index(t: NodeIndex_) -> Self {
61        Node::Index(t)
62    }
63}
64
65impl<'a> crate::graphmigrate::NodeData for Node {
66    type O = PgMigrateCtx;
67    type I = GraphId;
68
69    fn compare(&self, other: &Self, created: &HashSet<Self::I>) -> Comparison {
70        match PairwiseNode::pairs(self, &other) {
71            PairwiseNode::Table(current, old) => current.compare(old, created),
72            PairwiseNode::Field(current, old) => current.compare(old, created),
73            PairwiseNode::Constraint(current, old) => current.compare(old, created),
74            PairwiseNode::Index(current, old) => current.compare(old, created),
75            PairwiseNode::Nonmatching(_, _) => unreachable!(),
76        }
77    }
78
79    fn create(&self, ctx: &mut PgMigrateCtx) {
80        NodeDataDispatch::create(self, ctx)
81    }
82
83    fn delete(&self, ctx: &mut PgMigrateCtx) {
84        NodeDataDispatch::delete(self, ctx)
85    }
86
87    fn update(&self, ctx: &mut PgMigrateCtx, old: &Self) {
88        match PairwiseNode::pairs(self, &old) {
89            PairwiseNode::Table(current, old) => current.update(ctx, &old),
90            PairwiseNode::Field(current, old) => current.update(ctx, &old),
91            PairwiseNode::Constraint(current, old) => current.update(ctx, &old),
92            PairwiseNode::Index(current, old) => current.update(ctx, &old),
93            PairwiseNode::Nonmatching(_, _) => unreachable!(),
94        }
95    }
96
97    fn create_coalesce(&mut self, other: Self) -> Option<Self> {
98        NodeDataDispatch::create_coalesce(self, other)
99    }
100
101    fn delete_coalesce(&mut self, other: Self) -> Option<Self> {
102        NodeDataDispatch::delete_coalesce(self, other)
103    }
104}