1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
use std::collections::HashSet;
use crate::{
graphmigrate::Comparison,
sqlite::schema::{
constraint::{
Constraint,
ConstraintType,
},
},
utils::Tokens,
};
use super::{
utils::{
SqliteNodeDataDispatch,
SqliteMigrateCtx,
SqliteNodeData,
},
GraphId,
Node,
};
#[derive(Clone)]
pub(crate) struct NodeConstraint_ {
pub def: Constraint,
}
impl NodeConstraint_ {
pub fn compare(&self, old: &Self, created: &HashSet<GraphId>) -> Comparison {
if created.contains(&GraphId::Table(self.def.table.schema_id.clone())) || self.def.type_ != old.def.type_ ||
self.def.id != old.def.id {
Comparison::Recreate
} else {
Comparison::DoNothing
}
}
}
impl SqliteNodeDataDispatch for NodeConstraint_ {
fn create_coalesce(&mut self, other: Node) -> Option<Node> {
Some(other)
}
fn create(&self, ctx: &mut SqliteMigrateCtx) {
let mut stmt = Tokens::new();
stmt.s("alter table").id(&self.def.table.id).s("add constraint").id(&self.def.id);
match &self.def.type_ {
ConstraintType::PrimaryKey(x) => {
stmt.s("primary key (").f(|t| {
for (i, field) in x.fields.iter().enumerate() {
if i > 0 {
t.s(",");
}
t.id(&field.id);
}
}).s(")");
},
ConstraintType::ForeignKey(x) => {
stmt.s("foreign key (").f(|t| {
for (i, pair) in x.fields.iter().enumerate() {
if i > 0 {
t.s(",");
}
t.id(&pair.0.id);
}
}).s(") references ").f(|t| {
for (i, pair) in x.fields.iter().enumerate() {
if i == 0 {
t.id(&pair.1.table.id).s("(");
} else {
t.s(",");
}
t.id(&pair.1.id);
}
}).s(")");
},
}
ctx.statements.push(stmt.to_string());
}
fn delete_coalesce(&mut self, other: Node) -> Option<Node> {
Some(other)
}
fn delete(&self, ctx: &mut SqliteMigrateCtx) {
ctx
.statements
.push(
Tokens::new()
.s("alter table")
.id(&self.def.table.id)
.s("drop constraint")
.id(&self.def.id)
.to_string(),
);
}
}
impl SqliteNodeData for NodeConstraint_ {
fn update(&self, _ctx: &mut SqliteMigrateCtx, _old: &Self) {
unreachable!()
}
}