-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathexport-graphql.ts
More file actions
355 lines (313 loc) · 11.3 KB
/
Copy pathexport-graphql.ts
File metadata and controls
355 lines (313 loc) · 11.3 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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
/**
* GraphQL-based export orchestrator.
*
* This is a standalone GraphQL export flow that mirrors export-migrations.ts
* but fetches all data via GraphQL queries instead of direct SQL.
*
* Per Dan's guidance: "I would NOT do branching in those existing files.
* I would make the GraphQL flow its entire own flow at first."
*/
import { PgpmPackage, PgpmRow, SqlWriteOptions, writePgpmFiles, writePgpmPlan } from '@pgpmjs/core';
import { createClient } from '@pgpmjs/migrate-client';
import { ExportGranularity, PartitionConfig, partitionExportRows, restructureExportRows } from '@pgpmjs/transform';
import { Inquirerer } from 'inquirerer';
import { exportGraphQLMeta } from './export-graphql-meta';
import {
DB_REQUIRED_EXTENSIONS,
detectMissingModules,
installMissingModules,
makeReplacer,
META_COMMON_FOOTER,
META_COMMON_HEADER,
META_TABLE_ORDER,
normalizeOutdir,
preparePackage,
Schema,
SERVICE_REQUIRED_EXTENSIONS} from './export-utils';
import { GraphQLClient } from './graphql-client';
import { graphqlRowToPostgresRow } from './graphql-naming';
// =============================================================================
// Public API
// =============================================================================
export interface ExportGraphQLOptions {
project: PgpmPackage;
/** GraphQL endpoint for metaschema/services data (e.g. http://private.localhost:3002/graphql) */
metaEndpoint: string;
/** GraphQL endpoint for db_migrate data (e.g. http://db_migrate.localhost:3000/graphql) */
migrateEndpoint?: string;
/** Extra headers for the migrate endpoint (e.g. Host header for subdomain routing) */
migrateHeaders?: Record<string, string>;
/** Bearer token for authentication */
token?: string;
/** Extra headers to send with GraphQL requests (e.g. X-Meta-Schema) */
headers?: Record<string, string>;
/** Database ID to export */
databaseId: string;
/** Database display name */
databaseName: string;
/** Schema names selected for export */
schema_names: string[];
/** Schema rows (with name and schema_name) for the replacer */
schemas: Schema[];
/** Author string */
author: string;
/** Output directory for packages */
outdir: string;
/** Extension name for the DB module */
extensionName: string;
/** Description for the DB extension */
extensionDesc?: string;
/** Extension name for the service/meta module */
metaExtensionName: string;
/** Description for the service/meta extension */
metaExtensionDesc?: string;
prompter?: Inquirerer;
argv?: Record<string, any>;
repoName?: string;
username?: string;
serviceOutdir?: string;
skipSchemaRenaming?: boolean;
/**
* sql_actions categories to exclude from the export (e.g. ['security',
* 'permissions']). The migrate API no longer exposes the category column;
* exclusion is enforced server-side by the export_category_filter RLS
* policy (export.exclude_categories), so this flag is not applied client-side.
*/
excludeCategories?: string[];
/**
* Granularity dial for the exported database module. When set, exported SQL
* is routed through `restructureChanges` (`@pgpmjs/transform`): change paths
* are derived from the naming spec and `requires` from the statement graph.
* When omitted, sql_actions rows are written through unchanged.
*/
granularity?: ExportGranularity;
/**
* Partition dial: split the exported database module into multiple pgpm
* packages per the config's rules (`partitionUnits` in `@pgpmjs/transform`).
* Cross-package dependencies become `<pkg>:<path>` requires. Throws
* `PartitionCycleError` on an unshippable partition.
*/
partition?: PartitionConfig;
}
export const exportGraphQL = async ({
project,
metaEndpoint,
migrateEndpoint,
token,
headers,
migrateHeaders,
databaseId,
databaseName,
schema_names,
schemas,
author,
outdir,
extensionName,
extensionDesc,
metaExtensionName,
metaExtensionDesc,
prompter,
argv,
repoName,
username,
serviceOutdir,
skipSchemaRenaming = false,
granularity,
partition
}: ExportGraphQLOptions): Promise<void> => {
const normalizedOutdir = normalizeOutdir(outdir);
const svcOutdir = normalizeOutdir(serviceOutdir || outdir);
const name = extensionName;
const schemasForReplacement = skipSchemaRenaming
? []
: schemas.filter((schema) => schema_names.includes(schema.schema_name));
const { replacer } = makeReplacer({
schemas: schemasForReplacement,
name
});
// =========================================================================
// 1. Fetch sql_actions via @pgpmjs/migrate-client ORM (db_migrate endpoint)
// =========================================================================
let sqlActionRows: Record<string, unknown>[] = [];
if (migrateEndpoint) {
console.log(`Fetching sql_actions from ${migrateEndpoint}...`);
const db = createClient({
endpoint: migrateEndpoint,
headers: {
...(token ? { Authorization: `Bearer ${token}` } : {}),
...migrateHeaders
}
});
try {
// Paginate through all sql_actions for this database using the ORM.
// The ORM generates `where: SqlActionFilter` which uses the filter plugin's
// `equalTo` operator — the correct approach for Constructive's PostGraphile APIs.
let hasNextPage = true;
let afterCursor: string | undefined;
const PAGE_SIZE = 100;
while (hasNextPage) {
const result = await db.sqlAction.findMany({
select: {
id: true,
databaseId: true,
name: true,
deploy: true,
revert: true,
verify: true,
content: true,
deps: true,
actionName: true,
actionId: true,
actorId: true,
payload: true
},
where: {
databaseId: { equalTo: databaseId }
},
orderBy: ['ID_ASC'],
first: PAGE_SIZE,
...(afterCursor ? { after: afterCursor } : {})
}).unwrap();
const connection = result.sqlActions;
for (const node of connection.nodes) {
sqlActionRows.push(
graphqlRowToPostgresRow(node as unknown as Record<string, unknown>)
);
}
hasNextPage = connection.pageInfo?.hasNextPage ?? false;
afterCursor = connection.pageInfo?.endCursor ?? undefined;
}
console.log(` Found ${sqlActionRows.length} sql_actions`);
} catch (err) {
console.warn(` Warning: Could not fetch sql_actions: ${err instanceof Error ? err.message : err}`);
}
} else {
console.log('No migrate endpoint provided, skipping sql_actions export.');
}
const opts: SqlWriteOptions = {
name,
replacer,
outdir: normalizedOutdir,
author
};
const dbExtensionDesc = extensionDesc || `${name} database schema for ${databaseName}`;
if (sqlActionRows.length > 0) {
const dbMissingResult = await detectMissingModules(project, [...DB_REQUIRED_EXTENSIONS], prompter, argv);
let dbRows = sqlActionRows as unknown as PgpmRow[];
if (granularity) {
const { rows, warnings } = await restructureExportRows(dbRows, granularity);
dbRows = rows;
warnings.forEach(warning => console.warn(`restructure (${granularity}): ${warning}`));
}
if (partition) {
const { packages, warnings } = await partitionExportRows(dbRows, partition);
warnings.forEach(warning => console.warn(`partition: ${warning}`));
for (const pkg of packages) {
const pkgDir = await preparePackage({
project,
author,
outdir: normalizedOutdir,
name: pkg.name,
description: `${pkg.name} (partitioned from ${name})`,
extensions: [...DB_REQUIRED_EXTENSIONS, ...pkg.requires],
prompter,
repoName,
username
});
if (dbMissingResult.shouldInstall) {
await installMissingModules(pkgDir, dbMissingResult.missingModules);
}
const pkgOpts: SqlWriteOptions = { ...opts, name: pkg.name };
writePgpmPlan(pkg.rows, pkgOpts);
writePgpmFiles(pkg.rows, pkgOpts);
}
} else {
const dbModuleDir = await preparePackage({
project,
author,
outdir: normalizedOutdir,
name,
description: dbExtensionDesc,
extensions: [...DB_REQUIRED_EXTENSIONS],
prompter,
repoName,
username
});
if (dbMissingResult.shouldInstall) {
await installMissingModules(dbModuleDir, dbMissingResult.missingModules);
}
writePgpmPlan(dbRows, opts);
writePgpmFiles(dbRows, opts);
}
} else {
console.log('No sql_actions found. Skipping database module export.');
}
// =========================================================================
// 2. Fetch meta/services data via GraphQL
// =========================================================================
console.log(`Fetching metadata from ${metaEndpoint}...`);
const metaClient = new GraphQLClient({ endpoint: metaEndpoint, token, headers });
const metaResult = await exportGraphQLMeta({
client: metaClient,
database_id: databaseId
});
const metaTableCount = Object.keys(metaResult).length;
console.log(` Fetched ${metaTableCount} meta tables with data`);
if (metaTableCount > 0) {
const metaDesc = metaExtensionDesc || `${metaExtensionName} service utilities for managing domains, APIs, and services`;
const svcMissingResult = await detectMissingModules(project, [...SERVICE_REQUIRED_EXTENSIONS], prompter, argv);
const svcModuleDir = await preparePackage({
project,
author,
outdir: svcOutdir,
name: metaExtensionName,
description: metaDesc,
extensions: [...SERVICE_REQUIRED_EXTENSIONS],
prompter,
repoName,
username
});
if (svcMissingResult.shouldInstall) {
await installMissingModules(svcModuleDir, svcMissingResult.missingModules);
}
const metaSchemasForReplacement = skipSchemaRenaming
? []
: schemas.filter((schema) => schema_names.includes(schema.schema_name));
const metaReplacer = makeReplacer({
schemas: metaSchemasForReplacement,
name: metaExtensionName,
// Use extensionName for schema prefix — the services metadata references
// schemas owned by the application package (e.g. agent_db_auth_public),
// not the services package (agent_db_services_auth_public)
schemaPrefix: name
});
const metaPackage: PgpmRow[] = [];
const tablesWithContent: string[] = [];
for (const tableName of META_TABLE_ORDER) {
const tableSql = metaResult[tableName];
if (tableSql) {
const replacedSql = metaReplacer.replacer(tableSql);
const deps = tableName === 'database'
? []
: tablesWithContent.length > 0
? [`migrate/${tablesWithContent[tablesWithContent.length - 1]}`]
: [];
metaPackage.push({
deps,
deploy: `migrate/${tableName}`,
content: `${META_COMMON_HEADER}
${replacedSql}
${META_COMMON_FOOTER}
`
});
tablesWithContent.push(tableName);
}
}
opts.replacer = metaReplacer.replacer;
opts.name = metaExtensionName;
opts.outdir = svcOutdir;
writePgpmPlan(metaPackage, opts);
writePgpmFiles(metaPackage, opts);
}
console.log('GraphQL export complete.');
};