|
| 1 | +import { pgCache } from 'pg-cache'; |
| 2 | +import { getConnections, PgTestClient } from 'pgsql-test'; |
| 3 | + |
| 4 | +import { buildIntrospectionJSON } from '../src/build-introspection'; |
| 5 | +import { buildSchemaArtifacts } from '../src/build-schema'; |
| 6 | + |
| 7 | +jest.setTimeout(60000); |
| 8 | + |
| 9 | +let pg: PgTestClient; |
| 10 | +let teardown: () => Promise<void>; |
| 11 | +let database: string; |
| 12 | + |
| 13 | +beforeAll(async () => { |
| 14 | + ({ pg, teardown } = await getConnections()); |
| 15 | + database = pg.config.database; |
| 16 | + |
| 17 | + await pg.query(` |
| 18 | + CREATE SCHEMA schema_a; |
| 19 | + CREATE TABLE schema_a.alpha_items ( |
| 20 | + id serial PRIMARY KEY, |
| 21 | + title text NOT NULL |
| 22 | + ); |
| 23 | +
|
| 24 | + CREATE SCHEMA schema_b; |
| 25 | + CREATE TABLE schema_b.beta_widgets ( |
| 26 | + id serial PRIMARY KEY, |
| 27 | + label text NOT NULL |
| 28 | + ); |
| 29 | + `); |
| 30 | +}); |
| 31 | + |
| 32 | +afterAll(async () => { |
| 33 | + // Release the pg-cache pool created by buildSchemaArtifacts before the |
| 34 | + // ephemeral database is dropped. |
| 35 | + pgCache.delete(database); |
| 36 | + await pgCache.waitForDisposals(); |
| 37 | + await teardown(); |
| 38 | +}); |
| 39 | + |
| 40 | +function tableNames(tables: { tableName: string }[]): string[] { |
| 41 | + return tables.map((t) => t.tableName).sort(); |
| 42 | +} |
| 43 | + |
| 44 | +describe('buildSchemaArtifacts', () => { |
| 45 | + it('returns SDL and tablesMeta from the same schema build', async () => { |
| 46 | + const a = await buildSchemaArtifacts({ database, schemas: ['schema_a'] }); |
| 47 | + expect(a.sdl).toContain('AlphaItem'); |
| 48 | + expect(a.sdl).not.toContain('BetaWidget'); |
| 49 | + expect(tableNames(a.tablesMeta)).toEqual(['alpha_items']); |
| 50 | + |
| 51 | + const b = await buildSchemaArtifacts({ database, schemas: ['schema_b'] }); |
| 52 | + expect(b.sdl).toContain('BetaWidget'); |
| 53 | + expect(tableNames(b.tablesMeta)).toEqual(['beta_widgets']); |
| 54 | + |
| 55 | + // The earlier result must be unaffected by the later build. |
| 56 | + expect(tableNames(a.tablesMeta)).toEqual(['alpha_items']); |
| 57 | + }); |
| 58 | + |
| 59 | + it('keeps results correlated under a forced A write -> B write -> A read schedule', async () => { |
| 60 | + // Deterministically reproduce the unsafe ordering from the legacy split |
| 61 | + // contract: caller A finishes collecting metadata (legacy global written), |
| 62 | + // caller B builds fully (legacy global overwritten), then caller A resumes |
| 63 | + // and returns. The correlated artifact API must still return A's metadata. |
| 64 | + let releaseA!: () => void; |
| 65 | + const gateA = new Promise<void>((resolve) => { |
| 66 | + releaseA = resolve; |
| 67 | + }); |
| 68 | + let signalACollected!: () => void; |
| 69 | + const aCollected = new Promise<void>((resolve) => { |
| 70 | + signalACollected = resolve; |
| 71 | + }); |
| 72 | + |
| 73 | + const pendingA = buildSchemaArtifacts({ |
| 74 | + database, |
| 75 | + schemas: ['schema_a'], |
| 76 | + _onMetaCollected: async () => { |
| 77 | + signalACollected(); |
| 78 | + await gateA; |
| 79 | + } |
| 80 | + }); |
| 81 | + |
| 82 | + await aCollected; |
| 83 | + const b = await buildSchemaArtifacts({ database, schemas: ['schema_b'] }); |
| 84 | + releaseA(); |
| 85 | + const a = await pendingA; |
| 86 | + |
| 87 | + expect(tableNames(a.tablesMeta)).toEqual(['alpha_items']); |
| 88 | + expect(a.sdl).toContain('AlphaItem'); |
| 89 | + expect(tableNames(b.tablesMeta)).toEqual(['beta_widgets']); |
| 90 | + expect(b.sdl).toContain('BetaWidget'); |
| 91 | + }); |
| 92 | + |
| 93 | + it('keeps concurrent uncoordinated builds correlated', async () => { |
| 94 | + const [a, b] = await Promise.all([ |
| 95 | + buildSchemaArtifacts({ database, schemas: ['schema_a'] }), |
| 96 | + buildSchemaArtifacts({ database, schemas: ['schema_b'] }) |
| 97 | + ]); |
| 98 | + |
| 99 | + expect(tableNames(a.tablesMeta)).toEqual(['alpha_items']); |
| 100 | + expect(tableNames(b.tablesMeta)).toEqual(['beta_widgets']); |
| 101 | + }); |
| 102 | + |
| 103 | + it('returns explicit empty metadata when the meta plugin is disabled', async () => { |
| 104 | + const artifacts = await buildSchemaArtifacts({ |
| 105 | + database, |
| 106 | + schemas: ['schema_a'], |
| 107 | + graphile: { disablePlugins: ['MetaSchemaPlugin'] } |
| 108 | + }); |
| 109 | + |
| 110 | + expect(artifacts.sdl).toContain('AlphaItem'); |
| 111 | + expect(artifacts.sdl).not.toContain('_meta'); |
| 112 | + expect(artifacts.tablesMeta).toEqual([]); |
| 113 | + }); |
| 114 | +}); |
| 115 | + |
| 116 | +describe('buildIntrospectionJSON', () => { |
| 117 | + it('returns metadata correlated to its own build', async () => { |
| 118 | + let releaseA!: () => void; |
| 119 | + const gateA = new Promise<void>((resolve) => { |
| 120 | + releaseA = resolve; |
| 121 | + }); |
| 122 | + let signalACollected!: () => void; |
| 123 | + const aCollected = new Promise<void>((resolve) => { |
| 124 | + signalACollected = resolve; |
| 125 | + }); |
| 126 | + |
| 127 | + const pendingA = buildIntrospectionJSON({ |
| 128 | + database, |
| 129 | + schemas: ['schema_a'], |
| 130 | + _onMetaCollected: async () => { |
| 131 | + signalACollected(); |
| 132 | + await gateA; |
| 133 | + } |
| 134 | + }); |
| 135 | + |
| 136 | + await aCollected; |
| 137 | + const b = await buildIntrospectionJSON({ database, schemas: ['schema_b'] }); |
| 138 | + releaseA(); |
| 139 | + const a = await pendingA; |
| 140 | + |
| 141 | + expect(tableNames(a)).toEqual(['alpha_items']); |
| 142 | + expect(tableNames(b)).toEqual(['beta_widgets']); |
| 143 | + }); |
| 144 | +}); |
0 commit comments