-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathCLIDeployTestFixture.ts
More file actions
275 lines (232 loc) · 8.08 KB
/
CLIDeployTestFixture.ts
File metadata and controls
275 lines (232 loc) · 8.08 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
import { PgpmMigrate } from '@pgpmjs/core';
import { CLIOptions,Inquirerer } from 'inquirerer';
import { ParsedArgs } from 'minimist';
import { Pool } from 'pg';
import { getPgPool } from 'pg-cache';
import { getPgEnvOptions } from 'pg-env';
import { commands } from '../src/commands';
import { TestFixture } from './fixtures';
import { TestDatabase } from './TestDatabase';
export class CLIDeployTestFixture extends TestFixture {
private databases: TestDatabase[] = [];
private dbCounter = 0;
private pools: Pool[] = [];
constructor(...fixturePath: string[]) {
super(...fixturePath);
}
async setupTestDatabase(): Promise<TestDatabase> {
const dbName = `test_cli_${Date.now()}_${Math.random().toString(36).substring(2, 8)}_${this.dbCounter++}`;
// Get base config from environment using pg-env
const baseConfig = getPgEnvOptions({
database: 'postgres'
});
// Create database using admin pool
const adminPool = getPgPool(baseConfig);
try {
await adminPool.query(`CREATE DATABASE "${dbName}"`);
} catch (e) {
if (e && typeof e === 'object' && 'errors' in e && Array.isArray((e as any).errors)) {
for (const err of (e as any).errors) {
console.error('AggregateError item:', err);
}
} else {
console.error('Test failure:', e);
}
throw e;
}
// Get config for the new test database
const pgConfig = getPgEnvOptions({
database: dbName
});
const config = {
host: pgConfig.host,
port: pgConfig.port,
user: pgConfig.user,
password: pgConfig.password,
database: pgConfig.database
};
// Initialize migrate schema
const migrate = new PgpmMigrate(config);
await migrate.initialize();
// Get pool for test database operations
const pool = getPgPool(pgConfig);
this.pools.push(pool);
const db: TestDatabase = {
name: dbName,
config,
async query(sql: string, params?: any[]) {
return pool.query(sql, params);
},
async exists(type: 'schema' | 'table', name: string) {
if (type === 'schema') {
const result = await pool.query(
`SELECT EXISTS (
SELECT 1 FROM information_schema.schemata
WHERE schema_name = $1
) as exists`,
[name]
);
return result.rows[0].exists;
} else {
const [schema, table] = name.includes('.') ? name.split('.') : ['public', name];
const result = await pool.query(
`SELECT EXISTS (
SELECT 1 FROM information_schema.tables
WHERE table_schema = $1 AND table_name = $2
) as exists`,
[schema, table]
);
return result.rows[0].exists;
}
},
async getDeployedChanges() {
const result = await pool.query(
`SELECT package, change_name, deployed_at
FROM pgpm_migrate.changes
ORDER BY deployed_at`
);
return result.rows;
},
async getMigrationState() {
const changes = await pool.query(`
SELECT package, change_name, script_hash, deployed_at
FROM pgpm_migrate.changes
ORDER BY deployed_at
`);
const events = await pool.query(`
SELECT package, change_name, event_type, occurred_at, error_message, error_code
FROM pgpm_migrate.events
ORDER BY occurred_at
`);
const sanitizedEvents = events.rows;
// Remove timestamps from objects for consistent snapshots
const cleanChanges = changes.rows.map(({ deployed_at, ...change }) => change);
const cleanEvents = sanitizedEvents.map(({ occurred_at, ...event }) => event);
return {
changes: cleanChanges,
events: cleanEvents,
changeCount: cleanChanges.length,
eventCount: cleanEvents.length
};
},
async getDependencies(packageName: string, changeName: string) {
const result = await pool.query(
`SELECT d.requires
FROM pgpm_migrate.dependencies d
JOIN pgpm_migrate.changes c ON c.change_id = d.change_id
WHERE c.package = $1 AND c.change_name = $2`,
[packageName, changeName]
);
return result.rows.map((row: any) => row.requires);
},
async close() {
// Don't close the pool here as it's managed by pg-cache
// Just mark this database as closed
}
};
this.databases.push(db);
return db;
}
async exec(commandString: string, variables: Record<string, string> = {}): Promise<any[]> {
return this.runTerminalCommands(commandString, variables, true);
}
async runTerminalCommands(commandString: string, variables: Record<string, string> = {}, executeCommands: boolean = true): Promise<any[]> {
const results: any[] = [];
let currentDir = this.tempFixtureDir;
const commands = commandString
.split(/\n|;/)
.map(cmd => cmd.trim())
.filter(cmd => cmd.length > 0);
for (const command of commands) {
let processedCommand = command;
for (const [key, value] of Object.entries(variables)) {
processedCommand = processedCommand.replace(new RegExp(`\\$${key}`, 'g'), value);
}
const tokens = this.tokenizeCommand(processedCommand);
if (tokens[0] === 'cd') {
const targetDir = tokens[1];
if (targetDir.startsWith('/')) {
currentDir = targetDir;
} else {
currentDir = require('path').resolve(currentDir, targetDir);
}
results.push({ command: processedCommand, type: 'cd', result: { cwd: currentDir } });
} else if (tokens[0] === 'lql' || tokens[0] === 'launchql') {
// Handle LaunchQL CLI commands
const argv = this.parseCliCommand(tokens.slice(1), currentDir);
if (executeCommands) {
const result = await this.runCliCommand(argv);
results.push({ command: processedCommand, type: 'cli', result });
} else {
results.push({ command: processedCommand, type: 'cli', result: { argv } });
}
} else {
throw new Error(`Unsupported command: ${tokens[0]}`);
}
}
return results;
}
private tokenizeCommand(command: string): string[] {
const re = /("[^"\\]*(?:\\.[^"\\]*)*"|'[^'\\]*(?:\\.[^'\\]*)*'|--[a-zA-Z0-9_-]+|-[a-zA-Z0-9_]+|[^\s]+)|(\s+)/g;
const matches = command.match(re);
const tokens = matches ? matches.filter(t => t.trim()) : [];
return tokens;
}
private parseCliCommand(tokens: string[], cwd: string): ParsedArgs {
const argv: ParsedArgs = {
_: [],
cwd
};
let i = 0;
while (i < tokens.length) {
const token = tokens[i];
if (token.startsWith('--')) {
const key = token.substring(2);
// Handle --no-<thing> pattern by setting <thing>: false
if (key.startsWith('no-')) {
const baseKey = key.substring(3); // Remove 'no-' prefix
argv[baseKey] = false;
i += 1;
} else if (i + 1 < tokens.length && !tokens[i + 1].startsWith('--')) {
argv[key] = tokens[i + 1];
i += 2;
} else {
argv[key] = true;
i += 1;
}
} else {
argv._.push(token);
i += 1;
}
}
return argv;
}
private async runCliCommand(argv: ParsedArgs): Promise<any> {
const prompter = new Inquirerer({
input: process.stdin,
output: process.stdout,
noTty: true
});
const options: CLIOptions & { skipPgTeardown?: boolean } = {
noTty: true,
input: process.stdin,
output: process.stdout,
version: '1.0.0',
skipPgTeardown: true,
minimistOpts: {
alias: {
v: 'version',
h: 'help'
}
}
};
await commands(argv, prompter, options);
return { argv };
}
async cleanup(): Promise<void> {
// Don't close pools here - let pg-cache manage them
this.pools = [];
this.databases = [];
super.cleanup();
}
}