Skip to content

Collapse the five-step migration ladder into a single current-shape schema bootstrap #2688

Closed

Description

@malibio

Overview

packages/core/src/db/migrations/ implements a five-step versioned migration ladder (v001…v005, 942 lines including the runner) plus a PRAGMA user_version ratchet and a pre-migration backup system. Every part of that apparatus exists to carry an existing database created by an older build across a schema change. NodeSpace has zero users, zero production deployments, and zero releases, so no such database exists — a fresh database is the only kind there is.

The result is that a brand-new database today executes five migrations in sequence (including one that rewrites rows that cannot exist) to arrive at a shape a single CREATE TABLE batch could produce directly.

CLAUDE.md carves out this directory as legitimate, and that carve-out is correct as written: "a fresh database still needs its schema created, so that runner is legitimate and stays." But it licenses creating the tables — not replaying five historical versions to get there, and not the version ratchet and backup machinery layered on top. The carve-out has been read more broadly than it states.

Problem Statement

Three concrete costs, all observed rather than hypothetical:

1. It teaches the banned pattern. v004_schema_relationship_edges.rs is a genuine data migration — it reads properties.relationships JSON off existing schema nodes, converts each declaration to a relationship row, and strips the key. That is exactly what the no-data-migration policy prohibits; it survives only because it shipped before the rule was enforced. Because it is the most recent structural change to the relationship table, it is now cited as the precedent to imitate: #2638's Technical Specifications name it as the model migration to follow. Following it faithfully would mean writing a backfill, and #2638 only avoids that because it separately says not to. The ladder actively propagates the prohibited pattern to the next author who reads it.

2. Migrations carry compatibility workarounds for states that cannot occur. v002_embedding_origin.rs's module doc:

"Column-existence check: this migration shipped, as an ad-hoc idempotent check outside user_version tracking, before the migration runner existed. A database that already applied it under the old code has origin present but user_version still at 0 (never set), so this migration must tolerate running against a DB where its own effect already happened."

This is defensive code for a database shape produced by a build that no user ever ran.

3. Load-bearing code asserts a premise that is false under current policy. SqliteStore::backfill_fts_if_stale (packages/core/src/db/sqlite_store/mod.rs) documents itself as:

"any node predating the FTS table (user DBs are never reset — same reason the migration runner exists)"

"User DBs are never reset" is the opposite of the project's actual stance ("change the format and reset the database"). The FTS backfill's stated justification does not hold.

Proposed Solution

Replace the ladder with a single current-shape schema bootstrap.

  • Add packages/core/src/db/schema.rs exposing one create_schema(conn) that executes the current, final DDL for every table and index in one batch.
  • Delete v001_initial_schema.rs … v005_conflict_journal.rs and migrations/mod.rs.
  • Delete the version ratchet: PRAGMA user_version tracking, LATEST_VERSION, run_up_to, apply_migration, migration_name, and the newer-than-supported guard.
  • Delete the backup system: backup_before_pending_migrations, db_has_data, prune_backups, MAX_BACKUPS, and the VACUUM INTO snapshot path.
  • Update the two call sites in packages/core/src/db/sqlite_store/mod.rs (SqliteStore::new's backup call, initialize_schema's migrations::run) to call create_schema directly.
  • Rewrite packages/core/tests/migrations_test.rs against the bootstrap: the meaningful assertions become "a fresh database gets the full current shape" and "bootstrap is idempotent," not "v1 upgrades to v5."
  • Carry the current shape forward faithfully, including every column and index each migration added (embedding.origin and the rebuilt idx_emb_modified from v002; v003's partial expression indexes; v005's conflict/conflict_participant tables and their FK/cascade semantics). The schema produced must be byte-equivalent in effect to what the ladder produces today.
  • Re-evaluate backfill_fts_if_stale in light of its now-void justification. It may still be wanted for in-session index repair, but its "user DBs are never reset" rationale must be corrected or the code removed. Decide and document during implementation.
  • Update CLAUDE.md's DDL carve-out paragraph to point at the new bootstrap path and to state plainly that the project has no migration ladder.

Architecture Impact

No ADR mandates a versioned migration ladder. ADR-070 (../nodespace-docs/decisions/070-schema-relationship-declarations-are-edges.md) governs what the relationship table owns, not how the schema is created. This change is consistent with the pre-release stance in CLAUDE.md, and narrows a carve-out that has been read past its own wording.

If NodeSpace later ships to real users, a migration runner can be reintroduced at that point, against the schema as it exists then — which is a smaller and better-informed job than maintaining one now against a history nobody has.

Acceptance Criteria

  • packages/core/src/db/schema.rs creates the complete current schema in one batch
  • v001…v005 and migrations/mod.rs deleted
  • user_version ratchet deleted (LATEST_VERSION, run_up_to, apply_migration, migration_name, newer-than-supported guard)
  • Backup system deleted (backup_before_pending_migrations, db_has_data, prune_backups, MAX_BACKUPS)
  • SqliteStore::new and initialize_schema call create_schema; no reference to db::migrations remains anywhere
  • Resulting schema is equivalent to today's post-v005 shape — verified by a test that asserts the full table/column/index set, including embedding.origin, v003's partial indexes, and v005's conflict tables
  • Bootstrap is idempotent: opening an existing database created by this build is a no-op
  • backfill_fts_if_stale's "user DBs are never reset" rationale corrected or the code removed, with the decision documented
  • packages/core/tests/migrations_test.rs rewritten against the bootstrap
  • CLAUDE.md DDL carve-out paragraph updated
  • Tests pass (bun run test:all)
  • Code passes bun run quality:fix

Technical Specifications

Reference Files

  • packages/core/src/db/migrations/mod.rs — the 501-line runner, ratchet and backup system to delete
  • packages/core/src/db/migrations/v001_initial_schema.rs — base DDL; the starting point for the collapsed schema
  • packages/core/src/db/migrations/v002_embedding_origin.rs — embedding.origin + rebuilt idx_emb_modified; carries a compatibility workaround to drop
  • packages/core/src/db/migrations/v003_property_indexes.rs — partial expression indexes to fold in
  • packages/core/src/db/migrations/v004_schema_relationship_edges.rs — a data migration; its effect is already the current shape, so it contributes nothing to a fresh-database bootstrap and is deleted outright
  • packages/core/src/db/migrations/v005_conflict_journal.rs — conflict/conflict_participant tables to fold in
  • packages/core/src/db/sqlite_store/mod.rs — SqliteStore::new (~line 242), initialize_schema (~line 284), backfill_fts_if_stale, and the version-guard test (~line 533)
  • packages/core/tests/migrations_test.rs — to be rewritten
  • CLAUDE.md — the DDL-bootstrap carve-out paragraph

Dependencies

None. Independent of the relationship-reverse-name work, though landing this first would remove the need for a v006 migration there.

Related Issues

Non-Goals

  • Reintroducing a migration mechanism for a future shipped product — out of scope until there is one
  • Changing any table's actual shape, column set, or index set — this is a restructuring of how the schema is created, and the resulting schema must be equivalent
  • Revisiting the pre-release/no-backward-compatibility policy itself — this issue applies that policy, it does not reopen it

Activity

  1. malibio commented on Sep 17, 2026

    @malibio
    CollaboratorAuthor

    Baseline before implementation — Frontend: 247 test files passed, 5085 tests passed (87.70s).

  2. self-assigned this
    on Sep 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions