cat_gateway/db/index/schema/
mod.rs

1//! Index Schema
2
3use std::sync::Arc;
4
5use anyhow::Context;
6use cardano_blockchain_types::Network;
7use handlebars::Handlebars;
8use scylla::Session;
9use serde_json::json;
10use tracing::error;
11
12use crate::{settings::cassandra_db, utils::blake2b_hash::generate_uuid_string_from_data};
13
14/// Keyspace Create (Templated)
15const CREATE_NAMESPACE_CQL: &str = include_str!("./cql/namespace.cql");
16
17/// All Schema Creation Statements
18const SCHEMAS: &[(&str, &str)] = &[
19    (
20        // Sync Status Table Schema
21        include_str!("./cql/sync_status.cql"),
22        "Create Sync Status Table",
23    ),
24    (
25        // TXO by Stake Address Table Schema
26        include_str!("./cql/txo_by_stake_table.cql"),
27        "Create Table TXO By Stake Address",
28    ),
29    (
30        // TXO Assets by Stake Address Table Schema
31        include_str!("./cql/txo_assets_by_stake_table.cql"),
32        "Create Table TXO Assets By Stake Address",
33    ),
34    (
35        // TXO Unstaked Table Schema
36        include_str!("cql/unstaked_txo_by_txn_id.cql"),
37        "Create Table Unstaked TXO By Txn Hash",
38    ),
39    (
40        // TXO Unstaked Assets Table Schema
41        include_str!("cql/unstaked_txo_assets_by_txn_id.cql"),
42        "Create Table Unstaked TXO Assets By Txn Hash",
43    ),
44    (
45        // TXI by Stake Address table schema.
46        include_str!("cql/txi_by_txn_id_table.cql"),
47        "Create Table TXI By Stake Address",
48    ),
49    (
50        // Stake Address/Registration Table Schema
51        include_str!("cql/stake_registration.cql"),
52        "Create Table Stake Registration",
53    ),
54    (
55        // CIP-36 Registration Table Schema
56        include_str!("cql/cip36_registration.cql"),
57        "Create Table CIP-36 Registration",
58    ),
59    (
60        // CIP-36 invalid registration table schema.
61        include_str!("cql/cip36_registration_invalid.cql"),
62        "Create Table CIP-36 Registration Invalid",
63    ),
64    (
65        // CIP-36 registration for vote key table schema.
66        include_str!("cql/cip36_registration_for_vote_key.cql"),
67        "Create Table CIP-36 Registration For a stake address",
68    ),
69    (
70        // RBAC registration table schema.
71        include_str!("cql/rbac_registration.cql"),
72        "Create Table RBAC Registration",
73    ),
74    (
75        // RBAC invalid registration table schema.
76        include_str!("cql/rbac_invalid_registration.cql"),
77        "Create Table Invalid RBAC Registration",
78    ),
79    (
80        // Catalyst ID for transaction ID table schema.
81        include_str!("cql/catalyst_id_for_txn_id.cql"),
82        "Create Table Catalyst ID For TX ID",
83    ),
84    (
85        // Catalyst ID for stake address table schema.
86        include_str!("cql/catalyst_id_for_stake_address.cql"),
87        "Create Table Catalyst ID For Stake Address",
88    ),
89];
90
91/// Removes all comments from each line in the input query text and joins the remaining
92/// lines into a single string, reducing consecutive whitespace characters to a single
93/// space. Comments are defined as any text following `--` on a line.
94///
95/// # Arguments
96///
97/// * `text`: A string slice that holds the query to be cleaned.
98///
99/// # Returns
100///
101/// A new string with comments removed and whitespace reduced, where each remaining line
102/// from the original text is separated by a newline character.
103fn remove_comments_and_join_query_lines(text: &str) -> String {
104    // Split the input text into lines, removing any trailing empty lines
105    let raw_lines: Vec<&str> = text.lines().collect();
106    let mut clean_lines: Vec<String> = Vec::new();
107
108    // Filter out comments from each line
109    for line in raw_lines {
110        let mut clean_line = line.to_string();
111        if let Some(no_comment) = line.split_once("--") {
112            clean_line = no_comment.0.to_string();
113        }
114        clean_line = clean_line
115            .split_whitespace()
116            .collect::<Vec<&str>>()
117            .join(" ")
118            .trim()
119            .to_string();
120        if !clean_line.is_empty() {
121            clean_lines.push(clean_line);
122        }
123    }
124    clean_lines.join("\n")
125}
126
127/// Generates a unique schema version identifier based on the content of all CQL schemas.
128///
129/// This function processes each CQL schema, removes comments from its lines and joins
130/// them into a single string. It then sorts these processed strings to ensure consistency
131/// in schema versions regardless of their order in the list. Finally, it generates a UUID
132/// from a 127 bit hash of this sorted collection of schema contents, which serves as a
133/// unique identifier for the current version of all schemas.
134///
135/// # Returns
136///
137/// A string representing the UUID derived from the concatenated and cleaned CQL
138/// schema contents.
139fn generate_cql_schema_version() -> String {
140    // Where we will actually store the bytes we derive the UUID from.
141    let mut clean_schemas: Vec<String> = Vec::new();
142
143    // Iterate through each CQL schema and add it to the list of clean schemas documents.
144    for (schema, _) in SCHEMAS {
145        let schema = remove_comments_and_join_query_lines(schema);
146        if !schema.is_empty() {
147            clean_schemas.push(schema);
148        }
149    }
150
151    // make sure any re-ordering of the schemas in the list does not effect the generated
152    // schema version
153    clean_schemas.sort();
154
155    // Generate a unique hash of the clean schemas,
156    // and use it to form a UUID to identify the schema version.
157    generate_uuid_string_from_data("Catalyst-Gateway Index Database Schema", &clean_schemas)
158}
159
160/// Get the namespace for a particular db configuration
161pub(crate) fn namespace(persistent: bool, network: Network) -> String {
162    // Build and set the Keyspace to use.
163    let namespace = if persistent { "p" } else { "v" };
164    format!(
165        "{namespace}_{network}_{}",
166        generate_cql_schema_version().replace('-', "_")
167    )
168}
169
170/// Create the namespace we will use for this session
171/// Ok to run this if the namespace already exists.
172async fn create_namespace(
173    session: &mut Arc<Session>, cfg: &cassandra_db::EnvVars, persistent: bool, network: Network,
174) -> anyhow::Result<()> {
175    let keyspace = namespace(persistent, network);
176
177    let mut reg = Handlebars::new();
178    // disable default `html_escape` function
179    // which transforms `<`, `>` symbols to `&lt`, `&gt`
180    reg.register_escape_fn(|s| s.into());
181    let query = reg
182        .render_template(
183            CREATE_NAMESPACE_CQL,
184            &json!({"keyspace": keyspace,"options": cfg.deployment.clone().to_string()}),
185        )
186        .context(format!("Keyspace: {keyspace}"))?;
187
188    // Create the Keyspace if it doesn't exist already.
189    let stmt = session
190        .prepare(query)
191        .await
192        .context(format!("Keyspace: {keyspace}"))?;
193    session
194        .execute_unpaged(&stmt, ())
195        .await
196        .context(format!("Keyspace: {keyspace}"))?;
197
198    // Wait for the Schema to be ready.
199    session.await_schema_agreement().await?;
200
201    // Set the Keyspace to use for this session.
202    if let Err(error) = session.use_keyspace(keyspace.clone(), false).await {
203        error!(keyspace = keyspace, error = %error, "Failed to set keyspace");
204    }
205
206    Ok(())
207}
208
209/// Create the Schema on the connected Cassandra DB
210pub(crate) async fn create_schema(
211    session: &mut Arc<Session>, cfg: &cassandra_db::EnvVars, persistent: bool, network: Network,
212) -> anyhow::Result<()> {
213    create_namespace(session, cfg, persistent, network)
214        .await
215        .context("Creating Namespace")?;
216
217    let mut errors = Vec::with_capacity(SCHEMAS.len());
218
219    for (schema, schema_name) in SCHEMAS {
220        match session.prepare(*schema).await {
221            Ok(stmt) => {
222                if let Err(err) = session.execute_unpaged(&stmt, ()).await {
223                    error!(schema=schema_name, error=%err, "Failed to Execute Create Schema Query");
224                    errors.push(anyhow::anyhow!(
225                        "Failed to Execute Create Schema Query: {err}\n--\nSchema: {schema_name}\n--\n{schema}"
226                    ));
227                };
228            },
229            Err(err) => {
230                error!(schema=schema_name, error=%err, "Failed to Prepare Create Schema Query");
231                errors.push(anyhow::anyhow!(
232                    "Failed to Prepare Create Schema Query: {err}\n--\nSchema: {schema_name}\n--\n{schema}"
233                ));
234            },
235        }
236    }
237
238    if !errors.is_empty() {
239        let fmt_err: Vec<_> = errors.into_iter().map(|err| format!("{err}")).collect();
240        return Err(anyhow::anyhow!(format!(
241            "{} Error(s): {}",
242            fmt_err.len(),
243            fmt_err.join("\n")
244        )));
245    }
246
247    // Wait for the Schema to be ready.
248    session.await_schema_agreement().await?;
249
250    Ok(())
251}
252
253#[cfg(test)]
254mod tests {
255    use super::*;
256
257    /// The version of the Index DB Schema we SHOULD BE using.
258    /// DO NOT change this unless you are intentionally changing the Schema.
259    ///
260    /// This constant is ONLY used by Unit tests to identify when the schema version will
261    /// change accidentally, and is NOT to be used directly to set the schema version of
262    /// the table namespaces.
263    const SCHEMA_VERSION: &str = "504c9e72-9737-849c-8212-37824eff881a";
264
265    #[test]
266    /// This test is designed to fail if the schema version has changed.
267    /// It is used to help detect inadvertent schema version changes.
268    /// If you did NOT intend to change the index db schema and this test fails,
269    /// then revert or fix your changes to the schema files.
270    fn check_schema_version_has_not_changed() {
271        let calculated_version = generate_cql_schema_version();
272        assert_eq!(SCHEMA_VERSION, calculated_version);
273    }
274
275    #[test]
276    fn test_namespace_persistent() {
277        let network = Network::Preprod;
278        let persistent = true;
279        let namespace = namespace(persistent, network);
280        let schema_version = generate_cql_schema_version().replace('-', "_");
281        let expected = format!("p_{network}_{schema_version}");
282        assert_eq!(namespace, expected);
283    }
284
285    #[test]
286    fn test_namespace_volatile() {
287        let network = Network::Preprod;
288        let persistent = false;
289        let namespace = namespace(persistent, network);
290        let schema_version = generate_cql_schema_version().replace('-', "_");
291        let expected = format!("v_{network}_{schema_version}");
292        assert_eq!(namespace, expected);
293    }
294
295    #[test]
296    fn test_no_comments() {
297        let input = "SELECT * FROM table1;";
298        let expected_output = "SELECT * FROM table1;";
299        assert_eq!(remove_comments_and_join_query_lines(input), expected_output);
300    }
301
302    #[test]
303    fn test_single_line_comment() {
304        let input = "SELECT -- some comment * FROM table1;";
305        let expected_output = "SELECT";
306        assert_eq!(remove_comments_and_join_query_lines(input), expected_output);
307    }
308
309    #[test]
310    fn test_multi_line_comment() {
311        let input = "SELECT -- some comment\n* FROM table1;";
312        let expected_output = "SELECT\n* FROM table1;";
313        assert_eq!(remove_comments_and_join_query_lines(input), expected_output);
314    }
315
316    #[test]
317    fn test_multiple_lines() {
318        let input = "SELECT * FROM table1;\n-- another comment\nSELECT * FROM table2;";
319        let expected_output = "SELECT * FROM table1;\nSELECT * FROM table2;";
320        assert_eq!(remove_comments_and_join_query_lines(input), expected_output);
321    }
322
323    #[test]
324    fn test_empty_lines() {
325        let input = "\n\nSELECT * FROM table1;\n-- comment here\n\n";
326        let expected_output = "SELECT * FROM table1;";
327        assert_eq!(remove_comments_and_join_query_lines(input), expected_output);
328    }
329
330    #[test]
331    fn test_whitespace_only() {
332        let input = "   \n  -- comment here\n   ";
333        let expected_output = "";
334        assert_eq!(remove_comments_and_join_query_lines(input), expected_output);
335    }
336}