-
Notifications
You must be signed in to change notification settings - Fork 51
Test backwards compatibility of analyses with DB data #1075
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
kasbah
wants to merge
1
commit into
main
Choose a base branch
from
kb/backwards-compat-test
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,106 @@ | ||
| #!/usr/bin/env bash | ||
| # Dumps all analysis documents and their referenced models and diagrams from a database as a JSON object. | ||
| # | ||
| # Usage: dump-notebook-fixtures <ssh-target> <output-file> | ||
| # Example: dump-notebook-fixtures catcolab@backend-next.catcolab.org fixtures.json | ||
| # | ||
| # Output format: | ||
| # { | ||
| # "analyses": [...], // Array of analysis documents | ||
| # "models": [...], // Array of model documents referenced by analyses (directly or via diagrams) | ||
| # "diagrams": [...] // Array of diagram documents referenced by diagram analyses | ||
| # } | ||
|
|
||
| set -euo pipefail | ||
|
|
||
| ssh_target="${1:?Usage: dump-notebook-fixtures <ssh-target> <output-file>}" | ||
| output_file="${2:?Usage: dump-notebook-fixtures <ssh-target> <output-file>}" | ||
|
|
||
| echo "Dumping analysis documents, models, and diagrams from $ssh_target to $output_file..." >&2 | ||
|
|
||
| # Run the query remotely via SSH. The remote script: | ||
| # 1. Extracts DATABASE_URL from the agenix secrets file | ||
| # 2. Queries PostgreSQL to get all non-deleted analysis documents | ||
| # 3. Queries PostgreSQL to get all model documents referenced by model analyses | ||
| # 4. Queries PostgreSQL to get all diagram documents referenced by diagram analyses | ||
| # 5. Queries PostgreSQL to get all model documents referenced by those diagrams | ||
| # 6. Returns a JSON object with all three arrays | ||
| ssh "$ssh_target" bash -s > "$output_file" <<'REMOTE_SCRIPT' | ||
| set -euo pipefail | ||
|
|
||
| # Read DATABASE_URL from the agenix-decrypted secrets file | ||
| if [[ ! -f /run/agenix/catcolabSecrets ]]; then | ||
| echo "Error: /run/agenix/catcolabSecrets not found on remote host" >&2 | ||
| exit 1 | ||
| fi | ||
|
|
||
| db_url=$(grep '^DATABASE_URL=' /run/agenix/catcolabSecrets | cut -d= -f2-) | ||
|
|
||
| if [[ -z "$db_url" ]]; then | ||
| echo "Error: DATABASE_URL not found in /run/agenix/catcolabSecrets" >&2 | ||
| exit 1 | ||
| fi | ||
|
|
||
| # Query for all analysis documents and their referenced models and diagrams | ||
| psql "$db_url" -t -A -c " | ||
| WITH analysis_docs AS ( | ||
| SELECT jsonb_set(s.content, '{_refId}', to_jsonb(r.id::text)) AS content | ||
| FROM snapshots s | ||
| JOIN refs r ON r.current_snapshot = s.id | ||
| WHERE r.deleted_at IS NULL | ||
| AND s.content->>'type' = 'analysis' | ||
| ), | ||
| -- Model documents referenced directly by model analyses | ||
| model_analysis_refs AS ( | ||
| SELECT DISTINCT content->'analysisOf'->>'_id' AS model_ref_id | ||
| FROM analysis_docs | ||
| WHERE content->>'analysisType' = 'model' | ||
| AND content->'analysisOf'->>'_id' IS NOT NULL | ||
| ), | ||
| -- Diagram documents referenced by diagram analyses | ||
| diagram_refs AS ( | ||
| SELECT DISTINCT content->'analysisOf'->>'_id' AS diagram_ref_id | ||
| FROM analysis_docs | ||
| WHERE content->>'analysisType' = 'diagram' | ||
| AND content->'analysisOf'->>'_id' IS NOT NULL | ||
| ), | ||
| diagram_docs AS ( | ||
| SELECT jsonb_set(s.content, '{_refId}', to_jsonb(r.id::text)) AS content | ||
| FROM diagram_refs dr | ||
| JOIN refs r ON r.id = dr.diagram_ref_id::uuid | ||
| JOIN snapshots s ON s.id = r.current_snapshot | ||
| WHERE r.deleted_at IS NULL | ||
| AND s.content->>'type' = 'diagram' | ||
| ), | ||
| -- Model documents referenced by diagrams (via diagramIn._id) | ||
| diagram_model_refs AS ( | ||
| SELECT DISTINCT content->'diagramIn'->>'_id' AS model_ref_id | ||
| FROM diagram_docs | ||
| WHERE content->'diagramIn'->>'_id' IS NOT NULL | ||
| ), | ||
| -- All model documents: those referenced by model analyses + those referenced by diagrams | ||
| all_model_refs AS ( | ||
| SELECT model_ref_id FROM model_analysis_refs | ||
| UNION | ||
| SELECT model_ref_id FROM diagram_model_refs | ||
| ), | ||
| model_docs AS ( | ||
| SELECT jsonb_set(s.content, '{_refId}', to_jsonb(r.id::text)) AS content | ||
| FROM all_model_refs mr | ||
| JOIN refs r ON r.id = mr.model_ref_id::uuid | ||
| JOIN snapshots s ON s.id = r.current_snapshot | ||
| WHERE r.deleted_at IS NULL | ||
| AND s.content->>'type' = 'model' | ||
| ) | ||
| SELECT json_build_object( | ||
| 'analyses', COALESCE((SELECT json_agg(content) FROM analysis_docs), '[]'::json), | ||
| 'models', COALESCE((SELECT json_agg(content) FROM model_docs), '[]'::json), | ||
| 'diagrams', COALESCE((SELECT json_agg(content) FROM diagram_docs), '[]'::json) | ||
| ); | ||
| " | ||
| REMOTE_SCRIPT | ||
|
|
||
| analysis_count=$(jq '.analyses | length' "$output_file") | ||
| model_count=$(jq '.models | length' "$output_file") | ||
| diagram_count=$(jq '.diagrams | length' "$output_file") | ||
| echo "Dumped $analysis_count analysis documents, $model_count model documents, and $diagram_count diagram documents to $output_file" >&2 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| import { Nb } from "catcolab-document-methods"; | ||
| import type { Analysis, AnalysisType, Notebook } from "catlog-wasm"; | ||
| import type { AnalysisMeta, Theory } from "../theory"; | ||
|
|
||
| /** Migrate content of formal cells in an analysis notebook. | ||
| * | ||
| * This is a stop-gap (read: hacky) method to migrate the content of analyses when | ||
| * the set of fields changes. It allows new fields to be added. Renaming or removing | ||
| * existing fields is *not* supported. | ||
| * | ||
| * Fills in missing fields from analysis defaults. Mutates the notebook in place. | ||
| */ | ||
| export function migrateAnalysis<T extends Analysis>( | ||
| notebook: Notebook<T>, | ||
| theory: Theory, | ||
| analysisType: AnalysisType, | ||
| ): void { | ||
| for (const cell of Nb.getFormalCells(notebook)) { | ||
| const analysis = cell.content; | ||
| let meta: AnalysisMeta<unknown> | undefined; | ||
| switch (analysisType) { | ||
| case "model": | ||
| meta = theory.modelAnalysis(analysis.id); | ||
| break; | ||
| case "diagram": | ||
| meta = theory.diagramAnalysis(analysis.id); | ||
| break; | ||
| } | ||
| if (!meta) { | ||
| continue; | ||
| } | ||
| const initialContent = meta.initialContent() as Record<string, unknown>; | ||
| const cellContent = analysis.content; | ||
| for (const key in initialContent) { | ||
| if (!(key in cellContent)) { | ||
| cellContent[key] = initialContent[key]; | ||
| } | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Jason wrote:
I won't block the PR on it, but I agree with Jason's comment. Another advantage to writing it in Rust is that we'd check that the SQL queries are valid at compile time.