diff --git a/.compass.org b/.compass.org index 731ca402..263d5aad 100644 --- a/.compass.org +++ b/.compass.org @@ -29,7 +29,15 @@ where each value came from. The command line wins over this page: =--out=, One row per codec: its name, =yes= or =no= for enabled, and its options as space-separated =key=value= pairs. A codec with no row runs on its -defaults. =tangle= is off: the files it writes are not committed here. +defaults. =tangle= writes the templates of the MASD library from their +archetype pages under =share/masd/templates=, the one directory whose +tangled files are committed. =codegen= generates compass from its own +models with the MASD library under the project's library in +=projects/compass.codegen/library=, which holds the component catalogue, +the licence and the modelines. The MASD library is named by its checkout +path, =share/masd=, not as =@compass=, the copy installed beside the +binary: compass edits the library it generates itself with, and a template +read from the checkout is an input whose change regenerates what it wrote. =no_pr_triggers= is off: ORE Studio removed its pull-request checks because they came late and missed the paths their workflows did not name, but compass's one Linux workflow covers every path and builds a pull request, @@ -41,9 +49,10 @@ compass's own commits carry no story, task or environment trailers, so |-------------------+---------+--------------------------------------------------| | document_index | yes | path=build/output/compass/doc/document_index.tsv | | required_sections | yes | | -| tangle | no | | +| tangle | yes | directory=share/masd/templates | | tangle_check | yes | | | check_inventory | yes | id=84B5F778-16CD-42D1-8F4C-B412DBE0C9C1 created=2026-11-21 updated=2026-11-21 | | rule_table | yes | path=doc/knowledge/compass/rule_table.org | | no_pr_triggers | no | | | commit_provenance | no | | +| codegen | yes | library=share/masd,projects/compass.codegen/library product=compass year=2026 convention_component=db | diff --git a/doc/agile/versions/v0/sprint_01/own_the_masd_meta_model/story.org b/doc/agile/versions/v0/sprint_01/own_the_masd_meta_model/story.org index 2a22ce8f..dfca3ecd 100644 --- a/doc/agile/versions/v0/sprint_01/own_the_masd_meta_model/story.org +++ b/doc/agile/versions/v0/sprint_01/own_the_masd_meta_model/story.org @@ -32,7 +32,7 @@ code. | Now | [[id:7FD52505-1436-4925-A1DE-4A3EC88F0133][Investigation: How compass generates itself]] written; eight tasks planned from it. | | Waiting on | Nothing. | | Next | Review Name the product root and Ship the MASD library with compass; then Move the MASD pages and templates to compass. | -| Last touched | 2026-11-23 | +| Last touched | 2026-11-23 | * Acceptance @@ -65,8 +65,8 @@ code. | [[id:10FBAF8A-D1B9-4800-A867-CF1BC77C95D3][Ship the MASD library with compass]] | STARTED | 2026-11-22 | | Install the MASD library under share/compass/masd in every compass package, and let the codegen library option layer @compass under a project's own library, with the licence and product taken from the project. | | [[id:BECA92D2-95DA-46B4-9D3E-AC30FDDB7294][Move the MASD pages and templates to compass]] | BACKLOG | | | Move every page and template family the investigation classifies as MASD to compass, split the mixed ones, and delete them from ORE Studio in the same release, with ORE Studio's generated output unchanged by a byte. | | [[id:02089780-F445-4220-89E1-3CBCA0347B37][Model compass's tables]] | STARTED | 2026-11-22 | | Write the MASD entity context and the features compass's tables need (schema, key kinds, written_by, values, opclass, on_delete, computed and generated columns), the profiles content-addressed-row, owned-child-row and activity-record, and a model for each table of the corpus, test and activity schemas. | -| [[id:B8F7849D-1477-4C30-80A1-49706AB2AA57][Generate compass's schema and row types]] | BACKLOG | | | Write the masd.sql.table and masd.cpp.row templates, generate compass's schema and its row types, COPY writers and readers from the models, and replace the hand-written migration 1 and staging code, with the database tests passing unchanged. | -| [[id:6F19FA08-5DBE-4DE9-B853-03F541F5A4FC][Migrate a generated schema]] | BACKLOG | | | Keep hand-written, frozen migrations beside the generated schema, test that the migrations and the generated schema build the same catalog, and report a change to a retained table that has no migration. | +| [[id:B8F7849D-1477-4C30-80A1-49706AB2AA57][Generate compass's schema and row types]] | STARTED | 2026-11-23 | | Write the masd.sql.table and masd.cpp.row templates, generate compass's schema and its row types, COPY writers and readers from the models, and replace the hand-written migration 1 and staging code, with the database tests passing unchanged. | +| [[id:6F19FA08-5DBE-4DE9-B853-03F541F5A4FC][Migrate a generated schema]] | STARTED | 2026-11-23 | | Keep hand-written, frozen migrations beside the generated schema, test that the migrations and the generated schema build the same catalog, and report a change to a retained table that has no migration. | | [[id:55F578C5-6F25-4385-B0F2-CDFBDF9A753C][Model compass's service]] | STARTED | 2026-11-23 | | Add HTTP verb, path, field binding, MCP tool and caller properties to the operation model, and write the operation models of Serve the documents' endpoints and tools. | | [[id:E754BE53-3A6B-4491-BB26-450D1A34F159][Generate JSON codecs]] | BACKLOG | | | Generate the JSON reading and writing of every type compass models, row types, enums and the service's messages, and the JSON Schema of each message, so no JSON is written by hand. | | [[id:8CF40A62-82EB-4FE0-8CEC-2621BD0E66FE][Generate the HTTP and MCP service]] | BACKLOG | | | Write the cpp-httplib route, MCP tool description and tool table templates and the wire codec compass needs, and generate Serve the documents' HTTP and MCP service from its operation models. | diff --git a/doc/agile/versions/v0/sprint_01/own_the_masd_meta_model/task_generate_compass_schema_and_rows.org b/doc/agile/versions/v0/sprint_01/own_the_masd_meta_model/task_generate_compass_schema_and_rows.org index 8788f7f8..d829189b 100644 --- a/doc/agile/versions/v0/sprint_01/own_the_masd_meta_model/task_generate_compass_schema_and_rows.org +++ b/doc/agile/versions/v0/sprint_01/own_the_masd_meta_model/task_generate_compass_schema_and_rows.org @@ -7,12 +7,12 @@ #+level: s1 #+filetags: :own_the_masd_meta_model:sprint_01:v0: #+owner: marco -#+branch: -#+pr: +#+branch: feature/generate-compass-schema +#+pr: 196 #+blocked_on: #+blocked_since: #+created: 2026-11-22 -#+updated: 2026-11-22 +#+updated: 2026-11-23 #+environment: #+todo: DISCOVERED BACKLOG STARTED BLOCKED | DONE ABANDONED @@ -29,12 +29,12 @@ migration 1 and the staging code they reproduce. | Field | Value | |--------------+----------------------------------------| -| State | BACKLOG | +| State | STARTED | | Parent story | [[id:E3C81059-FE00-4403-AA56-190FDBB9904A][Own the MASD meta model and generate compass with it]] | -| Now | Not yet started. | -| Waiting on | [[id:02089780-F445-4220-89E1-3CBCA0347B37][Model compass's tables]]; [[https://github.com/MASD-Project/compass/pull/176][#176]] merged. | -| Next | Begin implementation. | -| Last touched | 2026-11-22 | +| Now | In review: [[https://github.com/MASD-Project/compass/pull/195][#195]] (the generator) and [[https://github.com/MASD-Project/compass/pull/196][#196]] (compass generates its schema and rows), stacked. | +| Waiting on | Review. | +| Next | Merge [[https://github.com/MASD-Project/compass/pull/195][#195]], then [[https://github.com/MASD-Project/compass/pull/196][#196]]. | +| Last touched | 2026-11-23 | * Acceptance @@ -44,10 +44,45 @@ migration 1 and the staging code they reproduce. * Plan -(Implementation strategy. Written when work starts; key decisions -are distilled into the parent story's =* Decisions= at close, but the -plan itself stays — it is the historical record of what we did.) - +compass generates its schema and row types with the generator it runs on +ORE Studio, through a small technical space of its own in the MASD library. + +- *The model types.* =compass.model= loads =masd.codegen.entity= and + =masd.codegen.schema= into typed structs + (=compass.model/types/masd_entity.hpp=). Every property, profile, profile + feature, =* SQL= section and table header has a closed list; anything else + is a =model_error= naming what it found and what the format has, which + =compass generate= reports with the model's path. +- *The profiles* are pages, =projects/modeling/variability_.org=, as + ORE Studio's are: =* Assignments= sets the one feature the format has, + =retained=, and =* Physical space= switches facets. + =content-addressed-row= and =owned-child-row= switch the COPY writer on, + =activity-record= marks the table retained, =environment-state= assigns + nothing, and =bookkeeping= switches every =masd= facet off. +- *The technical space.* The MASD library's nodes are =masd.*.org=, read + beside the product's own =.*.org=: =masd.sql.schema= (archetype + =masd.sql.schema.create=, =sql/schema.sql=), =masd.cpp.row= + (=masd.cpp.row.header=, =types/_row.hpp=) and =masd.cpp.copy=, off + by default (=masd.cpp.copy.header= and =.impl=, =io/_copy.hpp= and + =src/io/_copy.cpp=). Each archetype page holds its Mustache source + under =* Template=, which the =tangle= codec, now on for + =share/masd/templates=, writes to the =.mustache= file. +- *The context builder* (=compass.codegen/ops/masd_context.cpp=, about 450 + lines) derives the row members, their types and initialisers, the + includes, the enumerations, the COPY parameters and fields, and the + column definitions, table constraints and indexes, and orders the tables + so each follows the tables it references. +- *compass's configuration.* =.compass.org= turns =codegen= on with + =library=share/masd,projects/compass.codegen/library product=compass=. The + project library holds the component catalogue, the licence and the + modelines; =projects/modeling/cpp_custom_types.org= gives the header of + =store::types::blob_id=. +- *The hand-written code that stays*: the aggregates =corpus_document= and + =test_scenario= (a =scenario_row= with its steps), =environment_summary=, + the staging transaction and every query. =stage()= calls the generated + =write_copy= and the COPY statements name the generated column lists. +- *Migrations stay hand-written.* v0.0.13 shipped migrations 1 and 2, so + they are frozen: =schema.sql= must build their catalog, not replace them. * Measurements Every number this task claims about a tree, with the command that produced it @@ -60,10 +95,63 @@ task and name that file in the Command column. | What | Value | Command | Commit | |------+-------+---------+--------| -| | | | | - +| Files =compass generate= writes on compass | 30: =schema.sql=, 14 row headers, 7 COPY headers and 7 COPY sources, and the check inventory | =compass -j 2 generate= | =922bbde= | +| Files a second =compass generate= writes | 0 | =compass -j 2 generate= | =922bbde= | +| Database tests of =compass.db=, #176's and every later one unchanged | 61 of 61 pass | =compass.db.tests= | =922bbde= | +| ORE Studio's =compass diff= over a scratch copy of main, new binary against 0.0.13 | same, 0 files, both as ORE Studio configures codegen and with =library=@compass,projects/ores.codegen/library= | [[file:evidence/masd_schema_ore_diff.sh][masd_schema_ore_diff.sh]] | ORE Studio main, compass 0.0.13 against =b21f02a= | +| =compass check --ratchet --no-blame= | 0 new | =compass check --ratchet --no-blame= | =922bbde= | * Notes +** What the models could not say, and what they say now + +| Gap in the models' Notes | Resolution | +|--------------------------+------------| +| A member named apart from its column (=heading=, =heading_ord=) | =:member:= on the column. | +| A row type not named =_row= (=environment=, =placement=) | =:row_type:= in =* Flags=. | +| An owner's key that is not a content hash (=placement.environment=) | =key_kind: owner=. | +| A column the writer sets with no default (=environment.synced_at=) | =written_by: writer=. | +| An empty string standing for NULL (=environment::root=) | The row holds =std::optional=; =write_environment= passes it as is. | +| A table-level =UNIQUE= (=activity.campaign=) | A =** Unique= table under =* SQL=. | +| The heading path =tsv= weighs B, staged as a column the table lacks | The insert joins the staged heading and joins its =path=; every staged column is a table column. | +| =scenario_row::steps=, a member that is no column | The hand-written =test_scenario=, a =scenario_row= with its steps. | +| =activity-record= fixing an identity key | It fixes only =retained=; each model states its own key. | +| The default of a value list (=note.state=) | The member starts at the value the SQL default names. | +| An enumeration named apart from its table (=step_outcome=) | =:enum:= on the column. | +| The generated column =corpus.document.state= of migration 2 | =:generated:=, last in the table, as migration 2 added it. | +| The function =corpus.sprint_members= of migration 2 | The schema model's =* Functions=, emitted after the tables. | + +** Decisions + +- *Migrations are not replaced.* v0.0.13 shipped migrations 1 and 2, and no + check can show no database applied them, so =schema.sql= reproduces their + catalog instead; [[id:6F19FA08-5DBE-4DE9-B853-03F541F5A4FC][Migrate a generated schema]] says how that is checked. +- *No row readers.* compass has no hand-written reader of whole rows to + replace: every read is a query projection. The value lists' parsers, + =_of=, are generated, and replace the one hand-written reader, + =step_outcome_of= of =query/campaign.cpp=. A reader template waits for a + caller, which Generate the HTTP and MCP service is likely to be. +- *Enumerations live in the row header*: a model renders one file per + archetype, and an enumeration belongs to one column. +- *compass names its MASD library by its checkout path*, =share/masd=, not + =@compass=: the library compass edits is the one it generates with, and a + template read from the checkout is an input that regenerates its outputs. +- *Profiles are pages* of =projects/modeling/=, as ORE Studio's are, since + =physical_space_overrides= reads them there. Moving them to the MASD + library is Move the MASD pages and templates to compass. + +** Limits + +- A fresh checkout must tangle before codegen reads a template a page has + just gained: =compass generate= prepares codegen before tangle writes. The + committed =.mustache= files make that matter only when a template is new. +- =compass check --ratchet= runs the head's configuration over the base, so + a base without =projects/compass.codegen/library= cannot run codegen. The + generator, the library and the profiles therefore land first, in + [[https://github.com/MASD-Project/compass/pull/195][#195]], and compass enables codegen in [[https://github.com/MASD-Project/compass/pull/196][#196]]. +- =diff='s origin labels name the project library's =templates/= for a + template only =share/masd= holds, as Ship the MASD library recorded. +- Database tests need a local PostgreSQL; the database fixture skips them + without one, as for #176. * Test Scenarios Manual QA scenarios (scaffolded via =compass add test_scenario=) @@ -79,8 +167,8 @@ via its "Verifies task" field. | PR | Title | |----+-------| -| | | - +| [[https://github.com/MASD-Project/compass/pull/195][#195]] | [codegen] The MASD table space: schema, row and COPY templates | +| [[https://github.com/MASD-Project/compass/pull/196][#196]] | [db] Generate compass's schema, row types and COPY writers | * Review | # | Comment summary | File | Decision | Notes | diff --git a/doc/agile/versions/v0/sprint_01/own_the_masd_meta_model/task_migrate_a_generated_schema.org b/doc/agile/versions/v0/sprint_01/own_the_masd_meta_model/task_migrate_a_generated_schema.org index 9615b149..15e99897 100644 --- a/doc/agile/versions/v0/sprint_01/own_the_masd_meta_model/task_migrate_a_generated_schema.org +++ b/doc/agile/versions/v0/sprint_01/own_the_masd_meta_model/task_migrate_a_generated_schema.org @@ -7,12 +7,12 @@ #+level: s1 #+filetags: :own_the_masd_meta_model:sprint_01:v0: #+owner: marco -#+branch: -#+pr: +#+branch: feature/generate-compass-schema +#+pr: 196 #+blocked_on: #+blocked_since: #+created: 2026-11-22 -#+updated: 2026-11-22 +#+updated: 2026-11-23 #+environment: #+todo: DISCOVERED BACKLOG STARTED BLOCKED | DONE ABANDONED @@ -30,12 +30,12 @@ same catalog; and report a change to a retained table that has no migration. | Field | Value | |--------------+----------------------------------------| -| State | BACKLOG | +| State | STARTED | | Parent story | [[id:E3C81059-FE00-4403-AA56-190FDBB9904A][Own the MASD meta model and generate compass with it]] | -| Now | Not yet started. | -| Waiting on | [[id:B8F7849D-1477-4C30-80A1-49706AB2AA57][Generate compass's schema and row types]]. | -| Next | Begin implementation. | -| Last touched | 2026-11-22 | +| Now | In review in [[https://github.com/MASD-Project/compass/pull/196][#196]], stacked on [[https://github.com/MASD-Project/compass/pull/195][#195]]. | +| Waiting on | Review. | +| Next | Merge after Generate compass's schema and row types; pin each release's new migrations. | +| Last touched | 2026-11-23 | * Acceptance @@ -45,10 +45,36 @@ same catalog; and report a change to a retained table that has no migration. * Plan -(Implementation strategy. Written when work starts; key decisions -are distilled into the parent story's =* Decisions= at close, but the -plan itself stays — it is the historical record of what we did.) - +The safety net is three tests in =projects/compass.db/tests/ops_migrations_tests.cpp=, +which run in the throwaway cluster every database test of =compass.db= uses. + +- *The catalog comparison.* One database gets the bookkeeping SQL and every + migration, through =io::migrate=; another gets the bookkeeping SQL and the + generated =sql/schema.sql=. Each catalog becomes a set of lines: schemas, + relations, columns in their order with type, nullability, identity, + generation and default, constraints, indexes and functions. The sets must + be equal; a failure prints the lines only one side builds. +- *The retained-table check.* =schema.sql= says above each table of the + profile =activity-record= that it is retained. For each, every column the + migrations build must be in =schema.sql='s table with the same type; a + failure names the table and the column and says to add the migration that + says what becomes of it. +- *Frozen migrations.* Each released migration is pinned by the blob ID of + its SQL. Editing migration 1 or 2 to absorb a model change fails, so the + only way to make the catalogs agree again is a new migration. + +Migrations stay where #176 put them, the SQL texts of +=projects/compass.db/src/ops/migrations.cpp=, unchanged. Moving them to +=.sql= files would rewrite the text of two released migrations for no gain +in safety. + +*Was a v0.0.13 database created?* compass 0.0.13 is released and installed +on this machine (=~/.local/opt/compass=). =compass db init= records its +connection in =/compass/local.org=; no such file exists under +=/home/marco=, so no checkout here, compass's or ORE Studio's worktrees, +has a database. Other machines and ORE Studio's environments elsewhere could +not be checked, so migrations 1 and 2 are treated as released and frozen, +and migration 1 is not replaced. * Measurements Every number this task claims about a tree, with the command that produced it @@ -61,8 +87,11 @@ task and name that file in the Command column. | What | Value | Command | Commit | |------+-------+---------+--------| -| | | | | - +| The catalog of migrations 1 and 2 against =schema.sql='s | equal | =compass.db.tests "[migrations]"= | =922bbde= | +| =activity.run.tester= retyped to =integer= in the model, regenerated, no migration | both catalog tests fail, naming =column \| activity.run \| 6 \| tester= on each side and the retained table | the same, after the edit | =922bbde=, edit reverted | +| The same with a probe migration 3, =ALTER COLUMN tester TYPE integer= | all 3 tests pass | the same | =922bbde=, probe reverted | +| Migration 1 edited by one space | the freeze test fails: "migration 1 shipped in a release" | =compass.db.tests "a released migration is never edited"= | =922bbde=, edit reverted | +| =/compass/local.org= files, which =compass db init= writes, under =/home/marco= | 0 | =find /home/marco -path '*/compass/local.org'= | 2026-11-23 | * Notes * Test Scenarios @@ -80,8 +109,7 @@ via its "Verifies task" field. | PR | Title | |----+-------| -| | | - +| [[https://github.com/MASD-Project/compass/pull/196][#196]] | [db] Generate compass's schema, row types and COPY writers | * Review | # | Comment summary | File | Decision | Notes | diff --git a/doc/knowledge/compass/check_inventory.org b/doc/knowledge/compass/check_inventory.org index 09dedc5c..21ce3738 100644 --- a/doc/knowledge/compass/check_inventory.org +++ b/doc/knowledge/compass/check_inventory.org @@ -9,7 +9,7 @@ #+created: 2026-11-21 #+updated: 2026-11-21 -This page lists every check and generator compass runs on this repository, and the ones it could run: 6 of the 79 it knows here are enabled. Each entry says what the codec or check is for, the rules it reports under, and where it is defined: a check document of this repository, linked, or compass itself. The page is generated by compass's check_inventory codec from the codecs, the check documents and the configuration of every run. Edit those and run compass generate, never the page; compass check reports the page when it no longer says what runs. +This page lists every check and generator compass runs on this repository, and the ones it could run: 8 of the 79 it knows here are enabled. Each entry says what the codec or check is for, the rules it reports under, and where it is defined: a check document of this repository, linked, or compass itself. The page is generated by compass's check_inventory codec from the codecs, the check documents and the configuration of every run. Edit those and run compass generate, never the page; compass check reports the page when it no longer says what runs. * Detail @@ -18,14 +18,15 @@ This page lists every check and generator compass runs on this repository, and t | Name | Enabled | Kind | Severity | Purpose | Defined in | |------+---------+------+----------+---------+------------| | =check_inventory= | yes | generator | error | Writes the page of every codec and check that runs here: whether it is on, its purpose, its rules and where it is defined. | built into compass | +| =codegen= | yes | generator | error | Generates what ores.codegen's regenerate --all writes, and the outputs of the service registry. | built into compass | | =document_index= | yes | generator | error | Lists the ID, title, type and path of every document. | built into compass | | =evidence_location= | yes | check | error | Reports each evidence document outside an evidence/ directory. | built into compass | | =required_sections= | yes | check | error | Reports each required section a document lacks. | built into compass | | =rule_table= | yes | generator | error | Writes the page of every rule, the memories and documents it cites, and the memories no rule cites. | built into compass | +| =tangle= | yes | generator | error | Writes the files the source blocks of each document tangle to, as Emacs does. | built into compass | | =tangle_check= | yes | check | error | Reports each source block the tangle codec leaves out. | built into compass | | =backlog_indexes= | no | generator | error | Writes the index page of each product backlog bucket. | built into compass | | =bff_list_requests= | no | check | error | Reports a hand-written BFF list request that disagrees with the C++ protocol. | built into compass | -| =codegen= | no | generator | error | Generates what ores.codegen's regenerate --all writes, and the outputs of the service registry. | built into compass | | =commit_provenance= | no | check | error | Checks that every commit of a branch, and the branch's pull request, state the work they belong to, as the project's check document asks; check --ratchet checks the commits. | built into compass | | =component_docs= | no | check | error | Reports each component without a complete overview and diagram, and each link inside backticks under doc/. | built into compass | | =component_files= | no | generator | error | Writes the component_files.cmake source and header lists of each modelled component. | built into compass | @@ -87,7 +88,6 @@ This page lists every check and generator compass runs on this repository, and t | =sql_reachability= | no | check | error | Reports each SQL file no schema entry point reaches and each include that names no file. | built into compass | | =stream_coverage= | no | check | error | Reports each durably consumed subject no single stream covers, and each pair of streams whose subjects overlap. | built into compass | | =subject_conformance= | no | check | error | Reports each NATS subject the data-quality seed spells outside the entity protocol. | built into compass | -| =tangle= | no | generator | error | Writes the files the source blocks of each document tangle to, as Emacs does. | built into compass | | =template_sections= | no | check | error | Reports each template line that chains section tags of a section spanning lines. | built into compass | | =test_case_reachability= | no | check | error | Reports each test case inside a conditional compilation block and each test source whose conditionals cannot be read. | built into compass | | =trade_type_coverage= | no | generator | error | Writes the generated block of the ORE trade type coverage note. | built into compass | @@ -112,6 +112,19 @@ Writes the page of every codec and check that runs here: whether it is on, its p | =drift= | Every file a codec generates holds what the codec generates from its inputs; nobody edits it by hand. | | =names_missing_file= | The header of every file a codec generates names only scripts the checkout holds. | +*** codegen + +Generates what ores.codegen's regenerate --all writes, and the outputs of the service registry. + +- Kind: generator. +- Severity: error. +- Defined in: built into compass. + +| Rule | Statement | +|------+-----------| +| =drift= | Every file a codec generates holds what the codec generates from its inputs; nobody edits it by hand. | +| =names_missing_file= | The header of every file a codec generates names only scripts the checkout holds. | + *** document_index Lists the ID, title, type and path of every document. @@ -162,6 +175,19 @@ Writes the page of every rule, the memories and documents it cites, and the memo | =drift= | Every file a codec generates holds what the codec generates from its inputs; nobody edits it by hand. | | =names_missing_file= | The header of every file a codec generates names only scripts the checkout holds. | +*** tangle + +Writes the files the source blocks of each document tangle to, as Emacs does. + +- Kind: generator. +- Severity: error. +- Defined in: built into compass. + +| Rule | Statement | +|------+-----------| +| =drift= | Every file a codec generates holds what the codec generates from its inputs; nobody edits it by hand. | +| =names_missing_file= | The header of every file a codec generates names only scripts the checkout holds. | + *** tangle_check Reports each source block the tangle codec leaves out. @@ -204,19 +230,6 @@ Reports a hand-written BFF list request that disagrees with the C++ protocol. | =filter_mismatch= | A literal filter states exactly the members the filter struct declares. | | =wrong_rows= | readAll reads the rows a reply carries. | -*** codegen - -Generates what ores.codegen's regenerate --all writes, and the outputs of the service registry. - -- Kind: generator. -- Severity: error. -- Defined in: built into compass. - -| Rule | Statement | -|------+-----------| -| =drift= | Every file a codec generates holds what the codec generates from its inputs; nobody edits it by hand. | -| =names_missing_file= | The header of every file a codec generates names only scripts the checkout holds. | - *** commit_provenance Checks that every commit of a branch, and the branch's pull request, state the work they belong to, as the project's check document asks; check --ratchet checks the commits. @@ -1106,19 +1119,6 @@ Reports each NATS subject the data-quality seed spells outside the entity protoc |------+-----------| | =outside_protocol= | Every NATS subject the data-quality seed spells is inside the entity protocol. | -*** tangle - -Writes the files the source blocks of each document tangle to, as Emacs does. - -- Kind: generator. -- Severity: error. -- Defined in: built into compass. - -| Rule | Statement | -|------+-----------| -| =drift= | Every file a codec generates holds what the codec generates from its inputs; nobody edits it by hand. | -| =names_missing_file= | The header of every file a codec generates names only scripts the checkout holds. | - *** template_sections Reports each template line that chains section tags of a section spanning lines. diff --git a/projects/compass.db/include/compass.db/io/document_copy.hpp b/projects/compass.db/include/compass.db/io/document_copy.hpp new file mode 100644 index 00000000..85523e4f --- /dev/null +++ b/projects/compass.db/include/compass.db/io/document_copy.hpp @@ -0,0 +1,53 @@ +/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- + * + * Copyright (C) 2026 Marco Craveiro + * + * This program is free software; you can redistribute it and/or modify it under + * the terms of the GNU General Public License as published by the Free Software + * Foundation; either version 3 of the License, or (at your option) any later + * version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more + * details. + * + * You should have received a copy of the GNU General Public License along with + * this program; if not, write to the Free Software Foundation, Inc., 51 + * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + */ +/** + * AUTO-GENERATED FILE - DO NOT EDIT MANUALLY + * Template: masd_cpp_copy.hpp.mustache + * To modify, update the template and regenerate. + */ +#ifndef COMPASS_DB_IO_DOCUMENT_COPY_HPP +#define COMPASS_DB_IO_DOCUMENT_COPY_HPP + +#include +#include "compass.db/ops/copy_text.hpp" +#include "compass.db/types/document_row.hpp" +#include "compass.store/types/blob_id.hpp" + +namespace compass::db::io { + +/** + * @brief The columns of corpus.document that write_copy writes, + * in its order, for the column list of a COPY ... FROM STDIN. + */ +inline constexpr std::string_view document_copy_columns = + "hash, org_id, title, description, type, level, audience, created, " + "updated, tags, keywords, properties, source"; + +/** + * @brief Writes a row of corpus.document to @p w, in the order of + * document_copy_columns. + */ +void write_copy(ops::copy_writer& w, + const store::types::blob_id& hash, + const types::document_row& r); + +} + +#endif diff --git a/projects/compass.db/include/compass.db/io/heading_copy.hpp b/projects/compass.db/include/compass.db/io/heading_copy.hpp new file mode 100644 index 00000000..37010768 --- /dev/null +++ b/projects/compass.db/include/compass.db/io/heading_copy.hpp @@ -0,0 +1,55 @@ +/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- + * + * Copyright (C) 2026 Marco Craveiro + * + * This program is free software; you can redistribute it and/or modify it under + * the terms of the GNU General Public License as published by the Free Software + * Foundation; either version 3 of the License, or (at your option) any later + * version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more + * details. + * + * You should have received a copy of the GNU General Public License along with + * this program; if not, write to the Free Software Foundation, Inc., 51 + * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + */ +/** + * AUTO-GENERATED FILE - DO NOT EDIT MANUALLY + * Template: masd_cpp_copy.hpp.mustache + * To modify, update the template and regenerate. + */ +#ifndef COMPASS_DB_IO_HEADING_COPY_HPP +#define COMPASS_DB_IO_HEADING_COPY_HPP + +#include +#include +#include "compass.db/ops/copy_text.hpp" +#include "compass.db/types/heading_row.hpp" +#include "compass.store/types/blob_id.hpp" + +namespace compass::db::io { + +/** + * @brief The columns of corpus.heading that write_copy writes, + * in its order, for the column list of a COPY ... FROM STDIN. + */ +inline constexpr std::string_view heading_copy_columns = + "hash, ord, level, title, path, anchor, org_id, todo, priority, " + "tags, properties"; + +/** + * @brief Writes a row of corpus.heading to @p w, in the order of + * heading_copy_columns. + */ +void write_copy(ops::copy_writer& w, + const store::types::blob_id& hash, + std::size_t ord, + const types::heading_row& r); + +} + +#endif diff --git a/projects/compass.db/include/compass.db/io/link_copy.hpp b/projects/compass.db/include/compass.db/io/link_copy.hpp new file mode 100644 index 00000000..a8feca54 --- /dev/null +++ b/projects/compass.db/include/compass.db/io/link_copy.hpp @@ -0,0 +1,54 @@ +/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- + * + * Copyright (C) 2026 Marco Craveiro + * + * This program is free software; you can redistribute it and/or modify it under + * the terms of the GNU General Public License as published by the Free Software + * Foundation; either version 3 of the License, or (at your option) any later + * version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more + * details. + * + * You should have received a copy of the GNU General Public License along with + * this program; if not, write to the Free Software Foundation, Inc., 51 + * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + */ +/** + * AUTO-GENERATED FILE - DO NOT EDIT MANUALLY + * Template: masd_cpp_copy.hpp.mustache + * To modify, update the template and regenerate. + */ +#ifndef COMPASS_DB_IO_LINK_COPY_HPP +#define COMPASS_DB_IO_LINK_COPY_HPP + +#include +#include +#include "compass.db/ops/copy_text.hpp" +#include "compass.db/types/link_row.hpp" +#include "compass.store/types/blob_id.hpp" + +namespace compass::db::io { + +/** + * @brief The columns of corpus.link that write_copy writes, + * in its order, for the column list of a COPY ... FROM STDIN. + */ +inline constexpr std::string_view link_copy_columns = + "hash, ord, heading_ord, type, path, target_id, text"; + +/** + * @brief Writes a row of corpus.link to @p w, in the order of + * link_copy_columns. + */ +void write_copy(ops::copy_writer& w, + const store::types::blob_id& hash, + std::size_t ord, + const types::link_row& r); + +} + +#endif diff --git a/projects/compass.db/include/compass.db/io/placement_copy.hpp b/projects/compass.db/include/compass.db/io/placement_copy.hpp new file mode 100644 index 00000000..f24fdc11 --- /dev/null +++ b/projects/compass.db/include/compass.db/io/placement_copy.hpp @@ -0,0 +1,52 @@ +/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- + * + * Copyright (C) 2026 Marco Craveiro + * + * This program is free software; you can redistribute it and/or modify it under + * the terms of the GNU General Public License as published by the Free Software + * Foundation; either version 3 of the License, or (at your option) any later + * version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more + * details. + * + * You should have received a copy of the GNU General Public License along with + * this program; if not, write to the Free Software Foundation, Inc., 51 + * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + */ +/** + * AUTO-GENERATED FILE - DO NOT EDIT MANUALLY + * Template: masd_cpp_copy.hpp.mustache + * To modify, update the template and regenerate. + */ +#ifndef COMPASS_DB_IO_PLACEMENT_COPY_HPP +#define COMPASS_DB_IO_PLACEMENT_COPY_HPP + +#include +#include +#include "compass.db/ops/copy_text.hpp" +#include "compass.db/types/placement_row.hpp" + +namespace compass::db::io { + +/** + * @brief The columns of corpus.placement that write_copy writes, + * in its order, for the column list of a COPY ... FROM STDIN. + */ +inline constexpr std::string_view placement_copy_columns = + "environment, path, hash"; + +/** + * @brief Writes a row of corpus.placement to @p w, in the order of + * placement_copy_columns. + */ +void write_copy(ops::copy_writer& w, + std::int64_t environment, + const types::placement& r); + +} + +#endif diff --git a/projects/compass.db/include/compass.db/io/scenario_copy.hpp b/projects/compass.db/include/compass.db/io/scenario_copy.hpp new file mode 100644 index 00000000..d85bfda1 --- /dev/null +++ b/projects/compass.db/include/compass.db/io/scenario_copy.hpp @@ -0,0 +1,53 @@ +/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- + * + * Copyright (C) 2026 Marco Craveiro + * + * This program is free software; you can redistribute it and/or modify it under + * the terms of the GNU General Public License as published by the Free Software + * Foundation; either version 3 of the License, or (at your option) any later + * version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more + * details. + * + * You should have received a copy of the GNU General Public License along with + * this program; if not, write to the Free Software Foundation, Inc., 51 + * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + */ +/** + * AUTO-GENERATED FILE - DO NOT EDIT MANUALLY + * Template: masd_cpp_copy.hpp.mustache + * To modify, update the template and regenerate. + */ +#ifndef COMPASS_DB_IO_SCENARIO_COPY_HPP +#define COMPASS_DB_IO_SCENARIO_COPY_HPP + +#include +#include "compass.db/ops/copy_text.hpp" +#include "compass.db/types/scenario_row.hpp" +#include "compass.store/types/blob_id.hpp" + +namespace compass::db::io { + +/** + * @brief The columns of test.scenario that write_copy writes, + * in its order, for the column list of a COPY ... FROM STDIN. + */ +inline constexpr std::string_view scenario_copy_columns = + "hash, org_id, title, task_id, story_id, target, environment, " + "definition"; + +/** + * @brief Writes a row of test.scenario to @p w, in the order of + * scenario_copy_columns. + */ +void write_copy(ops::copy_writer& w, + const store::types::blob_id& hash, + const types::scenario_row& r); + +} + +#endif diff --git a/projects/compass.db/include/compass.db/io/section_copy.hpp b/projects/compass.db/include/compass.db/io/section_copy.hpp new file mode 100644 index 00000000..87fa023c --- /dev/null +++ b/projects/compass.db/include/compass.db/io/section_copy.hpp @@ -0,0 +1,54 @@ +/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- + * + * Copyright (C) 2026 Marco Craveiro + * + * This program is free software; you can redistribute it and/or modify it under + * the terms of the GNU General Public License as published by the Free Software + * Foundation; either version 3 of the License, or (at your option) any later + * version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more + * details. + * + * You should have received a copy of the GNU General Public License along with + * this program; if not, write to the Free Software Foundation, Inc., 51 + * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + */ +/** + * AUTO-GENERATED FILE - DO NOT EDIT MANUALLY + * Template: masd_cpp_copy.hpp.mustache + * To modify, update the template and regenerate. + */ +#ifndef COMPASS_DB_IO_SECTION_COPY_HPP +#define COMPASS_DB_IO_SECTION_COPY_HPP + +#include +#include +#include "compass.db/ops/copy_text.hpp" +#include "compass.db/types/section_row.hpp" +#include "compass.store/types/blob_id.hpp" + +namespace compass::db::io { + +/** + * @brief The columns of corpus.section that write_copy writes, + * in its order, for the column list of a COPY ... FROM STDIN. + */ +inline constexpr std::string_view section_copy_columns = + "hash, ord, heading_ord, text"; + +/** + * @brief Writes a row of corpus.section to @p w, in the order of + * section_copy_columns. + */ +void write_copy(ops::copy_writer& w, + const store::types::blob_id& hash, + std::size_t ord, + const types::section_row& r); + +} + +#endif diff --git a/projects/compass.db/include/compass.db/io/step_copy.hpp b/projects/compass.db/include/compass.db/io/step_copy.hpp new file mode 100644 index 00000000..64a76237 --- /dev/null +++ b/projects/compass.db/include/compass.db/io/step_copy.hpp @@ -0,0 +1,54 @@ +/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- + * + * Copyright (C) 2026 Marco Craveiro + * + * This program is free software; you can redistribute it and/or modify it under + * the terms of the GNU General Public License as published by the Free Software + * Foundation; either version 3 of the License, or (at your option) any later + * version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more + * details. + * + * You should have received a copy of the GNU General Public License along with + * this program; if not, write to the Free Software Foundation, Inc., 51 + * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + */ +/** + * AUTO-GENERATED FILE - DO NOT EDIT MANUALLY + * Template: masd_cpp_copy.hpp.mustache + * To modify, update the template and regenerate. + */ +#ifndef COMPASS_DB_IO_STEP_COPY_HPP +#define COMPASS_DB_IO_STEP_COPY_HPP + +#include +#include +#include "compass.db/ops/copy_text.hpp" +#include "compass.db/types/step_row.hpp" +#include "compass.store/types/blob_id.hpp" + +namespace compass::db::io { + +/** + * @brief The columns of test.step that write_copy writes, + * in its order, for the column list of a COPY ... FROM STDIN. + */ +inline constexpr std::string_view step_copy_columns = + "hash, ord, title, body"; + +/** + * @brief Writes a row of test.step to @p w, in the order of + * step_copy_columns. + */ +void write_copy(ops::copy_writer& w, + const store::types::blob_id& hash, + std::size_t ord, + const types::step_row& r); + +} + +#endif diff --git a/projects/compass.db/include/compass.db/ops/copy_text.hpp b/projects/compass.db/include/compass.db/ops/copy_text.hpp index 6fc68e98..3ccf932a 100644 --- a/projects/compass.db/include/compass.db/ops/copy_text.hpp +++ b/projects/compass.db/include/compass.db/ops/copy_text.hpp @@ -20,11 +20,15 @@ #ifndef COMPASS_DB_OPS_COPY_TEXT_HPP #define COMPASS_DB_OPS_COPY_TEXT_HPP +#include #include #include #include #include #include +#include +#include +#include "compass.store/types/blob_id.hpp" namespace compass::db::ops { @@ -44,8 +48,43 @@ class copy_writer final { void text(const char* value) { text(std::string_view(value)); } void text(const std::optional& value); void number(std::uint64_t value); + void integer(std::int64_t value); void null(); + /** + * @brief One field of a generated row, in the form its type takes in + * COPY text: text as text, an integer in decimal, a blob ID in hex, a + * vector of text as a text[], an enumeration as its to_string, and an + * empty optional as NULL. + */ + void field(std::string_view value) { text(value); } + void field(const std::string& value) { text(value); } + void field(const store::types::blob_id& value) { bytes(value.bytes); } + void field(const std::vector& values) { array(values); } + + template + requires(!std::same_as) + void field(T value) { + if constexpr (std::is_signed_v) + integer(value); + else + number(value); + } + + template + requires std::is_enum_v + void field(E value) { + text(to_string(value)); + } + + template + void field(const std::optional& value) { + if (value) + field(*value); + else + null(); + } + /** * @brief A bytea value, written in hex. */ diff --git a/projects/compass.db/include/compass.db/types/attached_row.hpp b/projects/compass.db/include/compass.db/types/attached_row.hpp new file mode 100644 index 00000000..5c877978 --- /dev/null +++ b/projects/compass.db/include/compass.db/types/attached_row.hpp @@ -0,0 +1,50 @@ +/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- + * + * Copyright (C) 2026 Marco Craveiro + * + * This program is free software; you can redistribute it and/or modify it under + * the terms of the GNU General Public License as published by the Free Software + * Foundation; either version 3 of the License, or (at your option) any later + * version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more + * details. + * + * You should have received a copy of the GNU General Public License along with + * this program; if not, write to the Free Software Foundation, Inc., 51 + * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + */ +/** + * AUTO-GENERATED FILE - DO NOT EDIT MANUALLY + * Template: masd_cpp_row.hpp.mustache + * To modify, update the template and regenerate. + */ +#ifndef COMPASS_DB_TYPES_ATTACHED_ROW_HPP +#define COMPASS_DB_TYPES_ATTACHED_ROW_HPP + +#include +#include +#include +#include "compass.store/types/blob_id.hpp" + +namespace compass::db::types { + +/** + * @brief An attachment attached to a run or a note. + * + * A row of activity.attached. + */ +struct attached_row { + store::types::blob_id attachment; + std::optional run; + std::optional note; + std::optional step_ord; + std::optional caption; +}; + +} + +#endif diff --git a/projects/compass.db/include/compass.db/types/attachment_row.hpp b/projects/compass.db/include/compass.db/types/attachment_row.hpp new file mode 100644 index 00000000..cf53f50d --- /dev/null +++ b/projects/compass.db/include/compass.db/types/attachment_row.hpp @@ -0,0 +1,44 @@ +/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- + * + * Copyright (C) 2026 Marco Craveiro + * + * This program is free software; you can redistribute it and/or modify it under + * the terms of the GNU General Public License as published by the Free Software + * Foundation; either version 3 of the License, or (at your option) any later + * version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more + * details. + * + * You should have received a copy of the GNU General Public License along with + * this program; if not, write to the Free Software Foundation, Inc., 51 + * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + */ +/** + * AUTO-GENERATED FILE - DO NOT EDIT MANUALLY + * Template: masd_cpp_row.hpp.mustache + * To modify, update the template and regenerate. + */ +#ifndef COMPASS_DB_TYPES_ATTACHMENT_ROW_HPP +#define COMPASS_DB_TYPES_ATTACHMENT_ROW_HPP + +#include + +namespace compass::db::types { + +/** + * @brief The bytes of an attached file, stored once. + * + * A row of activity.attachment. + */ +struct attachment_row { + std::string media_type; + std::string bytes; +}; + +} + +#endif diff --git a/projects/compass.db/include/compass.db/types/campaign.hpp b/projects/compass.db/include/compass.db/types/campaign.hpp index 9f1866cb..176b2c4a 100644 --- a/projects/compass.db/include/compass.db/types/campaign.hpp +++ b/projects/compass.db/include/compass.db/types/campaign.hpp @@ -23,8 +23,9 @@ #include #include #include -#include #include +#include "compass.db/types/run_row.hpp" +#include "compass.db/types/step_result_row.hpp" namespace compass::db::types { @@ -37,22 +38,6 @@ struct note_kinds { std::vector names; }; -/** - * @brief The outcome a tester gives one step of a run. - */ -enum class step_outcome { pass, fail, blocked, skipped }; - -/** - * @brief The outcome of a finished run, derived from its steps' outcomes. - */ -enum class run_outcome { passed, failed, blocked }; - -/** - * @brief Each outcome's name as the activity tables store it. - */ -[[nodiscard]] std::string_view to_string(step_outcome o) noexcept; -[[nodiscard]] std::string_view to_string(run_outcome o) noexcept; - /** * @brief Starts a run of the scenario whose :ID: is scenario_id, as the * environment holds it now, by tester. The first run of a story's diff --git a/projects/compass.db/include/compass.db/types/campaign_row.hpp b/projects/compass.db/include/compass.db/types/campaign_row.hpp new file mode 100644 index 00000000..ab56935b --- /dev/null +++ b/projects/compass.db/include/compass.db/types/campaign_row.hpp @@ -0,0 +1,48 @@ +/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- + * + * Copyright (C) 2026 Marco Craveiro + * + * This program is free software; you can redistribute it and/or modify it under + * the terms of the GNU General Public License as published by the Free Software + * Foundation; either version 3 of the License, or (at your option) any later + * version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more + * details. + * + * You should have received a copy of the GNU General Public License along with + * this program; if not, write to the Free Software Foundation, Inc., 51 + * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + */ +/** + * AUTO-GENERATED FILE - DO NOT EDIT MANUALLY + * Template: masd_cpp_row.hpp.mustache + * To modify, update the template and regenerate. + */ +#ifndef COMPASS_DB_TYPES_CAMPAIGN_ROW_HPP +#define COMPASS_DB_TYPES_CAMPAIGN_ROW_HPP + +#include +#include +#include + +namespace compass::db::types { + +/** + * @brief The testing of one story in one environment. + * + * A row of activity.campaign. + */ +struct campaign_row { + std::string environment; + std::string story_id; + std::optional sprint_id; + std::optional> exported_at; +}; + +} + +#endif diff --git a/projects/compass.db/include/compass.db/types/corpus_document.hpp b/projects/compass.db/include/compass.db/types/corpus_document.hpp index 12aa981d..48a4631f 100644 --- a/projects/compass.db/include/compass.db/types/corpus_document.hpp +++ b/projects/compass.db/include/compass.db/types/corpus_document.hpp @@ -20,110 +20,25 @@ #ifndef COMPASS_DB_TYPES_CORPUS_DOCUMENT_HPP #define COMPASS_DB_TYPES_CORPUS_DOCUMENT_HPP -#include #include -#include -#include #include +#include "compass.db/types/document_row.hpp" +#include "compass.db/types/heading_row.hpp" +#include "compass.db/types/link_row.hpp" +#include "compass.db/types/scenario_row.hpp" +#include "compass.db/types/section_row.hpp" +#include "compass.db/types/step_row.hpp" #include "compass.store/types/blob_id.hpp" namespace compass::db::types { -/** - * @brief A value of a corpus column that may be SQL NULL. - */ -using nullable = std::optional; - -/** - * @brief The row of corpus.document: the common metadata as columns, every - * keyword and the document's own properties as JSON objects, and the - * source, which the caller keeps alive. - * - * keywords maps each keyword name, in lower case, to the list of its - * values in order, so a repeated keyword keeps every value. properties - * maps each property of the drawer before the first heading, in upper - * case, to its value. - */ -struct document_row { - nullable org_id; - nullable title; - nullable description; - nullable type; - nullable level; - nullable audience; - nullable created; - nullable updated; - std::vector tags; - std::string keywords; - std::string properties; - std::string_view source; -}; - -/** - * @brief A row of corpus.heading. title is the title as plain text, a - * link replaced by its description; path is the titles from the - * outermost heading down to this one. anchor is the id compass.html gives - * the heading, so a search hit lands where the rendered page puts it. - */ -struct heading_row { - std::uint32_t level = 0; - std::string title; - std::vector path; - std::string anchor; - nullable org_id; - nullable todo; - nullable priority; - std::vector tags; - std::string properties; -}; - -/** - * @brief A row of corpus.section: the text a heading holds before its - * first subheading, or before the first heading for the preamble, as - * plain text. heading is the ord of its heading, none for the preamble. - */ -struct section_row { - std::optional heading; - std::string text; -}; - -/** - * @brief A row of corpus.link. heading is the ord of the heading the link - * sits under, none before the first heading. path is the link's path as - * written, target_id the path of an id: link, and text its description, - * or its path when it has none. - */ -struct link_row { - std::optional heading; - std::string type; - std::string path; - nullable target_id; - std::string text; -}; - -/** - * @brief A row of test.step: a heading under * Steps, and its text but - * the heading line and any Result subheading. - */ -struct step_row { - std::string title; - std::string body; -}; - /** * @brief The row of test.scenario and its steps, for a document of * #+type: test_scenario. definition is the blob ID of the page with every * Result or Results heading and the State row of Scenario Info left out, * so it changes when the steps change and not when a result is written. */ -struct scenario_row { - nullable org_id; - nullable title; - nullable task_id; - nullable story_id; - nullable target; - nullable environment; - store::types::blob_id definition; +struct test_scenario : scenario_row { std::vector steps; }; @@ -131,6 +46,10 @@ struct scenario_row { * @brief Every corpus row of one document, keyed by the blob ID of its * source. Headings, sections, links and steps are numbered by their place * in their vector, which is the ord of their row. + * + * The rows are generated from the models of compass.db/modeling, which + * say what each column holds; this aggregate, which is not a table, is + * written by hand. */ struct corpus_document { store::types::blob_id hash; @@ -138,7 +57,7 @@ struct corpus_document { std::vector headings; std::vector sections; std::vector links; - std::optional scenario; + std::optional scenario; }; } diff --git a/projects/compass.db/include/compass.db/types/document_row.hpp b/projects/compass.db/include/compass.db/types/document_row.hpp new file mode 100644 index 00000000..c75b4e83 --- /dev/null +++ b/projects/compass.db/include/compass.db/types/document_row.hpp @@ -0,0 +1,57 @@ +/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- + * + * Copyright (C) 2026 Marco Craveiro + * + * This program is free software; you can redistribute it and/or modify it under + * the terms of the GNU General Public License as published by the Free Software + * Foundation; either version 3 of the License, or (at your option) any later + * version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more + * details. + * + * You should have received a copy of the GNU General Public License along with + * this program; if not, write to the Free Software Foundation, Inc., 51 + * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + */ +/** + * AUTO-GENERATED FILE - DO NOT EDIT MANUALLY + * Template: masd_cpp_row.hpp.mustache + * To modify, update the template and regenerate. + */ +#ifndef COMPASS_DB_TYPES_DOCUMENT_ROW_HPP +#define COMPASS_DB_TYPES_DOCUMENT_ROW_HPP + +#include +#include +#include +#include + +namespace compass::db::types { + +/** + * @brief One org document of the corpus. + * + * A row of corpus.document. + */ +struct document_row { + std::optional org_id; + std::optional title; + std::optional description; + std::optional type; + std::optional level; + std::optional audience; + std::optional created; + std::optional updated; + std::vector tags; + std::string keywords; + std::string properties; + std::string_view source; +}; + +} + +#endif diff --git a/projects/compass.db/include/compass.db/types/environment.hpp b/projects/compass.db/include/compass.db/types/environment.hpp index 247232df..3397dad9 100644 --- a/projects/compass.db/include/compass.db/types/environment.hpp +++ b/projects/compass.db/include/compass.db/types/environment.hpp @@ -21,49 +21,12 @@ #define COMPASS_DB_TYPES_ENVIRONMENT_HPP #include -#include #include -#include -#include "compass.store/types/blob_id.hpp" +#include "compass.db/types/environment_row.hpp" +#include "compass.db/types/placement_row.hpp" namespace compass::db::types { -/** - * @brief What an environment is a copy of: a worktree's files, a branch, - * or a release synced from a commit at deploy. - */ -enum class environment_kind : std::uint8_t { worktree, branch, release }; - -[[nodiscard]] constexpr std::string_view to_string( - environment_kind k) noexcept { - switch (k) { - case environment_kind::worktree: return "worktree"; - case environment_kind::branch: return "branch"; - case environment_kind::release: return "release"; - } - return "worktree"; -} - -/** - * @brief The environment a sync writes. root is the checkout of a - * worktree, empty for a release; head is the commit its files are at, when - * the sync knows it. - */ -struct environment { - std::string name; - environment_kind kind = environment_kind::worktree; - std::string root; - std::optional head; -}; - -/** - * @brief A path of an environment and the blob ID of what it holds. - */ -struct placement { - std::string path; - store::types::blob_id hash; -}; - /** * @brief An environment as the database holds it, with how many paths it * places; synced_at is the server's text for the time, empty when never diff --git a/projects/compass.db/include/compass.db/types/environment_row.hpp b/projects/compass.db/include/compass.db/types/environment_row.hpp new file mode 100644 index 00000000..fb29eee9 --- /dev/null +++ b/projects/compass.db/include/compass.db/types/environment_row.hpp @@ -0,0 +1,79 @@ +/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- + * + * Copyright (C) 2026 Marco Craveiro + * + * This program is free software; you can redistribute it and/or modify it under + * the terms of the GNU General Public License as published by the Free Software + * Foundation; either version 3 of the License, or (at your option) any later + * version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more + * details. + * + * You should have received a copy of the GNU General Public License along with + * this program; if not, write to the Free Software Foundation, Inc., 51 + * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + */ +/** + * AUTO-GENERATED FILE - DO NOT EDIT MANUALLY + * Template: masd_cpp_row.hpp.mustache + * To modify, update the template and regenerate. + */ +#ifndef COMPASS_DB_TYPES_ENVIRONMENT_ROW_HPP +#define COMPASS_DB_TYPES_ENVIRONMENT_ROW_HPP + +#include +#include +#include + +namespace compass::db::types { + +/** + * @brief The values of corpus.environment.kind. + */ +enum class environment_kind { worktree, branch, release }; + +/** + * @brief Each value of environment_kind as corpus.environment stores it. + */ +[[nodiscard]] constexpr std::string_view to_string(environment_kind v) noexcept { + switch (v) { + case environment_kind::worktree: return "worktree"; + case environment_kind::branch: return "branch"; + case environment_kind::release: return "release"; + } + return "worktree"; +} + +/** + * @brief The environment_kind @p text names, or none for any other text. + */ +[[nodiscard]] constexpr std::optional environment_kind_of( + std::string_view text) noexcept { + if (text == "worktree") + return environment_kind::worktree; + if (text == "branch") + return environment_kind::branch; + if (text == "release") + return environment_kind::release; + return std::nullopt; +} + +/** + * @brief A worktree, branch or release of the repository. + * + * A row of corpus.environment. + */ +struct environment { + std::string name; + environment_kind kind = environment_kind::worktree; + std::optional root; + std::optional head; +}; + +} + +#endif diff --git a/projects/compass.db/include/compass.db/types/heading_row.hpp b/projects/compass.db/include/compass.db/types/heading_row.hpp new file mode 100644 index 00000000..a1909119 --- /dev/null +++ b/projects/compass.db/include/compass.db/types/heading_row.hpp @@ -0,0 +1,54 @@ +/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- + * + * Copyright (C) 2026 Marco Craveiro + * + * This program is free software; you can redistribute it and/or modify it under + * the terms of the GNU General Public License as published by the Free Software + * Foundation; either version 3 of the License, or (at your option) any later + * version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more + * details. + * + * You should have received a copy of the GNU General Public License along with + * this program; if not, write to the Free Software Foundation, Inc., 51 + * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + */ +/** + * AUTO-GENERATED FILE - DO NOT EDIT MANUALLY + * Template: masd_cpp_row.hpp.mustache + * To modify, update the template and regenerate. + */ +#ifndef COMPASS_DB_TYPES_HEADING_ROW_HPP +#define COMPASS_DB_TYPES_HEADING_ROW_HPP + +#include +#include +#include +#include + +namespace compass::db::types { + +/** + * @brief One heading of a corpus document. + * + * A row of corpus.heading. + */ +struct heading_row { + std::uint32_t level = 0; + std::string title; + std::vector path; + std::string anchor; + std::optional org_id; + std::optional todo; + std::optional priority; + std::vector tags; + std::string properties; +}; + +} + +#endif diff --git a/projects/compass.db/include/compass.db/types/link_row.hpp b/projects/compass.db/include/compass.db/types/link_row.hpp new file mode 100644 index 00000000..a7a3f559 --- /dev/null +++ b/projects/compass.db/include/compass.db/types/link_row.hpp @@ -0,0 +1,49 @@ +/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- + * + * Copyright (C) 2026 Marco Craveiro + * + * This program is free software; you can redistribute it and/or modify it under + * the terms of the GNU General Public License as published by the Free Software + * Foundation; either version 3 of the License, or (at your option) any later + * version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more + * details. + * + * You should have received a copy of the GNU General Public License along with + * this program; if not, write to the Free Software Foundation, Inc., 51 + * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + */ +/** + * AUTO-GENERATED FILE - DO NOT EDIT MANUALLY + * Template: masd_cpp_row.hpp.mustache + * To modify, update the template and regenerate. + */ +#ifndef COMPASS_DB_TYPES_LINK_ROW_HPP +#define COMPASS_DB_TYPES_LINK_ROW_HPP + +#include +#include +#include + +namespace compass::db::types { + +/** + * @brief One link of a corpus document. + * + * A row of corpus.link. + */ +struct link_row { + std::optional heading; + std::string type; + std::string path; + std::optional target_id; + std::string text; +}; + +} + +#endif diff --git a/projects/compass.db/include/compass.db/types/note_row.hpp b/projects/compass.db/include/compass.db/types/note_row.hpp new file mode 100644 index 00000000..52a28cf4 --- /dev/null +++ b/projects/compass.db/include/compass.db/types/note_row.hpp @@ -0,0 +1,84 @@ +/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- + * + * Copyright (C) 2026 Marco Craveiro + * + * This program is free software; you can redistribute it and/or modify it under + * the terms of the GNU General Public License as published by the Free Software + * Foundation; either version 3 of the License, or (at your option) any later + * version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more + * details. + * + * You should have received a copy of the GNU General Public License along with + * this program; if not, write to the Free Software Foundation, Inc., 51 + * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + */ +/** + * AUTO-GENERATED FILE - DO NOT EDIT MANUALLY + * Template: masd_cpp_row.hpp.mustache + * To modify, update the template and regenerate. + */ +#ifndef COMPASS_DB_TYPES_NOTE_ROW_HPP +#define COMPASS_DB_TYPES_NOTE_ROW_HPP + +#include +#include +#include +#include +#include + +namespace compass::db::types { + +/** + * @brief The values of activity.note.state. + */ +enum class note_state { open, addressed }; + +/** + * @brief Each value of note_state as activity.note stores it. + */ +[[nodiscard]] constexpr std::string_view to_string(note_state v) noexcept { + switch (v) { + case note_state::open: return "open"; + case note_state::addressed: return "addressed"; + } + return "open"; +} + +/** + * @brief The note_state @p text names, or none for any other text. + */ +[[nodiscard]] constexpr std::optional note_state_of( + std::string_view text) noexcept { + if (text == "open") + return note_state::open; + if (text == "addressed") + return note_state::addressed; + return std::nullopt; +} + +/** + * @brief A tester's remark on a campaign, and how it was addressed. + * + * A row of activity.note. + */ +struct note_row { + std::int64_t campaign = 0; + std::optional scenario_id; + std::optional step_ord; + std::string kind; + std::string text; + std::string author; + note_state state = note_state::open; + std::optional reply; + std::optional addressed_by; + std::optional> addressed_at; +}; + +} + +#endif diff --git a/projects/compass.db/include/compass.db/types/placement_row.hpp b/projects/compass.db/include/compass.db/types/placement_row.hpp new file mode 100644 index 00000000..af494ee8 --- /dev/null +++ b/projects/compass.db/include/compass.db/types/placement_row.hpp @@ -0,0 +1,45 @@ +/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- + * + * Copyright (C) 2026 Marco Craveiro + * + * This program is free software; you can redistribute it and/or modify it under + * the terms of the GNU General Public License as published by the Free Software + * Foundation; either version 3 of the License, or (at your option) any later + * version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more + * details. + * + * You should have received a copy of the GNU General Public License along with + * this program; if not, write to the Free Software Foundation, Inc., 51 + * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + */ +/** + * AUTO-GENERATED FILE - DO NOT EDIT MANUALLY + * Template: masd_cpp_row.hpp.mustache + * To modify, update the template and regenerate. + */ +#ifndef COMPASS_DB_TYPES_PLACEMENT_ROW_HPP +#define COMPASS_DB_TYPES_PLACEMENT_ROW_HPP + +#include +#include "compass.store/types/blob_id.hpp" + +namespace compass::db::types { + +/** + * @brief A path of an environment and the document it holds. + * + * A row of corpus.placement. + */ +struct placement { + std::string path; + store::types::blob_id hash; +}; + +} + +#endif diff --git a/projects/compass.db/include/compass.db/types/run_row.hpp b/projects/compass.db/include/compass.db/types/run_row.hpp new file mode 100644 index 00000000..d2c9e27d --- /dev/null +++ b/projects/compass.db/include/compass.db/types/run_row.hpp @@ -0,0 +1,85 @@ +/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- + * + * Copyright (C) 2026 Marco Craveiro + * + * This program is free software; you can redistribute it and/or modify it under + * the terms of the GNU General Public License as published by the Free Software + * Foundation; either version 3 of the License, or (at your option) any later + * version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more + * details. + * + * You should have received a copy of the GNU General Public License along with + * this program; if not, write to the Free Software Foundation, Inc., 51 + * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + */ +/** + * AUTO-GENERATED FILE - DO NOT EDIT MANUALLY + * Template: masd_cpp_row.hpp.mustache + * To modify, update the template and regenerate. + */ +#ifndef COMPASS_DB_TYPES_RUN_ROW_HPP +#define COMPASS_DB_TYPES_RUN_ROW_HPP + +#include +#include +#include +#include +#include +#include "compass.store/types/blob_id.hpp" + +namespace compass::db::types { + +/** + * @brief The values of activity.run.outcome. + */ +enum class run_outcome { passed, failed, blocked }; + +/** + * @brief Each value of run_outcome as activity.run stores it. + */ +[[nodiscard]] constexpr std::string_view to_string(run_outcome v) noexcept { + switch (v) { + case run_outcome::passed: return "passed"; + case run_outcome::failed: return "failed"; + case run_outcome::blocked: return "blocked"; + } + return "passed"; +} + +/** + * @brief The run_outcome @p text names, or none for any other text. + */ +[[nodiscard]] constexpr std::optional run_outcome_of( + std::string_view text) noexcept { + if (text == "passed") + return run_outcome::passed; + if (text == "failed") + return run_outcome::failed; + if (text == "blocked") + return run_outcome::blocked; + return std::nullopt; +} + +/** + * @brief One run of a test scenario in a campaign. + * + * A row of activity.run. + */ +struct run_row { + std::int64_t campaign = 0; + std::string scenario_id; + store::types::blob_id definition; + store::types::blob_id scenario_hash; + std::string tester; + std::optional> finished_at; + std::optional outcome; +}; + +} + +#endif diff --git a/projects/compass.db/include/compass.db/types/scenario_row.hpp b/projects/compass.db/include/compass.db/types/scenario_row.hpp new file mode 100644 index 00000000..8871857a --- /dev/null +++ b/projects/compass.db/include/compass.db/types/scenario_row.hpp @@ -0,0 +1,51 @@ +/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- + * + * Copyright (C) 2026 Marco Craveiro + * + * This program is free software; you can redistribute it and/or modify it under + * the terms of the GNU General Public License as published by the Free Software + * Foundation; either version 3 of the License, or (at your option) any later + * version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more + * details. + * + * You should have received a copy of the GNU General Public License along with + * this program; if not, write to the Free Software Foundation, Inc., 51 + * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + */ +/** + * AUTO-GENERATED FILE - DO NOT EDIT MANUALLY + * Template: masd_cpp_row.hpp.mustache + * To modify, update the template and regenerate. + */ +#ifndef COMPASS_DB_TYPES_SCENARIO_ROW_HPP +#define COMPASS_DB_TYPES_SCENARIO_ROW_HPP + +#include +#include +#include "compass.store/types/blob_id.hpp" + +namespace compass::db::types { + +/** + * @brief The test definition of a test_scenario document. + * + * A row of test.scenario. + */ +struct scenario_row { + std::optional org_id; + std::optional title; + std::optional task_id; + std::optional story_id; + std::optional target; + std::optional environment; + store::types::blob_id definition; +}; + +} + +#endif diff --git a/projects/compass.db/include/compass.db/types/section_row.hpp b/projects/compass.db/include/compass.db/types/section_row.hpp new file mode 100644 index 00000000..99e4df9d --- /dev/null +++ b/projects/compass.db/include/compass.db/types/section_row.hpp @@ -0,0 +1,46 @@ +/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- + * + * Copyright (C) 2026 Marco Craveiro + * + * This program is free software; you can redistribute it and/or modify it under + * the terms of the GNU General Public License as published by the Free Software + * Foundation; either version 3 of the License, or (at your option) any later + * version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more + * details. + * + * You should have received a copy of the GNU General Public License along with + * this program; if not, write to the Free Software Foundation, Inc., 51 + * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + */ +/** + * AUTO-GENERATED FILE - DO NOT EDIT MANUALLY + * Template: masd_cpp_row.hpp.mustache + * To modify, update the template and regenerate. + */ +#ifndef COMPASS_DB_TYPES_SECTION_ROW_HPP +#define COMPASS_DB_TYPES_SECTION_ROW_HPP + +#include +#include +#include + +namespace compass::db::types { + +/** + * @brief The searchable text of one heading of a corpus document. + * + * A row of corpus.section. + */ +struct section_row { + std::optional heading; + std::string text; +}; + +} + +#endif diff --git a/projects/compass.db/include/compass.db/types/step_result_row.hpp b/projects/compass.db/include/compass.db/types/step_result_row.hpp new file mode 100644 index 00000000..19fad7fa --- /dev/null +++ b/projects/compass.db/include/compass.db/types/step_result_row.hpp @@ -0,0 +1,83 @@ +/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- + * + * Copyright (C) 2026 Marco Craveiro + * + * This program is free software; you can redistribute it and/or modify it under + * the terms of the GNU General Public License as published by the Free Software + * Foundation; either version 3 of the License, or (at your option) any later + * version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more + * details. + * + * You should have received a copy of the GNU General Public License along with + * this program; if not, write to the Free Software Foundation, Inc., 51 + * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + */ +/** + * AUTO-GENERATED FILE - DO NOT EDIT MANUALLY + * Template: masd_cpp_row.hpp.mustache + * To modify, update the template and regenerate. + */ +#ifndef COMPASS_DB_TYPES_STEP_RESULT_ROW_HPP +#define COMPASS_DB_TYPES_STEP_RESULT_ROW_HPP + +#include +#include +#include +#include + +namespace compass::db::types { + +/** + * @brief The values of activity.step_result.outcome. + */ +enum class step_outcome { pass, fail, blocked, skipped }; + +/** + * @brief Each value of step_outcome as activity.step_result stores it. + */ +[[nodiscard]] constexpr std::string_view to_string(step_outcome v) noexcept { + switch (v) { + case step_outcome::pass: return "pass"; + case step_outcome::fail: return "fail"; + case step_outcome::blocked: return "blocked"; + case step_outcome::skipped: return "skipped"; + } + return "pass"; +} + +/** + * @brief The step_outcome @p text names, or none for any other text. + */ +[[nodiscard]] constexpr std::optional step_outcome_of( + std::string_view text) noexcept { + if (text == "pass") + return step_outcome::pass; + if (text == "fail") + return step_outcome::fail; + if (text == "blocked") + return step_outcome::blocked; + if (text == "skipped") + return step_outcome::skipped; + return std::nullopt; +} + +/** + * @brief The outcome of one step of a run. + * + * A row of activity.step_result. + */ +struct step_result_row { + std::int64_t run = 0; + std::int32_t step_ord = 0; + step_outcome outcome = step_outcome::pass; + std::optional notes; +}; + +} + +#endif diff --git a/projects/compass.db/include/compass.db/types/step_row.hpp b/projects/compass.db/include/compass.db/types/step_row.hpp new file mode 100644 index 00000000..154a9324 --- /dev/null +++ b/projects/compass.db/include/compass.db/types/step_row.hpp @@ -0,0 +1,44 @@ +/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- + * + * Copyright (C) 2026 Marco Craveiro + * + * This program is free software; you can redistribute it and/or modify it under + * the terms of the GNU General Public License as published by the Free Software + * Foundation; either version 3 of the License, or (at your option) any later + * version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more + * details. + * + * You should have received a copy of the GNU General Public License along with + * this program; if not, write to the Free Software Foundation, Inc., 51 + * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + */ +/** + * AUTO-GENERATED FILE - DO NOT EDIT MANUALLY + * Template: masd_cpp_row.hpp.mustache + * To modify, update the template and regenerate. + */ +#ifndef COMPASS_DB_TYPES_STEP_ROW_HPP +#define COMPASS_DB_TYPES_STEP_ROW_HPP + +#include + +namespace compass::db::types { + +/** + * @brief One step of a test scenario. + * + * A row of test.step. + */ +struct step_row { + std::string title; + std::string body; +}; + +} + +#endif diff --git a/projects/compass.db/modeling/compass.db.attached.org b/projects/compass.db/modeling/compass.db.attached.org index 777bb9d0..7c1de76d 100644 --- a/projects/compass.db/modeling/compass.db.attached.org +++ b/projects/compass.db/modeling/compass.db.attached.org @@ -112,9 +112,10 @@ Deleting a note deletes what is attached to it. * C++ -#176 has no C++ for this table; [[id:C1DC377F-4A1B-4131-B734-FE28DB63CF03][Serve the documents]] -will need its row, reader and insert. The row the model generates is the -specification of that code. +The generated row is =compass::db::types::attached_row= of +=compass.db/types/attached_row.hpp=. Nothing reads or writes it yet: +Record test campaigns writes the table with its own SQL. The profile leaves +the COPY writer off. * Notes diff --git a/projects/compass.db/modeling/compass.db.attachment.org b/projects/compass.db/modeling/compass.db.attachment.org index f1524dbe..f7114c0d 100644 --- a/projects/compass.db/modeling/compass.db.attachment.org +++ b/projects/compass.db/modeling/compass.db.attachment.org @@ -57,15 +57,16 @@ The file's bytes. * C++ -#176 has no C++ for this table; [[id:C1DC377F-4A1B-4131-B734-FE28DB63CF03][Serve the documents]] -will need its row, reader and insert. The row the model generates is the -specification of that code. +The generated row is =compass::db::types::attachment_row= of +=compass.db/types/attachment_row.hpp=. Nothing reads or writes it yet: +Record test campaigns writes the table with its own SQL. The profile leaves +the COPY writer off. * Notes =bytes= is the only =bytea= column of the schema that is not a blob ID; the -model states no =cpp_type= for it and leaves the choice of byte container to -the MASD entity context. +model states no =cpp_type=, and the MASD entity context maps =bytea= to +=std::string=, the bytes. * See also diff --git a/projects/compass.db/modeling/compass.db.campaign.org b/projects/compass.db/modeling/compass.db.campaign.org index 4eab4276..5ed09d2b 100644 --- a/projects/compass.db/modeling/compass.db.campaign.org +++ b/projects/compass.db/modeling/compass.db.campaign.org @@ -78,31 +78,28 @@ When the campaign was last exported to the story's pages; NULL until then. * SQL -** Indexes +** Unique -The schema has a =UNIQUE (environment, story_id)= table constraint, not an -index. The model states it as a unique index, the only form its indexes table -has; see Notes. +A story has one campaign in an environment. The rule is a table constraint, +=campaign_environment_story_id_key=, which =ON CONFLICT (environment, +story_id)= relies on, not a unique index. -| name | columns | unique | where | using | opclass | -|----------------------+-----------------------+--------+-------+-------+---------| -| environment_story_id | environment, story_id | true | | | | +| columns | +|-----------------------| +| environment, story_id | * C++ -#176 has no C++ for this table; [[id:C1DC377F-4A1B-4131-B734-FE28DB63CF03][Serve the documents]] -will need its row, reader and insert. The row the model generates is the -specification of that code. +The generated row is =compass::db::types::campaign_row= of +=compass.db/types/campaign_row.hpp=. Nothing writes it yet: Record test +campaigns writes the table with its own =INSERT=. * Notes A unique index and a =UNIQUE= constraint enforce the same rule but are -different catalog objects: the constraint is named -=campaign_environment_story_id_key= and is listed in =pg_constraint=, and -=ON CONFLICT ON CONSTRAINT= can name it. A template rendering this row writes -=CREATE UNIQUE INDEX campaign_environment_story_id=, which a catalog -comparison against migration 1 would report. The model needs a table-level -unique constraint, such as a =** Unique= table under =* SQL=. +different catalog objects, which the catalog comparison of the migrations +with =schema.sql= tells apart. The =** Unique= table under =* SQL= states +the constraint, so the generated table matches migration 1's. * See also diff --git a/projects/compass.db/modeling/compass.db.document.org b/projects/compass.db/modeling/compass.db.document.org index 4ca73e64..e2877e81 100644 --- a/projects/compass.db/modeling/compass.db.document.org +++ b/projects/compass.db/modeling/compass.db.document.org @@ -144,6 +144,17 @@ The document's text. The row holds a view of it, which the caller keeps alive. When the row was inserted. The database writes it, so it is in neither the row type nor the =COPY= column list. +** state +:PROPERTIES: +:type: text +:nullable: true +:generated: substring(source from '(?n)^[ \t]*\|[ \t]*State[ \t]*\|[ \t]*([^|[:space:]]+)') +:END: + +The State row of the document's Status table, the first table row that names +one, for search's state filter. Migration 2 added it, so it is the last +column; the database computes it from =source=. + * SQL ** Indexes @@ -157,15 +168,17 @@ When the row was inserted. The database writes it, so it is in neither the row t | tags | tags | false | | gin | | | keywords | keywords | false | | gin | jsonb_path_ops | | properties | properties | false | | gin | jsonb_path_ops | +| state | state | false | | | | * C++ The row is =compass::db::types::document_row= of -=compass.db/types/corpus_document.hpp=, field for field: =hash= and -=loaded_at= are left out, a nullable =text= is =std::optional= -(#176 spells it =nullable=), =text[]= is =std::vector= and -=jsonb= is the JSON text. The aggregate =corpus_document=, which holds the -hash and the vectors of child rows, stays hand-written. +=compass.db/types/document_row.hpp=, generated: =hash=, =loaded_at= and +=state= are left out, a nullable =text= is =std::optional=, +=text[]= is =std::vector= and =jsonb= is the JSON text. +=write_copy= of =compass.db/io/document_copy.hpp= takes the hash beside the +row. The aggregate =corpus_document=, which holds the hash and the vectors +of child rows, stays hand-written. * Notes diff --git a/projects/compass.db/modeling/compass.db.environment.org b/projects/compass.db/modeling/compass.db.environment.org index f4653b3a..0c9120e2 100644 --- a/projects/compass.db/modeling/compass.db.environment.org +++ b/projects/compass.db/modeling/compass.db.environment.org @@ -23,6 +23,7 @@ it and its placements. :component: db :schema: corpus :profile: environment-state +:row_type: environment :END: The profile =environment-state= is not one the investigation names; this model proposes it. It fixes =current_state= and neither =read_only= nor =retained=: a sync rewrites the row, and =drop_environment= deletes it. @@ -76,32 +77,29 @@ The commit the environment's files are at, when the sync knows it. A sync that d ** synced_at :PROPERTIES: -:type: timestamptz -:nullable: true +:type: timestamptz +:nullable: true +:written_by: writer :END: -When the last sync committed; NULL until the first. =write_environment= sets it to =now()= in an =UPDATE=, so it is in neither the row type nor the insert. It has no default, so =written_by: database= does not describe it; see Notes. +When the last sync committed; NULL until the first. =write_environment= sets it to =now()= in an =UPDATE=, so it is in neither the row type nor the insert: the writer writes it, not the row. * C++ The row is =compass::db::types::environment= of -=compass.db/types/environment.hpp=: =name=, =kind=, =root= and =head=. The -enumeration =environment_kind= (=worktree=, =branch=, =release=, on -=std::uint8_t=) and its =to_string= are what =:values:= on =kind= generates. +=compass.db/types/environment_row.hpp=, generated: =name=, =kind=, =root= and +=head=. =:row_type:= keeps #176's name. =:values:= on =kind= generates the +enumeration =environment_kind= with =to_string= and =environment_kind_of=. =environment_summary=, the read of an environment with its placement count, -is a query projection, not a row of this table. +is a query projection, not a row of this table, and stays hand-written in +=compass.db/types/environment.hpp=. * Notes -- #176's type is named =environment=, not =environment_row=. A generated row - takes the =_row= suffix unless the model can name it, which it cannot. -- =root= is nullable in the table and =std::string= in the row, an empty - string standing for NULL. A generated row makes it - =std::optional=. -- =synced_at= is written by the writer's =UPDATE ... SET synced_at = now()=, - not by a default. The investigation's =written_by= has one value, - =database=, for a column with a default; this column needs another, such - as =written_by: writer=. +- =root= is nullable, so the row holds =std::optional=: a + release has none. #176 stood an empty string for NULL. +- =synced_at= is set by the writer's =UPDATE ... SET synced_at = now()=, not + by a default: =written_by: writer= keeps it out of the row. - The upsert (=ON CONFLICT (name) DO UPDATE SET kind, root=) is the writer's, not the table's. diff --git a/projects/compass.db/modeling/compass.db.heading.org b/projects/compass.db/modeling/compass.db.heading.org index be259ed0..6d91c322 100644 --- a/projects/compass.db/modeling/compass.db.heading.org +++ b/projects/compass.db/modeling/compass.db.heading.org @@ -138,8 +138,9 @@ The document this row belongs to. Deleting the document deletes the row. * C++ The row is =compass::db::types::heading_row= of -=compass.db/types/corpus_document.hpp=, field for field: =hash= and =ord= -are left out by their key kinds. +=compass.db/types/heading_row.hpp=, generated: =hash= and =ord= are left out +by their key kinds, and =write_copy= of =compass.db/io/heading_copy.hpp= +takes them beside the row. * Notes diff --git a/projects/compass.db/modeling/compass.db.link.org b/projects/compass.db/modeling/compass.db.link.org index 26c68458..a0288935 100644 --- a/projects/compass.db/modeling/compass.db.link.org +++ b/projects/compass.db/modeling/compass.db.link.org @@ -52,6 +52,7 @@ The row's place among its document's rows of this table, from 0. It is the index :type: integer :cpp_type: std::uint32_t :nullable: true +:member: heading :END: The =ord= of the heading the link sits under; NULL before the first heading. The row type names the member =heading=; see Notes. @@ -106,13 +107,14 @@ The document this row belongs to. Deleting the document deletes the row. * C++ The row is =compass::db::types::link_row= of -=compass.db/types/corpus_document.hpp=: =heading=, =type=, =path=, -=target_id= and =text=. +=compass.db/types/link_row.hpp=, generated: =heading=, =type=, =path=, +=target_id= and =text=; =write_copy= of =compass.db/io/link_copy.hpp= takes +the hash and the ord beside it. * Notes -The member is =heading= and the column =heading_ord=, as in -=compass.db.section=; the model cannot say so. +=:member: heading= names the member of the column =heading_ord=, as in +=compass.db.section=. * See also diff --git a/projects/compass.db/modeling/compass.db.migration.org b/projects/compass.db/modeling/compass.db.migration.org index dc573c16..7e4489d1 100644 --- a/projects/compass.db/modeling/compass.db.migration.org +++ b/projects/compass.db/modeling/compass.db.migration.org @@ -56,19 +56,17 @@ When it was applied. The database writes it. * C++ -None. =schema_status= of =compass.db/io/schema.hpp= reads the version as a +None: the profile =bookkeeping= switches every MASD facet off. +=schema_status= of =compass.db/io/schema.hpp= reads the version as a =std::uint32_t=, and =migrate= inserts =version= and =name= with =exec=. * Notes -- The table is created by =bookkeeping_sql()= with =CREATE TABLE IF NOT - EXISTS=, before and outside the numbered migrations, so it can never be - created by one. The model cannot say =IF NOT EXISTS=, and it should not: - the bookkeeping SQL stays hand-written, and a generated =schema.sql= - leaves this schema out. -- The [[id:02089780-F445-4220-89E1-3CBCA0347B37][task]]'s acceptance keeps the schema =compass= hand-written. This - model exists so the product owner reviews every table in one form; whether - it stays is the product owner's call. +The table is created by =bookkeeping_sql()= with =CREATE TABLE IF NOT +EXISTS=, before and outside the numbered migrations, so no migration and no +=schema.sql= creates it: the schema model does not list the schema +=compass=, and the profile generates nothing. The model stays so every table +is reviewed in one form. * See also diff --git a/projects/compass.db/modeling/compass.db.note.org b/projects/compass.db/modeling/compass.db.note.org index 56d3267d..1d7ecd72 100644 --- a/projects/compass.db/modeling/compass.db.note.org +++ b/projects/compass.db/modeling/compass.db.note.org @@ -133,15 +133,16 @@ A campaign with notes cannot be deleted. * C++ -#176 has no C++ for this table; [[id:C1DC377F-4A1B-4131-B734-FE28DB63CF03][Serve the documents]] -will need its row, reader and insert. The row the model generates is the -specification of that code. +The generated row is =compass::db::types::note_row= of +=compass.db/types/note_row.hpp=. =:values:= on =state= generates =note_state=, and the member starts at +=note_state::open=, the default. Nothing reads or writes it yet: +Record test campaigns writes the table with its own SQL. The profile leaves +the COPY writer off. * Notes -- =state= has both a default and a value list. The default is written as SQL - text (='open'=); a generated enumeration would need the default as a value - of the list, =open=, to initialise its member. +- =state= has both a default and a value list: the default, ='open'=, + names the value its member starts with. - Nothing ties =state= to =reply=, =addressed_by= and =addressed_at=: the table lets an addressed note have no reply. The model states the table as it is. diff --git a/projects/compass.db/modeling/compass.db.placement.org b/projects/compass.db/modeling/compass.db.placement.org index 7c38ef73..c056edfb 100644 --- a/projects/compass.db/modeling/compass.db.placement.org +++ b/projects/compass.db/modeling/compass.db.placement.org @@ -22,6 +22,7 @@ transaction, so a reader sees the old placements or the new ones, never a mix. :component: db :schema: corpus :profile: environment-state +:row_type: placement :END: The profile =environment-state= is not one the investigation names; this model proposes it. It fixes =current_state= and neither =read_only= nor =retained=: a sync rewrites the row, and =drop_environment= deletes it. @@ -32,7 +33,7 @@ The profile =environment-state= is not one the investigation names; this model p :PROPERTIES: :type: bigint :primary_key: true -:key_kind: natural +:key_kind: owner :END: The environment that places the path: the owner's key. The writer takes it as an argument, so the row type leaves it out; see Notes. @@ -79,20 +80,27 @@ A placement keeps its document: a document is deleted only by collection, which |------+---------+--------+-------+-------+---------| | hash | hash | false | | | | +* Physical space + +A sync stages every placement of an environment with =COPY=, so the model +switches the COPY writer on; its profile leaves it off. + +| Address | Enabled | +|-----------------+---------| +| =masd.cpp.copy= | true | + * C++ The row is =compass::db::types::placement= of -=compass.db/types/environment.hpp=: =path= and =hash=. +=compass.db/types/placement_row.hpp=: =path= and =hash=. =:row_type:= keeps +#176's name. =environment= is an =owner= key: the key of the environment +that owns the row, which the COPY writer takes beside it. * Notes -- =environment= is part of the key and left out of the row, as an owner's key - is. None of the investigation's key kinds says that: =content_hash= is a - blob ID, =identity= is generated, =ord= is a position and =natural= stays in - the row. The model states =natural=, which is true of the column but would - put it in the row; the key kind this column needs is an owner's key, such - as =key_kind: owner=. -- #176's type is named =placement=, not =placement_row=. +The key kinds of the investigation had no word for a key that is the owner's +and not a content hash, so the format gained =key_kind: owner=; the type's +name, which is not =placement_row=, is =:row_type:= in =* Flags=. * See also diff --git a/projects/compass.db/modeling/compass.db.run.org b/projects/compass.db/modeling/compass.db.run.org index 1d2e5d83..2a66032a 100644 --- a/projects/compass.db/modeling/compass.db.run.org +++ b/projects/compass.db/modeling/compass.db.run.org @@ -121,9 +121,10 @@ A campaign with runs cannot be deleted. * C++ -#176 has no C++ for this table; [[id:C1DC377F-4A1B-4131-B734-FE28DB63CF03][Serve the documents]] -will need its row, reader and insert. The row the model generates is the -specification of that code. +The generated row is =compass::db::types::run_row= of +=compass.db/types/run_row.hpp=. =:values:= on =outcome= generates =run_outcome=. Nothing reads or writes it yet: +Record test campaigns writes the table with its own SQL. The profile leaves +the COPY writer off. * Notes diff --git a/projects/compass.db/modeling/compass.db.scenario.org b/projects/compass.db/modeling/compass.db.scenario.org index 054d8bd4..13cb6511 100644 --- a/projects/compass.db/modeling/compass.db.scenario.org +++ b/projects/compass.db/modeling/compass.db.scenario.org @@ -120,17 +120,16 @@ The scenario is deleted with its document. * C++ The row is =compass::db::types::scenario_row= of -=compass.db/types/corpus_document.hpp=: the six nullable texts and -=definition=. Its member =std::vector steps= is not a column: it -holds the =test.step= rows the scenario owns. A generated row has no such -member, so the steps move to the hand-written aggregate, or the row keeps a -hand-written wrapper. +=compass.db/types/scenario_row.hpp=, generated: the six nullable texts and +=definition=. The steps a scenario owns are no column, so they are in the +hand-written =test_scenario= of =compass.db/types/corpus_document.hpp=, a +=scenario_row= with its =std::vector steps=. * Notes -The key is also a foreign key. The model states it twice, once under -=* Columns= and once under =* Foreign keys=, which the DDL of migration 1 -writes as one column constraint, =hash bytea PRIMARY KEY REFERENCES +The key is also a foreign key. The model states it under =* Columns= and +under =* Foreign keys=, and =schema.sql= writes both as the one column +definition migration 1 has, =hash bytea PRIMARY KEY REFERENCES corpus.document ON DELETE CASCADE=. * See also diff --git a/projects/compass.db/modeling/compass.db.schema.org b/projects/compass.db/modeling/compass.db.schema.org new file mode 100644 index 00000000..d7c01cc3 --- /dev/null +++ b/projects/compass.db/modeling/compass.db.schema.org @@ -0,0 +1,82 @@ +:PROPERTIES: +:ID: 8F62072A-8E42-41ED-B409-0283C22A4BD3 +:END: +#+title: compass.db.schema +#+description: Codegen schema model for compass's database: the schemas sql/schema.sql creates, in order, and the functions it creates after the tables of the entity models beside it. +#+type: masd.codegen.schema +#+component: db +#+filetags: :model:schema:database: +#+created: 2026-11-23 +#+updated: 2026-11-23 + +The whole of compass's database at the current version, as +=projects/compass.db/sql/schema.sql= states it. Each table is the +=masd.codegen.entity= model beside this page; this model adds what belongs to +no one table: which schemas exist and in what order, and the functions. The +script is never run against a database that holds data. The migrations of +=src/ops/migrations.cpp= change such a database, and the catalog test of +=compass.db= checks that they build what this script builds. + +* Flags +:PROPERTIES: +:product: compass +:component: db +:END: + +* Schemas + +The schemas in the order the script creates them, which is the order a +foreign key may point in: a test scenario is a document of the corpus. The +schema =compass= is not listed: the migration runner creates it, with its +tables, before it applies the first migration. + +| name | description | +|----------+------------------------------------------------------------------| +| corpus | The documents of every environment and the paths that hold them. | +| test | The test scenarios among those documents and their steps. | +| activity | The test campaigns, runs, results, notes and screenshots. | + +* Functions + +** corpus.sprint_members + +A sprint's members follow the =id:= links of preambles, the sentence that +names a page's parent: a sprint page is in itself, a story or scenario names +its sprint, a task names its story. The links are followed backwards from +the sprint, through =link_target_id=, so a sprint costs a few index scans +rather than one probe per document. Migration 2 created it. + +#+begin_src sql +CREATE FUNCTION corpus.sprint_members(environment bigint, sprint text) +RETURNS SETOF bytea LANGUAGE sql STABLE AS $$ + WITH page AS ( + SELECT d.hash FROM corpus.document d + JOIN corpus.placement p ON p.hash = d.hash + AND p.environment = sprint_members.environment + WHERE d.org_id = sprint AND d.type = 'sprint' + ), + child AS ( + SELECT l.hash FROM corpus.link l + WHERE l.target_id = sprint AND l.heading_ord IS NULL + AND EXISTS (SELECT 1 FROM page) + ), + story AS ( + SELECT d.org_id FROM child c + JOIN corpus.document d ON d.hash = c.hash AND d.type = 'story' + JOIN corpus.placement p ON p.hash = d.hash + AND p.environment = sprint_members.environment + WHERE d.org_id IS NOT NULL + ) + SELECT hash FROM page + UNION + SELECT hash FROM child + UNION + SELECT l.hash FROM story s + JOIN corpus.link l ON l.target_id = s.org_id AND l.heading_ord IS NULL +$$; +#+end_src + +* See also + +- [[id:493B7D68-516E-4430-B4CE-688C6FBE1271][compass.db]]: the component overview, which lists every model. +- [[id:883A9765-56E8-4CF8-9871-73066663022E][masd.sql.schema]]: the facet that writes =schema.sql=. diff --git a/projects/compass.db/modeling/compass.db.section.org b/projects/compass.db/modeling/compass.db.section.org index 3d13385a..b1b9fed3 100644 --- a/projects/compass.db/modeling/compass.db.section.org +++ b/projects/compass.db/modeling/compass.db.section.org @@ -53,6 +53,7 @@ The row's place among its document's rows of this table, from 0. It is the index :type: integer :cpp_type: std::uint32_t :nullable: true +:member: heading :END: The =ord= of the section's heading in =corpus.heading=; NULL for the preamble. The row type names the member =heading=, not =heading_ord=; see Notes. @@ -105,22 +106,19 @@ The document this row belongs to. Deleting the document deletes the row. * C++ The row is =compass::db::types::section_row= of -=compass.db/types/corpus_document.hpp=: =std::optional +=compass.db/types/section_row.hpp=, generated: =std::optional heading= and =std::string text=. =hash= and =ord= are left out by their key -kinds and =tsv= because the writer computes it. +kinds and =tsv= because the writer computes it; =write_copy= of +=compass.db/io/section_copy.hpp= takes the hash and the ord beside the row. * Notes -- The member is =heading= and the column =heading_ord=. The model has no - property for a member name that differs from its column, so a row - generated from it names the member =heading_ord=, and #176's code that - reads =section_row::heading= would not compile against it. -- The =COPY= staging of #176 carries a column the table lacks: the heading - path joined to text, which only the =tsv= expression reads - (=stage_section (hash, ord, heading_ord, path, text)=). The model cannot - state an input a computed column needs that is not itself a column. -- The expression of =tsv= is prose here. No property carries it, and it - joins the staged document row, so it belongs to the writer, not the table. +- =:member: heading= names the member of the column =heading_ord=. +- The weight-B input of =tsv= is the heading's path. #176 staged it as a + column the table lacks; the insert now joins the staged heading and joins + its =path= with " / ", so every staged column is a column of the table. +- The expression of =tsv= stays the writer's: it reads the staged document's + title and the configured =regconfig=, which a generated column cannot. * See also diff --git a/projects/compass.db/modeling/compass.db.setting.org b/projects/compass.db/modeling/compass.db.setting.org index 12808fac..b82a5cf5 100644 --- a/projects/compass.db/modeling/compass.db.setting.org +++ b/projects/compass.db/modeling/compass.db.setting.org @@ -47,8 +47,8 @@ The setting's value as text. * C++ -None. =io/schema.cpp= and =io/corpus.cpp= read and write a setting with -=exec=. +None: the profile =bookkeeping= switches every MASD facet off. +=io/schema.cpp= and =io/corpus.cpp= read and write a setting with =exec=. * Notes diff --git a/projects/compass.db/modeling/compass.db.step.org b/projects/compass.db/modeling/compass.db.step.org index be28ceab..f2468c0e 100644 --- a/projects/compass.db/modeling/compass.db.step.org +++ b/projects/compass.db/modeling/compass.db.step.org @@ -74,7 +74,8 @@ Deleting the scenario, which its document's deletion does, deletes its steps. * C++ The row is =compass::db::types::step_row= of -=compass.db/types/corpus_document.hpp=: =title= and =body=. +=compass.db/types/step_row.hpp=, generated: =title= and =body=; =write_copy= +of =compass.db/io/step_copy.hpp= takes the hash and the ord beside it. * Notes diff --git a/projects/compass.db/modeling/compass.db.step_result.org b/projects/compass.db/modeling/compass.db.step_result.org index 6e081a3b..a3dabb33 100644 --- a/projects/compass.db/modeling/compass.db.step_result.org +++ b/projects/compass.db/modeling/compass.db.step_result.org @@ -53,6 +53,7 @@ The step's =ord= in =test.step= of the scenario the run ran. It is a value, not :PROPERTIES: :type: text :values: pass fail blocked skipped +:enum: step_outcome :END: The step's outcome. @@ -77,9 +78,11 @@ Deleting a run deletes its step results. * C++ -#176 has no C++ for this table; [[id:C1DC377F-4A1B-4131-B734-FE28DB63CF03][Serve the documents]] -will need its row, reader and insert. The row the model generates is the -specification of that code. +The generated row is =compass::db::types::step_result_row= of +=compass.db/types/step_result_row.hpp=. =:enum: step_outcome= names the enumeration of =outcome=, as +Record test campaigns named it. Nothing reads or writes it yet: +Record test campaigns writes the table with its own SQL. The profile leaves +the COPY writer off. * Notes diff --git a/projects/compass.db/modeling/component_overview.org b/projects/compass.db/modeling/component_overview.org index d42351a5..e704fbc2 100644 --- a/projects/compass.db/modeling/component_overview.org +++ b/projects/compass.db/modeling/component_overview.org @@ -256,11 +256,27 @@ say why, on a machine without the PostgreSQL server programs. * Models One codegen entity model per table, in =modeling/=, of =#+type: -masd.codegen.entity=. They describe the tables of migration 1 and the -bookkeeping schema as they are; nothing generates from them yet. +masd.codegen.entity=, and one schema model, [[id:8F62072A-8E42-41ED-B409-0283C22A4BD3][compass.db.schema]], of =#+type: +masd.codegen.schema=. =compass generate= renders them with the MASD library +of =share/masd=: + +- =sql/schema.sql=, the whole schema at the current version. No program + runs it. =ops_migrations_tests.cpp= builds one database from the + migrations and one from it and compares their catalogs, so a model change + without its migration fails; it names a retained table, of the profile + =activity-record=, that loses or retypes a column. It also pins each + released migration by its blob ID, so no released migration is edited. +- =include/compass.db/types/_row.hpp=, each table's row type and the + enumerations of its value lists. +- =include/compass.db/io/_copy.hpp= and =src/io/_copy.cpp=, + the COPY writer of each table a sync stages in bulk. + +The migrations of =src/ops/migrations.cpp= stay hand-written and frozen once +released. The aggregates =corpus_document= and =test_scenario=, and every +query, stay hand-written. [[id:7FD52505-1436-4925-A1DE-4A3EC88F0133][Investigation: How compass generates itself]] -names the features and profiles they use, and each model's =* Notes= lists -what the model cannot yet say about its table. +names the features and profiles the models use, and each model's =* Notes= +says what it chose where its table is unusual. | Model | Table | Profile | Brief | |-------+-------+---------+-------| diff --git a/projects/compass.db/sql/schema.sql b/projects/compass.db/sql/schema.sql new file mode 100644 index 00000000..6c3a0a22 --- /dev/null +++ b/projects/compass.db/sql/schema.sql @@ -0,0 +1,235 @@ +/* -*- sql-product: postgres; tab-width: 4; indent-tabs-mode: nil -*- + * + * Copyright (C) 2026 Marco Craveiro + * + * This program is free software; you can redistribute it and/or modify it under + * the terms of the GNU General Public License as published by the Free Software + * Foundation; either version 3 of the License, or (at your option) any later + * version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more + * details. + * + * You should have received a copy of the GNU General Public License along with + * this program; if not, write to the Free Software Foundation, Inc., 51 + * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + */ +/* + * AUTO-GENERATED FILE - DO NOT EDIT MANUALLY + * Template: masd_sql_schema.sql.mustache + * To modify, update the template and regenerate. + */ + +CREATE SCHEMA corpus; + +CREATE TABLE corpus.document ( + hash bytea PRIMARY KEY, + org_id text, + title text, + description text, + type text, + level text, + audience text, + created text, + updated text, + tags text[] NOT NULL DEFAULT '{}', + keywords jsonb NOT NULL DEFAULT '{}', + properties jsonb NOT NULL DEFAULT '{}', + source text NOT NULL, + loaded_at timestamptz NOT NULL DEFAULT now(), + state text GENERATED ALWAYS AS (substring(source from '(?n)^[ \t]*\|[ \t]*State[ \t]*\|[ \t]*([^|[:space:]]+)')) STORED +); +CREATE INDEX document_org_id ON corpus.document (org_id); +CREATE INDEX document_type ON corpus.document (type); +CREATE INDEX document_level ON corpus.document (level); +CREATE INDEX document_audience ON corpus.document (audience); +CREATE INDEX document_tags ON corpus.document USING gin (tags); +CREATE INDEX document_keywords ON corpus.document USING gin (keywords jsonb_path_ops); +CREATE INDEX document_properties ON corpus.document USING gin (properties jsonb_path_ops); +CREATE INDEX document_state ON corpus.document (state); + +CREATE TABLE corpus.environment ( + id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY, + name text NOT NULL UNIQUE, + kind text NOT NULL CHECK (kind IN ('worktree', 'branch', 'release')), + root text, + head text, + synced_at timestamptz +); + +CREATE TABLE corpus.heading ( + hash bytea NOT NULL REFERENCES corpus.document ON DELETE CASCADE, + ord integer NOT NULL, + level integer NOT NULL, + title text NOT NULL, + path text[] NOT NULL, + anchor text NOT NULL, + org_id text, + todo text, + priority text, + tags text[] NOT NULL DEFAULT '{}', + properties jsonb NOT NULL DEFAULT '{}', + PRIMARY KEY (hash, ord) +); +CREATE INDEX heading_org_id ON corpus.heading (org_id) WHERE org_id is not null; + +CREATE TABLE corpus.link ( + hash bytea NOT NULL REFERENCES corpus.document ON DELETE CASCADE, + ord integer NOT NULL, + heading_ord integer, + type text NOT NULL, + path text NOT NULL, + target_id text, + text text NOT NULL, + PRIMARY KEY (hash, ord) +); +CREATE INDEX link_target_id ON corpus.link (target_id) WHERE target_id is not null; + +CREATE TABLE corpus.placement ( + environment bigint NOT NULL REFERENCES corpus.environment ON DELETE CASCADE, + path text NOT NULL, + hash bytea NOT NULL REFERENCES corpus.document, + PRIMARY KEY (environment, path) +); +CREATE INDEX placement_hash ON corpus.placement (hash); + +CREATE TABLE corpus.section ( + hash bytea NOT NULL REFERENCES corpus.document ON DELETE CASCADE, + ord integer NOT NULL, + heading_ord integer, + text text NOT NULL, + tsv tsvector NOT NULL, + PRIMARY KEY (hash, ord) +); +CREATE INDEX section_tsv ON corpus.section USING gin (tsv); + +CREATE SCHEMA test; + +CREATE TABLE test.scenario ( + hash bytea PRIMARY KEY REFERENCES corpus.document ON DELETE CASCADE, + org_id text, + title text, + task_id text, + story_id text, + target text, + environment text, + definition bytea NOT NULL +); +CREATE INDEX scenario_org_id ON test.scenario (org_id); +CREATE INDEX scenario_story_id ON test.scenario (story_id); + +CREATE TABLE test.step ( + hash bytea NOT NULL REFERENCES test.scenario ON DELETE CASCADE, + ord integer NOT NULL, + title text NOT NULL, + body text NOT NULL, + PRIMARY KEY (hash, ord) +); + +CREATE SCHEMA activity; + +-- activity.attachment is retained: its rows exist nowhere else, so a change +-- to it needs a migration. +CREATE TABLE activity.attachment ( + hash bytea PRIMARY KEY, + media_type text NOT NULL, + bytes bytea NOT NULL +); + +-- activity.campaign is retained: its rows exist nowhere else, so a change +-- to it needs a migration. +CREATE TABLE activity.campaign ( + id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY, + environment text NOT NULL, + story_id text NOT NULL, + sprint_id text, + opened_at timestamptz NOT NULL DEFAULT now(), + exported_at timestamptz, + UNIQUE (environment, story_id) +); + +-- activity.note is retained: its rows exist nowhere else, so a change +-- to it needs a migration. +CREATE TABLE activity.note ( + id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY, + campaign bigint NOT NULL REFERENCES activity.campaign, + scenario_id text, + step_ord integer, + kind text NOT NULL, + text text NOT NULL, + author text NOT NULL, + created_at timestamptz NOT NULL DEFAULT now(), + state text NOT NULL DEFAULT 'open' CHECK (state IN ('open', 'addressed')), + reply text, + addressed_by text, + addressed_at timestamptz +); + +-- activity.run is retained: its rows exist nowhere else, so a change +-- to it needs a migration. +CREATE TABLE activity.run ( + id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY, + campaign bigint NOT NULL REFERENCES activity.campaign, + scenario_id text NOT NULL, + definition bytea NOT NULL, + scenario_hash bytea NOT NULL, + tester text NOT NULL, + started_at timestamptz NOT NULL DEFAULT now(), + finished_at timestamptz, + outcome text CHECK (outcome IN ('passed', 'failed', 'blocked')) +); +CREATE INDEX run_scenario_hash ON activity.run (scenario_hash); +CREATE INDEX run_scenario_id ON activity.run (scenario_id); + +-- activity.attached is retained: its rows exist nowhere else, so a change +-- to it needs a migration. +CREATE TABLE activity.attached ( + id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY, + attachment bytea NOT NULL REFERENCES activity.attachment, + run bigint REFERENCES activity.run ON DELETE CASCADE, + note bigint REFERENCES activity.note ON DELETE CASCADE, + step_ord integer, + caption text, + CHECK ((run is null) <> (note is null)) +); + +-- activity.step_result is retained: its rows exist nowhere else, so a change +-- to it needs a migration. +CREATE TABLE activity.step_result ( + run bigint NOT NULL REFERENCES activity.run ON DELETE CASCADE, + step_ord integer NOT NULL, + outcome text NOT NULL CHECK (outcome IN ('pass', 'fail', 'blocked', 'skipped')), + notes text, + PRIMARY KEY (run, step_ord) +); + +CREATE FUNCTION corpus.sprint_members(environment bigint, sprint text) +RETURNS SETOF bytea LANGUAGE sql STABLE AS $$ + WITH page AS ( + SELECT d.hash FROM corpus.document d + JOIN corpus.placement p ON p.hash = d.hash + AND p.environment = sprint_members.environment + WHERE d.org_id = sprint AND d.type = 'sprint' + ), + child AS ( + SELECT l.hash FROM corpus.link l + WHERE l.target_id = sprint AND l.heading_ord IS NULL + AND EXISTS (SELECT 1 FROM page) + ), + story AS ( + SELECT d.org_id FROM child c + JOIN corpus.document d ON d.hash = c.hash AND d.type = 'story' + JOIN corpus.placement p ON p.hash = d.hash + AND p.environment = sprint_members.environment + WHERE d.org_id IS NOT NULL + ) + SELECT hash FROM page + UNION + SELECT hash FROM child + UNION + SELECT l.hash FROM story s + JOIN corpus.link l ON l.target_id = s.org_id AND l.heading_ord IS NULL +$$; diff --git a/projects/compass.db/src/CMakeLists.txt b/projects/compass.db/src/CMakeLists.txt index 900ca9fb..ce703edd 100644 --- a/projects/compass.db/src/CMakeLists.txt +++ b/projects/compass.db/src/CMakeLists.txt @@ -21,7 +21,14 @@ set(lib_target_name ${name}.lib) add_library(${lib_target_name} io/connection.cpp io/corpus.cpp + io/document_copy.cpp + io/heading_copy.cpp + io/link_copy.cpp + io/placement_copy.cpp + io/scenario_copy.cpp io/schema.cpp + io/section_copy.cpp + io/step_copy.cpp ops/copy_text.cpp ops/extract.cpp ops/migrations.cpp diff --git a/projects/compass.db/src/io/corpus.cpp b/projects/compass.db/src/io/corpus.cpp index 31e6459a..1463dfc3 100644 --- a/projects/compass.db/src/io/corpus.cpp +++ b/projects/compass.db/src/io/corpus.cpp @@ -23,6 +23,13 @@ #include #include #include +#include "compass.db/io/document_copy.hpp" +#include "compass.db/io/heading_copy.hpp" +#include "compass.db/io/link_copy.hpp" +#include "compass.db/io/placement_copy.hpp" +#include "compass.db/io/scenario_copy.hpp" +#include "compass.db/io/section_copy.hpp" +#include "compass.db/io/step_copy.hpp" #include "compass.db/ops/copy_text.hpp" #include "compass.store/ops/blob_hash.hpp" @@ -76,20 +83,16 @@ std::string text_array(std::span values) { return out + "}"; } -std::uint64_t to_number(std::string_view text) { - std::uint64_t v = 0; +std::int64_t to_id(std::string_view text) { + std::int64_t v = 0; std::from_chars(text.data(), text.data() + text.size(), v); return v; } -std::string joined(const std::vector& path) { - std::string out; - for (const auto& p : path) { - if (!out.empty()) - out += " / "; - out += p; - } - return out; +std::uint64_t to_number(std::string_view text) { + std::uint64_t v = 0; + std::from_chars(text.data(), text.data() + text.size(), v); + return v; } /** @@ -106,98 +109,36 @@ struct staged { staged stage(std::span documents) { staged s; + ops::copy_writer document(s.documents); + ops::copy_writer heading(s.headings); + ops::copy_writer section(s.sections); + ops::copy_writer link(s.links); + ops::copy_writer scenario(s.scenarios); + ops::copy_writer step(s.steps); for (const auto& d : documents) { - const auto& m = d.document; - ops::copy_writer w(s.documents); - w.bytes(d.hash.bytes); - w.text(m.org_id); - w.text(m.title); - w.text(m.description); - w.text(m.type); - w.text(m.level); - w.text(m.audience); - w.text(m.created); - w.text(m.updated); - w.array(m.tags); - w.text(m.keywords); - w.text(m.properties); - w.text(m.source); - w.end(); - - ops::copy_writer h(s.headings); - for (std::size_t ord = 0; ord < d.headings.size(); ++ord) { - const auto& r = d.headings[ord]; - h.bytes(d.hash.bytes); - h.number(ord); - h.number(r.level); - h.text(r.title); - h.array(r.path); - h.text(r.anchor); - h.text(r.org_id); - h.text(r.todo); - h.text(r.priority); - h.array(r.tags); - h.text(r.properties); - h.end(); - } - - ops::copy_writer sw(s.sections); - for (std::size_t ord = 0; ord < d.sections.size(); ++ord) { - const auto& r = d.sections[ord]; - sw.bytes(d.hash.bytes); - sw.number(ord); - if (r.heading) { - sw.number(*r.heading); - sw.text(joined(d.headings[*r.heading].path)); - } else { - sw.null(); - sw.text(std::string_view{}); - } - sw.text(r.text); - sw.end(); - } - - ops::copy_writer l(s.links); - for (std::size_t ord = 0; ord < d.links.size(); ++ord) { - const auto& r = d.links[ord]; - l.bytes(d.hash.bytes); - l.number(ord); - if (r.heading) - l.number(*r.heading); - else - l.null(); - l.text(r.type); - l.text(r.path); - l.text(r.target_id); - l.text(r.text); - l.end(); - } - + write_copy(document, d.hash, d.document); + for (std::size_t ord = 0; ord < d.headings.size(); ++ord) + write_copy(heading, d.hash, ord, d.headings[ord]); + for (std::size_t ord = 0; ord < d.sections.size(); ++ord) + write_copy(section, d.hash, ord, d.sections[ord]); + for (std::size_t ord = 0; ord < d.links.size(); ++ord) + write_copy(link, d.hash, ord, d.links[ord]); if (!d.scenario) continue; - const auto& sc = *d.scenario; - ops::copy_writer c(s.scenarios); - c.bytes(d.hash.bytes); - c.text(sc.org_id); - c.text(sc.title); - c.text(sc.task_id); - c.text(sc.story_id); - c.text(sc.target); - c.text(sc.environment); - c.bytes(sc.definition.bytes); - c.end(); - ops::copy_writer st(s.steps); - for (std::size_t ord = 0; ord < sc.steps.size(); ++ord) { - st.bytes(d.hash.bytes); - st.number(ord); - st.text(sc.steps[ord].title); - st.text(sc.steps[ord].body); - st.end(); - } + write_copy(scenario, d.hash, *d.scenario); + for (std::size_t ord = 0; ord < d.scenario->steps.size(); ++ord) + write_copy(step, d.hash, ord, d.scenario->steps[ord]); } return s; } +/** + * @brief COPY of @p columns of @p table from standard input. + */ +std::string copy_into(std::string_view table, std::string_view columns) { + return std::format("COPY {} ({}) FROM STDIN", table, columns); +} + /** * @brief Inserts the staged rows of a batch of documents, keeping every * row already present, and returns how many documents were new. @@ -209,19 +150,19 @@ CREATE TEMP TABLE stage_document (LIKE corpus.document INCLUDING DEFAULTS) ON COMMIT DROP; CREATE TEMP TABLE stage_heading (LIKE corpus.heading) ON COMMIT DROP; CREATE TEMP TABLE stage_section (hash bytea, ord integer, heading_ord integer, - path text, text text) ON COMMIT DROP; + text text) ON COMMIT DROP; CREATE TEMP TABLE stage_link (LIKE corpus.link) ON COMMIT DROP; CREATE TEMP TABLE stage_scenario (LIKE test.scenario) ON COMMIT DROP; CREATE TEMP TABLE stage_step (LIKE test.step) ON COMMIT DROP; )sql"); - c.copy_in("COPY stage_document (hash, org_id, title, description, type, " - "level, audience, created, updated, tags, keywords, properties, " - "source) FROM STDIN", s.documents); - c.copy_in("COPY stage_heading FROM STDIN", s.headings); - c.copy_in("COPY stage_section FROM STDIN", s.sections); - c.copy_in("COPY stage_link FROM STDIN", s.links); - c.copy_in("COPY stage_scenario FROM STDIN", s.scenarios); - c.copy_in("COPY stage_step FROM STDIN", s.steps); + c.copy_in(copy_into("stage_document", document_copy_columns), + s.documents); + c.copy_in(copy_into("stage_heading", heading_copy_columns), s.headings); + c.copy_in(copy_into("stage_section", section_copy_columns), s.sections); + c.copy_in(copy_into("stage_link", link_copy_columns), s.links); + c.copy_in(copy_into("stage_scenario", scenario_copy_columns), + s.scenarios); + c.copy_in(copy_into("stage_step", step_copy_columns), s.steps); const auto inserted = c.exec(R"sql( INSERT INTO corpus.document (hash, org_id, title, description, type, level, audience, created, updated, tags, keywords, properties, source) @@ -235,9 +176,11 @@ FROM stage_document ON CONFLICT DO NOTHING INSERT INTO corpus.section (hash, ord, heading_ord, text, tsv) SELECT s.hash, s.ord, s.heading_ord, s.text, setweight(to_tsvector($1::regconfig, coalesce(d.title, '')), 'A') || - setweight(to_tsvector($1::regconfig, s.path), 'B') || + setweight(to_tsvector($1::regconfig, + coalesce(array_to_string(h.path, ' / '), '')), 'B') || setweight(to_tsvector($1::regconfig, s.text), 'D') FROM stage_section s JOIN stage_document d ON d.hash = s.hash +LEFT JOIN stage_heading h ON h.hash = s.hash AND h.ord = s.heading_ord ON CONFLICT DO NOTHING )sql", {text_search}); c.exec("INSERT INTO corpus.link SELECT * FROM stage_link " @@ -284,22 +227,19 @@ write_result write_environment(connection& c, const types::environment& e, INSERT INTO corpus.environment (name, kind, root, head) VALUES ($1, $2, $3, $4) ON CONFLICT (name) DO UPDATE SET kind = EXCLUDED.kind, root = EXCLUDED.root RETURNING id -)sql", {e.name, std::string(types::to_string(e.kind)), - e.root.empty() ? types::nullable{} : types::nullable{e.root}, +)sql", {e.name, std::string(types::to_string(e.kind)), e.root, e.head}).value(0, 0)); std::string rows; { ops::copy_writer w(rows); - for (const auto& p : placements) { - w.text(p.path); - w.bytes(p.hash.bytes); - w.end(); - } + const auto environment = to_id(id); + for (const auto& p : placements) + write_copy(w, environment, p); } - c.run_script("CREATE TEMP TABLE stage_placement (path text, hash bytea) " - "ON COMMIT DROP"); - c.copy_in("COPY stage_placement FROM STDIN", rows); + c.run_script("CREATE TEMP TABLE stage_placement " + "(LIKE corpus.placement) ON COMMIT DROP"); + c.copy_in(copy_into("stage_placement", placement_copy_columns), rows); std::vector given; given.reserve(documents.size()); diff --git a/projects/compass.db/src/io/document_copy.cpp b/projects/compass.db/src/io/document_copy.cpp new file mode 100644 index 00000000..a184f3a0 --- /dev/null +++ b/projects/compass.db/src/io/document_copy.cpp @@ -0,0 +1,48 @@ +/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- + * + * Copyright (C) 2026 Marco Craveiro + * + * This program is free software; you can redistribute it and/or modify it under + * the terms of the GNU General Public License as published by the Free Software + * Foundation; either version 3 of the License, or (at your option) any later + * version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more + * details. + * + * You should have received a copy of the GNU General Public License along with + * this program; if not, write to the Free Software Foundation, Inc., 51 + * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + */ +/** + * AUTO-GENERATED FILE - DO NOT EDIT MANUALLY + * Template: masd_cpp_copy.cpp.mustache + * To modify, update the template and regenerate. + */ +#include "compass.db/io/document_copy.hpp" + +namespace compass::db::io { + +void write_copy(ops::copy_writer& w, + const store::types::blob_id& hash, + const types::document_row& r) { + w.field(hash); + w.field(r.org_id); + w.field(r.title); + w.field(r.description); + w.field(r.type); + w.field(r.level); + w.field(r.audience); + w.field(r.created); + w.field(r.updated); + w.field(r.tags); + w.field(r.keywords); + w.field(r.properties); + w.field(r.source); + w.end(); +} + +} diff --git a/projects/compass.db/src/io/heading_copy.cpp b/projects/compass.db/src/io/heading_copy.cpp new file mode 100644 index 00000000..d3098427 --- /dev/null +++ b/projects/compass.db/src/io/heading_copy.cpp @@ -0,0 +1,47 @@ +/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- + * + * Copyright (C) 2026 Marco Craveiro + * + * This program is free software; you can redistribute it and/or modify it under + * the terms of the GNU General Public License as published by the Free Software + * Foundation; either version 3 of the License, or (at your option) any later + * version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more + * details. + * + * You should have received a copy of the GNU General Public License along with + * this program; if not, write to the Free Software Foundation, Inc., 51 + * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + */ +/** + * AUTO-GENERATED FILE - DO NOT EDIT MANUALLY + * Template: masd_cpp_copy.cpp.mustache + * To modify, update the template and regenerate. + */ +#include "compass.db/io/heading_copy.hpp" + +namespace compass::db::io { + +void write_copy(ops::copy_writer& w, + const store::types::blob_id& hash, + std::size_t ord, + const types::heading_row& r) { + w.field(hash); + w.field(ord); + w.field(r.level); + w.field(r.title); + w.field(r.path); + w.field(r.anchor); + w.field(r.org_id); + w.field(r.todo); + w.field(r.priority); + w.field(r.tags); + w.field(r.properties); + w.end(); +} + +} diff --git a/projects/compass.db/src/io/link_copy.cpp b/projects/compass.db/src/io/link_copy.cpp new file mode 100644 index 00000000..4c82bcfe --- /dev/null +++ b/projects/compass.db/src/io/link_copy.cpp @@ -0,0 +1,43 @@ +/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- + * + * Copyright (C) 2026 Marco Craveiro + * + * This program is free software; you can redistribute it and/or modify it under + * the terms of the GNU General Public License as published by the Free Software + * Foundation; either version 3 of the License, or (at your option) any later + * version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more + * details. + * + * You should have received a copy of the GNU General Public License along with + * this program; if not, write to the Free Software Foundation, Inc., 51 + * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + */ +/** + * AUTO-GENERATED FILE - DO NOT EDIT MANUALLY + * Template: masd_cpp_copy.cpp.mustache + * To modify, update the template and regenerate. + */ +#include "compass.db/io/link_copy.hpp" + +namespace compass::db::io { + +void write_copy(ops::copy_writer& w, + const store::types::blob_id& hash, + std::size_t ord, + const types::link_row& r) { + w.field(hash); + w.field(ord); + w.field(r.heading); + w.field(r.type); + w.field(r.path); + w.field(r.target_id); + w.field(r.text); + w.end(); +} + +} diff --git a/projects/compass.db/src/io/placement_copy.cpp b/projects/compass.db/src/io/placement_copy.cpp new file mode 100644 index 00000000..7deedf8b --- /dev/null +++ b/projects/compass.db/src/io/placement_copy.cpp @@ -0,0 +1,38 @@ +/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- + * + * Copyright (C) 2026 Marco Craveiro + * + * This program is free software; you can redistribute it and/or modify it under + * the terms of the GNU General Public License as published by the Free Software + * Foundation; either version 3 of the License, or (at your option) any later + * version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more + * details. + * + * You should have received a copy of the GNU General Public License along with + * this program; if not, write to the Free Software Foundation, Inc., 51 + * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + */ +/** + * AUTO-GENERATED FILE - DO NOT EDIT MANUALLY + * Template: masd_cpp_copy.cpp.mustache + * To modify, update the template and regenerate. + */ +#include "compass.db/io/placement_copy.hpp" + +namespace compass::db::io { + +void write_copy(ops::copy_writer& w, + std::int64_t environment, + const types::placement& r) { + w.field(environment); + w.field(r.path); + w.field(r.hash); + w.end(); +} + +} diff --git a/projects/compass.db/src/io/scenario_copy.cpp b/projects/compass.db/src/io/scenario_copy.cpp new file mode 100644 index 00000000..81a2bb20 --- /dev/null +++ b/projects/compass.db/src/io/scenario_copy.cpp @@ -0,0 +1,43 @@ +/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- + * + * Copyright (C) 2026 Marco Craveiro + * + * This program is free software; you can redistribute it and/or modify it under + * the terms of the GNU General Public License as published by the Free Software + * Foundation; either version 3 of the License, or (at your option) any later + * version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more + * details. + * + * You should have received a copy of the GNU General Public License along with + * this program; if not, write to the Free Software Foundation, Inc., 51 + * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + */ +/** + * AUTO-GENERATED FILE - DO NOT EDIT MANUALLY + * Template: masd_cpp_copy.cpp.mustache + * To modify, update the template and regenerate. + */ +#include "compass.db/io/scenario_copy.hpp" + +namespace compass::db::io { + +void write_copy(ops::copy_writer& w, + const store::types::blob_id& hash, + const types::scenario_row& r) { + w.field(hash); + w.field(r.org_id); + w.field(r.title); + w.field(r.task_id); + w.field(r.story_id); + w.field(r.target); + w.field(r.environment); + w.field(r.definition); + w.end(); +} + +} diff --git a/projects/compass.db/src/io/section_copy.cpp b/projects/compass.db/src/io/section_copy.cpp new file mode 100644 index 00000000..e24b4a6f --- /dev/null +++ b/projects/compass.db/src/io/section_copy.cpp @@ -0,0 +1,40 @@ +/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- + * + * Copyright (C) 2026 Marco Craveiro + * + * This program is free software; you can redistribute it and/or modify it under + * the terms of the GNU General Public License as published by the Free Software + * Foundation; either version 3 of the License, or (at your option) any later + * version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more + * details. + * + * You should have received a copy of the GNU General Public License along with + * this program; if not, write to the Free Software Foundation, Inc., 51 + * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + */ +/** + * AUTO-GENERATED FILE - DO NOT EDIT MANUALLY + * Template: masd_cpp_copy.cpp.mustache + * To modify, update the template and regenerate. + */ +#include "compass.db/io/section_copy.hpp" + +namespace compass::db::io { + +void write_copy(ops::copy_writer& w, + const store::types::blob_id& hash, + std::size_t ord, + const types::section_row& r) { + w.field(hash); + w.field(ord); + w.field(r.heading); + w.field(r.text); + w.end(); +} + +} diff --git a/projects/compass.db/src/io/step_copy.cpp b/projects/compass.db/src/io/step_copy.cpp new file mode 100644 index 00000000..5e3dfb88 --- /dev/null +++ b/projects/compass.db/src/io/step_copy.cpp @@ -0,0 +1,40 @@ +/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- + * + * Copyright (C) 2026 Marco Craveiro + * + * This program is free software; you can redistribute it and/or modify it under + * the terms of the GNU General Public License as published by the Free Software + * Foundation; either version 3 of the License, or (at your option) any later + * version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more + * details. + * + * You should have received a copy of the GNU General Public License along with + * this program; if not, write to the Free Software Foundation, Inc., 51 + * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + */ +/** + * AUTO-GENERATED FILE - DO NOT EDIT MANUALLY + * Template: masd_cpp_copy.cpp.mustache + * To modify, update the template and regenerate. + */ +#include "compass.db/io/step_copy.hpp" + +namespace compass::db::io { + +void write_copy(ops::copy_writer& w, + const store::types::blob_id& hash, + std::size_t ord, + const types::step_row& r) { + w.field(hash); + w.field(ord); + w.field(r.title); + w.field(r.body); + w.end(); +} + +} diff --git a/projects/compass.db/src/ops/copy_text.cpp b/projects/compass.db/src/ops/copy_text.cpp index 32e69286..331c32bd 100644 --- a/projects/compass.db/src/ops/copy_text.cpp +++ b/projects/compass.db/src/ops/copy_text.cpp @@ -74,6 +74,11 @@ void copy_writer::number(std::uint64_t value) { out_ += std::to_string(value); } +void copy_writer::integer(std::int64_t value) { + separate(); + out_ += std::to_string(value); +} + void copy_writer::null() { separate(); out_ += "\\N"; diff --git a/projects/compass.db/src/ops/extract.cpp b/projects/compass.db/src/ops/extract.cpp index 4d062830..b8eb36cb 100644 --- a/projects/compass.db/src/ops/extract.cpp +++ b/projects/compass.db/src/ops/extract.cpp @@ -334,10 +334,10 @@ bool is_result_heading(std::string_view title) { * @brief The test definition of a test_scenario page: Scenario Info's * links and target, the steps under * Steps, and the definition hash. */ -types::scenario_row scenario_of(const reader& r, +types::test_scenario scenario_of(const reader& r, const types::document_row& d) { const auto& t = r.t; - types::scenario_row s; + types::test_scenario s; s.org_id = d.org_id; s.title = d.title; std::vector left_out; @@ -352,7 +352,7 @@ types::scenario_row scenario_of(const reader& r, const auto cells = cells_of(r.text_of(range)); if (cells.size() < 2) continue; - const auto first_id_link = [&]() -> types::nullable { + const auto first_id_link = [&]() -> std::optional { for (std::uint32_t l = 0; l < t.links.size(); ++l) { const ot::link_id id{l}; const auto at = r.link_range(id).begin; @@ -442,7 +442,7 @@ types::corpus_document extract(const store::types::blob_id& hash, const auto name = name_text(t, t.keywords.names[id]); const auto value = r.text_of(trimmed(source, t.keywords.values[id])); keywords.add(name, value); - const auto set = [&](types::nullable& column) { + const auto set = [&](std::optional& column) { if (!column && !value.empty()) column = std::string(value); }; diff --git a/projects/compass.db/src/ops/run_outcome.cpp b/projects/compass.db/src/ops/run_outcome.cpp index bfac29fc..d1aaef36 100644 --- a/projects/compass.db/src/ops/run_outcome.cpp +++ b/projects/compass.db/src/ops/run_outcome.cpp @@ -43,26 +43,3 @@ derive_run_outcome(std::span> steps) { } } - -namespace compass::db::types { - -std::string_view to_string(step_outcome o) noexcept { - switch (o) { - case step_outcome::pass: return "pass"; - case step_outcome::fail: return "fail"; - case step_outcome::blocked: return "blocked"; - case step_outcome::skipped: return "skipped"; - } - return "pass"; -} - -std::string_view to_string(run_outcome o) noexcept { - switch (o) { - case run_outcome::passed: return "passed"; - case run_outcome::failed: return "failed"; - case run_outcome::blocked: return "blocked"; - } - return "passed"; -} - -} diff --git a/projects/compass.db/src/query/campaign.cpp b/projects/compass.db/src/query/campaign.cpp index f59bbe2a..a8a3468c 100644 --- a/projects/compass.db/src/query/campaign.cpp +++ b/projects/compass.db/src/query/campaign.cpp @@ -130,11 +130,8 @@ void require_step(io::connection& c, const std::string& scenario_hash, } types::step_outcome step_outcome_of(std::string_view s) { - using types::step_outcome; - for (const auto o : {step_outcome::pass, step_outcome::fail, - step_outcome::blocked, step_outcome::skipped}) - if (types::to_string(o) == s) - return o; + if (const auto o = types::step_outcome_of(s)) + return *o; throw io::error("activity.step_result holds an unknown outcome " + std::string(s), ""); } diff --git a/projects/compass.db/tests/CMakeLists.txt b/projects/compass.db/tests/CMakeLists.txt index 6e4fe169..649902a1 100644 --- a/projects/compass.db/tests/CMakeLists.txt +++ b/projects/compass.db/tests/CMakeLists.txt @@ -25,6 +25,7 @@ add_executable(${tests_target_name} ops_campaign_page_tests.cpp ops_copy_text_tests.cpp ops_extract_tests.cpp + ops_migrations_tests.cpp query_campaign_tests.cpp query_search_tests.cpp query_testing_tests.cpp) @@ -37,4 +38,10 @@ target_link_libraries(${tests_target_name} compass.testing.lib Catch2::Catch2WithMain) +# The catalog test reads the generated schema from the checkout, as no +# program runs it against a database. +target_compile_definitions(${tests_target_name} + PRIVATE + COMPASS_DB_SCHEMA_SQL="${CMAKE_SOURCE_DIR}/projects/compass.db/sql/schema.sql") + compass_add_catch2_tests(${name} ${tests_target_name}) diff --git a/projects/compass.db/tests/ops_migrations_tests.cpp b/projects/compass.db/tests/ops_migrations_tests.cpp new file mode 100644 index 00000000..d904a77f --- /dev/null +++ b/projects/compass.db/tests/ops_migrations_tests.cpp @@ -0,0 +1,206 @@ +/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- + * + * Copyright (C) 2026 Marco Craveiro + * + * This program is free software; you can redistribute it and/or modify it under + * the terms of the GNU General Public License as published by the Free Software + * Foundation; either version 3 of the License, or (at your option) any later + * version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more + * details. + * + * You should have received a copy of the GNU General Public License along with + * this program; if not, write to the Free Software Foundation, Inc., 51 + * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + */ +#include "compass.db/ops/migrations.hpp" + +#include +#include +#include +#include +#include +#include +#include +#include +#include "compass.db/io/schema.hpp" +#include "compass.store/ops/blob_hash.hpp" +#include "database.hpp" + +namespace { + +using namespace compass::db; + +/** + * @brief The migrations of each released compass, by version, and the + * blob ID of the SQL each released with. A released migration is never + * edited, since a database may already have applied it; a change to the + * schema is a new migration. A release adds the migrations it ships here. + */ +const std::map released{ + {1, "2041f9dd93535db458d9539aca852c864b1a6fda"}, + {2, "93311482ee2f7160c89ae77e54066b26214eac02"}}; + +std::string schema_sql() { + std::ifstream is(COMPASS_DB_SCHEMA_SQL, std::ios::binary); + REQUIRE(is); + return {std::istreambuf_iterator(is), {}}; +} + +constexpr std::string_view user_schemas = + "nspname NOT IN ('pg_catalog', 'information_schema') AND " + "nspname NOT LIKE 'pg\\_%'"; + +/** + * @brief Every object of the database's own schemas as a line of text: + * schemas, relations, columns in their order with type, nullability, + * identity, generation and default, constraints, indexes and functions. + * Two databases with the same lines have the same catalog. + */ +std::set catalog(io::connection& c) { + std::set out; + const auto add = [&](std::string_view sql) { + const auto r = c.exec(sql); + for (std::size_t row = 0; row < r.rows(); ++row) { + std::string line; + for (std::size_t col = 0; col < r.columns(); ++col) + line += (col ? " | " : "") + std::string(r.value(row, col)); + out.insert(std::move(line)); + } + }; + const std::string where(user_schemas); + add("SELECT 'schema', nspname FROM pg_namespace WHERE " + where); + add("SELECT 'relation', nspname, relname, relkind FROM pg_class c " + "JOIN pg_namespace n ON n.oid = c.relnamespace WHERE " + where); + add("SELECT 'column', nspname || '.' || relname, row_number() OVER " + "(PARTITION BY c.oid ORDER BY a.attnum), attname, " + "format_type(atttypid, atttypmod), attnotnull, attidentity, " + "attgenerated, coalesce(pg_get_expr(d.adbin, d.adrelid), '') " + "FROM pg_attribute a JOIN pg_class c ON c.oid = a.attrelid " + "JOIN pg_namespace n ON n.oid = c.relnamespace " + "LEFT JOIN pg_attrdef d ON d.adrelid = a.attrelid " + "AND d.adnum = a.attnum WHERE c.relkind = 'r' AND a.attnum > 0 " + "AND NOT a.attisdropped AND " + where); + add("SELECT 'constraint', conrelid::regclass::text, conname, contype, " + "pg_get_constraintdef(o.oid) FROM pg_constraint o " + "JOIN pg_namespace n ON n.oid = o.connamespace WHERE " + where); + add("SELECT 'index', schemaname, indexname, indexdef FROM pg_indexes " + "WHERE schemaname NOT IN ('pg_catalog', 'information_schema')"); + add("SELECT 'function', nspname, proname, pg_get_functiondef(p.oid) " + "FROM pg_proc p JOIN pg_namespace n ON n.oid = p.pronamespace " + "WHERE " + where); + return out; +} + +std::set migrated_catalog() { + io::connection c(tests::fresh_database()); + io::migrate(c); + return catalog(c); +} + +std::set generated_catalog(const std::string& sql) { + io::connection c(tests::fresh_database()); + c.run_script(ops::bookkeeping_sql()); + c.run_script(sql); + return catalog(c); +} + +std::string only_in(const std::set& a, + const std::set& b) { + std::string out; + for (const auto& line : a) + if (!b.contains(line)) + out += "\n " + line; + return out; +} + +/** + * @brief The tables schema.sql says are retained. + */ +std::set retained_tables(const std::string& sql) { + constexpr std::string_view opening = "-- "; + constexpr std::string_view marker = " is retained:"; + std::set out; + std::size_t start = 0; + while (start < sql.size()) { + auto end = sql.find('\n', start); + if (end == std::string::npos) + end = sql.size(); + const std::string_view line(sql.data() + start, end - start); + const auto at = line.find(marker); + if (line.starts_with(opening) && at != std::string_view::npos) + out.emplace(line.substr(opening.size(), at - opening.size())); + start = end + 1; + } + return out; +} + +/** + * @brief The columns of @p table in @p catalog, each name with its type. + */ +std::map columns_of( + const std::set& catalog, const std::string& table) { + std::map out; + const auto prefix = "column | " + table + " | "; + for (const auto& line : catalog) { + if (!line.starts_with(prefix)) + continue; + const auto rest = line.substr(prefix.size()); + const auto name_at = rest.find(" | ") + 3; + const auto type_at = rest.find(" | ", name_at) + 3; + const auto type_end = rest.find(" | ", type_at); + out[rest.substr(name_at, type_at - 3 - name_at)] = + rest.substr(type_at, type_end - type_at); + } + return out; +} + +} + +TEST_CASE("a released migration is never edited", "[ops][migrations]") { + for (const auto& m : ops::migrations()) { + const auto it = released.find(m.version); + if (it == released.end()) + continue; + INFO("migration " << m.version << " shipped in a release; a change " + "to the schema is a new migration"); + CHECK(compass::store::ops::to_hex( + compass::store::ops::blob_id_of(m.sql)) == it->second); + } +} + +TEST_CASE("the migrations build the catalog the generated schema.sql builds", + "[ops][migrations][database]") { + const auto migrated = migrated_catalog(); + const auto generated = generated_catalog(schema_sql()); + INFO("only the migrations build:" << only_in(migrated, generated)); + INFO("only schema.sql builds:" << only_in(generated, migrated)); + INFO("fix: a model change needs a new migration in src/ops/migrations.cpp " + "that takes the last released schema to the one schema.sql states"); + CHECK(migrated == generated); +} + +TEST_CASE("a retained table loses or retypes a column only through a " + "migration", "[ops][migrations][database]") { + const auto sql = schema_sql(); + const auto retained = retained_tables(sql); + REQUIRE_FALSE(retained.empty()); + const auto migrated = migrated_catalog(); + const auto generated = generated_catalog(sql); + for (const auto& table : retained) { + const auto before = columns_of(migrated, table); + const auto after = columns_of(generated, table); + for (const auto& [column, type] : before) { + const auto it = after.find(column); + INFO(table << " is retained: its rows exist nowhere else; fix: " + "add a migration that says what becomes of " << column); + CHECK(it != after.end()); + if (it != after.end()) + CHECK(it->second == type); + } + } +}