schema.rs (40836B)
1 //! Exact stock catalogs. Enrollment-only version1 is retained without upgrade. 2 use super::WorkspaceError; 3 use sqlx::SqliteConnection; 4 pub(super) const CURRENT: i64 = 20; 5 const AUTHORITY: &str = "CREATE TABLE stock_authority (singleton INTEGER PRIMARY KEY CHECK(singleton=1), protected BLOB NOT NULL CHECK(length(protected)>0 AND length(protected)<=262144)) STRICT"; 6 const GRANTS: &str = "CREATE TABLE stock_grants (grant_id BLOB PRIMARY KEY CHECK(length(grant_id)=32), protected BLOB NOT NULL CHECK(length(protected)>0 AND length(protected)<=262144)) STRICT, WITHOUT ROWID"; 7 const POOLS: &str = "CREATE TABLE stock_pools (pool_id BLOB PRIMARY KEY CHECK(length(pool_id)=32), revision INTEGER NOT NULL CHECK(revision>0), protected BLOB NOT NULL CHECK(length(protected)>0 AND length(protected)<=262144)) STRICT, WITHOUT ROWID"; 8 const OPERATIONS: &str = "CREATE TABLE stock_operations (actor BLOB NOT NULL CHECK(length(actor)=32), grant_id BLOB NOT NULL REFERENCES stock_grants(grant_id), operation_id BLOB NOT NULL CHECK(length(operation_id)=32), command_digest BLOB NOT NULL CHECK(length(command_digest)=32), transaction_id BLOB NOT NULL CHECK(length(transaction_id)=32), pool_id BLOB NOT NULL REFERENCES stock_pools(pool_id) DEFERRABLE INITIALLY DEFERRED, revision INTEGER NOT NULL CHECK(revision>0), chain_head BLOB NOT NULL CHECK(length(chain_head)=32), protected BLOB NOT NULL CHECK(length(protected)>0 AND length(protected)<=262144), PRIMARY KEY(actor,grant_id,operation_id)) STRICT, WITHOUT ROWID"; 9 const TX: &str = 10 "CREATE UNIQUE INDEX stock_operation_transaction_identity ON stock_operations(transaction_id)"; 11 const REVISION: &str = 12 "CREATE UNIQUE INDEX stock_operation_revision_identity ON stock_operations(revision)"; 13 const HEAD: &str = 14 "CREATE UNIQUE INDEX stock_operation_head_identity ON stock_operations(chain_head)"; 15 const OPERATIONS_V7: &str = "CREATE TABLE stock_operations (actor BLOB NOT NULL CHECK(length(actor)=32), grant_id BLOB NOT NULL REFERENCES stock_grants(grant_id), operation_id BLOB NOT NULL CHECK(length(operation_id)=32), command_digest BLOB NOT NULL CHECK(length(command_digest)=32), transaction_id BLOB NOT NULL CHECK(length(transaction_id)=32), pool_id BLOB REFERENCES stock_pools(pool_id) DEFERRABLE INITIALLY DEFERRED, revision INTEGER NOT NULL CHECK(revision>0), chain_head BLOB NOT NULL CHECK(length(chain_head)=32), protected BLOB NOT NULL CHECK(length(protected)>0 AND length(protected)<=262144), family INTEGER NOT NULL DEFAULT 0 CHECK(family BETWEEN 0 AND 2), order_id BLOB CHECK(order_id IS NULL OR length(order_id)=32), related_operation BLOB CHECK(related_operation IS NULL OR length(related_operation)=32), subject_grant BLOB REFERENCES stock_grants(grant_id) CHECK(subject_grant IS NULL OR length(subject_grant)=32), PRIMARY KEY(actor,grant_id,operation_id), CHECK((family=0 AND pool_id IS NOT NULL AND order_id IS NULL AND related_operation IS NULL AND subject_grant IS NULL) OR (family=1 AND pool_id IS NULL AND order_id IS NOT NULL AND related_operation IS NOT NULL AND subject_grant IS NOT NULL) OR (family=2 AND pool_id IS NULL AND order_id IS NOT NULL AND related_operation IS NULL AND subject_grant IS NULL))) STRICT, WITHOUT ROWID"; 16 const OPERATIONS_V10: &str = "CREATE TABLE stock_operations (actor BLOB NOT NULL CHECK(length(actor)=32), grant_id BLOB NOT NULL REFERENCES stock_grants(grant_id), operation_id BLOB NOT NULL CHECK(length(operation_id)=32), command_digest BLOB NOT NULL CHECK(length(command_digest)=32), transaction_id BLOB NOT NULL CHECK(length(transaction_id)=32), pool_id BLOB REFERENCES stock_pools(pool_id) DEFERRABLE INITIALLY DEFERRED, revision INTEGER NOT NULL CHECK(revision>0), chain_head BLOB NOT NULL CHECK(length(chain_head)=32), protected BLOB NOT NULL CHECK(length(protected)>0 AND length(protected)<=262144), family INTEGER NOT NULL DEFAULT 0 CHECK(family BETWEEN 0 AND 3), order_id BLOB CHECK(order_id IS NULL OR length(order_id)=32), related_operation BLOB CHECK(related_operation IS NULL OR length(related_operation)=32), subject_grant BLOB REFERENCES stock_grants(grant_id) CHECK(subject_grant IS NULL OR length(subject_grant)=32), PRIMARY KEY(actor,grant_id,operation_id), CHECK((family=0 AND pool_id IS NOT NULL AND order_id IS NULL AND related_operation IS NULL AND subject_grant IS NULL) OR (family=1 AND pool_id IS NULL AND order_id IS NOT NULL AND related_operation IS NOT NULL AND subject_grant IS NOT NULL) OR (family=2 AND pool_id IS NULL AND order_id IS NOT NULL AND related_operation IS NULL AND subject_grant IS NULL) OR (family=3 AND pool_id IS NULL AND order_id IS NOT NULL AND related_operation IS NOT NULL AND subject_grant IS NULL))) STRICT, WITHOUT ROWID"; 17 const OPERATIONS_V11: &str = "CREATE TABLE stock_operations (actor BLOB NOT NULL CHECK(length(actor)=32), grant_id BLOB NOT NULL REFERENCES stock_grants(grant_id), operation_id BLOB NOT NULL CHECK(length(operation_id)=32), command_digest BLOB NOT NULL CHECK(length(command_digest)=32), transaction_id BLOB NOT NULL CHECK(length(transaction_id)=32), pool_id BLOB REFERENCES stock_pools(pool_id) DEFERRABLE INITIALLY DEFERRED, revision INTEGER NOT NULL CHECK(revision>0), chain_head BLOB NOT NULL CHECK(length(chain_head)=32), protected BLOB NOT NULL CHECK(length(protected)>0 AND length(protected)<=262144), family INTEGER NOT NULL DEFAULT 0 CHECK(family BETWEEN 0 AND 5), order_id BLOB CHECK(order_id IS NULL OR length(order_id)=32), related_operation BLOB CHECK(related_operation IS NULL OR length(related_operation)=32), subject_grant BLOB REFERENCES stock_grants(grant_id) CHECK(subject_grant IS NULL OR length(subject_grant)=32), PRIMARY KEY(actor,grant_id,operation_id), CHECK((family=0 AND pool_id IS NOT NULL AND order_id IS NULL AND related_operation IS NULL AND subject_grant IS NULL) OR (family=1 AND pool_id IS NULL AND order_id IS NOT NULL AND related_operation IS NOT NULL AND subject_grant IS NOT NULL) OR (family=2 AND pool_id IS NULL AND order_id IS NOT NULL AND related_operation IS NULL AND subject_grant IS NULL) OR (family BETWEEN 3 AND 5 AND pool_id IS NULL AND order_id IS NOT NULL AND related_operation IS NOT NULL AND subject_grant IS NULL))) STRICT, WITHOUT ROWID"; 18 const OPERATIONS_V12: &str = "CREATE TABLE stock_operations (actor BLOB NOT NULL CHECK(length(actor)=32), grant_id BLOB NOT NULL REFERENCES stock_grants(grant_id), operation_id BLOB NOT NULL CHECK(length(operation_id)=32), command_digest BLOB NOT NULL CHECK(length(command_digest)=32), transaction_id BLOB NOT NULL CHECK(length(transaction_id)=32), pool_id BLOB REFERENCES stock_pools(pool_id) DEFERRABLE INITIALLY DEFERRED, revision INTEGER NOT NULL CHECK(revision>0), chain_head BLOB NOT NULL CHECK(length(chain_head)=32), protected BLOB NOT NULL CHECK(length(protected)>0 AND length(protected)<=262144), family INTEGER NOT NULL DEFAULT 0 CHECK(family BETWEEN 0 AND 6), order_id BLOB CHECK(order_id IS NULL OR length(order_id)=32), related_operation BLOB CHECK(related_operation IS NULL OR length(related_operation)=32), subject_grant BLOB REFERENCES stock_grants(grant_id) CHECK(subject_grant IS NULL OR length(subject_grant)=32), PRIMARY KEY(actor,grant_id,operation_id), CHECK((family=0 AND pool_id IS NOT NULL AND order_id IS NULL AND related_operation IS NULL AND subject_grant IS NULL) OR (family=1 AND pool_id IS NULL AND order_id IS NOT NULL AND related_operation IS NOT NULL AND subject_grant IS NOT NULL) OR (family=2 AND pool_id IS NULL AND order_id IS NOT NULL AND related_operation IS NULL AND subject_grant IS NULL) OR (family BETWEEN 3 AND 6 AND pool_id IS NULL AND order_id IS NOT NULL AND related_operation IS NOT NULL AND subject_grant IS NULL))) STRICT, WITHOUT ROWID"; 19 const OPERATIONS_V13: &str = "CREATE TABLE stock_operations (actor BLOB NOT NULL CHECK(length(actor)=32), grant_id BLOB NOT NULL REFERENCES stock_grants(grant_id), operation_id BLOB NOT NULL CHECK(length(operation_id)=32), command_digest BLOB NOT NULL CHECK(length(command_digest)=32), transaction_id BLOB NOT NULL CHECK(length(transaction_id)=32), pool_id BLOB REFERENCES stock_pools(pool_id) DEFERRABLE INITIALLY DEFERRED, revision INTEGER NOT NULL CHECK(revision>0), chain_head BLOB NOT NULL CHECK(length(chain_head)=32), protected BLOB NOT NULL CHECK(length(protected)>0 AND length(protected)<=262144), family INTEGER NOT NULL DEFAULT 0 CHECK(family BETWEEN 0 AND 7), order_id BLOB CHECK(order_id IS NULL OR length(order_id)=32), related_operation BLOB CHECK(related_operation IS NULL OR length(related_operation)=32), subject_grant BLOB REFERENCES stock_grants(grant_id) CHECK(subject_grant IS NULL OR length(subject_grant)=32), PRIMARY KEY(actor,grant_id,operation_id), CHECK((family=0 AND pool_id IS NOT NULL AND order_id IS NULL AND related_operation IS NULL AND subject_grant IS NULL) OR (family=1 AND pool_id IS NULL AND order_id IS NOT NULL AND related_operation IS NOT NULL AND subject_grant IS NOT NULL) OR (family=2 AND pool_id IS NULL AND order_id IS NOT NULL AND related_operation IS NULL AND subject_grant IS NULL) OR (family BETWEEN 3 AND 7 AND pool_id IS NULL AND order_id IS NOT NULL AND related_operation IS NOT NULL AND subject_grant IS NULL))) STRICT, WITHOUT ROWID"; 20 const OPERATIONS_V14: &str = "CREATE TABLE stock_operations (actor BLOB NOT NULL CHECK(length(actor)=32), grant_id BLOB NOT NULL REFERENCES stock_grants(grant_id), operation_id BLOB NOT NULL CHECK(length(operation_id)=32), command_digest BLOB NOT NULL CHECK(length(command_digest)=32), transaction_id BLOB NOT NULL CHECK(length(transaction_id)=32), pool_id BLOB REFERENCES stock_pools(pool_id) DEFERRABLE INITIALLY DEFERRED, revision INTEGER NOT NULL CHECK(revision>0), chain_head BLOB NOT NULL CHECK(length(chain_head)=32), protected BLOB NOT NULL CHECK(length(protected)>0 AND length(protected)<=262144), family INTEGER NOT NULL DEFAULT 0 CHECK(family BETWEEN 0 AND 8), order_id BLOB CHECK(order_id IS NULL OR length(order_id)=32), related_operation BLOB CHECK(related_operation IS NULL OR length(related_operation)=32), subject_grant BLOB REFERENCES stock_grants(grant_id) CHECK(subject_grant IS NULL OR length(subject_grant)=32), PRIMARY KEY(actor,grant_id,operation_id), CHECK((family=0 AND pool_id IS NOT NULL AND order_id IS NULL AND related_operation IS NULL AND subject_grant IS NULL) OR (family=1 AND pool_id IS NULL AND order_id IS NOT NULL AND related_operation IS NOT NULL AND subject_grant IS NOT NULL) OR (family=2 AND pool_id IS NULL AND order_id IS NOT NULL AND related_operation IS NULL AND subject_grant IS NULL) OR (family BETWEEN 3 AND 8 AND pool_id IS NULL AND order_id IS NOT NULL AND related_operation IS NOT NULL AND subject_grant IS NULL))) STRICT, WITHOUT ROWID"; 21 const OPERATIONS_V15: &str = "CREATE TABLE stock_operations (actor BLOB NOT NULL CHECK(length(actor)=32), grant_id BLOB NOT NULL REFERENCES stock_grants(grant_id), operation_id BLOB NOT NULL CHECK(length(operation_id)=32), command_digest BLOB NOT NULL CHECK(length(command_digest)=32), transaction_id BLOB NOT NULL CHECK(length(transaction_id)=32), pool_id BLOB REFERENCES stock_pools(pool_id) DEFERRABLE INITIALLY DEFERRED, revision INTEGER NOT NULL CHECK(revision>0), chain_head BLOB NOT NULL CHECK(length(chain_head)=32), protected BLOB NOT NULL CHECK(length(protected)>0 AND length(protected)<=262144), family INTEGER NOT NULL DEFAULT 0 CHECK(family BETWEEN 0 AND 9), order_id BLOB CHECK(order_id IS NULL OR length(order_id)=32), related_operation BLOB CHECK(related_operation IS NULL OR length(related_operation)=32), subject_grant BLOB REFERENCES stock_grants(grant_id) CHECK(subject_grant IS NULL OR length(subject_grant)=32), PRIMARY KEY(actor,grant_id,operation_id), CHECK((family=0 AND pool_id IS NOT NULL AND order_id IS NULL AND related_operation IS NULL AND subject_grant IS NULL) OR (family=1 AND pool_id IS NULL AND order_id IS NOT NULL AND related_operation IS NOT NULL AND subject_grant IS NOT NULL) OR (family=2 AND pool_id IS NULL AND order_id IS NOT NULL AND related_operation IS NULL AND subject_grant IS NULL) OR (family BETWEEN 3 AND 9 AND pool_id IS NULL AND order_id IS NOT NULL AND related_operation IS NOT NULL AND subject_grant IS NULL))) STRICT, WITHOUT ROWID"; 22 const OPERATIONS_V16: &str = "CREATE TABLE stock_operations (actor BLOB NOT NULL CHECK(length(actor)=32), grant_id BLOB NOT NULL REFERENCES stock_grants(grant_id), operation_id BLOB NOT NULL CHECK(length(operation_id)=32), command_digest BLOB NOT NULL CHECK(length(command_digest)=32), transaction_id BLOB NOT NULL CHECK(length(transaction_id)=32), pool_id BLOB REFERENCES stock_pools(pool_id) DEFERRABLE INITIALLY DEFERRED, revision INTEGER NOT NULL CHECK(revision>0), chain_head BLOB NOT NULL CHECK(length(chain_head)=32), protected BLOB NOT NULL CHECK(length(protected)>0 AND length(protected)<=262144), family INTEGER NOT NULL DEFAULT 0 CHECK(family BETWEEN 0 AND 10), order_id BLOB CHECK(order_id IS NULL OR length(order_id)=32), related_operation BLOB CHECK(related_operation IS NULL OR length(related_operation)=32), subject_grant BLOB REFERENCES stock_grants(grant_id) CHECK(subject_grant IS NULL OR length(subject_grant)=32), PRIMARY KEY(actor,grant_id,operation_id), CHECK((family=0 AND pool_id IS NOT NULL AND order_id IS NULL AND related_operation IS NULL AND subject_grant IS NULL) OR (family=1 AND pool_id IS NULL AND order_id IS NOT NULL AND related_operation IS NOT NULL AND subject_grant IS NOT NULL) OR (family=2 AND pool_id IS NULL AND order_id IS NOT NULL AND related_operation IS NULL AND subject_grant IS NULL) OR (family BETWEEN 3 AND 10 AND pool_id IS NULL AND order_id IS NOT NULL AND related_operation IS NOT NULL AND subject_grant IS NULL))) STRICT, WITHOUT ROWID"; 23 const OPERATIONS_V17: &str = "CREATE TABLE stock_operations (actor BLOB NOT NULL CHECK(length(actor)=32), grant_id BLOB NOT NULL REFERENCES stock_grants(grant_id), operation_id BLOB NOT NULL CHECK(length(operation_id)=32), command_digest BLOB NOT NULL CHECK(length(command_digest)=32), transaction_id BLOB NOT NULL CHECK(length(transaction_id)=32), pool_id BLOB REFERENCES stock_pools(pool_id) DEFERRABLE INITIALLY DEFERRED, revision INTEGER NOT NULL CHECK(revision>0), chain_head BLOB NOT NULL CHECK(length(chain_head)=32), protected BLOB NOT NULL CHECK(length(protected)>0 AND length(protected)<=262144), family INTEGER NOT NULL DEFAULT 0 CHECK(family BETWEEN 0 AND 12), order_id BLOB CHECK(order_id IS NULL OR length(order_id)=32), related_operation BLOB CHECK(related_operation IS NULL OR length(related_operation)=32), subject_grant BLOB REFERENCES stock_grants(grant_id) CHECK(subject_grant IS NULL OR length(subject_grant)=32), PRIMARY KEY(actor,grant_id,operation_id), CHECK((family=0 AND pool_id IS NOT NULL AND order_id IS NULL AND related_operation IS NULL AND subject_grant IS NULL) OR (family=1 AND pool_id IS NULL AND order_id IS NOT NULL AND related_operation IS NOT NULL AND subject_grant IS NOT NULL) OR (family=2 AND pool_id IS NULL AND order_id IS NOT NULL AND related_operation IS NULL AND subject_grant IS NULL) OR (family BETWEEN 3 AND 12 AND pool_id IS NULL AND order_id IS NOT NULL AND related_operation IS NOT NULL AND subject_grant IS NULL))) STRICT, WITHOUT ROWID"; 24 const OPERATIONS_V18: &str = "CREATE TABLE stock_operations (actor BLOB NOT NULL CHECK(length(actor)=32), grant_id BLOB NOT NULL REFERENCES stock_grants(grant_id), operation_id BLOB NOT NULL CHECK(length(operation_id)=32), command_digest BLOB NOT NULL CHECK(length(command_digest)=32), transaction_id BLOB NOT NULL CHECK(length(transaction_id)=32), pool_id BLOB REFERENCES stock_pools(pool_id) DEFERRABLE INITIALLY DEFERRED, revision INTEGER NOT NULL CHECK(revision>0), chain_head BLOB NOT NULL CHECK(length(chain_head)=32), protected BLOB NOT NULL CHECK(length(protected)>0 AND length(protected)<=262144), family INTEGER NOT NULL DEFAULT 0 CHECK(family BETWEEN 0 AND 14), order_id BLOB CHECK(order_id IS NULL OR length(order_id)=32), related_operation BLOB CHECK(related_operation IS NULL OR length(related_operation)=32), subject_grant BLOB REFERENCES stock_grants(grant_id) CHECK(subject_grant IS NULL OR length(subject_grant)=32), PRIMARY KEY(actor,grant_id,operation_id), CHECK((family=0 AND pool_id IS NOT NULL AND order_id IS NULL AND related_operation IS NULL AND subject_grant IS NULL) OR (family=1 AND pool_id IS NULL AND order_id IS NOT NULL AND related_operation IS NOT NULL AND subject_grant IS NOT NULL) OR (family=2 AND pool_id IS NULL AND order_id IS NOT NULL AND related_operation IS NULL AND subject_grant IS NULL) OR (family BETWEEN 3 AND 14 AND pool_id IS NULL AND order_id IS NOT NULL AND related_operation IS NOT NULL AND subject_grant IS NULL))) STRICT, WITHOUT ROWID"; 25 const MOVEMENTS: &str = "CREATE TABLE stock_movements (movement_id BLOB PRIMARY KEY CHECK(length(movement_id)=32), actor BLOB NOT NULL CHECK(length(actor)=32), grant_id BLOB NOT NULL CHECK(length(grant_id)=32), operation_id BLOB NOT NULL CHECK(length(operation_id)=32), pool_id BLOB NOT NULL REFERENCES stock_pools(pool_id), protected BLOB NOT NULL CHECK(length(protected)>0 AND length(protected)<=262144), FOREIGN KEY(actor,grant_id,operation_id) REFERENCES stock_operations(actor,grant_id,operation_id)) STRICT, WITHOUT ROWID"; 26 pub(super) fn objects( 27 version: i64, 28 ) -> Result<Vec<(&'static str, &'static str, &'static str, &'static str)>, WorkspaceError> { 29 let mut objects = vec![ 30 ("table", "stock_authority", "stock_authority", AUTHORITY), 31 ("table", "stock_grants", "stock_grants", GRANTS), 32 ]; 33 match version { 34 1 => {} 35 2..=20 => objects.extend([ 36 ("table", "stock_pools", "stock_pools", POOLS), 37 ( 38 "table", 39 "stock_operations", 40 "stock_operations", 41 if version >= 18 { 42 OPERATIONS_V18 43 } else if version >= 17 { 44 OPERATIONS_V17 45 } else if version >= 16 { 46 OPERATIONS_V16 47 } else if version >= 15 { 48 OPERATIONS_V15 49 } else if version >= 14 { 50 OPERATIONS_V14 51 } else if version >= 13 { 52 OPERATIONS_V13 53 } else if version >= 12 { 54 OPERATIONS_V12 55 } else if version >= 11 { 56 OPERATIONS_V11 57 } else if version >= 10 { 58 OPERATIONS_V10 59 } else if version >= 7 { 60 OPERATIONS_V7 61 } else { 62 OPERATIONS 63 }, 64 ), 65 ( 66 "index", 67 "stock_operation_transaction_identity", 68 "stock_operations", 69 TX, 70 ), 71 ( 72 "index", 73 "stock_operation_revision_identity", 74 "stock_operations", 75 REVISION, 76 ), 77 ( 78 "index", 79 "stock_operation_head_identity", 80 "stock_operations", 81 HEAD, 82 ), 83 ("table", "stock_movements", "stock_movements", MOVEMENTS), 84 ]), 85 _ => return Err(WorkspaceError::RecoveryRequired), 86 } 87 if version >= 4 { 88 objects.push(("table", "stock_grant_changes", "stock_grant_changes", "CREATE TABLE stock_grant_changes (transaction_id BLOB PRIMARY KEY CHECK(length(transaction_id)=32), revision INTEGER NOT NULL CHECK(revision>1), protected BLOB NOT NULL CHECK(length(protected)>0 AND length(protected)<=262144)) STRICT, WITHOUT ROWID")); 89 objects.push(( 90 "index", 91 "stock_grant_change_revision", 92 "stock_grant_changes", 93 "CREATE UNIQUE INDEX stock_grant_change_revision ON stock_grant_changes(revision)", 94 )); 95 objects.push(("index", "stock_actor_operation_identity", "stock_operations", "CREATE UNIQUE INDEX stock_actor_operation_identity ON stock_operations(actor,operation_id)")); 96 } 97 if version >= 5 { 98 objects.push(("table","stock_mappings","stock_mappings","CREATE TABLE stock_mappings (listing_author BLOB NOT NULL CHECK(length(listing_author)=32), listing_d BLOB NOT NULL CHECK(length(listing_d)=16), option_id BLOB NOT NULL CHECK(length(option_id)=32), pool_id BLOB NOT NULL REFERENCES stock_pools(pool_id), revision INTEGER NOT NULL CHECK(revision>0), protected BLOB NOT NULL CHECK(length(protected)>0 AND length(protected)<=262144), PRIMARY KEY(listing_author,listing_d,option_id)) STRICT, WITHOUT ROWID")); 99 } 100 if (6..=20).contains(&version) { 101 objects.push(("table","stock_source_records","stock_source_records","CREATE TABLE stock_source_records (record_id BLOB PRIMARY KEY CHECK(length(record_id)=32), protected BLOB NOT NULL CHECK(length(protected)>0 AND length(protected)<=262144)) STRICT, WITHOUT ROWID")); 102 objects.push(("table","stock_source_evidence","stock_source_evidence","CREATE TABLE stock_source_evidence (envelope_id BLOB PRIMARY KEY CHECK(length(envelope_id)=32), record_id BLOB REFERENCES stock_source_records(record_id), transaction_id BLOB NOT NULL CHECK(length(transaction_id)=32), revision INTEGER NOT NULL CHECK(revision>0), chain_head BLOB NOT NULL CHECK(length(chain_head)=32), protected BLOB NOT NULL CHECK(length(protected)>0 AND length(protected)<=262144)) STRICT, WITHOUT ROWID")); 103 objects.push(("index","stock_source_transaction_identity","stock_source_evidence","CREATE UNIQUE INDEX stock_source_transaction_identity ON stock_source_evidence(transaction_id)")); 104 objects.push(( 105 "index", 106 "stock_source_revision_identity", 107 "stock_source_evidence", 108 "CREATE UNIQUE INDEX stock_source_revision_identity ON stock_source_evidence(revision)", 109 )); 110 objects.push(( 111 "index", 112 "stock_source_head_identity", 113 "stock_source_evidence", 114 "CREATE UNIQUE INDEX stock_source_head_identity ON stock_source_evidence(chain_head)", 115 )); 116 objects.push(("table","stock_source_parts","stock_source_parts","CREATE TABLE stock_source_parts (envelope_id BLOB NOT NULL REFERENCES stock_source_evidence(envelope_id), role INTEGER NOT NULL CHECK(role BETWEEN 0 AND 2), part INTEGER NOT NULL CHECK(part BETWEEN 0 AND 3), protected BLOB NOT NULL CHECK(length(protected)>0 AND length(protected)<=262144), PRIMARY KEY(envelope_id,role,part)) STRICT, WITHOUT ROWID")); 117 } 118 if version >= 7 { 119 objects.push(("table","stock_operation_parts","stock_operation_parts","CREATE TABLE stock_operation_parts (actor BLOB NOT NULL CHECK(length(actor)=32), grant_id BLOB NOT NULL CHECK(length(grant_id)=32), operation_id BLOB NOT NULL CHECK(length(operation_id)=32), part INTEGER NOT NULL CHECK(part BETWEEN 0 AND 15), protected BLOB NOT NULL CHECK(length(protected)>0 AND length(protected)<=262144), PRIMARY KEY(actor,operation_id,part), FOREIGN KEY(actor,grant_id,operation_id) REFERENCES stock_operations(actor,grant_id,operation_id)) STRICT, WITHOUT ROWID")); 120 objects.push(("index","stock_consent_target_identity","stock_operations","CREATE UNIQUE INDEX stock_consent_target_identity ON stock_operations(actor,related_operation,subject_grant) WHERE family=1")); 121 objects.push(("index","stock_order_operation_position","stock_operations","CREATE INDEX stock_order_operation_position ON stock_operations(order_id,revision) WHERE order_id IS NOT NULL")); 122 if version >= 13 { 123 objects.push(("table","stock_orders","stock_orders","CREATE TABLE stock_orders (actor BLOB NOT NULL CHECK(length(actor)=32), operation_id BLOB NOT NULL CHECK(length(operation_id)=32), order_id BLOB NOT NULL CHECK(length(order_id)=32), revision INTEGER NOT NULL CHECK(revision=1), protected BLOB NOT NULL CHECK(length(protected)>0 AND length(protected)<=262144), PRIMARY KEY(actor,operation_id), FOREIGN KEY(actor,operation_id) REFERENCES stock_operations(actor,operation_id)) STRICT, WITHOUT ROWID")); 124 objects.push(("table","stock_order_states","stock_order_states","CREATE TABLE stock_order_states (order_id BLOB PRIMARY KEY CHECK(length(order_id)=32), actor BLOB NOT NULL CHECK(length(actor)=32), operation_id BLOB NOT NULL CHECK(length(operation_id)=32), entry_id BLOB NOT NULL CHECK(length(entry_id)=32), revision INTEGER NOT NULL CHECK(revision>0), phase INTEGER NOT NULL CHECK(phase BETWEEN 0 AND 2), FOREIGN KEY(actor,operation_id) REFERENCES stock_orders(actor,operation_id), FOREIGN KEY(actor,entry_id) REFERENCES stock_operations(actor,operation_id)) STRICT, WITHOUT ROWID")); 125 objects.push(("table","stock_aborts","stock_aborts","CREATE TABLE stock_aborts (actor BLOB NOT NULL CHECK(length(actor)=32), operation_id BLOB NOT NULL CHECK(length(operation_id)=32), entry_id BLOB NOT NULL CHECK(length(entry_id)=32), PRIMARY KEY(actor,operation_id), FOREIGN KEY(actor,operation_id) REFERENCES stock_orders(actor,operation_id), FOREIGN KEY(actor,entry_id) REFERENCES stock_operations(actor,operation_id)) STRICT, WITHOUT ROWID")); 126 objects.push(( 127 "index", 128 "stock_abort_entry", 129 "stock_aborts", 130 "CREATE UNIQUE INDEX stock_abort_entry ON stock_aborts(actor,entry_id)", 131 )); 132 } else { 133 objects.push(("table","stock_orders","stock_orders","CREATE TABLE stock_orders (order_id BLOB PRIMARY KEY CHECK(length(order_id)=32), revision INTEGER NOT NULL CHECK(revision>0), protected BLOB NOT NULL CHECK(length(protected)>0 AND length(protected)<=262144)) STRICT, WITHOUT ROWID")); 134 } 135 } 136 if matches!(version, 8..=20) { 137 objects.push(("table","stock_public_events","stock_public_events","CREATE TABLE stock_public_events (event_id BLOB PRIMARY KEY CHECK(length(event_id)=32), listing_author BLOB NOT NULL CHECK(length(listing_author)=32), listing_d BLOB NOT NULL CHECK(length(listing_d)=16), created_at BLOB NOT NULL CHECK(length(created_at)=8), transaction_id BLOB NOT NULL CHECK(length(transaction_id)=32), revision INTEGER NOT NULL CHECK(revision>0), chain_head BLOB NOT NULL CHECK(length(chain_head)=32), protected BLOB NOT NULL CHECK(length(protected)>0 AND length(protected)<=262144)) STRICT, WITHOUT ROWID")); 138 objects.push(("index","stock_public_transaction_identity","stock_public_events","CREATE UNIQUE INDEX stock_public_transaction_identity ON stock_public_events(transaction_id)")); 139 objects.push(( 140 "index", 141 "stock_public_revision_identity", 142 "stock_public_events", 143 "CREATE UNIQUE INDEX stock_public_revision_identity ON stock_public_events(revision)", 144 )); 145 objects.push(( 146 "index", 147 "stock_public_head_identity", 148 "stock_public_events", 149 "CREATE UNIQUE INDEX stock_public_head_identity ON stock_public_events(chain_head)", 150 )); 151 objects.push(("table","stock_public_parts","stock_public_parts","CREATE TABLE stock_public_parts (event_id BLOB NOT NULL REFERENCES stock_public_events(event_id), part INTEGER NOT NULL CHECK(part BETWEEN 0 AND 3), protected BLOB NOT NULL CHECK(length(protected)>0 AND length(protected)<=262144), PRIMARY KEY(event_id,part)) STRICT, WITHOUT ROWID")); 152 objects.push(("table","stock_public_heads","stock_public_heads","CREATE TABLE stock_public_heads (listing_author BLOB NOT NULL CHECK(length(listing_author)=32), listing_d BLOB NOT NULL CHECK(length(listing_d)=16), event_id BLOB NOT NULL REFERENCES stock_public_events(event_id), revision INTEGER NOT NULL CHECK(revision>0), protected BLOB NOT NULL CHECK(length(protected)>0 AND length(protected)<=262144), PRIMARY KEY(listing_author,listing_d)) STRICT, WITHOUT ROWID")); 153 } 154 if version >= 10 { 155 objects.push(("index", "stock_signing_plan_identity", "stock_operations", "CREATE UNIQUE INDEX stock_signing_plan_identity ON stock_operations(actor,related_operation) WHERE family=3")); 156 } 157 if version >= 11 { 158 objects.push(("table","stock_signing_claims","stock_signing_claims","CREATE TABLE stock_signing_claims (actor BLOB NOT NULL CHECK(length(actor)=32), operation_id BLOB NOT NULL CHECK(length(operation_id)=32), recipient BLOB NOT NULL CHECK(length(recipient)=32), entry_id BLOB NOT NULL CHECK(length(entry_id)=32), PRIMARY KEY(actor,operation_id,recipient), FOREIGN KEY(actor,entry_id) REFERENCES stock_operations(actor,operation_id), FOREIGN KEY(actor,operation_id) REFERENCES stock_operations(actor,operation_id)) STRICT, WITHOUT ROWID")); 159 objects.push(( 160 "index", 161 "stock_signing_claim_entry", 162 "stock_signing_claims", 163 "CREATE UNIQUE INDEX stock_signing_claim_entry ON stock_signing_claims(actor,entry_id)", 164 )); 165 objects.push(("table","stock_signing_evidence","stock_signing_evidence","CREATE TABLE stock_signing_evidence (actor BLOB NOT NULL CHECK(length(actor)=32), operation_id BLOB NOT NULL CHECK(length(operation_id)=32), recipient BLOB NOT NULL CHECK(length(recipient)=32), evidence_digest BLOB NOT NULL CHECK(length(evidence_digest)=32), entry_id BLOB NOT NULL CHECK(length(entry_id)=32), PRIMARY KEY(actor,operation_id,recipient,evidence_digest), FOREIGN KEY(actor,entry_id) REFERENCES stock_operations(actor,operation_id), FOREIGN KEY(actor,operation_id,recipient) REFERENCES stock_signing_claims(actor,operation_id,recipient)) STRICT, WITHOUT ROWID")); 166 objects.push(("index","stock_signing_evidence_entry","stock_signing_evidence","CREATE UNIQUE INDEX stock_signing_evidence_entry ON stock_signing_evidence(actor,entry_id)")); 167 } 168 if version >= 12 { 169 objects.push(("table","stock_agreements","stock_agreements","CREATE TABLE stock_agreements (order_id BLOB NOT NULL CHECK(length(order_id)=32), commit_sequence INTEGER NOT NULL CHECK(commit_sequence>0), actor BLOB NOT NULL CHECK(length(actor)=32), operation_id BLOB NOT NULL CHECK(length(operation_id)=32), entry_id BLOB NOT NULL CHECK(length(entry_id)=32), record_id BLOB NOT NULL CHECK(length(record_id)=32), PRIMARY KEY(order_id,commit_sequence), FOREIGN KEY(actor,entry_id) REFERENCES stock_operations(actor,operation_id), FOREIGN KEY(actor,operation_id) REFERENCES stock_operations(actor,operation_id)) STRICT, WITHOUT ROWID")); 170 objects.push(( 171 "index", 172 "stock_agreement_operation", 173 "stock_agreements", 174 "CREATE UNIQUE INDEX stock_agreement_operation ON stock_agreements(actor,operation_id)", 175 )); 176 objects.push(( 177 "index", 178 "stock_agreement_entry", 179 "stock_agreements", 180 "CREATE UNIQUE INDEX stock_agreement_entry ON stock_agreements(actor,entry_id)", 181 )); 182 objects.push(( 183 "index", 184 "stock_agreement_record", 185 "stock_agreements", 186 "CREATE UNIQUE INDEX stock_agreement_record ON stock_agreements(record_id)", 187 )); 188 objects.push(("table","stock_publication_work","stock_publication_work","CREATE TABLE stock_publication_work (actor BLOB NOT NULL CHECK(length(actor)=32), operation_id BLOB NOT NULL CHECK(length(operation_id)=32), recipient BLOB NOT NULL CHECK(length(recipient)=32), installation_entry BLOB NOT NULL CHECK(length(installation_entry)=32), PRIMARY KEY(actor,operation_id,recipient), FOREIGN KEY(actor,installation_entry) REFERENCES stock_operations(actor,operation_id), FOREIGN KEY(actor,operation_id) REFERENCES stock_agreements(actor,operation_id)) STRICT, WITHOUT ROWID")); 189 } 190 if version >= 14 { 191 objects.push(("table","stock_original_unissued","stock_original_unissued","CREATE TABLE stock_original_unissued (actor BLOB NOT NULL CHECK(length(actor)=32), operation_id BLOB NOT NULL CHECK(length(operation_id)=32), recipient BLOB NOT NULL CHECK(length(recipient)=32), reason INTEGER NOT NULL CHECK(reason BETWEEN 1 AND 9), entry_id BLOB NOT NULL CHECK(length(entry_id)=32), PRIMARY KEY(actor,operation_id,recipient), FOREIGN KEY(actor,operation_id,recipient) REFERENCES stock_signing_claims(actor,operation_id,recipient), FOREIGN KEY(actor,entry_id) REFERENCES stock_operations(actor,operation_id)) STRICT, WITHOUT ROWID")); 192 objects.push(("index","stock_original_unissued_entry","stock_original_unissued","CREATE UNIQUE INDEX stock_original_unissued_entry ON stock_original_unissued(actor,entry_id)")); 193 } 194 if version >= 15 { 195 objects.push(("table","stock_replay_admissions","stock_replay_admissions","CREATE TABLE stock_replay_admissions (actor BLOB NOT NULL CHECK(length(actor)=32), operation_id BLOB NOT NULL CHECK(length(operation_id)=32), recipient BLOB NOT NULL CHECK(length(recipient)=32), entry_id BLOB NOT NULL CHECK(length(entry_id)=32), PRIMARY KEY(actor,operation_id,recipient), FOREIGN KEY(actor,operation_id,recipient) REFERENCES stock_signing_claims(actor,operation_id,recipient), FOREIGN KEY(actor,entry_id) REFERENCES stock_operations(actor,operation_id)) STRICT, WITHOUT ROWID")); 196 objects.push(("index","stock_replay_admission_entry","stock_replay_admissions","CREATE UNIQUE INDEX stock_replay_admission_entry ON stock_replay_admissions(actor,entry_id)")); 197 } 198 199 if version >= 16 { 200 objects.push(("table","stock_envelopes","stock_envelopes","CREATE TABLE stock_envelopes (actor BLOB NOT NULL CHECK(length(actor)=32), operation_id BLOB NOT NULL CHECK(length(operation_id)=32), recipient BLOB NOT NULL CHECK(length(recipient)=32), entry_id BLOB NOT NULL CHECK(length(entry_id)=32), PRIMARY KEY(actor,operation_id,recipient), FOREIGN KEY(actor,operation_id,recipient) REFERENCES stock_publication_work(actor,operation_id,recipient), FOREIGN KEY(actor,entry_id) REFERENCES stock_operations(actor,operation_id)) STRICT, WITHOUT ROWID")); 201 objects.push(( 202 "index", 203 "stock_envelope_entry", 204 "stock_envelopes", 205 "CREATE UNIQUE INDEX stock_envelope_entry ON stock_envelopes(actor,entry_id)", 206 )); 207 } 208 if version >= 17 { 209 objects.push(("table","stock_delivery_attempts","stock_delivery_attempts","CREATE TABLE stock_delivery_attempts (actor BLOB NOT NULL CHECK(length(actor)=32), operation_id BLOB NOT NULL CHECK(length(operation_id)=32), recipient BLOB NOT NULL CHECK(length(recipient)=32), request_key BLOB NOT NULL CHECK(length(request_key)=32), entry_id BLOB NOT NULL CHECK(length(entry_id)=32), PRIMARY KEY(actor,operation_id,recipient,request_key), FOREIGN KEY(actor,operation_id,recipient) REFERENCES stock_envelopes(actor,operation_id,recipient), FOREIGN KEY(actor,entry_id) REFERENCES stock_operations(actor,operation_id)) STRICT, WITHOUT ROWID")); 210 objects.push(("index","stock_delivery_attempt_entry","stock_delivery_attempts","CREATE UNIQUE INDEX stock_delivery_attempt_entry ON stock_delivery_attempts(actor,entry_id)")); 211 objects.push(("table","stock_delivery_observations","stock_delivery_observations","CREATE TABLE stock_delivery_observations (actor BLOB NOT NULL CHECK(length(actor)=32), operation_id BLOB NOT NULL CHECK(length(operation_id)=32), recipient BLOB NOT NULL CHECK(length(recipient)=32), request_key BLOB NOT NULL CHECK(length(request_key)=32), target_key BLOB NOT NULL CHECK(length(target_key)=32), kind INTEGER NOT NULL CHECK(kind BETWEEN 1 AND 6), entry_id BLOB NOT NULL CHECK(length(entry_id)=32), PRIMARY KEY(actor,operation_id,recipient,request_key,target_key,kind), FOREIGN KEY(actor,operation_id,recipient,request_key) REFERENCES stock_delivery_attempts(actor,operation_id,recipient,request_key), FOREIGN KEY(actor,entry_id) REFERENCES stock_operations(actor,operation_id)) STRICT, WITHOUT ROWID")); 212 objects.push(("index","stock_delivery_observation_entry","stock_delivery_observations","CREATE UNIQUE INDEX stock_delivery_observation_entry ON stock_delivery_observations(actor,entry_id)")); 213 } 214 if version >= 18 { 215 objects.push(("table","stock_rewrap_admissions","stock_rewrap_admissions","CREATE TABLE stock_rewrap_admissions (actor BLOB NOT NULL CHECK(length(actor)=32), operation_id BLOB NOT NULL CHECK(length(operation_id)=32), recipient BLOB NOT NULL CHECK(length(recipient)=32), request_id BLOB NOT NULL CHECK(length(request_id)=32), entry_id BLOB NOT NULL CHECK(length(entry_id)=32), PRIMARY KEY(actor,operation_id,recipient,request_id), FOREIGN KEY(actor,operation_id,recipient) REFERENCES stock_envelopes(actor,operation_id,recipient), FOREIGN KEY(actor,entry_id) REFERENCES stock_operations(actor,operation_id)) STRICT, WITHOUT ROWID")); 216 objects.push(("index","stock_rewrap_admission_entry","stock_rewrap_admissions","CREATE UNIQUE INDEX stock_rewrap_admission_entry ON stock_rewrap_admissions(actor,entry_id)")); 217 objects.push(("table","stock_rewraps","stock_rewraps","CREATE TABLE stock_rewraps (actor BLOB NOT NULL CHECK(length(actor)=32), operation_id BLOB NOT NULL CHECK(length(operation_id)=32), recipient BLOB NOT NULL CHECK(length(recipient)=32), admission_entry BLOB NOT NULL CHECK(length(admission_entry)=32), event_id BLOB NOT NULL CHECK(length(event_id)=32), entry_id BLOB NOT NULL CHECK(length(entry_id)=32), PRIMARY KEY(actor,operation_id,recipient,event_id), FOREIGN KEY(actor,operation_id,recipient) REFERENCES stock_envelopes(actor,operation_id,recipient), FOREIGN KEY(actor,admission_entry) REFERENCES stock_rewrap_admissions(actor,entry_id), FOREIGN KEY(actor,entry_id) REFERENCES stock_operations(actor,operation_id)) STRICT, WITHOUT ROWID")); 218 objects.push(("index","stock_rewrap_capture_admission","stock_rewraps","CREATE UNIQUE INDEX stock_rewrap_capture_admission ON stock_rewraps(actor,admission_entry)")); 219 objects.push(( 220 "index", 221 "stock_rewrap_entry", 222 "stock_rewraps", 223 "CREATE UNIQUE INDEX stock_rewrap_entry ON stock_rewraps(actor,entry_id)", 224 )); 225 } 226 if version >= 19 { 227 objects.push(("table", "stock_deficits", "stock_deficits", "CREATE TABLE stock_deficits (actor BLOB NOT NULL CHECK(length(actor)=32), operation_id BLOB NOT NULL CHECK(length(operation_id)=32), acceptance_id BLOB NOT NULL CHECK(length(acceptance_id)=32), installation_id BLOB NOT NULL CHECK(length(installation_id)=32), line_id BLOB NOT NULL CHECK(length(line_id)=32), pool_id BLOB NOT NULL REFERENCES stock_pools(pool_id), PRIMARY KEY(actor,operation_id,acceptance_id,line_id), FOREIGN KEY(actor,operation_id) REFERENCES stock_operations(actor,operation_id), FOREIGN KEY(actor,acceptance_id) REFERENCES stock_orders(actor,operation_id), FOREIGN KEY(actor,installation_id) REFERENCES stock_operations(actor,operation_id)) STRICT, WITHOUT ROWID")); 228 } 229 if version >= 20 { 230 objects.push(("table", "stock_obligation_changes", "stock_obligation_changes", "CREATE TABLE stock_obligation_changes (actor BLOB NOT NULL CHECK(length(actor)=32), operation_id BLOB NOT NULL CHECK(length(operation_id)=32), acceptance_id BLOB NOT NULL CHECK(length(acceptance_id)=32), installation_id BLOB NOT NULL CHECK(length(installation_id)=32), line_id BLOB NOT NULL CHECK(length(line_id)=32), pool_id BLOB NOT NULL REFERENCES stock_pools(pool_id), PRIMARY KEY(actor,operation_id,acceptance_id,line_id,pool_id), FOREIGN KEY(actor,operation_id) REFERENCES stock_operations(actor,operation_id), FOREIGN KEY(actor,acceptance_id) REFERENCES stock_orders(actor,operation_id), FOREIGN KEY(actor,installation_id) REFERENCES stock_operations(actor,operation_id)) STRICT, WITHOUT ROWID")); 231 } 232 Ok(objects) 233 } 234 pub(super) async fn admit(connection: &mut SqliteConnection) -> Result<i64, WorkspaceError> { 235 let version: i64 = sqlx::query_scalar("PRAGMA user_version") 236 .fetch_one(&mut *connection) 237 .await 238 .map_err(|_| WorkspaceError::RecoveryRequired)?; 239 let objects = objects(version)?; 240 let mut sql = String::from("SELECT CASE WHEN count(*)=? AND sum(CASE"); 241 for _ in &objects { 242 sql.push_str(" WHEN type=? AND name=? AND tbl_name=? AND sql=? THEN 1"); 243 } 244 sql.push_str(" ELSE 0 END)=? THEN 1 ELSE 0 END FROM sqlite_schema"); 245 let mut query = sqlx::query_scalar(sqlx::AssertSqlSafe(sql)).bind(objects.len() as i64); 246 for (kind, name, owner, definition) in &objects { 247 query = query.bind(*kind).bind(*name).bind(*owner).bind(*definition); 248 } 249 let valid: i64 = query 250 .bind(objects.len() as i64) 251 .fetch_one(connection) 252 .await 253 .map_err(|_| WorkspaceError::RecoveryRequired)?; 254 if valid != 1 { 255 return Err(WorkspaceError::RecoveryRequired); 256 } 257 Ok(version) 258 } 259 pub(super) async fn integrity(connection: &mut SqliteConnection) -> Result<(), WorkspaceError> { 260 let check: String = sqlx::query_scalar("PRAGMA quick_check(1)") 261 .fetch_one(connection) 262 .await 263 .map_err(|_| WorkspaceError::RecoveryRequired)?; 264 if check != "ok" { 265 return Err(WorkspaceError::RecoveryRequired); 266 } 267 Ok(()) 268 }