@biffo/cli 0.315.1 → 0.315.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -33,7 +33,7 @@
33
33
  "pnpm": {
34
34
  "overrides": {
35
35
  "brace-expansion@<5.0.9": ">=5.0.9",
36
- "js-yaml@<4.3.1": ">=4.3.1 <5",
36
+ "js-yaml@<4.3.2": ">=4.3.2 <5",
37
37
  "nanoid@<3.3.17": ">=3.3.17 <4"
38
38
  }
39
39
  }
@@ -6,7 +6,7 @@ settings:
6
6
 
7
7
  overrides:
8
8
  brace-expansion@<5.0.9: '>=5.0.9'
9
- js-yaml@<4.3.1: '>=4.3.1 <5'
9
+ js-yaml@<4.3.2: '>=4.3.2 <5'
10
10
  nanoid@<3.3.17: '>=3.3.17 <4'
11
11
 
12
12
  importers:
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@biffo/cli",
3
- "version": "0.315.1",
3
+ "version": "0.315.3",
4
4
  "description": "Biffo project scaffolding CLI",
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -35,8 +35,9 @@
35
35
  # `verify.sh` does: forks drift, and a per-instance copy of this would drift from
36
36
  # the DDL layout it is meant to build. Everything instance-specific is DERIVED --
37
37
  # the schema directories from `db/imports/*/`, the engine image from whether the
38
- # DDL asks for PostGIS, and the did-it-build threshold from the number of
39
- # policies the DDL itself declares. Nothing here names a product.
38
+ # DDL asks for PostGIS, and the did-it-build check from what THIS repo's own
39
+ # last known-good build actually produced (see #2023 below), never from a
40
+ # guessed ratio. Nothing here names a product.
40
41
  #
41
42
  # ## Usage
42
43
  #
@@ -557,20 +558,63 @@ fingerprint() {
557
558
 
558
559
  WANT=$(fingerprint)
559
560
  HAVE=""
561
+ HAVE_POLICIES=""
562
+ HAVE_MODULES=""
560
563
  if [ "$RECREATE" -eq 0 ] &&
561
564
  psql_admin -tAc "SELECT 1 FROM pg_database WHERE datname='$DB'" 2>/dev/null | grep -q 1; then
562
565
  HAVE=$(psql -tAq -h "$HOST" -p "$PORT" -U "$USER_" -d "$DB" \
563
566
  -c "SELECT value FROM biffo_pg_test_fingerprint LIMIT 1" 2>/dev/null || true)
567
+ # policy_count/module_count were added alongside this guard (#2023). A row
568
+ # written before that migration has neither column, so this query fails
569
+ # (unknown column) rather than returning NULL, and `|| true` turns that
570
+ # failure into the same empty string a genuinely missing value would give --
571
+ # which is exactly what "unverifiable, so rebuild" needs below: it must read
572
+ # identically to "no stored count", never silently as "verified".
573
+ HAVE_POLICIES=$(psql -tAq -h "$HOST" -p "$PORT" -U "$USER_" -d "$DB" \
574
+ -c "SELECT policy_count FROM biffo_pg_test_fingerprint LIMIT 1" 2>/dev/null || true)
575
+ HAVE_MODULES=$(psql -tAq -h "$HOST" -p "$PORT" -U "$USER_" -d "$DB" \
576
+ -c "SELECT module_count FROM biffo_pg_test_fingerprint LIMIT 1" 2>/dev/null || true)
564
577
  fi
565
578
 
579
+ # ── #2023: reuse must be verified against a known-good build, not a guess ────
580
+ #
581
+ # The old guard compared the live policy count to HALF of what the DDL
582
+ # *declares* (`grep -c CREATE POLICY`) -- a number a correct, complete build
583
+ # never reaches (521 declared vs. 362 produced on tabsii-platform, so the
584
+ # healthy ratio is ~0.70, not 1.0). A database missing a quarter of its schema
585
+ # passed it and was then blessed with a fingerprint every later run trusted.
586
+ #
587
+ # Worse, that check only ever ran on a fresh REBUILD (old section 4 below). The
588
+ # reuse path here never re-verified anything beyond the content hash matching --
589
+ # so a template that drifted after being blessed, by any means, was reused
590
+ # forever with no check of its own at all.
591
+ #
592
+ # The fix compares against reality instead of a guess: on every successful
593
+ # build (section 4), THIS script records the counts that build actually
594
+ # produced. A fingerprint match only says the DDL inputs are unchanged; it says
595
+ # nothing about whether the database in front of us still reflects what was
596
+ # built from them. So reuse additionally requires the live counts to equal the
597
+ # stored ones, exactly -- and a row with no stored counts (pre-#2023) is
598
+ # unverifiable, not innocent, so it rebuilds rather than being trusted.
566
599
  if [ -n "$HAVE" ] && [ "$HAVE" = "$WANT" ]; then
567
- say "schema is current, reusing $DB"
568
- clone_for_this_run
569
- emit
570
- exit 0
600
+ if [ -z "$HAVE_POLICIES" ] || [ -z "$HAVE_MODULES" ]; then
601
+ say "fingerprint matches but this row predates stored policy/module counts - unverifiable, rebuilding rather than trusting it"
602
+ else
603
+ _live_policies=$(psql -tAq -h "$HOST" -p "$PORT" -U "$USER_" -d "$DB" \
604
+ -c "SELECT count(*) FROM pg_policies" 2>/dev/null || echo 0)
605
+ _live_modules=0
606
+ [ -n "$DDL_FILES" ] && _live_modules=$(echo "$DDL_FILES" | wc -l | tr -d ' ')
607
+ if [ "${_live_policies:-0}" = "$HAVE_POLICIES" ] && [ "$_live_modules" = "$HAVE_MODULES" ]; then
608
+ say "schema is current ($_live_policies policies, $_live_modules modules), reusing $DB"
609
+ clone_for_this_run
610
+ emit
611
+ exit 0
612
+ fi
613
+ say "fingerprint matches but live schema diverged from its own record (have $_live_policies policies/$_live_modules modules, recorded $HAVE_POLICIES/$HAVE_MODULES) - rebuilding rather than reusing a partial schema"
614
+ fi
571
615
  fi
572
616
 
573
- [ -n "$HAVE" ] && say "schema inputs changed - rebuilding rather than serving a stale schema"
617
+ [ -n "$HAVE" ] && [ "$HAVE" != "$WANT" ] && say "schema inputs changed - rebuilding rather than serving a stale schema"
574
618
 
575
619
  # --- 3. rebuild the way the app and CI do ------------------------------------
576
620
  say "rebuilding $DB"
@@ -589,6 +633,7 @@ if [ -n "$ALEMBIC_DIR" ]; then
589
633
  say "alembic upgrade head"
590
634
  fi
591
635
 
636
+ _module_count=0
592
637
  if [ -n "$DDL_FILES" ]; then
593
638
  # ONE psql session, sorted by filename, mirroring the API's own DDL import.
594
639
  # Session state an early module sets -- typically `SET search_path` in the
@@ -598,38 +643,37 @@ if [ -n "$DDL_FILES" ]; then
598
643
  # shellcheck disable=SC2046
599
644
  psql -q -v ON_ERROR_STOP=1 -h "$HOST" -p "$PORT" -U "$USER_" -d "$DB" \
600
645
  --single-transaction $(echo "$DDL_FILES" | sed 's/^/-f /' | tr '\n' ' ') >/dev/null
601
- say "$(echo "$DDL_FILES" | wc -l | tr -d ' ') DDL modules applied"
646
+ _module_count=$(echo "$DDL_FILES" | wc -l | tr -d ' ')
647
+ say "$_module_count DDL modules applied"
602
648
  fi
603
649
 
604
- # --- 4. refuse to bless a half-built schema ----------------------------------
605
- #
606
- # The threshold is derived, not guessed: count the policies the DDL declares and
607
- # require the database to hold at least half. Recording a fingerprint against a
608
- # partial schema is worse than failing, because the NEXT run would trust it and
609
- # every failure after that would look like the developer's own change.
650
+ # --- 4. record what this build actually produced (#2023) ---------------------
651
+ #
652
+ # No guessed threshold here anymore. The DDL apply above is one
653
+ # `--single-transaction`, `ON_ERROR_STOP=1` psql session, so a build that fails
654
+ # partway already aborts the whole script before reaching this line -- it does
655
+ # not limp to here half-applied. What DOES reach here is simply recorded, and
656
+ # it is that RECORD -- this build's own live counts, not a guess at what they
657
+ # "should" be -- that the reuse guard above checks every later database
658
+ # against. See the comment there for why a guessed ratio (521 declared, 362
659
+ # produced) blessed a schema missing a quarter of itself.
660
+ _policy_count=0
610
661
  if [ -n "$DDL_FILES" ]; then
611
- _declared=$(echo "$DDL_FILES" | xargs grep -ciE '^[[:space:]]*CREATE[[:space:]]+POLICY' 2>/dev/null |
612
- awk -F: '{s+=$NF} END {print s+0}')
613
- if [ "${_declared:-0}" -gt 0 ]; then
614
- _actual=$(psql -tAq -h "$HOST" -p "$PORT" -U "$USER_" -d "$DB" \
615
- -c "SELECT count(*) FROM pg_policies" 2>/dev/null || echo 0)
616
- if [ "${_actual:-0}" -lt $((_declared / 2)) ]; then
617
- say "only ${_actual:-0} policies present against $_declared declared - the schema did not build."
618
- say "Not recording a fingerprint; fix the DDL and re-run."
619
- exit 1
620
- fi
621
- say "$_actual RLS policies ($_declared declared)"
622
- fi
662
+ _policy_count=$(psql -tAq -h "$HOST" -p "$PORT" -U "$USER_" -d "$DB" \
663
+ -c "SELECT count(*) FROM pg_policies" 2>/dev/null || echo 0)
664
+ say "$_policy_count RLS policies, $_module_count DDL modules applied"
623
665
  fi
624
666
 
625
667
  psql_db \
626
668
  -c "CREATE TABLE IF NOT EXISTS biffo_pg_test_fingerprint (value text primary key)" \
669
+ -c "ALTER TABLE biffo_pg_test_fingerprint ADD COLUMN IF NOT EXISTS policy_count integer" \
670
+ -c "ALTER TABLE biffo_pg_test_fingerprint ADD COLUMN IF NOT EXISTS module_count integer" \
627
671
  -c "TRUNCATE biffo_pg_test_fingerprint" \
628
- -c "INSERT INTO biffo_pg_test_fingerprint (value) VALUES ('$WANT')" >/dev/null
672
+ -c "INSERT INTO biffo_pg_test_fingerprint (value, policy_count, module_count) VALUES ('$WANT', $_policy_count, $_module_count)" >/dev/null
629
673
 
630
- # Only now, with the fingerprint recorded against a schema that passed the
631
- # check above, is the template fit to copy. Cloning earlier would hand out a
632
- # half-built database and record the failure against whoever ran next.
674
+ # Only now, with the fingerprint and its counts recorded against a build that
675
+ # actually completed, is the template fit to copy. Cloning earlier would hand
676
+ # out a half-built database and record the failure against whoever ran next.
633
677
  clone_for_this_run
634
678
 
635
679
  say "ready"