good_ormning/sqlite/graph/
index.rs

1use std::collections::HashSet;
2use crate::{
3    graphmigrate::Comparison,
4    sqlite::schema::index::Index,
5    utils::Tokens,
6};
7use super::{
8    utils::{
9        SqliteNodeDataDispatch,
10        SqliteMigrateCtx,
11        SqliteNodeData,
12    },
13    GraphId,
14    Node,
15};
16
17#[derive(Clone)]
18pub(crate) struct NodeIndex_ {
19    pub def: Index,
20}
21
22impl NodeIndex_ {
23    pub fn compare(&self, old: &Self, created: &HashSet<GraphId>) -> Comparison {
24        if created.contains(&GraphId::Table(self.def.table.schema_id.clone())) ||
25            self.def.fields != old.def.fields ||
26            self.def.id != old.def.id {
27            Comparison::Recreate
28        } else {
29            Comparison::DoNothing
30        }
31    }
32}
33
34impl SqliteNodeDataDispatch for NodeIndex_ {
35    fn create_coalesce(&mut self, other: Node) -> Option<Node> {
36        Some(other)
37    }
38
39    fn create(&self, ctx: &mut SqliteMigrateCtx) {
40        ctx.statements.push(Tokens::new().s("create").f(|t| {
41            if self.def.unique {
42                t.s("unique");
43            }
44        }).s("index").id(&self.def.id).s("on").id(&self.def.table.id).s("(").f(|t| {
45            for (i, field) in self.def.fields.iter().enumerate() {
46                if i > 0 {
47                    t.s(",");
48                }
49                t.id(&field.id);
50            }
51        }).s(")").to_string());
52    }
53
54    fn delete_coalesce(&mut self, other: Node) -> Option<Node> {
55        Some(other)
56    }
57
58    fn delete(&self, ctx: &mut SqliteMigrateCtx) {
59        ctx.statements.push(Tokens::new().s("drop index").id(&self.def.id).to_string());
60    }
61}
62
63impl SqliteNodeData for NodeIndex_ {
64    fn update(&self, _ctx: &mut SqliteMigrateCtx, _old: &Self) {
65        unreachable!()
66    }
67}