Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/backend/entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
set -e

echo "🔄 Running database migrations..."
npx @ariga/atlas migrate apply --dir file://drizzle/migrations --url "$DATABASE_URL" 2>&1 || echo "⚠️ Migrations skipped or failed (non-fatal)"
npx @stokaro/ptah-compat migrate apply --dir file://drizzle/migrations --url "$DATABASE_URL" 2>&1 || echo "⚠️ Migrations skipped or failed (non-fatal)"

echo "🚀 Starting Rebase backend..."
exec node dist/src/index.js
2 changes: 1 addition & 1 deletion app/backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@
"start:dev": "tsx src/index.ts"
},
"dependencies": {
"@ariga/atlas": "^1.2.3",
"@aws-sdk/client-s3": "^3.1096.0",
"@hono/node-server": "^2.0.12",
"@rebasepro/server": "workspace:*",
"@rebasepro/server-postgres": "workspace:*",
"@stokaro/ptah-compat": "^0.30.0",
"dotenv": "^17.4.2",
"drizzle-orm": "^0.45.2",
"hono": "^4.13.5",
Expand Down
11 changes: 0 additions & 11 deletions infra/docker/server.Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -56,17 +56,6 @@ COPY packages/cms-types/package.json packages/cms-types/
COPY packages/forms/package.json packages/forms/
COPY packages/inference/package.json packages/inference/

# The runtime never runs atlas — it is imported only by server-postgres's CLI
# (migration authoring), never on the boot path, and the image only supports
# REBASE_MIGRATE_ON_BOOT=ensure|none, neither of which shells out to it. Its
# install script downloads a binary from a third-party host (atlasbinaries.com),
# so building it here adds a network dependency and a supply-chain surface for a
# tool the runtime cannot use. Mark it explicitly not-built so pnpm skips the
# download AND does not fail asking for a decision (pnpm 11 errors on an
# undecided ignored build). The package still installs, it just has no binary —
# exactly right, because nothing in the runtime invokes one.
RUN sed -i "s#'@ariga/atlas': true#'@ariga/atlas': false#" pnpm-workspace.yaml

# `@rebasepro/cms...` is not a dependency of anything the server declares: the
# views build reaches the admin through source aliases, so its packages' own
# dependencies (react-router, prosemirror, …) have to be installed by name.
Expand Down
14 changes: 6 additions & 8 deletions packages/cli/src/commands/db.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,19 +123,17 @@ describe("absolutizeLocalPathArgs", () => {

describe("atlasDevDatabaseFor", () => {
/**
* PGlite serves one database; Atlas needs a second empty one to diff
* against. `CREATE DATABASE` against PGlite reports success and creates
* nothing, so push, generate and migrate were refused on the managed
* database — and with them every way to drop, rename or narrow a column
* there. A throwaway second PGlite now stands in for the scratch database.
* PGlite serves one database; Ptah needs a second empty one to plan push
* and generate. `migrate apply` reads only the target and its migration
* directory, so it does not need a scratch database.
*/
const fake = () => {
const stop = vi.fn(async () => undefined);
const start = vi.fn(async () => ({ url: "postgresql://postgres@127.0.0.1:61000/postgres?sslmode=disable", stop }));
return { start, stop };
};

it.each(["push", "generate", "migrate"])("starts one for db %s on the managed database", async (sub) => {
it.each(["push", "generate"])("starts one for db %s on the managed database", async (sub) => {
const { start } = fake();
const dev = await atlasDevDatabaseFor(["node", "rebase", "db", sub], "managed", {}, start);
expect(start).toHaveBeenCalledTimes(1);
Expand All @@ -150,9 +148,9 @@ describe("atlasDevDatabaseFor", () => {
expect(start).not.toHaveBeenCalled();
});

it("starts none for subcommands that do not use Atlas", async () => {
it("starts none for subcommands that do not need a planning database", async () => {
const { start } = fake();
for (const sub of ["backup", "restore", "backups", "branch", "pull"]) {
for (const sub of ["migrate", "backup", "restore", "backups", "branch", "pull"]) {
expect(await atlasDevDatabaseFor(["node", "rebase", "db", sub], "managed", {}, start)).toBeUndefined();
}
expect(start).not.toHaveBeenCalled();
Expand Down
29 changes: 17 additions & 12 deletions packages/cli/src/commands/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -255,15 +255,15 @@ const OWN_SUBCOMMAND_PARSERS: Readonly<Record<string, ((rawArgs: readonly string
* push `rebase dev` performs during start-up reaches the managed database too.
*/
/**
* The `db` subcommands that plan their work with Atlas.
* The `db` subcommands that plan their work with Ptah.
*
* Atlas computes a diff by replaying the desired schema into a **second, empty
* database** and comparing. That is why these three are listed and `backup` and
* `restore` are not — those shell out to `pg_dump`, which fails loudly on its
* own. `branch` is refused too, but for its own reason and with its own
* message: see {@link refuseBranchOnManagedDatabase}.
* Ptah computes a diff by replaying the desired schema into a **second, empty
* database** and comparing. Push and generate use that database. Migration
* application does not. Backup and restore use `pg_dump`, which fails loudly
* on its own. Branch has its own guard; see
* {@link refuseBranchOnManagedDatabase}.
*/
const ATLAS_BACKED_SUBCOMMANDS = new Set(["push", "generate", "migrate"]);
const ATLAS_BACKED_SUBCOMMANDS = new Set(["push", "generate"]);

/**
* A managed-database refusal, raised so the caller decides what it costs.
Expand Down Expand Up @@ -352,17 +352,17 @@ export async function atlasDevDatabaseFor(
try {
return await start();
} catch (err) {
const summary = `rebase db ${subcommand} could not start the database Atlas plans against.`;
const summary = `rebase db ${subcommand} could not start the database Ptah plans against.`;
throw new ManagedDatabaseRefusal(summary, [
"",
chalk.red(` ✗ ${summary}`),
chalk.gray(` ${err instanceof Error ? err.message : String(err)}`),
"",
chalk.gray(" Atlas plans every change against a second, empty database. On the managed"),
chalk.gray(" Ptah plans every change against a second, empty database. On the managed"),
chalk.gray(" database that is a throwaway PGlite, started for the command."),
"",
chalk.gray(` ${chalk.cyan("rebase dev")} still applies your tables at boot, additively. Otherwise,`),
chalk.gray(` set ${ATLAS_DEV_URL_ENV} to an empty Postgres database Atlas may fill and clear,`),
chalk.gray(` set ${ATLAS_DEV_URL_ENV} to an empty Postgres database Ptah may fill and clear,`),
chalk.gray(" or point the project at a real Postgres (uncomment DATABASE_URL in .env)."),
""
]);
Expand Down Expand Up @@ -648,7 +648,12 @@ export async function runDriverDbCommand(
// Started after the managed database, stopped when the driver exits —
// whatever it exits with.
const atlasDev = await atlasDevDatabaseFor(rawArgs, prepared.database.kind, env);
if (atlasDev) env[ATLAS_DEV_URL_ENV] = atlasDev.url;
if (atlasDev) {
env[ATLAS_DEV_URL_ENV] = atlasDev.url;
// This PGlite instance exists only for this command and may be reset.
env.PTAH_DEV_SERVER_DISPOSABLE = "1";
env.REBASE_PTAH_DEV_PGLITE = "1";
}
env[INVOCATION_CWD_ENV] = process.cwd();

// Resolved against the directory the developer is standing in, before the
Expand Down Expand Up @@ -1797,7 +1802,7 @@ const DB_ACTION_HELP: Record<string, { usage: string; summary: string; notes?: s
"merge --on-conflict <abort|branch|target> decides a row both sides changed: write nothing (abort, the default), take the branch's, or keep the parent's. --skip-blocked merges the tables that can be and skips the rest. --sample-limit <n> rows are compared per table.",
"delete <name> asks before it drops the branch's database; --yes answers for you.",
"prune [--older-than <14|14d|2w>] removes branches nothing is using, and the entries that drifted.",
"prune --include-dev-diff also removes the Atlas scratch databases `db push` leaves behind.",
"prune --include-dev-diff also removes the migration scratch databases `db push` leaves behind.",
"delete, merge and prune ask first; --yes, -y answers for you, which is the form a CI job wants.",
"--force disconnects other sessions first — Postgres refuses to copy or drop a database anything is connected to, usually your own `rebase dev`."
]
Expand Down
65 changes: 26 additions & 39 deletions packages/cli/src/commands/doctor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,15 +216,23 @@ export function collectEnvironmentFindings(
}

/**
* What is actually on disk for `@ariga/atlas`, from the project's point of view.
* What is actually on disk for `@stokaro/ptah-compat`, from the project's point of view.
*
* Three separate questions, because the three states they distinguish need
* three different remedies — see `checkAtlasBinary`. Directories are searched
* The package and native executable need separate remedies. Directories are searched
* from the project root upwards and through the workspaces a scaffold has,
* because `db push` runs from `backend/` and the install may be hoisted to the
* root.
*/
export function readAtlasBinaryState(projectRoot: string): AtlasBinaryState | null {
export function readAtlasBinaryState(
projectRoot: string,
resolveFrom: (from: string, request: string) => string | null = (from, request) => {
try {
return createRequire(pathToFileURL(from)).resolve(request);
} catch {
return null;
}
}
): AtlasBinaryState | null {
const roots = [projectRoot, path.join(projectRoot, "backend"), path.join(projectRoot, "config")];

const findUpwards = (starts: string[], relative: string): string | null => {
Expand All @@ -241,72 +249,51 @@ export function readAtlasBinaryState(projectRoot: string): AtlasBinaryState | nu
return null;
};

// `@ariga/atlas` is a dependency of `@rebasepro/server-postgres`, not of the
// project, so under pnpm's isolated layout — the scaffold default — it lives
// in `.pnpm/` and a directory walk over `node_modules/` never sees it.
// Resolve through the module graph the way the driver itself does.
const resolveFrom = (from: string, request: string): string | null => {
try {
return createRequire(pathToFileURL(from)).resolve(request);
} catch {
return null;
}
};
// Under pnpm's isolated layout, a compatibility CLI installed beside the
// driver lives in `.pnpm/`. Resolve through the module graph as well as
// walking the project's `node_modules/`.
const bases = roots.map(dir => path.join(dir, "package.json")).filter(file => fs.existsSync(file));
const driverManifests = bases
.map(base => resolveFrom(base, "@rebasepro/server-postgres/package.json"))
.filter((file): file is string => file !== null);

let manifest: string | null = findUpwards(roots, path.join("@ariga", "atlas", "package.json"));
let manifest: string | null = findUpwards(roots, path.join("@stokaro", "ptah-compat", "package.json"));
if (!manifest) {
for (const base of [...bases, ...driverManifests]) {
manifest = resolveFrom(base, "@ariga/atlas/package.json");
manifest = resolveFrom(base, "@stokaro/ptah-compat/package.json");
if (manifest) break;
}
}

// The `.bin` shim is written beside whichever package declared the
// dependency, so the driver's own directory is a search root too.
// On Windows the npm package cannot install at all, so the binary that
// counts is Atlas's own `atlas.exe` on PATH — the same one the driver
// finds through `where atlas`.
const onPath = findUpwards(
[...roots, ...driverManifests.map(file => path.dirname(file))],
path.join(".bin", "atlas")
) !== null || (process.platform === "win32" && windowsPathHas("atlas.exe"));

let binaryOnDisk = false;
if (manifest) {
try {
const pkg = JSON.parse(fs.readFileSync(manifest, "utf-8")) as {
const realManifest = fs.realpathSync(manifest);
const pkg = JSON.parse(fs.readFileSync(realManifest, "utf-8")) as {
bin?: string | Record<string, string>;
};
const dir = path.dirname(manifest);
const dir = path.dirname(realManifest);
const targets = typeof pkg.bin === "string"
? [pkg.bin]
: Object.values(pkg.bin ?? {}).filter((t): t is string => typeof t === "string");
binaryOnDisk = targets.some(target => fs.existsSync(path.resolve(dir, target)));
const ptah = resolveFrom(realManifest, "@stokaro/ptah/package.json");
const native = ptah && resolveFrom(ptah, `@stokaro/ptah-${process.platform}-${process.arch}/package.json`);
const executable = native && path.join(path.dirname(native), "bin", `ptah-compat${process.platform === "win32" ? ".exe" : ""}`);
binaryOnDisk = targets.some(target => fs.existsSync(path.resolve(dir, target)))
&& !!executable && fs.existsSync(executable);
} catch { /* unreadable manifest: treat the binary as absent */ }
}

return {
onPath,
packageInstalled: manifest !== null,
binaryOnDisk,
manager: readPackageManagerName(projectRoot),
workspaceDir: fs.existsSync(path.join(projectRoot, "backend", "package.json")) && isWorkspaceRoot(projectRoot)
? "backend"
: null,
platform: process.platform
: null
};
}

/** Whether a directory on `PATH` holds `file`. `Path` is how Windows spells it. */
function windowsPathHas(file: string): boolean {
const value = process.env.PATH ?? process.env.Path ?? "";
return value.split(path.delimiter).some(dir => dir !== "" && fs.existsSync(path.join(dir, file)));
}

/** Whether `dir` is a workspace root, where `pnpm add` refuses without `-w`. */
function isWorkspaceRoot(dir: string): boolean {
if (fs.existsSync(path.join(dir, "pnpm-workspace.yaml"))) return true;
Expand Down
58 changes: 18 additions & 40 deletions packages/cli/src/commands/init.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1545,11 +1545,11 @@ runtime: "managed" });

/**
* `rebase db push`, `db generate` and `db migrate` run the driver with
* `cwd: backend/` and need the `atlas` binary from that workspace's
* `cwd: backend/` and need the `atlas` executable from that workspace's
* `node_modules/.bin`. The overlay ships `"db:migrate": "rebase db migrate"`,
* and `init --headless --introspect` ends on "You can now run `rebase db
* generate`", yet its backend did not install Atlas: both failed on "The
* atlas binary is missing", and the printed fix failed at the project root.
* and `init --headless --introspect` points users to `rebase db generate`.
* The backend must include `@stokaro/ptah-compat` so database commands can
* find the `atlas` executable.
*/
it.each([
["template", TEMPLATE_DIR],
Expand All @@ -1561,31 +1561,10 @@ runtime: "managed" });

const atlasScripts = Object.values<string>(pkg.scripts ?? {}).filter(script => /\brebase db (push|generate|migrate)\b/.test(script));
expect(atlasScripts.length).toBeGreaterThan(0);
// The same range the CMS backend ships, so `rebase upgrade` and the
// template gates see one Atlas, not two.
expect(backend.optionalDependencies?.["@ariga/atlas"]).toBe(shipped.optionalDependencies["@ariga/atlas"]);
expect(pkg.pnpm?.onlyBuiltDependencies).toContain("@ariga/atlas");
});

/**
* `@ariga/atlas` ships Linux and macOS builds only: on Windows its
* `preinstall` exits 1 with "Unsupported platform". As a plain dependency
* that failed the whole `pnpm install`, so a scaffold could not be installed
* on Windows at all — although only `db push`, `db generate` and `db
* migrate` ever run Atlas. An optional dependency whose install script fails
* is skipped, by pnpm and npm alike, and the install carries on.
*/
it.each([
["template", TEMPLATE_DIR],
["baas overlay", overlay]
])("%s: Atlas is optional, so a Windows install does not fail on it", (_name, root) => {
const backend = JSON.parse(fs.readFileSync(path.join(root, "backend", "package.json"), "utf8"));
const rootPkg = JSON.parse(fs.readFileSync(path.join(root, "package.json"), "utf8"));
expect(backend.optionalDependencies?.["@ariga/atlas"]).toBeDefined();
for (const manifest of [backend, rootPkg]) {
expect(manifest.dependencies?.["@ariga/atlas"]).toBeUndefined();
expect(manifest.devDependencies?.["@ariga/atlas"]).toBeUndefined();
}
// Keep the same range as the shipped backend so upgrade and template
// installs use one Ptah version.
expect(backend.dependencies?.["@stokaro/ptah-compat"]).toBe(shipped.dependencies["@stokaro/ptah-compat"]);
expect(backend.dependencies?.["@ariga/atlas"]).toBeUndefined();
});

/**
Expand Down Expand Up @@ -1839,31 +1818,30 @@ describe("choosing a free port for the local database", () => {
});

/**
* The scaffold has to tell *both* supported package managers that
* `@ariga/atlas` may run its install script, in each one's own dialect.
* Atlas needed *both* supported package managers to allow its install script
* in each one's own dialect.
*
* pnpm 10 and npm 12 both refuse a dependency's lifecycle scripts unless the
* project allowlists them, and `@ariga/atlas` downloads its binary in
* `preinstall`. The scaffold has carried pnpm's key for a long time; npm's
* (`allowScripts`) was missing, so on npm the install exited 0 with a warning,
* `preinstall`. The scaffold carried pnpm's key for a long time. It omitted
* npm's `allowScripts`, so on npm the install exited 0 with a warning,
* `node_modules/.bin/atlas` never appeared, and `db:push` — the command the
* quickstart points at for your own Postgres — failed on a binary nobody had
* been told was skipped.
*
* Asserted as a pair rather than as two separate facts, because the failure is
* silent on whichever manager gets forgotten and is only ever noticed by
* somebody who already has a broken project.
* Ptah ships platform binaries without install scripts. The paired checks
* prevent either manager from retaining an Atlas allowance or adding a Ptah one.
*/
describe("the template's install-script allowlists", () => {
const templates: Array<[string, string]> = [
["template", path.join(findCliRoot(), "templates", "template", "package.json")],
["baas overlay", path.join(findCliRoot(), "templates", "overlays", "baas", "package.json")]
];

it.each(templates)("%s allows @ariga/atlas under both pnpm and npm", (_name, pkgPath) => {
it.each(templates)("%s needs no migration CLI install-script allowance under pnpm", (_name, pkgPath) => {
const pkg = JSON.parse(fs.readFileSync(pkgPath, "utf8"));
expect(pkg.pnpm?.onlyBuiltDependencies).toContain("@ariga/atlas");
expect(pkg.allowScripts?.["@ariga/atlas"]).toBe(true);
expect(pkg.pnpm?.onlyBuiltDependencies).not.toContain("@ariga/atlas");
expect(pkg.pnpm?.onlyBuiltDependencies).not.toContain("@stokaro/ptah-compat");
});

it.each(templates)("%s grants npm no more than it needs", (_name, pkgPath) => {
Expand All @@ -1875,7 +1853,7 @@ describe("the template's install-script allowlists", () => {
// needed is a grant with no benefit, so this fails if one is added here
// without a reason.
const pkg = JSON.parse(fs.readFileSync(pkgPath, "utf8"));
expect(Object.keys(pkg.allowScripts ?? {})).toEqual(["@ariga/atlas"]);
expect(Object.keys(pkg.allowScripts ?? {})).toEqual([]);
});
});

Expand Down
Loading