schema.rs (7528B)
1 //! Exact retained outbox catalogs, shared by creation and read-only admission. 2 3 use super::{WorkspaceError, attempt, delivery, evidence, installation, listing, operation, plan}; 4 use sqlx::SqliteConnection; 5 6 pub(super) const CURRENT_VERSION: i64 = 11; 7 const GRANT: &str = "CREATE TABLE workspace_grant (singleton INTEGER PRIMARY KEY CHECK(singleton=1), workspace BLOB NOT NULL CHECK(length(workspace)=32), grant_id BLOB NOT NULL CHECK(length(grant_id)=32), protected BLOB NOT NULL CHECK(length(protected)>0 AND length(protected)<=262144)) STRICT"; 8 9 pub(super) struct Object { 10 kind: &'static str, 11 name: &'static str, 12 owner: &'static str, 13 pub sql: &'static str, 14 } 15 impl Object { 16 const fn table(name: &'static str, sql: &'static str) -> Self { 17 Self { 18 kind: "table", 19 name, 20 owner: name, 21 sql, 22 } 23 } 24 const fn index(name: &'static str, owner: &'static str, sql: &'static str) -> Self { 25 Self { 26 kind: "index", 27 name, 28 owner, 29 sql, 30 } 31 } 32 } 33 34 /// These are retained identities, never automatic migrations. Adding a schema 35 /// requires an explicit catalog and its independently qualified admission path. 36 pub(super) fn catalog(version: i64) -> Result<Vec<Object>, WorkspaceError> { 37 if !matches!(version, 7..=11) { 38 return Err(WorkspaceError::RecoveryRequired); 39 } 40 let artifacts = version >= 9; 41 let mut objects = vec![ 42 Object::table("workspace_grant", GRANT), 43 Object::table( 44 "operations", 45 if version == 7 { 46 operation::TABLE_V7 47 } else { 48 operation::TABLE 49 }, 50 ), 51 Object::index( 52 "operation_transaction_identity", 53 "operations", 54 operation::INDEX, 55 ), 56 Object::table("signing_plans", plan::TABLE), 57 Object::table("signing_plan_parts", plan::PARTS), 58 Object::table( 59 "signing_attempts", 60 if artifacts { 61 attempt::TABLE 62 } else { 63 attempt::TABLE_V8 64 }, 65 ), 66 Object::index( 67 "signing_attempt_identity", 68 "signing_attempts", 69 attempt::ID_INDEX, 70 ), 71 Object::index( 72 "signing_attempt_transaction_identity", 73 "signing_attempts", 74 attempt::TX_INDEX, 75 ), 76 Object::table( 77 if artifacts { 78 "artifact_evidence" 79 } else { 80 "seal_evidence" 81 }, 82 if artifacts { 83 evidence::TABLE 84 } else { 85 evidence::TABLE_V8 86 }, 87 ), 88 Object::table( 89 if artifacts { 90 "artifact_evidence_parts" 91 } else { 92 "seal_evidence_parts" 93 }, 94 if artifacts { 95 evidence::PARTS 96 } else { 97 evidence::PARTS_V8 98 }, 99 ), 100 Object::index( 101 if artifacts { 102 "artifact_evidence_transaction_identity" 103 } else { 104 "seal_evidence_transaction_identity" 105 }, 106 if artifacts { 107 "artifact_evidence" 108 } else { 109 "seal_evidence" 110 }, 111 if artifacts { 112 evidence::TX_INDEX 113 } else { 114 evidence::TX_INDEX_V8 115 }, 116 ), 117 Object::table("chat_messages", installation::TABLE), 118 Object::index( 119 "chat_message_operation_identity", 120 "chat_messages", 121 installation::OP_INDEX, 122 ), 123 Object::index( 124 "chat_message_transaction_identity", 125 "chat_messages", 126 installation::TX_INDEX, 127 ), 128 Object::index( 129 "chat_message_thread", 130 "chat_messages", 131 installation::THREAD_INDEX, 132 ), 133 Object::table( 134 "chat_publication_work", 135 if artifacts { 136 installation::WORK 137 } else { 138 installation::WORK_V8 139 }, 140 ), 141 Object::table( 142 if version == 11 { 143 "artifact_delivery_attempts" 144 } else { 145 "chat_delivery_attempts" 146 }, 147 if version == 11 { 148 delivery::TABLE 149 } else { 150 delivery::TABLE_V10 151 }, 152 ), 153 Object::table( 154 if version == 11 { 155 "artifact_delivery_parts" 156 } else { 157 "chat_delivery_parts" 158 }, 159 if version == 11 { 160 delivery::PARTS 161 } else { 162 delivery::PARTS_V10 163 }, 164 ), 165 Object::index( 166 if version == 11 { 167 "artifact_delivery_transaction_identity" 168 } else { 169 "chat_delivery_transaction_identity" 170 }, 171 if version == 11 { 172 "artifact_delivery_attempts" 173 } else { 174 "chat_delivery_attempts" 175 }, 176 if version == 11 { 177 delivery::TX_INDEX 178 } else { 179 delivery::TX_INDEX_V10 180 }, 181 ), 182 Object::table( 183 if version == 11 { 184 "artifact_delivery_observations" 185 } else { 186 "chat_delivery_observations" 187 }, 188 if version == 11 { 189 delivery::OBSERVATIONS 190 } else { 191 delivery::OBSERVATIONS_V10 192 }, 193 ), 194 ]; 195 if version >= 10 { 196 objects.extend([ 197 Object::table("listing_creations", listing::CREATIONS), 198 Object::index( 199 "listing_coordinate_identity", 200 "listing_creations", 201 listing::COORDINATE_INDEX, 202 ), 203 Object::table("listing_installations", super::listing_installation::TABLE), 204 Object::index( 205 "listing_installation_transaction_identity", 206 "listing_installations", 207 super::listing_installation::TX_INDEX, 208 ), 209 Object::table("public_publication_work", super::listing_installation::WORK), 210 ]); 211 } 212 Ok(objects) 213 } 214 215 /// The aggregate materializes one scalar, never attacker-sized schema text. 216 /// Both the complete object count and each exact identity/definition must match. 217 pub(super) async fn admit( 218 connection: &mut SqliteConnection, 219 version: i64, 220 ) -> Result<(), WorkspaceError> { 221 let objects = catalog(version)?; 222 let mut sql = String::from("SELECT CASE WHEN count(*)=? AND sum(CASE"); 223 for _ in &objects { 224 sql.push_str(" WHEN type=? AND name=? AND tbl_name=? AND sql=? THEN 1"); 225 } 226 sql.push_str(" ELSE 0 END)=? THEN 1 ELSE 0 END FROM sqlite_schema"); 227 let mut query = sqlx::query_scalar(sqlx::AssertSqlSafe(sql)).bind(objects.len() as i64); 228 for object in &objects { 229 query = query 230 .bind(object.kind) 231 .bind(object.name) 232 .bind(object.owner) 233 .bind(object.sql); 234 } 235 let matched: i64 = query 236 .bind(objects.len() as i64) 237 .fetch_one(connection) 238 .await 239 .map_err(|_| WorkspaceError::RecoveryRequired)?; 240 if matched != 1 { 241 return Err(WorkspaceError::RecoveryRequired); 242 } 243 Ok(()) 244 }