Skip to content
Merged
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
42 changes: 42 additions & 0 deletions server/account/src/__tests__/migrations.test.ts
Original file line number Diff line number Diff line change
@@ -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)'
])
}
)
})
9 changes: 4 additions & 5 deletions server/account/src/collections/postgres/migrations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
`
]
Expand Down
Loading