|
| 1 | +import { spawnSync } from 'node:child_process' |
| 2 | +import { mkdtemp, rm, writeFile } from 'node:fs/promises' |
| 3 | +import { tmpdir } from 'node:os' |
| 4 | +import { join } from 'node:path' |
| 5 | +import { fileURLToPath } from 'node:url' |
| 6 | +import { prepareDevSchema } from '@sim/db/scripts/prepare-dev-schema' |
| 7 | +import { generateId } from '@sim/utils/id' |
| 8 | +import postgres, { type Sql } from 'postgres' |
| 9 | +import { afterAll, beforeAll, beforeEach, describe, expect, it } from 'vitest' |
| 10 | + |
| 11 | +const databaseUrl = process.env.DEV_SCHEMA_TEST_DATABASE_URL |
| 12 | + |
| 13 | +describe.skipIf(!databaseUrl)('dev schema preparation', () => { |
| 14 | + const databaseName = `dev_schema_${generateId().replaceAll('-', '')}` |
| 15 | + let admin: Sql |
| 16 | + let sql: Sql |
| 17 | + let fixtureUrl: string |
| 18 | + let directory: string |
| 19 | + |
| 20 | + beforeAll(async () => { |
| 21 | + admin = postgres(databaseUrl!, { max: 1, onnotice: () => {} }) |
| 22 | + await admin`CREATE DATABASE ${admin(databaseName)}` |
| 23 | + const url = new URL(databaseUrl!) |
| 24 | + url.pathname = `/${databaseName}` |
| 25 | + fixtureUrl = url.toString() |
| 26 | + sql = postgres(fixtureUrl, { max: 1, onnotice: () => {} }) |
| 27 | + directory = await mkdtemp(join(tmpdir(), 'dev-schema-')) |
| 28 | + await writeFile( |
| 29 | + join(directory, 'schema.ts'), |
| 30 | + `import { pgTable, text, boolean } from ${JSON.stringify(import.meta.resolve('drizzle-orm/pg-core'))} |
| 31 | +export const organization = pgTable('organization', { |
| 32 | + id: text('id').primaryKey(), |
| 33 | + requireSso: boolean('require_sso').notNull().default(false), |
| 34 | +})` |
| 35 | + ) |
| 36 | + await writeFile( |
| 37 | + join(directory, 'drizzle.config.ts'), |
| 38 | + `export default { |
| 39 | + dialect: 'postgresql', |
| 40 | + schema: ${JSON.stringify(join(directory, 'schema.ts'))}, |
| 41 | + dbCredentials: { url: process.env.DATABASE_URL }, |
| 42 | +}` |
| 43 | + ) |
| 44 | + }) |
| 45 | + |
| 46 | + beforeEach(async () => { |
| 47 | + await sql`DROP TABLE IF EXISTS organization` |
| 48 | + }) |
| 49 | + |
| 50 | + afterAll(async () => { |
| 51 | + await sql?.end() |
| 52 | + if (admin) { |
| 53 | + await admin`DROP DATABASE IF EXISTS ${admin(databaseName)}` |
| 54 | + await admin.end() |
| 55 | + } |
| 56 | + if (directory) await rm(directory, { recursive: true, force: true }) |
| 57 | + }) |
| 58 | + |
| 59 | + async function createLegacyOrganization() { |
| 60 | + await sql`CREATE TABLE organization (id text PRIMARY KEY, departed_member_usage numeric NOT NULL DEFAULT 0)` |
| 61 | + await sql`INSERT INTO organization (id, departed_member_usage) VALUES ('existing-org', 12.5)` |
| 62 | + } |
| 63 | + |
| 64 | + /** Run the actual CLI without a TTY, matching the deployment failure. */ |
| 65 | + function push() { |
| 66 | + return spawnSync( |
| 67 | + 'bunx', |
| 68 | + [ |
| 69 | + '--no-install', |
| 70 | + 'drizzle-kit', |
| 71 | + 'push', |
| 72 | + '--config', |
| 73 | + join(directory, 'drizzle.config.ts'), |
| 74 | + '--force', |
| 75 | + ], |
| 76 | + { |
| 77 | + env: { ...process.env, DATABASE_URL: fixtureUrl }, |
| 78 | + encoding: 'utf8', |
| 79 | + timeout: 30_000, |
| 80 | + } |
| 81 | + ) |
| 82 | + } |
| 83 | + |
| 84 | + it('resolves the real noninteractive rename failure without renaming existing data', async () => { |
| 85 | + await createLegacyOrganization() |
| 86 | + const before = push() |
| 87 | + expect(before.error).toBeUndefined() |
| 88 | + expect(before.stdout + before.stderr).toContain('Interactive prompts require a TTY terminal') |
| 89 | + |
| 90 | + expect(await prepareDevSchema(sql)).toBe(true) |
| 91 | + expect(await sql`SELECT * FROM organization`).toEqual([ |
| 92 | + { id: 'existing-org', departed_member_usage: '12.5', require_sso: false }, |
| 93 | + ]) |
| 94 | + |
| 95 | + const after = push() |
| 96 | + expect(after.error).toBeUndefined() |
| 97 | + expect(after.status).toBe(0) |
| 98 | + expect(after.stdout + after.stderr).not.toContain('Interactive prompts require a TTY terminal') |
| 99 | + expect(await sql`SELECT * FROM organization`).toEqual([ |
| 100 | + { id: 'existing-org', require_sso: false }, |
| 101 | + ]) |
| 102 | + }, 60_000) |
| 103 | + |
| 104 | + it('preserves an enabled SSO policy when rerun', async () => { |
| 105 | + await createLegacyOrganization() |
| 106 | + await prepareDevSchema(sql) |
| 107 | + await sql`UPDATE organization SET require_sso = true` |
| 108 | + expect(await prepareDevSchema(sql)).toBe(false) |
| 109 | + expect(await sql`SELECT require_sso, departed_member_usage FROM organization`).toEqual([ |
| 110 | + { require_sso: true, departed_member_usage: '12.5' }, |
| 111 | + ]) |
| 112 | + }) |
| 113 | + |
| 114 | + it('runs the CI entry point with an empty optional direct URL', async () => { |
| 115 | + await createLegacyOrganization() |
| 116 | + const result = spawnSync( |
| 117 | + 'bun', |
| 118 | + ['run', fileURLToPath(new URL('./prepare-dev-schema.ts', import.meta.url))], |
| 119 | + { |
| 120 | + env: { |
| 121 | + ...process.env, |
| 122 | + SIM_DEV_DB_PUSH: '1', |
| 123 | + DATABASE_URL: fixtureUrl, |
| 124 | + MIGRATION_DATABASE_URL: '', |
| 125 | + }, |
| 126 | + encoding: 'utf8', |
| 127 | + timeout: 30_000, |
| 128 | + } |
| 129 | + ) |
| 130 | + expect(result.error).toBeUndefined() |
| 131 | + expect(result.status).toBe(0) |
| 132 | + expect(await sql`SELECT require_sso FROM organization`).toEqual([{ require_sso: false }]) |
| 133 | + }, 30_000) |
| 134 | + |
| 135 | + it('leaves fresh databases for push to initialize', async () => { |
| 136 | + expect(await prepareDevSchema(sql)).toBe(false) |
| 137 | + const result = push() |
| 138 | + expect(result.error).toBeUndefined() |
| 139 | + expect(result.status).toBe(0) |
| 140 | + await sql`INSERT INTO organization (id) VALUES ('new-org')` |
| 141 | + expect(await sql`SELECT require_sso FROM organization`).toEqual([{ require_sso: false }]) |
| 142 | + }, 30_000) |
| 143 | + |
| 144 | + it('serializes overlapping preparation attempts', async () => { |
| 145 | + await createLegacyOrganization() |
| 146 | + const other = postgres(fixtureUrl, { max: 1, onnotice: () => {} }) |
| 147 | + try { |
| 148 | + const results = await Promise.all([prepareDevSchema(sql), prepareDevSchema(other)]) |
| 149 | + expect(results.sort()).toEqual([false, true]) |
| 150 | + expect(await sql`SELECT require_sso FROM organization`).toEqual([{ require_sso: false }]) |
| 151 | + } finally { |
| 152 | + await other.end() |
| 153 | + } |
| 154 | + }) |
| 155 | +}) |
0 commit comments