-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathtransform-e2e.test.ts
More file actions
313 lines (280 loc) · 10.7 KB
/
Copy pathtransform-e2e.test.ts
File metadata and controls
313 lines (280 loc) · 10.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
/**
* e2e for `pgpm transform` against live Postgres.
*
* Fixture module: two schemas, tables + FK, function, trigger, policy, grant,
* index. Transforms to each granularity, deploys each output, asserts catalog
* equivalence with the original, exercises --check/--dry-run/overwrite guard,
* verifies, reverts, partitions into pkg-app + pkg-security, and round-trips
* consolidated -> atomic -> consolidated.
*
* PREREQUISITES: a running PostgreSQL instance via standard PG* env vars.
*/
import { diffCatalogSnapshots, snapshotCatalog } from '@pgpmjs/export';
import * as fs from 'fs';
import * as path from 'path';
import { teardownPgPools } from 'pg-cache';
import { CLIDeployTestFixture } from '../test-utils';
jest.setTimeout(180000);
afterAll(async () => {
await teardownPgPools();
});
const MODULE_NAME = 'transform-e2e';
const WS = 'transform-ws';
interface ChangeSpec {
name: string;
deps?: string[];
deploy: string;
}
/** Two schemas, tables + FK, function, trigger, policy, grant, index. */
const CHANGES: ChangeSpec[] = [
{
name: 'schemas/tfx_app/schema',
deploy: 'CREATE SCHEMA tfx_app;'
},
{
name: 'schemas/tfx_sec/schema',
deploy: 'CREATE SCHEMA tfx_sec;'
},
{
name: 'schemas/tfx_app/tables/users/table',
deps: ['schemas/tfx_app/schema'],
deploy: 'CREATE TABLE tfx_app.users (id serial PRIMARY KEY, email text NOT NULL);'
},
{
name: 'schemas/tfx_sec/tables/audit/table',
deps: ['schemas/tfx_sec/schema', 'schemas/tfx_app/tables/users/table'],
deploy: [
'CREATE TABLE tfx_sec.audit (',
' id serial PRIMARY KEY,',
' user_id int NOT NULL REFERENCES tfx_app.users (id),',
' note text',
');'
].join('\n')
},
{
name: 'schemas/tfx_app/procedures/touch',
deps: ['schemas/tfx_app/schema'],
deploy:
'CREATE FUNCTION tfx_app.touch() RETURNS trigger AS $$ BEGIN RETURN NEW; END $$ LANGUAGE plpgsql;'
},
{
name: 'schemas/tfx_app/tables/users/triggers/users_touch',
deps: ['schemas/tfx_app/tables/users/table', 'schemas/tfx_app/procedures/touch'],
deploy:
'CREATE TRIGGER users_touch BEFORE UPDATE ON tfx_app.users FOR EACH ROW EXECUTE FUNCTION tfx_app.touch();'
},
{
name: 'schemas/tfx_app/tables/users/policies/users_self',
deps: ['schemas/tfx_app/tables/users/table'],
deploy: [
'ALTER TABLE tfx_app.users ENABLE ROW LEVEL SECURITY;',
'CREATE POLICY users_self ON tfx_app.users FOR SELECT USING (true);'
].join('\n')
},
{
name: 'schemas/tfx_app/tables/users/grants/select',
deps: ['schemas/tfx_app/tables/users/table'],
deploy: 'GRANT SELECT ON tfx_app.users TO PUBLIC;'
},
{
name: 'schemas/tfx_app/tables/users/indexes/users_email_idx',
deps: ['schemas/tfx_app/tables/users/table'],
deploy: 'CREATE INDEX users_email_idx ON tfx_app.users (email);'
}
];
const writeModule = (moduleDir: string) => {
const workspaceDir = path.dirname(moduleDir);
fs.mkdirSync(workspaceDir, { recursive: true });
fs.writeFileSync(path.join(workspaceDir, 'pgpm.json'), '{\n "packages": [\n "*"\n ]\n}');
fs.mkdirSync(moduleDir, { recursive: true });
const planLines = CHANGES.map((c, i) => {
const deps = c.deps && c.deps.length > 0 ? ` [${c.deps.join(' ')}]` : '';
return `${c.name}${deps} 2024-01-0${(i % 9) + 1}T00:00:00Z test <test@example.com> # add ${c.name}`;
});
fs.writeFileSync(
path.join(moduleDir, 'pgpm.plan'),
`%syntax-version=1.0.0\n%project=${MODULE_NAME}\n%uri=https://github.com/test/${MODULE_NAME}\n\n${planLines.join('\n')}\n`
);
fs.writeFileSync(
path.join(moduleDir, `${MODULE_NAME}.control`),
`comment = 'Transform e2e module'\ndefault_version = '0.0.1'\nrelocatable = false\nsuperuser = false\n`
);
for (const c of CHANGES) {
const filePath = path.join(moduleDir, 'deploy', `${c.name}.sql`);
fs.mkdirSync(path.dirname(filePath), { recursive: true });
fs.writeFileSync(filePath, `-- Deploy ${c.name} to pg\n\nBEGIN;\n\n${c.deploy}\n\nCOMMIT;\n`);
const revertPath = path.join(moduleDir, 'revert', `${c.name}.sql`);
fs.mkdirSync(path.dirname(revertPath), { recursive: true });
fs.writeFileSync(revertPath, `-- Revert ${c.name} from pg\n\nBEGIN;\n\n-- noop\n\nCOMMIT;\n`);
const verifyPath = path.join(moduleDir, 'verify', `${c.name}.sql`);
fs.mkdirSync(path.dirname(verifyPath), { recursive: true });
fs.writeFileSync(verifyPath, `-- Verify ${c.name} on pg\n\nBEGIN;\n\nROLLBACK;\n`);
}
};
const PARTITION_CONFIG = {
defaultPackage: 'pkg-app',
splitRiders: ['grant'],
rules: [
{
package: 'pkg-security',
select: [{ kind: 'policy' }, { statementKind: 'grant' }]
}
]
};
describe('pgpm transform e2e', () => {
let fixture: CLIDeployTestFixture;
let originalDb: any;
let wsDir: string;
let moduleDir: string;
beforeAll(async () => {
fixture = new CLIDeployTestFixture();
wsDir = path.join(fixture.tempFixtureDir, WS);
moduleDir = path.join(wsDir, MODULE_NAME);
writeModule(moduleDir);
originalDb = await fixture.setupTestDatabase();
await fixture.runTerminalCommands(
`
cd ${WS}/${MODULE_NAME}
pgpm deploy --database ${originalDb.name} --package ${MODULE_NAME} --yes
`,
{ database: originalDb.name }
);
});
afterAll(async () => {
await fixture.cleanup();
});
it('--dry-run prints the plan without writing anything', async () => {
await fixture.runTerminalCommands(
`
cd ${WS}/${MODULE_NAME}
pgpm transform --granularity object --dry-run
`,
{}
);
expect(fs.existsSync(path.join(wsDir, `${MODULE_NAME}-object`))).toBe(false);
});
it.each(['atomic', 'object', 'consolidated'] as const)(
'transform --granularity %s deploys with an equivalent catalog, verifies, and reverts clean',
async granularity => {
await fixture.runTerminalCommands(
`
cd ${WS}/${MODULE_NAME}
pgpm transform --granularity ${granularity}
`,
{}
);
const outDir = path.join(wsDir, `${MODULE_NAME}-${granularity}`);
expect(fs.existsSync(path.join(outDir, 'pgpm.plan'))).toBe(true);
expect(fs.existsSync(path.join(outDir, `${MODULE_NAME}-${granularity}.control`))).toBe(true);
const testDb = await fixture.setupTestDatabase();
await fixture.runTerminalCommands(
`
cd ${WS}/${MODULE_NAME}-${granularity}
pgpm deploy --database ${testDb.name} --package ${MODULE_NAME}-${granularity} --yes
`,
{ database: testDb.name }
);
const snapOriginal = await snapshotCatalog(originalDb);
const snapTransformed = await snapshotCatalog(testDb);
expect(diffCatalogSnapshots(snapOriginal, snapTransformed)).toEqual([]);
await fixture.runTerminalCommands(
`
cd ${WS}/${MODULE_NAME}-${granularity}
pgpm verify --database ${testDb.name} --package ${MODULE_NAME}-${granularity} --yes
pgpm revert --database ${testDb.name} --package ${MODULE_NAME}-${granularity} --yes
`,
{ database: testDb.name }
);
expect(await testDb.exists('schema', 'tfx_app')).toBe(false);
expect(await testDb.exists('schema', 'tfx_sec')).toBe(false);
const remaining = await testDb.query(
`SELECT COUNT(*)::int AS count FROM pgpm_migrate.changes WHERE package = $1`,
[`${MODULE_NAME}-${granularity}`]
);
expect(remaining.rows[0].count).toBe(0);
}
);
it('rewrites an existing output when --write is passed', async () => {
// The refusal path (no --write) exits the process, so it is covered by the
// checkOverwrite unit tests; here we prove --write re-generates in place.
const outDir = path.join(wsDir, `${MODULE_NAME}-object`);
expect(fs.existsSync(outDir)).toBe(true);
await fixture.runTerminalCommands(
`
cd ${WS}/${MODULE_NAME}
pgpm transform --granularity object --write
`,
{}
);
expect(fs.existsSync(path.join(outDir, 'pgpm.plan'))).toBe(true);
});
it('--check proves the transform is structurally lossless', async () => {
await fixture.runTerminalCommands(
`
cd ${WS}/${MODULE_NAME}
pgpm transform --granularity consolidated --write --check
`,
{}
);
});
it('partitions into pkg-app + pkg-security with catalog equivalence', async () => {
fs.writeFileSync(path.join(wsDir, 'partition.json'), JSON.stringify(PARTITION_CONFIG, null, 2));
await fixture.runTerminalCommands(
`
cd ${WS}/${MODULE_NAME}
pgpm transform --granularity object --partition ../partition.json
`,
{}
);
const appDir = path.join(wsDir, 'pkg-app');
const secDir = path.join(wsDir, 'pkg-security');
expect(fs.existsSync(path.join(appDir, 'pgpm.plan'))).toBe(true);
expect(fs.existsSync(path.join(secDir, 'pgpm.plan'))).toBe(true);
// security package requires the app package (policies/grants target app tables)
const secControl = fs.readFileSync(path.join(secDir, 'pkg-security.control'), 'utf-8');
expect(secControl).toMatch(/requires = '.*pkg-app.*'/);
const testDb = await fixture.setupTestDatabase();
await fixture.runTerminalCommands(
`
cd ${WS}/pkg-app
pgpm deploy --database ${testDb.name} --package pkg-app --yes
cd ../pkg-security
pgpm deploy --database ${testDb.name} --package pkg-security --yes
`,
{ database: testDb.name }
);
const snapOriginal = await snapshotCatalog(originalDb);
const snapPartitioned = await snapshotCatalog(testDb);
expect(diffCatalogSnapshots(snapOriginal, snapPartitioned)).toEqual([]);
});
it('round-trips consolidated -> atomic -> consolidated to the same object set', async () => {
// consolidated already exists at <module>-consolidated (from --check test)
const c1 = path.join(wsDir, `${MODULE_NAME}-consolidated`);
expect(fs.existsSync(path.join(c1, 'pgpm.plan'))).toBe(true);
await fixture.runTerminalCommands(
`
cd ${WS}/${MODULE_NAME}-consolidated
pgpm transform --granularity atomic --out ../roundtrip-atomic --write
cd ../roundtrip-atomic/${MODULE_NAME}-consolidated-atomic
pgpm transform --granularity consolidated --out ../../roundtrip-consolidated --write
`,
{}
);
const c2 = path.join(
wsDir,
'roundtrip-consolidated',
`${MODULE_NAME}-consolidated-atomic-consolidated`
);
expect(fs.existsSync(path.join(c2, 'pgpm.plan'))).toBe(true);
const changeSet = (planPath: string): string[] =>
fs
.readFileSync(planPath, 'utf-8')
.split('\n')
.filter(line => line && !line.startsWith('%'))
.map(line => line.split(' ')[0])
.sort();
expect(changeSet(path.join(c2, 'pgpm.plan'))).toEqual(
changeSet(path.join(c1, 'pgpm.plan'))
);
});
});