cat_gateway/db/index/schema/
mod.rs1use 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
14const CREATE_NAMESPACE_CQL: &str = include_str!("./cql/namespace.cql");
16
17const SCHEMAS: &[(&str, &str)] = &[
19 (
20 include_str!("./cql/sync_status.cql"),
22 "Create Sync Status Table",
23 ),
24 (
25 include_str!("./cql/txo_by_stake_table.cql"),
27 "Create Table TXO By Stake Address",
28 ),
29 (
30 include_str!("./cql/txo_assets_by_stake_table.cql"),
32 "Create Table TXO Assets By Stake Address",
33 ),
34 (
35 include_str!("cql/unstaked_txo_by_txn_id.cql"),
37 "Create Table Unstaked TXO By Txn Hash",
38 ),
39 (
40 include_str!("cql/unstaked_txo_assets_by_txn_id.cql"),
42 "Create Table Unstaked TXO Assets By Txn Hash",
43 ),
44 (
45 include_str!("cql/txi_by_txn_id_table.cql"),
47 "Create Table TXI By Stake Address",
48 ),
49 (
50 include_str!("cql/stake_registration.cql"),
52 "Create Table Stake Registration",
53 ),
54 (
55 include_str!("cql/cip36_registration.cql"),
57 "Create Table CIP-36 Registration",
58 ),
59 (
60 include_str!("cql/cip36_registration_invalid.cql"),
62 "Create Table CIP-36 Registration Invalid",
63 ),
64 (
65 include_str!("cql/cip36_registration_for_vote_key.cql"),
67 "Create Table CIP-36 Registration For a stake address",
68 ),
69 (
70 include_str!("cql/rbac_registration.cql"),
72 "Create Table RBAC Registration",
73 ),
74 (
75 include_str!("cql/rbac_invalid_registration.cql"),
77 "Create Table Invalid RBAC Registration",
78 ),
79 (
80 include_str!("cql/catalyst_id_for_txn_id.cql"),
82 "Create Table Catalyst ID For TX ID",
83 ),
84 (
85 include_str!("cql/catalyst_id_for_stake_address.cql"),
87 "Create Table Catalyst ID For Stake Address",
88 ),
89];
90
91fn remove_comments_and_join_query_lines(text: &str) -> String {
104 let raw_lines: Vec<&str> = text.lines().collect();
106 let mut clean_lines: Vec<String> = Vec::new();
107
108 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
127fn generate_cql_schema_version() -> String {
140 let mut clean_schemas: Vec<String> = Vec::new();
142
143 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 clean_schemas.sort();
154
155 generate_uuid_string_from_data("Catalyst-Gateway Index Database Schema", &clean_schemas)
158}
159
160pub(crate) fn namespace(persistent: bool, network: Network) -> String {
162 let namespace = if persistent { "p" } else { "v" };
164 format!(
165 "{namespace}_{network}_{}",
166 generate_cql_schema_version().replace('-', "_")
167 )
168}
169
170async 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 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 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 session.await_schema_agreement().await?;
200
201 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
209pub(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 session.await_schema_agreement().await?;
249
250 Ok(())
251}
252
253#[cfg(test)]
254mod tests {
255 use super::*;
256
257 const SCHEMA_VERSION: &str = "504c9e72-9737-849c-8212-37824eff881a";
264
265 #[test]
266 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}