Skip to content

Create and alter collections against a running backend #5

Open

Description

@fgatti675

Guiding rule for this phase: every capability ships as an open-source interface with a working zero-configuration default. Hosted infrastructure is a different implementation behind that same interface — never the only implementation, and never a different API.

The schema editor writes TypeScript into collectionsDir, and is disabled under NODE_ENV=production for a good reason: a deployed server's files are rebuilt from the repository on every deploy, so an edit made there would be discarded (packages/server/src/init.ts:1495).

That means creating or altering a collection against a running backend is not possible today. This is the phase that makes it possible — and it belongs in @rebasepro/server, so that one implementation serves an admin panel wherever it runs.

Work

  • A schema service in @rebasepro/server that takes a collection diff, generates a migration, applies it in a transaction, and re-derives row-level security through the existing hashed-name contract
  • Decide where a collection definition lives in production — the database, the bundle, or both. Recommendation: the database, with a mechanical export back to source so a repository-first workflow loses nothing
  • Preserve the existing contracts: policy names are hashed and injected, derived identifiers are frozen once shipped, and row-level security is migration-applied
  • Guard against the known traps — the collection-key allowlist silently dropping unrecognised keys, and a stale generated schema
  • One code path, used by every admin surface

Guards to add alongside it

CI already proves that the backend never reaches React, using scripts/headless-guard/check.mjs and contracts/derived-names.txt. Two checks in the same shape keep this phase honest:

  • Every published API has an open-source implementation. Everything in api-surface/server.api.txt must be reachable from a plain boot with no hosted infrastructure
  • Configuration parity. Hosted deployments configure the runtime through the same environment-derived names any deployment uses

Activity

  1. added
    enhancement
    roadmap
    on Aug 21, 2026
  2. fgatti675 commented on Aug 22, 2026

    @fgatti675
    MemberAuthor

    Design written and up for review in #14 — docs/live-schema-editing-plan-2026-08.md. No code yet, deliberately.

    What it decides

    Making the database authoritative was the obvious answer and it's wrong. serializeCollections already defines a collection's serializable subset, and withoutAdminBlock drops the admin block wholesale while the walk drops every function — callbacks, dynamicProps, conditions. What survives is slug, table, properties, relations, securityRules. A panel that can only edit that is a SQL editor with row-level security.

    So the repository stays the source of truth and the panel writes to it: commit first, then apply the DDL live. Immediate feedback, one source of truth, and no ceiling on what the panel can eventually edit.

    Two things the doc pins down

    The order is load-bearing. Apply-then-commit leaves a column in the database the repo doesn't describe — and the ensure path is strictly additive, so the next deploy neither removes it nor mentions it. Commit-then-apply fails into "repo ahead of database", the ordinary state between an edit and a deploy, which boot already reconciles.

    The commit is the work. Not the collection file — also backend/src/schema.generated.ts, a migration with a correctly minted when (Drizzle skips out-of-order ones), and the regenerated policies.sql / schema.sql / search.sql. Without all of it, a deploy against a fresh database produces something other than what was clicked.

    What blocks implementation

    A project's gitRepoUrl is metadata only. Deploys work by the CLI packaging a source tarball and uploading it, so nothing in the pipeline can write to that repository. A credential path — GitHub App or per-project token, stored in a SecretStore, with scope and rotation — has to exist before any of this can be enabled in production. That gates the ship date more than the DDL work does.

    Where to start

    Stages 1 and 2 of the plan — classify a change as safe/unsafe, and generate the complete commit as a set of file contents — have no dependency on the credential work and carry most of the risk. Both are pure logic and fully testable: assert that a fresh database built from the generated commit matches the schema the change describes.

    Needs an answer before stage 3 means anything

    • Which branch does a deployment track, and where is that recorded? The project model has the repo URL and not the branch.
    • Does a schema change made against production also need to land in whatever branch staging tracks, or do environments diverge deliberately?
    • GitHub App or per-project token?
    • Should PR-only be the default for every project rather than a fallback for protected branches?
  3. fgatti675 commented on Aug 23, 2026

    @fgatti675
    MemberAuthor

    Shipped in #21 (merged) and #23 (open).

    Working, verified against a real Postgres and a real repository, and driven in a browser end to end:

    • GET/POST /api/admin/schema/{status,plan,apply} — plan has no side effects, apply commits then applies
    • Three verdicts, with withheldConstraints for a change that applies and still leaves something unenforced
    • The collection editor plans a change, shows the SQL, the files and the commit message, and does nothing until somebody confirms
    • Applying is a second privilege from being an admin — a commit needs an author, so machines plan and people apply

    Fixed along the way, each of which made the feature or the boot path report something untrue:

    • /apply could never succeed: the dirty-tree guard ran after the write it was guarding and refused every change on its own edit
    • An added enum value never reached the database; a required property on an empty table arrived nullable; relaxing required left the constraint — all three read as success
    • additiveStatements planned against an empty database, emitting ADD COLUMN … NOT NULL that a live table would reject
    • Escape mid-apply reported "cancelled" while the commit and DDL carried on
    • A nested project committed its generated files to the repository root, leaving the real ones stale

    Not done, and the reason:

    Rebase Cloud is not wired. createGitHubRepository — the Git Data API backend — is written and tested, including against a real repository, but nothing constructs it, so a Cloud tenant gets the same SCHEMA_EDITING_NO_REPOSITORY refusal as any bundle. The docs say this.

    It is not a config gap. The AST editor rewrites the collection file on disk, and a bundle deployment has none, so the cloud path needs fetch-from-GitHub → edit → commit-back. That is the remaining design work and it is worth its own issue rather than being hidden inside this one.

    Leaving this open for that reason.

  4. fgatti675 commented on Aug 23, 2026

    @fgatti675
    MemberAuthor

    Cloud is wired. Correcting my earlier comment, which said it was design work left to do — it was, and it is done.

    The blocker was never authentication or the Git Data API; both worked and were tested. It was that the AST editor rewrites a collection file on disk, and a bundle deployment ships compiled output, so there was nothing for it to open.

    rewriteRemoteCollection reads the source from the repository, puts it in a scratch directory shaped the way the editor expects, runs the unchanged editor, and reads the result back. Reusing it rather than reimplementing matters: it is the part that preserves a collection file's imports, comments and formatting.

    Configured as liveSchema.repository:

    liveSchema: {
        repository: {
            kind: "github",
            owner: "acme", repo: "storefront", branch: "main",
            collectionsPath: "config/collections",
            auth: { kind: "token", token: process.env.GITHUB_TOKEN! }
            // or { kind: "app", appId, privateKey, installationId }
        }
    }

    Token for a single project committing to a repository its owner already has. App for a control plane holding one key across many projects — one App, an installation per project, no per-customer secret to rotate.

    Verified against a real repository with no local source: creates a collection that did not exist, rewrites one that did, and the rewritten file still carries the comment that was above it.

    Four things that decide whether it is safe, each covered by a test:

    • the scratch directory holds one file and is removed on every path, including the failing one — it is a copy of customer source
    • a repository that cannot read refuses rather than returning nothing; treating that as "empty file" would have the editor recreate the collection from the payload and the commit delete the rest of it
    • an absent file is a normal answer, not an error — a new collection has no source yet
    • a configured repository wins over a working tree that happens to sit beside the process

    The deployment table in the docs now reads: rebase dev yes, self-host with the project mounted yes, self-host from a bundle yes with liveSchema.repository, Rebase Cloud yes with the same.

  5. fgatti675 commented on Aug 25, 2026

    @fgatti675
    MemberAuthor

    Bookkeeping pass — four of the five items are done and the checkboxes now say so. Nothing new was built here tonight; this is the audit.

    Done, across #21, #23 and #24: the schema service, the decision about where a collection definition lives in production (the repository, via liveSchema.repository, with the AST editor reading source from GitHub and committing back), the existing contracts preserved, and one code path used by every admin surface.

    "Guard against the known traps" is left unticked, but it is much closer to done than the item reads. Both named traps already have a guard; each has a gap beside it:

    • The collection-key allowlist is guarded, at compile time. COLLECTION_KEY_LIST in packages/server/src/collections/validate-config.ts is an as const tuple, and AssertNever<MissingCollectionKeys> fails the build if a key on any engine's config type is missing from it — so adding a key and forgetting the list is a compile error rather than a block that silently does nothing in someone's project. The gap is the sibling list: PROPERTY_KEYS_BY_TYPE, in the same file, is hand-maintained with no equivalent assertion. A new property option is still dropped in silence, which is the same failure one level down.
    • Stale generated schema has a targeted guard: rebase schema stale, which rebase dev runs with --fix. It is deliberately narrow — it answers "does this name a foreign key the way the previous rule did", not "is this what we would generate now", because the wide question reports every generator whitespace change as fatal and a check that cries wolf gets switched off. The gap is that nothing runs it in CI on this repository's own app/backend/src/schema.generated.ts, which is the file whose staleness broke every deploy once already.

    So: two small, well-shaped additions rather than two absent guards.

    The two CI guards at the bottom of this issue are still open, and I want to flag a risk in one rather than write it badly. "Every published API has an open-source implementation" is the right property, but the cheap implementation — grepping packages/server/src for hosted-only hostnames and control-plane variable names — needs an allowlist on day one (EMAIL_LOGO_URL alone points at a hosted asset), and an allowlisted grep is a gate that passes because it was taught to. A real version compares what a plain boot mounts against what the published surface promises. I would rather leave it open than land a check that reports green for the wrong reason.

  6. fgatti675 commented on Aug 25, 2026

    @fgatti675
    MemberAuthor

    Both gaps from the comment above are closed, so "guard against the known traps" is now done — and each one turned out to be hiding something.

    The property-key allowlist has the assertion its collection-level sibling had. AssertNever over every key of every member of the Property union, with ADMIN_PROPERTY_KEYS excluded rather than listed — listing those here is its own known bug, because this allowlist is consulted first, so a listed key is accepted and the migration hint telling the author to move it into admin is never reached. PROPERTY_KEYS_BY_TYPE becomes as const satisfies Record<Property["type"], readonly string[]> for it, which makes the runtime lookup narrow too.

    Verified by mutation in both directions: dropping "dimensions" from the vector list, and adding a new optional key to VectorProperty, each fail the build naming the key.

    CI now checks this repository's own generated schema (pnpm check:schema-fresh, wired into verify.yml beside the derived-names step, reading source so a stale dist cannot make it pass).

    Two things worth recording from writing it:

    • The narrow check alone would have been a green light on an unchecked file. No relation in this repository has a name the 0.12 and 0.13 rules disagree about, so the rename check has nothing to catch and passes vacuously. findMissingGeneratedNames is what gives the gate content: does the module declare every table and every derived foreign-key column the collections name? That is the ordinary way a generated schema goes stale — a collection added, the generator not re-run — and it caught both mutations I threw at the real file.
    • It found a blind spot in the existing check. tableBlock matched only pgTable("x", and a collection with schema: "rebase" — the auth users collection, in every scaffold — is emitted as rebaseSchema.table("x", …). The block came back empty, an empty block reads as "a table the generator was not asked about", and so the rename check had never inspected a single table outside public. Both forms match now, which also widens the check rebase dev --fix runs on every developer's machine.

    Still open on this issue: the two CI guards at the bottom, with the caveat about the first one in the comment above.

    Landed on main locally and not pushed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementroadmap

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions