diff --git a/.github/workflows/test-template.yml b/.github/workflows/test-template.yml index 96a74a356cec..66e7a34caed9 100644 --- a/.github/workflows/test-template.yml +++ b/.github/workflows/test-template.yml @@ -597,6 +597,9 @@ jobs: - run: pnpm tsc -p tsconfig.utils.typecheck.json + - name: Test publish channel selection + run: pnpm tsx --test scripts/ci/publish-channel.test.ts + # # INTEGRATION-TESTS # diff --git a/AGENTS.md b/AGENTS.md index 370a46e18146..3285ef591ded 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -11,6 +11,7 @@ - **Key packages**: `packages/cli` (Prisma CLI entry point), `packages/migrate` (migrate/db namespace + fixtures), `packages/client` (client runtime), `packages/client-generator-ts` (new `prisma-client` generator), `packages/client-generator-js` (traditional `prisma-client-js` generator), `packages/client-generator-registry` (generator registry for managing generators), `packages/client-engine-runtime` (the core part of the new Rust binary free client based on Wasm query compiler, used by `ClientEngine` class in `packages/client`), `packages/client-common` (shared client utilities), `packages/client-runtime-utils` (utility types and singletons for Prisma Client), `packages/config` (`PrismaConfigInternal` + loader), `packages/internals` (shared CLI + engine glue), `packages/engines` (Rust binaries download wrapper), `packages/integration-tests` (matrix suites), `packages/query-plan-executor` (standalone query plan executor service for Prisma Accelerate), `packages/credentials-store` (credential storage utilities), sqlcommenter packages under `packages/sqlcommenter*`, and numerous driver adapters under `packages/adapter-*`. - **Driver adapters & runtimes**: `packages/bundled-js-drivers` plus the `adapter-*` packages ship JS driver adapters: `adapter-pg`, `adapter-neon`, `adapter-libsql`, `adapter-planetscale`, `adapter-d1`, `adapter-better-sqlite3`, `adapter-mssql`, `adapter-mariadb`, `adapter-ppg` (Prisma Postgres Serverless). These are built on helpers in `driver-adapter-utils`; migrate/client fixtures exercise them, so adapter changes typically require fixture/test updates. CI tests driver adapters with flavors: `js_pg`, `js_neon`, `js_libsql`, `js_planetscale`, `js_d1`, `js_better_sqlite3`, `js_mssql`, `js_mariadb`, `js_pg_cockroachdb`. - **Build & tooling**: Typescript-first repo with WASM/Rust assets (downloaded by `@prisma/engines`). Multiple `tsconfig.*` drive bundle vs runtime builds. Lint via `pnpm lint`, format via `pnpm format`. Maintenance scripts live in `scripts/` (e.g. `bump-engines.ts`, `bench.ts`, `ci/publish.ts` orchestrates build/test/publish flows). Build configuration uses esbuild via `helpers/compile/build.ts` with configs in `helpers/compile/configs.ts`. Most packages use `bundledConfig` which bundles to both CJS and ESM with type declarations. + - **npm dist-tags on this branch.** Two publishers share the npm package `prisma`: prisma/prisma-cli (Prisma 8) owns its `latest`, `dev` and `next` tags, and this branch owns `prev`, `prev-dev`, `patch-dev` and `integration`. Stable releases put `prisma` on `prev` and the `@prisma/*` packages on `latest`, so for `@prisma/*` the `prev-dev` tag means "Prisma 7 dev build", not "dev line of `prev`". Every push to `v7` publishes all packages as `-dev.N` under `prev-dev`. Tag and base-version selection live in `scripts/ci/publish-channel.ts`; `getPackageDistTag` there refuses to publish `prisma` under a tag prisma/prisma-cli owns. Its test runs in the `Workspace types` CI job (`pnpm tsx --test scripts/ci/publish-channel.test.ts`). Revisit these tag names when Prisma 9 ships and 8.x needs `prev`. - Build helpers that copy Wasm artifacts should resolve package assets through `require.resolve(...)` instead of hardcoding `packages/*/node_modules` paths. With pnpm hoisted installs, dependencies can live only at workspace root and package-local `node_modules/@prisma/*` entries may be absent. - **Benchmarking**: Comprehensive performance benchmarks using Benchmark.js with CodSpeed integration. See `docs/benchmarking.md` for full documentation. Key commands: - `pnpm bench` - Run all benchmarks (outputs to `output.txt`) diff --git a/scripts/ci/publish-channel.test.ts b/scripts/ci/publish-channel.test.ts new file mode 100644 index 000000000000..06097eff2713 --- /dev/null +++ b/scripts/ci/publish-channel.test.ts @@ -0,0 +1,118 @@ +/* eslint-disable @typescript-eslint/no-floating-promises -- node:test tracks the promises that describe() and test() return */ + +import assert from 'node:assert/strict' +import { describe, test } from 'node:test' + +import { + getNextMinorStable, + getPackageDistTag, + getPublishChannel, + parseCustomDistTag, + type PublishChannel, + type PublishChannelInput, +} from './publish-channel' + +const push: PublishChannelInput = { + branch: 'v7', + patchBranch: null, + releaseVersion: undefined, + forceIntegrationRelease: false, +} + +describe('getPublishChannel', () => { + const cases: [string, PublishChannelInput, PublishChannel][] = [ + ['push to v7', push, { kind: 'dev', tag: 'prev-dev' }], + [ + '--release on v7', + { ...push, releaseVersion: '7.11.0' }, + { kind: 'release', tag: 'latest', ecosystemTestsBranch: 'dev' }, + ], + [ + 'push to a patch branch', + { ...push, branch: '7.9.x', patchBranch: '7.9.x' }, + { kind: 'patch-dev', tag: 'patch-dev' }, + ], + [ + '--release on a patch branch', + { ...push, branch: '7.9.x', patchBranch: '7.9.x', releaseVersion: '7.9.2' }, + { kind: 'release', tag: 'latest', ecosystemTestsBranch: 'patch-dev' }, + ], + ['push to integration/*', { ...push, branch: 'integration/foo' }, { kind: 'integration', tag: 'integration' }], + [ + 'forced integration release wins over --release', + { ...push, forceIntegrationRelease: true, releaseVersion: '7.11.0' }, + { kind: 'integration', tag: 'integration' }, + ], + ] + + for (const [name, input, expected] of cases) { + test(name, () => { + assert.deepEqual(getPublishChannel(input), expected) + }) + } +}) + +describe('getPackageDistTag', () => { + const cases: [string, string, string][] = [ + ['prisma', 'prev-dev', 'prev-dev'], + ['@prisma/client', 'prev-dev', 'prev-dev'], + ['prisma', 'latest', 'prev'], + ['@prisma/client', 'latest', 'latest'], + ['prisma', 'patch-dev', 'patch-dev'], + ['prisma', 'integration', 'integration'], + ['prisma', 'my-custom-tag', 'my-custom-tag'], + ['@prisma/client', 'dev', 'dev'], + ] + + for (const [pkgName, tag, expected] of cases) { + test(`${pkgName} on ${tag} publishes under ${expected}`, () => { + assert.equal(getPackageDistTag(pkgName, tag), expected) + }) + } + + for (const tag of ['dev', 'next']) { + test(`refuses to publish prisma under ${tag}`, () => { + assert.throws(() => getPackageDistTag('prisma', tag), /prisma\/prisma-cli owns it.*"prev-dev"/) + }) + } +}) + +describe('parseCustomDistTag', () => { + test('removes the whitespace a shell would drop', () => { + assert.equal(parseCustomDistTag(' next'), 'next') + assert.equal(parseCustomDistTag('dev '), 'dev') + }) + + test('a padded prisma-cli tag is still refused for prisma', () => { + assert.throws(() => getPackageDistTag('prisma', parseCustomDistTag(' dev ')), /prisma\/prisma-cli owns it/) + }) + + for (const input of ['', 'my tag', 'Dev', 'dev;next', '--tag']) { + test(`refuses ${JSON.stringify(input)}`, () => { + assert.throws(() => parseCustomDistTag(input), /not a usable npm dist-tag/) + }) + } +}) + +describe('getNextMinorStable', () => { + test('returns the next minor after the highest stable 7.x version', () => { + assert.equal(getNextMinorStable(['7.9.0', '7.10.0', '7.10.1', '7.10.0-dev.58']), '7.11.0') + }) + + test('uses the highest version, not the newest one in the list', () => { + assert.equal(getNextMinorStable(['7.10.1', '7.9.2']), '7.11.0') + }) + + test('ignores other majors and prereleases', () => { + assert.equal( + getNextMinorStable(['6.19.3', '7.10.1', '7.11.0-dev.1', '7.12.0-integration-foo.1', '8.0.0', '8.0.0-rc.34']), + '7.11.0', + ) + }) + + test('throws when no stable 7.x version exists', () => { + for (const versions of [[], ['6.19.3', '8.0.0', '7.11.0-dev.1', '7.10']]) { + assert.throws(() => getNextMinorStable(versions), /no stable 7\.x\.y version of prisma/) + } + }) +}) diff --git a/scripts/ci/publish-channel.ts b/scripts/ci/publish-channel.ts new file mode 100644 index 000000000000..427eb17b5c75 --- /dev/null +++ b/scripts/ci/publish-channel.ts @@ -0,0 +1,91 @@ +/** + * Two publishers share the npm package `prisma`. prisma/prisma-cli (Prisma 8) owns its `latest`, `dev` and `next` + * tags. This branch (Prisma 7) owns `prev`, `prev-dev`, `patch-dev` and `integration`. + * + * The `@prisma/*` packages still get stable releases on `latest` from this branch, so for them `prev-dev` means + * "Prisma 7 dev build", not "dev line of `prev`". Revisit these names when Prisma 9 ships and 8.x needs `prev`. + */ +export const DEV_DIST_TAG = 'prev-dev' + +export const PRISMA_STABLE_DIST_TAG = 'prev' + +const PRISMA_MAJOR = 7 + +const PRISMA_CLI_OWNED_DIST_TAGS: ReadonlySet = new Set(['latest', 'dev', 'next']) + +export type PublishChannelInput = { + branch: string | undefined + patchBranch: string | null + releaseVersion: string | undefined + forceIntegrationRelease: boolean +} + +export type PublishChannel = { + kind: 'integration' | 'patch-dev' | 'release' | 'dev' + tag: string + ecosystemTestsBranch?: string +} + +export function getPublishChannel({ + branch, + patchBranch, + releaseVersion, + forceIntegrationRelease, +}: PublishChannelInput): PublishChannel { + if (branch && (forceIntegrationRelease || branch.startsWith('integration/'))) { + return { kind: 'integration', tag: 'integration' } + } + if (patchBranch) { + return releaseVersion + ? { kind: 'release', tag: 'latest', ecosystemTestsBranch: 'patch-dev' } + : { kind: 'patch-dev', tag: 'patch-dev' } + } + if (releaseVersion) { + return { kind: 'release', tag: 'latest', ecosystemTestsBranch: 'dev' } + } + return { kind: 'dev', tag: DEV_DIST_TAG } +} + +/** + * Returns the npm dist-tag to publish `pkgName` under. Every publish goes through this, including a custom dist tag. + * Throws if `prisma` would land on a tag prisma/prisma-cli owns. + */ +export function getPackageDistTag(pkgName: string, tag: string): string { + if (pkgName !== 'prisma') return tag + if (tag === 'latest') return PRISMA_STABLE_DIST_TAG + if (PRISMA_CLI_OWNED_DIST_TAGS.has(tag)) { + throw new Error( + `Refusing to publish prisma under the "${tag}" dist-tag: prisma/prisma-cli owns it for Prisma 8. Prisma 7 dev builds use "${DEV_DIST_TAG}".`, + ) + } + return tag +} + +/** + * Returns a custom dist-tag as npm will receive it. The tag reaches `pnpm publish --tag` through a shell, which drops + * surrounding whitespace, so ` dev` would pass a comparison with `dev` and still publish under it. + */ +export function parseCustomDistTag(input: string): string { + const tag = input.trim() + if (!/^[a-z][a-z0-9._-]*$/.test(tag)) { + throw new Error(`"${input}" is not a usable npm dist-tag: use lowercase letters, digits, ".", "_" and "-".`) + } + return tag +} + +/** + * Returns the next minor after the highest stable Prisma 7 version in `publishedVersions`, e.g. `7.10.1` -> `7.11.0`. + * Reads the version list rather than a dist-tag, because a release from an older patch branch moves `prev` back. + */ +export function getNextMinorStable(publishedVersions: readonly string[]): string { + const minors = publishedVersions.flatMap((version) => { + const match = /^(\d+)\.(\d+)\.\d+$/.exec(version.trim()) + return match && Number(match[1]) === PRISMA_MAJOR ? [Number(match[2])] : [] + }) + if (minors.length === 0) { + throw new Error( + `Found no stable ${PRISMA_MAJOR}.x.y version of prisma on npm. Dev and integration versions are based on the highest one.`, + ) + } + return `${PRISMA_MAJOR}.${Math.max(...minors) + 1}.0` +} diff --git a/scripts/ci/publish.ts b/scripts/ci/publish.ts index 583a24c0c258..17b725b718af 100644 --- a/scripts/ci/publish.ts +++ b/scripts/ci/publish.ts @@ -12,6 +12,8 @@ import { blue, bold, cyan, dim, magenta, red, underline } from 'kleur/colors' import pRetry from 'p-retry' import semver from 'semver' +import { getNextMinorStable, getPackageDistTag, getPublishChannel, parseCustomDistTag } from './publish-channel' + const onlyPackages = process.env.ONLY_PACKAGES ? process.env.ONLY_PACKAGES.split(',') : null const skipPackages = process.env.SKIP_PACKAGES ? process.env.SKIP_PACKAGES.split(',') : null @@ -217,12 +219,6 @@ export function getPublishOrder(packages: Packages): string[][] { return topo(dag) // TODO: this is now done by pnpm (top sort), can we remove? } -function zeroOutPatch(version: string): string { - const parts = version.split('.') - parts[parts.length - 1] = '0' - return parts.join('.') -} - /** * Takes the max dev version + 1 * For now supporting X.Y.Z-dev.# @@ -231,10 +227,7 @@ function zeroOutPatch(version: string): string { async function getNewDevVersion(packages: Packages): Promise { const before = Math.round(performance.now()) console.log('\nCalculating new dev version...') - // Why are we calling zeroOutPatch? - // Because here we're only interested in the 2.5.0 <- the next minor stable version - // If the current version would be 2.4.7, we would end up with 2.5.7 - const nextStable = zeroOutPatch((await getNextMinorStable())!) + const nextStable = await fetchNextMinorStable() console.log(`getNewDevVersion: Next minor stable: ${nextStable}`) @@ -254,10 +247,7 @@ async function getNewDevVersion(packages: Packages): Promise { async function getNewIntegrationVersion(packages: Packages, branch: string): Promise { const before = Math.round(performance.now()) console.log('\nCalculating new integration version...') - // Why are we calling zeroOutPatch? - // Because here we're only interested in the 2.5.0 <- the next minor stable version - // If the current version would be 2.4.7, we would end up with 2.5.7 - const nextStable = zeroOutPatch((await getNextMinorStable())!) + const nextStable = await fetchNextMinorStable() console.log(`getNewIntegrationVersion: Next minor stable: ${nextStable}`) @@ -401,11 +391,15 @@ function getMaxPatchVersionIncrement(versions: string[]): number { /** * @param pkgs - * @param channel + * @param prereleaseId * @param prefix - * @returns All versions published on npm for a given channel and prefix + * @returns All versions published on npm that contain `prereleaseId` and start with `prefix` */ -export async function getAllVersionsPublishedFor(pkgs: Packages, channel: string, prefix: string): Promise { +export async function getAllVersionsPublishedFor( + pkgs: Packages, + prereleaseId: string, + prefix: string, +): Promise { // We check the versions for the `@prisma/debug` package // Why? // Because `@prisma/debug` is the first package that will be published @@ -435,7 +429,11 @@ export async function getAllVersionsPublishedFor(pkgs: Packages, channel: string const remoteVersions: string = JSON.parse(remoteVersionsString) for (const remoteVersion of remoteVersions) { - if (remoteVersion.includes(channel) && remoteVersion.startsWith(prefix) && !pkgVersions.includes(remoteVersion)) { + if ( + remoteVersion.includes(prereleaseId) && + remoteVersion.startsWith(prefix) && + !pkgVersions.includes(remoteVersion) + ) { pkgVersions.push(remoteVersion) } } @@ -447,18 +445,13 @@ export async function getAllVersionsPublishedFor(pkgs: Packages, channel: string } /** - * Only used when publishing to the `dev` and `integration` npm channels - * (see `getNewDevVersion()` and `getNewIntegrationVersion()`) - * @returns The next minor version for the `latest` channel - * Example: If latest is `4.9.0` it will return `4.10.0` + * Only used for dev and integration versions (see `getNewDevVersion()` and `getNewIntegrationVersion()`) + * @returns The next minor version after the highest stable Prisma 7 release, e.g. `7.11.0` when it is `7.10.1` */ -async function getNextMinorStable() { - // We check the Prisma CLI `latest` version - // We retry a few times if it fails - // npm can have some hiccups - const remoteVersionString = await pRetry( +async function fetchNextMinorStable(): Promise { + const remoteVersionsString = await pRetry( async () => { - return await runResult('.', `npm info prisma version`) + return await runResult('.', `npm info prisma versions --json`) }, { retries: 6, @@ -467,7 +460,7 @@ async function getNextMinorStable() { }, }, ) - return increaseMinor(remoteVersionString) + return getNextMinorStable(JSON.parse(remoteVersionsString) as string[]) } // TODO: could probably use the semver package @@ -556,10 +549,6 @@ async function publish() { throw new Error(`Oops, there are circular dependencies: ${circles}`) } - let prismaVersion: undefined | string - let tag: undefined | string - let tagForEcosystemTestsCheck: undefined | string - const patchBranch = getPatchBranch() console.log({ patchBranch }) @@ -567,46 +556,50 @@ async function publish() { const branch = await getPrismaBranch() console.log({ branch }) - // For branches that are named "integration/" we publish to the integration npm tag - if (branch && (process.env.FORCE_INTEGRATION_RELEASE === 'true' || branch.startsWith('integration/'))) { - prismaVersion = await getNewIntegrationVersion(packages, branch) - tag = 'integration' - } - // Is it a patch branch? (Like 2.20.x) - else if (patchBranch) { - prismaVersion = await getNewPatchDevVersion(packages, patchBranch) - tag = 'patch-dev' - if (args['--release']) { - tagForEcosystemTestsCheck = 'patch-dev' //? - prismaVersion = args['--release'] - tag = 'latest' - } - } else if (args['--release']) { - // TODO:Where each patch branch goes - prismaVersion = args['--release'] - tag = 'latest' - tagForEcosystemTestsCheck = 'dev' - } else { - prismaVersion = await getNewDevVersion(packages) - tag = 'dev' + const channel = getPublishChannel({ + branch, + patchBranch, + releaseVersion: args['--release'], + forceIntegrationRelease: process.env.FORCE_INTEGRATION_RELEASE === 'true', + }) + const { ecosystemTestsBranch } = channel + let tag = channel.tag + + let prismaVersion: string + switch (channel.kind) { + case 'integration': + prismaVersion = await getNewIntegrationVersion(packages, branch!) + break + case 'patch-dev': + prismaVersion = await getNewPatchDevVersion(packages, patchBranch!) + break + case 'release': + prismaVersion = args['--release']! + break + case 'dev': + prismaVersion = await getNewDevVersion(packages) + break } if (args['--custom-dist-tag']) { - console.log(`Using custom dist tag: ${args['--custom-dist-tag']}`) - tag = args['--custom-dist-tag'] + tag = parseCustomDistTag(args['--custom-dist-tag']) + console.log(`Using custom dist tag: ${tag}`) } + const publishesPrisma = (args['--publish'] || dryRun) && !isSkipped('prisma') + const prismaTag = publishesPrisma ? getPackageDistTag('prisma', tag) : undefined console.log({ patchBranch, tag, - tagForEcosystemTestsCheck, + prismaTag, + ecosystemTestsBranch, prismaVersion, }) if (typeof process.env.GITHUB_OUTPUT == 'string' && process.env.GITHUB_OUTPUT.length > 0) { fs.appendFileSync(process.env.GITHUB_OUTPUT, `patchBranch=${patchBranch}\n`) fs.appendFileSync(process.env.GITHUB_OUTPUT, `tag=${tag}\n`) - fs.appendFileSync(process.env.GITHUB_OUTPUT, `tagForEcosystemTestsCheck=${tagForEcosystemTestsCheck}\n`) + fs.appendFileSync(process.env.GITHUB_OUTPUT, `tagForEcosystemTestsCheck=${ecosystemTestsBranch}\n`) fs.appendFileSync(process.env.GITHUB_OUTPUT, `prismaVersion=${prismaVersion}\n`) } @@ -621,13 +614,13 @@ async function publish() { if (args['--publish'] || dryRun) { if (args['--release']) { - if (!tagForEcosystemTestsCheck) { - throw new Error(`tagForEcosystemTestsCheck missing`) + if (!ecosystemTestsBranch) { + throw new Error(`ecosystemTestsBranch missing`) } - const passing = await areEcosystemTestsPassing(tagForEcosystemTestsCheck) + const passing = await areEcosystemTestsPassing(ecosystemTestsBranch) if (!passing && process.env.SKIP_ECOSYSTEMTESTS_CHECK !== 'true') { - throw new Error(`We can't release, as the ecosystem-tests are not passing for the ${tag} npm tag! -Check them out at https://github.com/prisma/ecosystem-tests/actions?query=workflow%3Atest+branch%3A${tag}`) + throw new Error(`We can't release, as the ecosystem-tests are not passing on the ${ecosystemTestsBranch} branch! +Check them out at https://github.com/prisma/ecosystem-tests/actions?query=workflow%3Atest+branch%3A${ecosystemTestsBranch}`) } } @@ -708,15 +701,6 @@ function intersection(arr1: T[], arr2: T[]): T[] { const semverRegex = /^(?0|[1-9]\d*)\.(?0|[1-9]\d*)\.(?0|[1-9]\d*)(?:-(?(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+(?[0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?$/ -function increaseMinor(version: string) { - const match = semverRegex.exec(version) - if (match?.groups) { - return `${match.groups.major}.${Number(match.groups.minor) + 1}.${match.groups.patch}` - } - - return undefined -} - function filterPublishOrder(publishOrder: string[][], packages: string[]): string[][] { return publishOrder.reduce((acc, curr) => { if (Array.isArray(curr)) { @@ -811,7 +795,7 @@ async function publishPackages( const pkgDir = path.dirname(pkg.path) const newVersion = prismaVersion - const packageTag = pkgName === 'prisma' && tag === 'latest' ? 'prev' : tag + const packageTag = isSkipped(pkgName) ? tag : getPackageDistTag(pkgName, tag) console.log(`\nPublishing ${magenta(`${pkgName}@${newVersion}`)} ${dim(`on ${packageTag}`)}`) @@ -918,11 +902,11 @@ async function getPrismaBranch(): Promise { return undefined } -async function areEcosystemTestsPassing(tag: string): Promise { +async function areEcosystemTestsPassing(branch: string): Promise { let svgUrl = 'https://github.com/prisma/ecosystem-tests/workflows/test/badge.svg?branch=' - if (tag === 'patch-dev') { - svgUrl += tag + if (branch === 'patch-dev') { + svgUrl += branch } else { svgUrl += 'dev' }