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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
//! # fuel_indexer_schema::db::tables
//!
//! Runtime logic used to create SQL tables and persist those tables (and various metadata
//! about the structure of those tables) to the database.
//!
//! Also used to load tables from the database when web requests are made.

use crate::db::IndexerSchemaDbResult;
use fuel_indexer_database::{
    queries, types::*, DbType, IndexerConnection, IndexerConnectionPool,
};
use fuel_indexer_lib::graphql::{GraphQLSchema, ParsedGraphQLSchema};
use fuel_indexer_lib::manifest::Manifest;
use itertools::Itertools;

/// `IndexerSchema` is used to encapsulate most of the logic related to parsing
/// GraphQL types, generating SQL from those types, and committing that SQL to
/// the database.
#[derive(Default)]
pub struct IndexerSchema {
    /// The database type.
    db_type: DbType,

    /// The parsed GraphQL schema.
    parsed: ParsedGraphQLSchema,

    /// The GraphQL schema.
    schema: GraphQLSchema,

    /// The tables generated from the GraphQL schema.
    tables: Vec<Table>,

    /// The namespace of the indexer.
    namespace: String,

    /// The identifier of the indexer.
    identifier: String,
}

impl IndexerSchema {
    /// Create a new `IndexerSchema`.
    pub fn new(
        namespace: &str,
        identifier: &str,
        schema: &GraphQLSchema,
        db_type: DbType,
    ) -> IndexerSchemaDbResult<Self> {
        Ok(IndexerSchema {
            db_type,
            namespace: namespace.to_string(),
            identifier: identifier.to_string(),
            schema: schema.to_owned(),
            parsed: ParsedGraphQLSchema::new(namespace, identifier, Some(schema))?,
            tables: Vec::new(),
        })
    }

    /// Return the associated `ParsedGraphQLSchema`.
    pub fn parsed(&self) -> &ParsedGraphQLSchema {
        &self.parsed
    }

    /// Generate table SQL for each indexable object in the given GraphQL schema.
    ///
    /// Ideally all of these queries should return the objects that they persist to the
    /// DB (e.g., `INSERT .. RETURNING *`).
    ///
    /// TODO: We should also be caching as much of this `IndexerSchema` as possible
    pub async fn commit(
        mut self,
        schema: &GraphQLSchema,
        conn: &mut IndexerConnection,
    ) -> IndexerSchemaDbResult<Self> {
        let parsed_schema =
            ParsedGraphQLSchema::new(&self.namespace, &self.identifier, Some(schema))?;

        let mut statements = Vec::new();
        self.schema = schema.to_owned();
        self.parsed = parsed_schema;

        let root = GraphRoot {
            version: schema.version().to_owned(),
            schema_name: self.namespace.to_owned(),
            schema_identifier: self.identifier.to_owned(),
            schema: self.schema.to_string(),
            ..GraphRoot::default()
        };

        queries::new_graph_root(conn, root).await?;

        // TODO: Abstract this into a SQLSchema (or named something else)?
        match self.db_type {
            DbType::Postgres => {
                let create = format!(
                    "CREATE SCHEMA IF NOT EXISTS {};",
                    self.parsed.fully_qualified_namespace(),
                );
                statements.push(create);
            }
        }

        let mut type_ids = self
            .parsed
            .type_defs()
            .iter()
            .map(|(_, t)| TypeId::from_typedef(t, &self.parsed))
            .unique_by(|t| t.id)
            .collect::<Vec<TypeId>>();

        // Since join tables aren't derived from `TypeDefinition`s, we have to create them
        // separately. But since `TypeId`s and `Table`s for join tables `impl SqlFragment`,
        // we can group them all together when generating SQL.

        let mut join_type_ids = self
            .parsed
            .join_table_meta()
            .iter()
            .flat_map(|(_, meta)| {
                meta.iter()
                    .map(|m| TypeId::from_join_meta(m.to_owned(), &self.parsed))
            })
            .collect::<Vec<TypeId>>();

        type_ids.append(&mut join_type_ids);

        queries::type_id_insert(conn, type_ids).await?;

        let mut tables = self
            .parsed
            .storage_backed_typedefs()
            .iter()
            .map(|(_, t)| Table::from_typedef(t, &self.parsed))
            .collect::<Vec<Table>>();

        let mut join_tables = self
            .parsed
            .join_table_meta()
            .iter()
            .flat_map(|(_, meta)| {
                meta.iter()
                    .map(|m| Table::from_join_meta(m.to_owned(), &self.parsed))
            })
            .collect::<Vec<Table>>();

        tables.append(&mut join_tables);

        let columns = tables
            .iter()
            .flat_map(|t| t.columns())
            .map(|c| c.to_owned())
            .collect::<Vec<Column>>();

        queries::new_column_insert(conn, columns).await?;

        let table_stmnts = tables
            .iter()
            .filter_map(|t| {
                let stmnt = t.create();
                if stmnt.is_empty() {
                    return None;
                }
                Some(stmnt)
            })
            .collect::<Vec<String>>();
        statements.extend(table_stmnts);

        let constraint_stmnts = tables
            .iter()
            .flat_map(|t| t.constraints())
            .map(|c| c.create())
            .collect::<Vec<String>>();

        statements.extend(constraint_stmnts);

        for stmnt in statements.iter() {
            queries::execute_query(conn, stmnt.to_owned()).await?;
        }

        self.tables = tables;

        Ok(self)
    }

    /// Load a `IndexerSchema` from the database.
    pub async fn load(
        pool: &IndexerConnectionPool,
        namespace: &str,
        identifier: &str,
    ) -> IndexerSchemaDbResult<Self> {
        let mut conn = pool.acquire().await?;
        let root = queries::graph_root_latest(&mut conn, namespace, identifier).await?;

        let indexer_id =
            queries::get_indexer_id(&mut conn, namespace, identifier).await?;
        let IndexerAsset { bytes, .. } =
            queries::indexer_asset(&mut conn, &indexer_id, IndexerAssetType::Manifest)
                .await?;
        let _manifest = Manifest::try_from(&bytes)?;

        let schema = GraphQLSchema::new(root.schema.clone());
        let parsed = ParsedGraphQLSchema::new(namespace, identifier, Some(&schema))?;

        let tables = parsed
            .storage_backed_typedefs()
            .iter()
            .map(|(_, t)| Table::from_typedef(t, &parsed))
            .collect::<Vec<Table>>();

        Ok(IndexerSchema {
            namespace: root.schema_name,
            identifier: root.schema_identifier,
            schema,
            tables,
            parsed,
            db_type: DbType::Postgres,
        })
    }
}