(args: SnapshotArgs)
| 82 | } |
| 83 | |
| 84 | pub async fn snapshot(args: SnapshotArgs) -> Result<(), Report<GraphError>> { |
| 85 | SnapshotEntry::install_error_stack_hook(); |
| 86 | |
| 87 | let mut settings = PostgresStoreSettings::default(); |
| 88 | if let SnapshotCommand::Restore(args) = &args.command { |
| 89 | settings.validate_links = !args.skip_validation; |
| 90 | } |
| 91 | |
| 92 | let pool = PostgresStorePool::new(&args.db_info, &args.pool_config, NoTls, settings) |
| 93 | .await |
| 94 | .change_context(GraphError) |
| 95 | .map_err(|report| { |
| 96 | tracing::error!(error = ?report, "Failed to connect to database"); |
| 97 | report |
| 98 | })?; |
| 99 | |
| 100 | match args.command { |
| 101 | SnapshotCommand::Dump(args) => { |
| 102 | let write = FramedWrite::new( |
| 103 | io::BufWriter::new(io::stdout()), |
| 104 | JsonLinesEncoder::default(), |
| 105 | ); |
| 106 | let settings = SnapshotDumpSettings { |
| 107 | chunk_size: 10_000, |
| 108 | dump_principals: !args.no_principals, |
| 109 | dump_actions: !args.no_actions, |
| 110 | dump_policies: !args.no_policies, |
| 111 | dump_entities: !args.no_entities, |
| 112 | dump_entity_types: !args.no_entity_types, |
| 113 | dump_property_types: !args.no_property_types, |
| 114 | dump_data_types: !args.no_data_types, |
| 115 | dump_embeddings: !args.no_embeddings, |
| 116 | }; |
| 117 | |
| 118 | pool.dump_snapshot(write, settings) |
| 119 | .change_context(GraphError) |
| 120 | .attach("Failed to produce snapshot dump")?; |
| 121 | |
| 122 | tracing::info!("Snapshot dumped successfully"); |
| 123 | } |
| 124 | SnapshotCommand::Restore(args) => { |
| 125 | let read = |
| 126 | FramedRead::new(io::BufReader::new(io::stdin()), JsonLinesDecoder::default()); |
| 127 | SnapshotStore::new( |
| 128 | pool.acquire(None) |
| 129 | .await |
| 130 | .change_context(GraphError) |
| 131 | .map_err(|report| { |
| 132 | tracing::error!(error = ?report, "Failed to acquire database connection"); |
| 133 | report |
| 134 | })?, |
| 135 | ) |
| 136 | .restore_snapshot(read, 10_000, args.ignore_validation_errors) |
| 137 | .await |
| 138 | .change_context(GraphError) |
| 139 | .attach("Failed to restore snapshot")?; |
| 140 | |
| 141 | tracing::info!("Snapshot restored successfully"); |
no test coverage detected