diff --git a/server/account/src/__tests__/migrations.test.ts b/server/account/src/__tests__/migrations.test.ts new file mode 100644 index 00000000000..4e5dcd6b97f --- /dev/null +++ b/server/account/src/__tests__/migrations.test.ts @@ -0,0 +1,42 @@ +// +// Copyright © 2026 Hardcore Engineering Inc. +// +// Licensed under the Eclipse Public License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. You may +// obtain a copy of the License at https://www.eclipse.org/legal/epl-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// +// See the License for the specific language governing permissions and +// limitations under the License. +// + +import { getMigrations } from '../collections/postgres/migrations' + +describe('account db migrations', () => { + const splitStatements = (ddl: string): string[] => + ddl + .split('\n') + .filter((line) => !line.trim().startsWith('--')) + .join('\n') + .split(';') + .map((s) => s.trim().replace(/\s+/g, ' ')) + .filter((s) => s.length > 0) + + it.each(['postgres', 'cockroach'] as const)( + 'replaces the social_id primary key in a single statement (%s)', + (flavor) => { + const migration = getMigrations('global_account', flavor).find( + ([name]) => name === 'account_db_v2_social_id_pk_change' + ) + expect(migration).toBeDefined() + + const pkStatements = splitStatements(migration?.[1] ?? '').filter((s) => s.includes('social_id_pk')) + expect(pkStatements).toEqual([ + 'ALTER TABLE global_account.social_id DROP CONSTRAINT IF EXISTS social_id_pk, ADD CONSTRAINT social_id_pk PRIMARY KEY (_id)' + ]) + } + ) +}) diff --git a/server/account/src/collections/postgres/migrations.ts b/server/account/src/collections/postgres/migrations.ts index b8fd79c547c..ddc74cc1dbc 100644 --- a/server/account/src/collections/postgres/migrations.ts +++ b/server/account/src/collections/postgres/migrations.ts @@ -285,12 +285,11 @@ function getV2Migration2 (ns: string, flavor: DBFlavor): [string, string] { ALTER TABLE ${ns}.otp DROP CONSTRAINT IF EXISTS otp_social_id_fk; - -- Drop the existing primary key on social_id - ALTER TABLE ${ns}.social_id - DROP CONSTRAINT IF EXISTS social_id_pk; - - -- Add the new primary key on _id + -- Replace the primary key on social_id with a new one on _id. + -- Both changes must be in a single statement: CockroachDB v25+ commits DDL + -- statements individually and rejects a table left without a primary key. ALTER TABLE ${ns}.social_id + DROP CONSTRAINT IF EXISTS social_id_pk, ADD CONSTRAINT social_id_pk PRIMARY KEY (_id); ` ]