mz_deploy/client/
provisioning.rs1use crate::client::connection::ProvisioningClient;
31use crate::client::errors::ConnectionError;
32use crate::client::models::ClusterConfig;
33use crate::client::quote_identifier;
34use mz_sql_parser::ast::Ident;
35use mz_sql_parser::ast::display::AstDisplay;
36
37impl ProvisioningClient<'_> {
38 pub async fn create_database(&self, database: &str) -> Result<(), ConnectionError> {
40 let sql = format!(
41 "CREATE DATABASE IF NOT EXISTS {}",
42 quote_identifier(database)
43 );
44
45 self.client.execute(&sql, &[]).await.map_err(|e| {
46 ConnectionError::DatabaseCreationFailed {
47 database: database.to_string(),
48 source: Box::new(e),
49 }
50 })?;
51
52 Ok(())
53 }
54
55 pub async fn create_schema(&self, database: &str, schema: &str) -> Result<(), ConnectionError> {
57 let sql = format!(
58 "CREATE SCHEMA IF NOT EXISTS {}.{}",
59 quote_identifier(database),
60 quote_identifier(schema)
61 );
62
63 self.client.execute(&sql, &[]).await.map_err(|e| {
64 ConnectionError::SchemaCreationFailed {
65 database: database.to_string(),
66 schema: schema.to_string(),
67 source: Box::new(e),
68 }
69 })?;
70
71 Ok(())
72 }
73
74 pub async fn create_cluster_with_config(
76 &self,
77 name: &str,
78 config: &ClusterConfig,
79 ) -> Result<(), ConnectionError> {
80 let grants = match config {
81 ClusterConfig::Managed {
82 create_stmt,
83 grants,
84 } => {
85 let mut create_stmt = create_stmt.clone();
86 create_stmt.name =
87 Ident::new(name).map_err(|e| ConnectionError::ClusterCreationFailed {
88 name: name.to_string(),
89 source: Box::new(e),
90 })?;
91
92 self.client
93 .execute(&create_stmt.to_ast_string_simple(), &[])
94 .await
95 .map_err(|e| {
96 if e.to_string().contains("already exists") {
97 ConnectionError::ClusterAlreadyExists {
98 name: name.to_string(),
99 }
100 } else {
101 ConnectionError::ClusterCreationFailed {
102 name: name.to_string(),
103 source: Box::new(e),
104 }
105 }
106 })?;
107
108 grants
109 }
110 ClusterConfig::Unmanaged {
111 replicas,
112 grants,
113 query_policy,
114 } => {
115 let create_cluster_sql = unmanaged_cluster_sql(name, query_policy.as_deref());
116
117 self.client
118 .execute(&create_cluster_sql, &[])
119 .await
120 .map_err(|e| {
121 if e.to_string().contains("already exists") {
122 ConnectionError::ClusterAlreadyExists {
123 name: name.to_string(),
124 }
125 } else {
126 ConnectionError::ClusterCreationFailed {
127 name: name.to_string(),
128 source: Box::new(e),
129 }
130 }
131 })?;
132
133 for replica in replicas {
134 let mut options_parts = vec![format!("SIZE = '{}'", replica.size)];
135
136 if let Some(ref az) = replica.availability_zone {
137 options_parts.push(format!("AVAILABILITY ZONE '{}'", az));
138 }
139
140 let create_replica_sql = format!(
141 "CREATE CLUSTER REPLICA {}.{} ({})",
142 quote_identifier(name),
143 quote_identifier(&replica.name),
144 options_parts.join(", ")
145 );
146
147 self.client
148 .execute(&create_replica_sql, &[])
149 .await
150 .map_err(|e| ConnectionError::ClusterCreationFailed {
151 name: format!("{}.{}", name, replica.name),
152 source: Box::new(e),
153 })?;
154 }
155
156 grants
157 }
158 };
159
160 for grant in grants {
161 let sql = format!(
162 "GRANT {} ON CLUSTER {} TO {}",
163 grant.privilege_type,
164 quote_identifier(name),
165 quote_identifier(&grant.grantee)
166 );
167 self.client.execute(&sql, &[]).await.map_err(|e| {
168 ConnectionError::Message(format!(
169 "Failed to grant {} to {} on cluster '{}': {}",
170 grant.privilege_type, grant.grantee, name, e
171 ))
172 })?;
173 }
174
175 Ok(())
176 }
177}
178
179fn unmanaged_cluster_sql(name: &str, query_policy: Option<&str>) -> String {
180 let mut sql = format!("CREATE CLUSTER {} REPLICAS ()", quote_identifier(name));
181 if let Some(policy) = query_policy {
182 sql.push_str(&format!(", QUERY POLICY = {}", quote_identifier(policy)));
183 }
184 sql
185}
186
187#[cfg(test)]
188mod tests {
189 use super::*;
190
191 #[mz_ore::test]
192 fn unmanaged_cluster_policy() {
193 for policy in [None, Some("policy"), Some("Policy \"quoted\"")] {
194 let sql = unmanaged_cluster_sql("Cluster \"quoted\"", policy);
195 let parsed = crate::client::parse_create_cluster(&sql).unwrap();
196 let expected = match policy {
197 None => r#"CREATE CLUSTER "Cluster ""quoted""" (REPLICAS = ())"#,
198 Some("policy") => {
199 r#"CREATE CLUSTER "Cluster ""quoted""" (REPLICAS = (), QUERY POLICY = "policy")"#
200 }
201 Some(_) => {
202 r#"CREATE CLUSTER "Cluster ""quoted""" (REPLICAS = (), QUERY POLICY = "Policy ""quoted""")"#
203 }
204 };
205 assert_eq!(
206 parsed,
207 crate::client::parse_create_cluster(expected).unwrap()
208 );
209 }
210 }
211}