good_ormning/sqlite/graph/
constraint.rs

1use std::collections::HashSet;
2use crate::{
3    graphmigrate::Comparison,
4    sqlite::schema::{
5        constraint::{
6            Constraint,
7        },
8    },
9    utils::Tokens,
10};
11use super::{
12    utils::{
13        SqliteNodeDataDispatch,
14        SqliteMigrateCtx,
15        SqliteNodeData,
16    },
17    GraphId,
18    Node,
19};
20
21#[derive(Clone)]
22pub(crate) struct NodeConstraint_ {
23    pub def: Constraint,
24}
25
26impl NodeConstraint_ {
27    pub fn compare(&self, old: &Self, created: &HashSet<GraphId>) -> Comparison {
28        if created.contains(&GraphId::Table(self.def.table.schema_id.clone())) || self.def.type_ != old.def.type_ ||
29            self.def.id != old.def.id {
30            Comparison::Recreate
31        } else {
32            Comparison::DoNothing
33        }
34    }
35
36    fn display_path(&self) -> rpds::Vector<String> {
37        rpds::vector![self.def.to_string()]
38    }
39}
40
41impl SqliteNodeDataDispatch for NodeConstraint_ {
42    fn create_coalesce(&mut self, other: Node) -> Option<Node> {
43        Some(other)
44    }
45
46    fn create(&self, ctx: &mut SqliteMigrateCtx) {
47        ctx.errs.err(&self.display_path(), format!("New constraints cannot be added after a table was created"));
48    }
49
50    fn delete_coalesce(&mut self, other: Node) -> Option<Node> {
51        Some(other)
52    }
53
54    fn delete(&self, ctx: &mut SqliteMigrateCtx) {
55        ctx
56            .statements
57            .push(
58                Tokens::new()
59                    .s("alter table")
60                    .id(&self.def.table.id)
61                    .s("drop constraint")
62                    .id(&self.def.id)
63                    .to_string(),
64            );
65    }
66}
67
68impl SqliteNodeData for NodeConstraint_ {
69    fn update(&self, _ctx: &mut SqliteMigrateCtx, _old: &Self) {
70        unreachable!()
71    }
72}