@pgpmjs/export 1.14.2 → 1.15.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,11 +1,20 @@
1
+ /**
2
+ * GraphQL-based export orchestrator.
3
+ *
4
+ * This is a standalone GraphQL export flow that mirrors export-migrations.ts
5
+ * but fetches all data via GraphQL queries instead of direct SQL.
6
+ *
7
+ * Per Dan's guidance: "I would NOT do branching in those existing files.
8
+ * I would make the GraphQL flow its entire own flow at first."
9
+ */
1
10
  import { writePgpmFiles, writePgpmPlan } from '@pgpmjs/core';
2
11
  import { createClient } from '@pgpmjs/migrate-client';
3
- import { GraphQLClient } from './graphql-client';
12
+ import { partitionExportRows, restructureExportRows } from '@pgpmjs/transform';
4
13
  import { exportGraphQLMeta } from './export-graphql-meta';
14
+ import { DB_REQUIRED_EXTENSIONS, detectMissingModules, installMissingModules, makeReplacer, META_COMMON_FOOTER, META_COMMON_HEADER, META_TABLE_ORDER, normalizeOutdir, preparePackage, SERVICE_REQUIRED_EXTENSIONS } from './export-utils';
15
+ import { GraphQLClient } from './graphql-client';
5
16
  import { graphqlRowToPostgresRow } from './graphql-naming';
6
- import { restructureExportRows } from './restructure';
7
- import { DB_REQUIRED_EXTENSIONS, SERVICE_REQUIRED_EXTENSIONS, META_COMMON_HEADER, META_COMMON_FOOTER, META_TABLE_ORDER, detectMissingModules, installMissingModules, makeReplacer, preparePackage, normalizeOutdir } from './export-utils';
8
- 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 }) => {
17
+ 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 }) => {
9
18
  const normalizedOutdir = normalizeOutdir(outdir);
10
19
  const svcOutdir = normalizeOutdir(serviceOutdir || outdir);
11
20
  const name = extensionName;
@@ -84,28 +93,53 @@ export const exportGraphQL = async ({ project, metaEndpoint, migrateEndpoint, to
84
93
  const dbExtensionDesc = extensionDesc || `${name} database schema for ${databaseName}`;
85
94
  if (sqlActionRows.length > 0) {
86
95
  const dbMissingResult = await detectMissingModules(project, [...DB_REQUIRED_EXTENSIONS], prompter, argv);
87
- const dbModuleDir = await preparePackage({
88
- project,
89
- author,
90
- outdir: normalizedOutdir,
91
- name,
92
- description: dbExtensionDesc,
93
- extensions: [...DB_REQUIRED_EXTENSIONS],
94
- prompter,
95
- repoName,
96
- username
97
- });
98
- if (dbMissingResult.shouldInstall) {
99
- await installMissingModules(dbModuleDir, dbMissingResult.missingModules);
100
- }
101
96
  let dbRows = sqlActionRows;
102
97
  if (granularity) {
103
98
  const { rows, warnings } = await restructureExportRows(dbRows, granularity);
104
99
  dbRows = rows;
105
100
  warnings.forEach(warning => console.warn(`restructure (${granularity}): ${warning}`));
106
101
  }
107
- writePgpmPlan(dbRows, opts);
108
- writePgpmFiles(dbRows, opts);
102
+ if (partition) {
103
+ const { packages, warnings } = await partitionExportRows(dbRows, partition);
104
+ warnings.forEach(warning => console.warn(`partition: ${warning}`));
105
+ for (const pkg of packages) {
106
+ const pkgDir = await preparePackage({
107
+ project,
108
+ author,
109
+ outdir: normalizedOutdir,
110
+ name: pkg.name,
111
+ description: `${pkg.name} (partitioned from ${name})`,
112
+ extensions: [...DB_REQUIRED_EXTENSIONS, ...pkg.requires],
113
+ prompter,
114
+ repoName,
115
+ username
116
+ });
117
+ if (dbMissingResult.shouldInstall) {
118
+ await installMissingModules(pkgDir, dbMissingResult.missingModules);
119
+ }
120
+ const pkgOpts = { ...opts, name: pkg.name };
121
+ writePgpmPlan(pkg.rows, pkgOpts);
122
+ writePgpmFiles(pkg.rows, pkgOpts);
123
+ }
124
+ }
125
+ else {
126
+ const dbModuleDir = await preparePackage({
127
+ project,
128
+ author,
129
+ outdir: normalizedOutdir,
130
+ name,
131
+ description: dbExtensionDesc,
132
+ extensions: [...DB_REQUIRED_EXTENSIONS],
133
+ prompter,
134
+ repoName,
135
+ username
136
+ });
137
+ if (dbMissingResult.shouldInstall) {
138
+ await installMissingModules(dbModuleDir, dbMissingResult.missingModules);
139
+ }
140
+ writePgpmPlan(dbRows, opts);
141
+ writePgpmFiles(dbRows, opts);
142
+ }
109
143
  }
110
144
  else {
111
145
  console.log('No sql_actions found. Skipping database module export.');
@@ -1,9 +1,9 @@
1
- import { getPgPool } from 'pg-cache';
2
1
  import { writePgpmFiles, writePgpmPlan } from '@pgpmjs/core';
2
+ import { partitionExportRows, restructureExportRows } from '@pgpmjs/transform';
3
+ import { getPgPool } from 'pg-cache';
3
4
  import { exportMeta } from './export-meta';
4
- import { restructureExportRows } from './restructure';
5
- import { DB_REQUIRED_EXTENSIONS, SERVICE_REQUIRED_EXTENSIONS, META_COMMON_HEADER, META_COMMON_FOOTER, META_TABLE_ORDER, detectMissingModules, installMissingModules, makeReplacer, preparePackage, normalizeOutdir } from './export-utils';
6
- const exportMigrationsToDisk = async ({ project, options, database, databaseId, databaseName, author, outdir, schema_names, extensionName, extensionDesc, metaExtensionName, metaExtensionDesc, prompter, argv, repoName, username, serviceOutdir, skipSchemaRenaming = false, excludeCategories, granularity }) => {
5
+ import { DB_REQUIRED_EXTENSIONS, detectMissingModules, installMissingModules, makeReplacer, META_COMMON_FOOTER, META_COMMON_HEADER, META_TABLE_ORDER, normalizeOutdir, preparePackage, SERVICE_REQUIRED_EXTENSIONS } from './export-utils';
6
+ const exportMigrationsToDisk = async ({ project, options, database, databaseId, databaseName, author, outdir, schema_names, extensionName, extensionDesc, metaExtensionName, metaExtensionDesc, prompter, argv, repoName, username, serviceOutdir, skipSchemaRenaming = false, excludeCategories, granularity, partition }) => {
7
7
  const normalizedOutdir = normalizeOutdir(outdir);
8
8
  // Use serviceOutdir for service module, defaulting to outdir if not provided
9
9
  const svcOutdir = normalizeOutdir(serviceOutdir || outdir);
@@ -61,30 +61,58 @@ const exportMigrationsToDisk = async ({ project, options, database, databaseId,
61
61
  if (appRows.length > 0) {
62
62
  // Detect missing modules at workspace level and prompt user
63
63
  const dbMissingResult = await detectMissingModules(project, [...DB_REQUIRED_EXTENSIONS], prompter, argv);
64
- // Create/prepare the module directory
65
- const dbModuleDir = await preparePackage({
66
- project,
67
- author,
68
- outdir: normalizedOutdir,
69
- name,
70
- description: dbExtensionDesc,
71
- extensions: [...DB_REQUIRED_EXTENSIONS],
72
- prompter,
73
- repoName,
74
- username
75
- });
76
- // Install missing modules if user confirmed (now that module exists)
77
- if (dbMissingResult.shouldInstall) {
78
- await installMissingModules(dbModuleDir, dbMissingResult.missingModules);
79
- }
80
64
  let dbRows = appRows;
81
65
  if (granularity) {
82
66
  const { rows, warnings } = await restructureExportRows(appRows, granularity);
83
67
  dbRows = rows;
84
68
  warnings.forEach(warning => console.warn(`restructure (${granularity}): ${warning}`));
85
69
  }
86
- writePgpmPlan(dbRows, opts);
87
- writePgpmFiles(dbRows, opts);
70
+ if (partition) {
71
+ // Partition dial: split the database module into N packages. Each
72
+ // package requires its cross-package prerequisites as extensions so
73
+ // recursive deploys order them correctly.
74
+ const { packages, warnings } = await partitionExportRows(dbRows, partition);
75
+ warnings.forEach(warning => console.warn(`partition: ${warning}`));
76
+ for (const pkg of packages) {
77
+ const pkgDir = await preparePackage({
78
+ project,
79
+ author,
80
+ outdir: normalizedOutdir,
81
+ name: pkg.name,
82
+ description: `${pkg.name} (partitioned from ${name})`,
83
+ extensions: [...DB_REQUIRED_EXTENSIONS, ...pkg.requires],
84
+ prompter,
85
+ repoName,
86
+ username
87
+ });
88
+ if (dbMissingResult.shouldInstall) {
89
+ await installMissingModules(pkgDir, dbMissingResult.missingModules);
90
+ }
91
+ const pkgOpts = { ...opts, name: pkg.name };
92
+ writePgpmPlan(pkg.rows, pkgOpts);
93
+ writePgpmFiles(pkg.rows, pkgOpts);
94
+ }
95
+ }
96
+ else {
97
+ // Create/prepare the module directory
98
+ const dbModuleDir = await preparePackage({
99
+ project,
100
+ author,
101
+ outdir: normalizedOutdir,
102
+ name,
103
+ description: dbExtensionDesc,
104
+ extensions: [...DB_REQUIRED_EXTENSIONS],
105
+ prompter,
106
+ repoName,
107
+ username
108
+ });
109
+ // Install missing modules if user confirmed (now that module exists)
110
+ if (dbMissingResult.shouldInstall) {
111
+ await installMissingModules(dbModuleDir, dbMissingResult.missingModules);
112
+ }
113
+ writePgpmPlan(dbRows, opts);
114
+ writePgpmFiles(dbRows, opts);
115
+ }
88
116
  }
89
117
  else {
90
118
  console.log('No sql_actions found — skipping database module. Meta/service module will still be exported.');
@@ -186,7 +214,7 @@ ${META_COMMON_FOOTER}
186
214
  writePgpmPlan(metaPackage, opts);
187
215
  writePgpmFiles(metaPackage, opts);
188
216
  };
189
- export const exportMigrations = async ({ project, options, dbInfo, author, outdir, schema_names, extensionName, extensionDesc, metaExtensionName, metaExtensionDesc, prompter, argv, repoName, username, serviceOutdir, skipSchemaRenaming, excludeCategories, granularity }) => {
217
+ export const exportMigrations = async ({ project, options, dbInfo, author, outdir, schema_names, extensionName, extensionDesc, metaExtensionName, metaExtensionDesc, prompter, argv, repoName, username, serviceOutdir, skipSchemaRenaming, excludeCategories, granularity, partition }) => {
190
218
  for (let v = 0; v < dbInfo.database_ids.length; v++) {
191
219
  const databaseId = dbInfo.database_ids[v];
192
220
  await exportMigrationsToDisk({
@@ -209,7 +237,8 @@ export const exportMigrations = async ({ project, options, dbInfo, author, outdi
209
237
  serviceOutdir,
210
238
  skipSchemaRenaming,
211
239
  excludeCategories,
212
- granularity
240
+ granularity,
241
+ partition
213
242
  });
214
243
  }
215
244
  };
@@ -15,7 +15,7 @@
15
15
  * db_migrate.sql_actions -> sqlActions
16
16
  * column database_id -> databaseId
17
17
  */
18
- import { toCamelCase, toPascalCase, toSnakeCase, distinctPluralize, singularizeLast } from 'inflekt';
18
+ import { distinctPluralize, singularizeLast, toCamelCase, toPascalCase, toSnakeCase } from 'inflekt';
19
19
  import { lookupByGqlType } from './type-map';
20
20
  /**
21
21
  * Get the GraphQL query field name for a given Postgres table name.
package/esm/index.js CHANGED
@@ -1,11 +1,10 @@
1
1
  export * from './export-data';
2
- export * from './export-meta';
3
- export * from './export-migrations';
4
2
  export * from './export-graphql';
5
3
  export * from './export-graphql-meta';
6
- export { EXPORT_GRANULARITIES, isExportGranularity, restructureExportRows } from './restructure';
4
+ export * from './export-meta';
5
+ export * from './export-migrations';
6
+ export { DB_REQUIRED_EXTENSIONS, detectMissingModules, installMissingModules, makeReplacer, mapPgTypeToFieldType, META_COMMON_FOOTER, META_COMMON_HEADER, META_TABLE_CONFIG, META_TABLE_ORDER, META_TABLE_OVERRIDES, normalizeOutdir, preparePackage, SERVICE_REQUIRED_EXTENSIONS } from './export-utils';
7
7
  export { GraphQLClient } from './graphql-client';
8
- export { getGraphQLQueryName, getGraphQLTypeName, graphqlRowToPostgresRow, buildFieldsFragment, mapGraphQLTypeToFieldType, unwrapGraphQLType } from './graphql-naming';
9
- export { DB_REQUIRED_EXTENSIONS, SERVICE_REQUIRED_EXTENSIONS, META_COMMON_HEADER, META_COMMON_FOOTER, META_TABLE_ORDER, META_TABLE_CONFIG, META_TABLE_OVERRIDES, mapPgTypeToFieldType, makeReplacer, preparePackage, normalizeOutdir, detectMissingModules, installMissingModules } from './export-utils';
10
- export { PG_TYPE_MAP, lookupByPgUdt, lookupByGqlType } from './type-map';
8
+ export { buildFieldsFragment, getGraphQLQueryName, getGraphQLTypeName, graphqlRowToPostgresRow, mapGraphQLTypeToFieldType, unwrapGraphQLType } from './graphql-naming';
11
9
  export { intervalToPostgres, parsePgInterval } from './interval-utils';
10
+ export { lookupByGqlType, lookupByPgUdt, PG_TYPE_MAP } from './type-map';
@@ -7,9 +7,9 @@
7
7
  * Per Dan's guidance: "I would NOT do branching in those existing files.
8
8
  * I would make the GraphQL flow its entire own flow at first."
9
9
  */
10
- import { Inquirerer } from 'inquirerer';
11
10
  import { PgpmPackage } from '@pgpmjs/core';
12
- import { ExportGranularity } from './restructure';
11
+ import { ExportGranularity, PartitionConfig } from '@pgpmjs/transform';
12
+ import { Inquirerer } from 'inquirerer';
13
13
  import { Schema } from './export-utils';
14
14
  export interface ExportGraphQLOptions {
15
15
  project: PgpmPackage;
@@ -63,5 +63,12 @@ export interface ExportGraphQLOptions {
63
63
  * When omitted, sql_actions rows are written through unchanged.
64
64
  */
65
65
  granularity?: ExportGranularity;
66
+ /**
67
+ * Partition dial: split the exported database module into multiple pgpm
68
+ * packages per the config's rules (`partitionUnits` in `@pgpmjs/transform`).
69
+ * Cross-package dependencies become `<pkg>:<path>` requires. Throws
70
+ * `PartitionCycleError` on an unshippable partition.
71
+ */
72
+ partition?: PartitionConfig;
66
73
  }
67
- export declare const exportGraphQL: ({ project, metaEndpoint, migrateEndpoint, token, headers, migrateHeaders, databaseId, databaseName, schema_names, schemas, author, outdir, extensionName, extensionDesc, metaExtensionName, metaExtensionDesc, prompter, argv, repoName, username, serviceOutdir, skipSchemaRenaming, granularity }: ExportGraphQLOptions) => Promise<void>;
74
+ export declare const exportGraphQL: ({ project, metaEndpoint, migrateEndpoint, token, headers, migrateHeaders, databaseId, databaseName, schema_names, schemas, author, outdir, extensionName, extensionDesc, metaExtensionName, metaExtensionDesc, prompter, argv, repoName, username, serviceOutdir, skipSchemaRenaming, granularity, partition }: ExportGraphQLOptions) => Promise<void>;
package/export-graphql.js CHANGED
@@ -1,14 +1,23 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.exportGraphQL = void 0;
4
+ /**
5
+ * GraphQL-based export orchestrator.
6
+ *
7
+ * This is a standalone GraphQL export flow that mirrors export-migrations.ts
8
+ * but fetches all data via GraphQL queries instead of direct SQL.
9
+ *
10
+ * Per Dan's guidance: "I would NOT do branching in those existing files.
11
+ * I would make the GraphQL flow its entire own flow at first."
12
+ */
4
13
  const core_1 = require("@pgpmjs/core");
5
14
  const migrate_client_1 = require("@pgpmjs/migrate-client");
6
- const graphql_client_1 = require("./graphql-client");
15
+ const transform_1 = require("@pgpmjs/transform");
7
16
  const export_graphql_meta_1 = require("./export-graphql-meta");
8
- const graphql_naming_1 = require("./graphql-naming");
9
- const restructure_1 = require("./restructure");
10
17
  const export_utils_1 = require("./export-utils");
11
- 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 }) => {
18
+ const graphql_client_1 = require("./graphql-client");
19
+ const graphql_naming_1 = require("./graphql-naming");
20
+ 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 }) => {
12
21
  const normalizedOutdir = (0, export_utils_1.normalizeOutdir)(outdir);
13
22
  const svcOutdir = (0, export_utils_1.normalizeOutdir)(serviceOutdir || outdir);
14
23
  const name = extensionName;
@@ -87,28 +96,53 @@ const exportGraphQL = async ({ project, metaEndpoint, migrateEndpoint, token, he
87
96
  const dbExtensionDesc = extensionDesc || `${name} database schema for ${databaseName}`;
88
97
  if (sqlActionRows.length > 0) {
89
98
  const dbMissingResult = await (0, export_utils_1.detectMissingModules)(project, [...export_utils_1.DB_REQUIRED_EXTENSIONS], prompter, argv);
90
- const dbModuleDir = await (0, export_utils_1.preparePackage)({
91
- project,
92
- author,
93
- outdir: normalizedOutdir,
94
- name,
95
- description: dbExtensionDesc,
96
- extensions: [...export_utils_1.DB_REQUIRED_EXTENSIONS],
97
- prompter,
98
- repoName,
99
- username
100
- });
101
- if (dbMissingResult.shouldInstall) {
102
- await (0, export_utils_1.installMissingModules)(dbModuleDir, dbMissingResult.missingModules);
103
- }
104
99
  let dbRows = sqlActionRows;
105
100
  if (granularity) {
106
- const { rows, warnings } = await (0, restructure_1.restructureExportRows)(dbRows, granularity);
101
+ const { rows, warnings } = await (0, transform_1.restructureExportRows)(dbRows, granularity);
107
102
  dbRows = rows;
108
103
  warnings.forEach(warning => console.warn(`restructure (${granularity}): ${warning}`));
109
104
  }
110
- (0, core_1.writePgpmPlan)(dbRows, opts);
111
- (0, core_1.writePgpmFiles)(dbRows, opts);
105
+ if (partition) {
106
+ const { packages, warnings } = await (0, transform_1.partitionExportRows)(dbRows, partition);
107
+ warnings.forEach(warning => console.warn(`partition: ${warning}`));
108
+ for (const pkg of packages) {
109
+ const pkgDir = await (0, export_utils_1.preparePackage)({
110
+ project,
111
+ author,
112
+ outdir: normalizedOutdir,
113
+ name: pkg.name,
114
+ description: `${pkg.name} (partitioned from ${name})`,
115
+ extensions: [...export_utils_1.DB_REQUIRED_EXTENSIONS, ...pkg.requires],
116
+ prompter,
117
+ repoName,
118
+ username
119
+ });
120
+ if (dbMissingResult.shouldInstall) {
121
+ await (0, export_utils_1.installMissingModules)(pkgDir, dbMissingResult.missingModules);
122
+ }
123
+ const pkgOpts = { ...opts, name: pkg.name };
124
+ (0, core_1.writePgpmPlan)(pkg.rows, pkgOpts);
125
+ (0, core_1.writePgpmFiles)(pkg.rows, pkgOpts);
126
+ }
127
+ }
128
+ else {
129
+ const dbModuleDir = await (0, export_utils_1.preparePackage)({
130
+ project,
131
+ author,
132
+ outdir: normalizedOutdir,
133
+ name,
134
+ description: dbExtensionDesc,
135
+ extensions: [...export_utils_1.DB_REQUIRED_EXTENSIONS],
136
+ prompter,
137
+ repoName,
138
+ username
139
+ });
140
+ if (dbMissingResult.shouldInstall) {
141
+ await (0, export_utils_1.installMissingModules)(dbModuleDir, dbMissingResult.missingModules);
142
+ }
143
+ (0, core_1.writePgpmPlan)(dbRows, opts);
144
+ (0, core_1.writePgpmFiles)(dbRows, opts);
145
+ }
112
146
  }
113
147
  else {
114
148
  console.log('No sql_actions found. Skipping database module export.');
@@ -1,7 +1,7 @@
1
+ import { PgpmPackage } from '@pgpmjs/core';
2
+ import { ExportGranularity, PartitionConfig } from '@pgpmjs/transform';
1
3
  import { PgpmOptions } from '@pgpmjs/types';
2
4
  import { Inquirerer } from 'inquirerer';
3
- import { PgpmPackage } from '@pgpmjs/core';
4
- import { ExportGranularity } from './restructure';
5
5
  interface ExportOptions {
6
6
  project: PgpmPackage;
7
7
  options: PgpmOptions;
@@ -44,6 +44,13 @@ interface ExportOptions {
44
44
  * When omitted, sql_actions rows are written through unchanged.
45
45
  */
46
46
  granularity?: ExportGranularity;
47
+ /**
48
+ * Partition dial: split the exported database module into multiple pgpm
49
+ * packages per the config's rules (`partitionUnits` in `@pgpmjs/transform`).
50
+ * Cross-package dependencies become `<pkg>:<path>` requires. Throws
51
+ * `PartitionCycleError` on an unshippable partition.
52
+ */
53
+ partition?: PartitionConfig;
47
54
  }
48
- export declare const exportMigrations: ({ project, options, dbInfo, author, outdir, schema_names, extensionName, extensionDesc, metaExtensionName, metaExtensionDesc, prompter, argv, repoName, username, serviceOutdir, skipSchemaRenaming, excludeCategories, granularity }: ExportOptions) => Promise<void>;
55
+ export declare const exportMigrations: ({ project, options, dbInfo, author, outdir, schema_names, extensionName, extensionDesc, metaExtensionName, metaExtensionDesc, prompter, argv, repoName, username, serviceOutdir, skipSchemaRenaming, excludeCategories, granularity, partition }: ExportOptions) => Promise<void>;
49
56
  export {};
@@ -1,12 +1,12 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.exportMigrations = void 0;
4
- const pg_cache_1 = require("pg-cache");
5
4
  const core_1 = require("@pgpmjs/core");
5
+ const transform_1 = require("@pgpmjs/transform");
6
+ const pg_cache_1 = require("pg-cache");
6
7
  const export_meta_1 = require("./export-meta");
7
- const restructure_1 = require("./restructure");
8
8
  const export_utils_1 = require("./export-utils");
9
- const exportMigrationsToDisk = async ({ project, options, database, databaseId, databaseName, author, outdir, schema_names, extensionName, extensionDesc, metaExtensionName, metaExtensionDesc, prompter, argv, repoName, username, serviceOutdir, skipSchemaRenaming = false, excludeCategories, granularity }) => {
9
+ const exportMigrationsToDisk = async ({ project, options, database, databaseId, databaseName, author, outdir, schema_names, extensionName, extensionDesc, metaExtensionName, metaExtensionDesc, prompter, argv, repoName, username, serviceOutdir, skipSchemaRenaming = false, excludeCategories, granularity, partition }) => {
10
10
  const normalizedOutdir = (0, export_utils_1.normalizeOutdir)(outdir);
11
11
  // Use serviceOutdir for service module, defaulting to outdir if not provided
12
12
  const svcOutdir = (0, export_utils_1.normalizeOutdir)(serviceOutdir || outdir);
@@ -64,30 +64,58 @@ const exportMigrationsToDisk = async ({ project, options, database, databaseId,
64
64
  if (appRows.length > 0) {
65
65
  // Detect missing modules at workspace level and prompt user
66
66
  const dbMissingResult = await (0, export_utils_1.detectMissingModules)(project, [...export_utils_1.DB_REQUIRED_EXTENSIONS], prompter, argv);
67
- // Create/prepare the module directory
68
- const dbModuleDir = await (0, export_utils_1.preparePackage)({
69
- project,
70
- author,
71
- outdir: normalizedOutdir,
72
- name,
73
- description: dbExtensionDesc,
74
- extensions: [...export_utils_1.DB_REQUIRED_EXTENSIONS],
75
- prompter,
76
- repoName,
77
- username
78
- });
79
- // Install missing modules if user confirmed (now that module exists)
80
- if (dbMissingResult.shouldInstall) {
81
- await (0, export_utils_1.installMissingModules)(dbModuleDir, dbMissingResult.missingModules);
82
- }
83
67
  let dbRows = appRows;
84
68
  if (granularity) {
85
- const { rows, warnings } = await (0, restructure_1.restructureExportRows)(appRows, granularity);
69
+ const { rows, warnings } = await (0, transform_1.restructureExportRows)(appRows, granularity);
86
70
  dbRows = rows;
87
71
  warnings.forEach(warning => console.warn(`restructure (${granularity}): ${warning}`));
88
72
  }
89
- (0, core_1.writePgpmPlan)(dbRows, opts);
90
- (0, core_1.writePgpmFiles)(dbRows, opts);
73
+ if (partition) {
74
+ // Partition dial: split the database module into N packages. Each
75
+ // package requires its cross-package prerequisites as extensions so
76
+ // recursive deploys order them correctly.
77
+ const { packages, warnings } = await (0, transform_1.partitionExportRows)(dbRows, partition);
78
+ warnings.forEach(warning => console.warn(`partition: ${warning}`));
79
+ for (const pkg of packages) {
80
+ const pkgDir = await (0, export_utils_1.preparePackage)({
81
+ project,
82
+ author,
83
+ outdir: normalizedOutdir,
84
+ name: pkg.name,
85
+ description: `${pkg.name} (partitioned from ${name})`,
86
+ extensions: [...export_utils_1.DB_REQUIRED_EXTENSIONS, ...pkg.requires],
87
+ prompter,
88
+ repoName,
89
+ username
90
+ });
91
+ if (dbMissingResult.shouldInstall) {
92
+ await (0, export_utils_1.installMissingModules)(pkgDir, dbMissingResult.missingModules);
93
+ }
94
+ const pkgOpts = { ...opts, name: pkg.name };
95
+ (0, core_1.writePgpmPlan)(pkg.rows, pkgOpts);
96
+ (0, core_1.writePgpmFiles)(pkg.rows, pkgOpts);
97
+ }
98
+ }
99
+ else {
100
+ // Create/prepare the module directory
101
+ const dbModuleDir = await (0, export_utils_1.preparePackage)({
102
+ project,
103
+ author,
104
+ outdir: normalizedOutdir,
105
+ name,
106
+ description: dbExtensionDesc,
107
+ extensions: [...export_utils_1.DB_REQUIRED_EXTENSIONS],
108
+ prompter,
109
+ repoName,
110
+ username
111
+ });
112
+ // Install missing modules if user confirmed (now that module exists)
113
+ if (dbMissingResult.shouldInstall) {
114
+ await (0, export_utils_1.installMissingModules)(dbModuleDir, dbMissingResult.missingModules);
115
+ }
116
+ (0, core_1.writePgpmPlan)(dbRows, opts);
117
+ (0, core_1.writePgpmFiles)(dbRows, opts);
118
+ }
91
119
  }
92
120
  else {
93
121
  console.log('No sql_actions found — skipping database module. Meta/service module will still be exported.');
@@ -189,7 +217,7 @@ ${export_utils_1.META_COMMON_FOOTER}
189
217
  (0, core_1.writePgpmPlan)(metaPackage, opts);
190
218
  (0, core_1.writePgpmFiles)(metaPackage, opts);
191
219
  };
192
- const exportMigrations = async ({ project, options, dbInfo, author, outdir, schema_names, extensionName, extensionDesc, metaExtensionName, metaExtensionDesc, prompter, argv, repoName, username, serviceOutdir, skipSchemaRenaming, excludeCategories, granularity }) => {
220
+ const exportMigrations = async ({ project, options, dbInfo, author, outdir, schema_names, extensionName, extensionDesc, metaExtensionName, metaExtensionDesc, prompter, argv, repoName, username, serviceOutdir, skipSchemaRenaming, excludeCategories, granularity, partition }) => {
193
221
  for (let v = 0; v < dbInfo.database_ids.length; v++) {
194
222
  const databaseId = dbInfo.database_ids[v];
195
223
  await exportMigrationsToDisk({
@@ -212,7 +240,8 @@ const exportMigrations = async ({ project, options, dbInfo, author, outdir, sche
212
240
  serviceOutdir,
213
241
  skipSchemaRenaming,
214
242
  excludeCategories,
215
- granularity
243
+ granularity,
244
+ partition
216
245
  });
217
246
  }
218
247
  };
package/index.d.ts CHANGED
@@ -1,13 +1,11 @@
1
1
  export * from './export-data';
2
- export * from './export-meta';
3
- export * from './export-migrations';
4
2
  export * from './export-graphql';
5
3
  export * from './export-graphql-meta';
6
- export { EXPORT_GRANULARITIES, isExportGranularity, restructureExportRows } from './restructure';
7
- export type { ExportGranularity, RestructureExportRowsResult } from './restructure';
4
+ export * from './export-meta';
5
+ export * from './export-migrations';
6
+ export type { FieldType, MakeReplacerOptions, MetaExportTableEntry, MissingModulesResult, PreparePackageOptions, ReplacerResult, Schema, TableConfig } from './export-utils';
7
+ export { DB_REQUIRED_EXTENSIONS, detectMissingModules, installMissingModules, makeReplacer, mapPgTypeToFieldType, META_COMMON_FOOTER, META_COMMON_HEADER, META_TABLE_CONFIG, META_TABLE_ORDER, META_TABLE_OVERRIDES, normalizeOutdir, preparePackage, SERVICE_REQUIRED_EXTENSIONS } from './export-utils';
8
8
  export { GraphQLClient } from './graphql-client';
9
- export { getGraphQLQueryName, getGraphQLTypeName, graphqlRowToPostgresRow, buildFieldsFragment, mapGraphQLTypeToFieldType, unwrapGraphQLType, GraphQLTypeInfo } from './graphql-naming';
10
- export { DB_REQUIRED_EXTENSIONS, SERVICE_REQUIRED_EXTENSIONS, META_COMMON_HEADER, META_COMMON_FOOTER, META_TABLE_ORDER, META_TABLE_CONFIG, META_TABLE_OVERRIDES, mapPgTypeToFieldType, makeReplacer, preparePackage, normalizeOutdir, detectMissingModules, installMissingModules } from './export-utils';
11
- export type { FieldType, TableConfig, MetaExportTableEntry, Schema, MakeReplacerOptions, ReplacerResult, PreparePackageOptions, MissingModulesResult } from './export-utils';
12
- export { PG_TYPE_MAP, TypeMapEntry, lookupByPgUdt, lookupByGqlType } from './type-map';
9
+ export { buildFieldsFragment, getGraphQLQueryName, getGraphQLTypeName, graphqlRowToPostgresRow, GraphQLTypeInfo, mapGraphQLTypeToFieldType, unwrapGraphQLType } from './graphql-naming';
13
10
  export { intervalToPostgres, parsePgInterval, PgInterval } from './interval-utils';
11
+ export { lookupByGqlType, lookupByPgUdt, PG_TYPE_MAP, TypeMapEntry } from './type-map';
package/index.js CHANGED
@@ -14,43 +14,39 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
14
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
15
  };
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
- exports.parsePgInterval = exports.intervalToPostgres = exports.lookupByGqlType = exports.lookupByPgUdt = exports.PG_TYPE_MAP = exports.installMissingModules = exports.detectMissingModules = exports.normalizeOutdir = exports.preparePackage = exports.makeReplacer = exports.mapPgTypeToFieldType = exports.META_TABLE_OVERRIDES = exports.META_TABLE_CONFIG = exports.META_TABLE_ORDER = exports.META_COMMON_FOOTER = exports.META_COMMON_HEADER = exports.SERVICE_REQUIRED_EXTENSIONS = exports.DB_REQUIRED_EXTENSIONS = exports.unwrapGraphQLType = exports.mapGraphQLTypeToFieldType = exports.buildFieldsFragment = exports.graphqlRowToPostgresRow = exports.getGraphQLTypeName = exports.getGraphQLQueryName = exports.GraphQLClient = exports.restructureExportRows = exports.isExportGranularity = exports.EXPORT_GRANULARITIES = void 0;
17
+ exports.PG_TYPE_MAP = exports.lookupByPgUdt = exports.lookupByGqlType = exports.parsePgInterval = exports.intervalToPostgres = exports.unwrapGraphQLType = exports.mapGraphQLTypeToFieldType = exports.graphqlRowToPostgresRow = exports.getGraphQLTypeName = exports.getGraphQLQueryName = exports.buildFieldsFragment = exports.GraphQLClient = exports.SERVICE_REQUIRED_EXTENSIONS = exports.preparePackage = exports.normalizeOutdir = exports.META_TABLE_OVERRIDES = exports.META_TABLE_ORDER = exports.META_TABLE_CONFIG = exports.META_COMMON_HEADER = exports.META_COMMON_FOOTER = exports.mapPgTypeToFieldType = exports.makeReplacer = exports.installMissingModules = exports.detectMissingModules = exports.DB_REQUIRED_EXTENSIONS = void 0;
18
18
  __exportStar(require("./export-data"), exports);
19
- __exportStar(require("./export-meta"), exports);
20
- __exportStar(require("./export-migrations"), exports);
21
19
  __exportStar(require("./export-graphql"), exports);
22
20
  __exportStar(require("./export-graphql-meta"), exports);
23
- var restructure_1 = require("./restructure");
24
- Object.defineProperty(exports, "EXPORT_GRANULARITIES", { enumerable: true, get: function () { return restructure_1.EXPORT_GRANULARITIES; } });
25
- Object.defineProperty(exports, "isExportGranularity", { enumerable: true, get: function () { return restructure_1.isExportGranularity; } });
26
- Object.defineProperty(exports, "restructureExportRows", { enumerable: true, get: function () { return restructure_1.restructureExportRows; } });
21
+ __exportStar(require("./export-meta"), exports);
22
+ __exportStar(require("./export-migrations"), exports);
23
+ var export_utils_1 = require("./export-utils");
24
+ Object.defineProperty(exports, "DB_REQUIRED_EXTENSIONS", { enumerable: true, get: function () { return export_utils_1.DB_REQUIRED_EXTENSIONS; } });
25
+ Object.defineProperty(exports, "detectMissingModules", { enumerable: true, get: function () { return export_utils_1.detectMissingModules; } });
26
+ Object.defineProperty(exports, "installMissingModules", { enumerable: true, get: function () { return export_utils_1.installMissingModules; } });
27
+ Object.defineProperty(exports, "makeReplacer", { enumerable: true, get: function () { return export_utils_1.makeReplacer; } });
28
+ Object.defineProperty(exports, "mapPgTypeToFieldType", { enumerable: true, get: function () { return export_utils_1.mapPgTypeToFieldType; } });
29
+ Object.defineProperty(exports, "META_COMMON_FOOTER", { enumerable: true, get: function () { return export_utils_1.META_COMMON_FOOTER; } });
30
+ Object.defineProperty(exports, "META_COMMON_HEADER", { enumerable: true, get: function () { return export_utils_1.META_COMMON_HEADER; } });
31
+ Object.defineProperty(exports, "META_TABLE_CONFIG", { enumerable: true, get: function () { return export_utils_1.META_TABLE_CONFIG; } });
32
+ Object.defineProperty(exports, "META_TABLE_ORDER", { enumerable: true, get: function () { return export_utils_1.META_TABLE_ORDER; } });
33
+ Object.defineProperty(exports, "META_TABLE_OVERRIDES", { enumerable: true, get: function () { return export_utils_1.META_TABLE_OVERRIDES; } });
34
+ Object.defineProperty(exports, "normalizeOutdir", { enumerable: true, get: function () { return export_utils_1.normalizeOutdir; } });
35
+ Object.defineProperty(exports, "preparePackage", { enumerable: true, get: function () { return export_utils_1.preparePackage; } });
36
+ Object.defineProperty(exports, "SERVICE_REQUIRED_EXTENSIONS", { enumerable: true, get: function () { return export_utils_1.SERVICE_REQUIRED_EXTENSIONS; } });
27
37
  var graphql_client_1 = require("./graphql-client");
28
38
  Object.defineProperty(exports, "GraphQLClient", { enumerable: true, get: function () { return graphql_client_1.GraphQLClient; } });
29
39
  var graphql_naming_1 = require("./graphql-naming");
40
+ Object.defineProperty(exports, "buildFieldsFragment", { enumerable: true, get: function () { return graphql_naming_1.buildFieldsFragment; } });
30
41
  Object.defineProperty(exports, "getGraphQLQueryName", { enumerable: true, get: function () { return graphql_naming_1.getGraphQLQueryName; } });
31
42
  Object.defineProperty(exports, "getGraphQLTypeName", { enumerable: true, get: function () { return graphql_naming_1.getGraphQLTypeName; } });
32
43
  Object.defineProperty(exports, "graphqlRowToPostgresRow", { enumerable: true, get: function () { return graphql_naming_1.graphqlRowToPostgresRow; } });
33
- Object.defineProperty(exports, "buildFieldsFragment", { enumerable: true, get: function () { return graphql_naming_1.buildFieldsFragment; } });
34
44
  Object.defineProperty(exports, "mapGraphQLTypeToFieldType", { enumerable: true, get: function () { return graphql_naming_1.mapGraphQLTypeToFieldType; } });
35
45
  Object.defineProperty(exports, "unwrapGraphQLType", { enumerable: true, get: function () { return graphql_naming_1.unwrapGraphQLType; } });
36
- var export_utils_1 = require("./export-utils");
37
- Object.defineProperty(exports, "DB_REQUIRED_EXTENSIONS", { enumerable: true, get: function () { return export_utils_1.DB_REQUIRED_EXTENSIONS; } });
38
- Object.defineProperty(exports, "SERVICE_REQUIRED_EXTENSIONS", { enumerable: true, get: function () { return export_utils_1.SERVICE_REQUIRED_EXTENSIONS; } });
39
- Object.defineProperty(exports, "META_COMMON_HEADER", { enumerable: true, get: function () { return export_utils_1.META_COMMON_HEADER; } });
40
- Object.defineProperty(exports, "META_COMMON_FOOTER", { enumerable: true, get: function () { return export_utils_1.META_COMMON_FOOTER; } });
41
- Object.defineProperty(exports, "META_TABLE_ORDER", { enumerable: true, get: function () { return export_utils_1.META_TABLE_ORDER; } });
42
- Object.defineProperty(exports, "META_TABLE_CONFIG", { enumerable: true, get: function () { return export_utils_1.META_TABLE_CONFIG; } });
43
- Object.defineProperty(exports, "META_TABLE_OVERRIDES", { enumerable: true, get: function () { return export_utils_1.META_TABLE_OVERRIDES; } });
44
- Object.defineProperty(exports, "mapPgTypeToFieldType", { enumerable: true, get: function () { return export_utils_1.mapPgTypeToFieldType; } });
45
- Object.defineProperty(exports, "makeReplacer", { enumerable: true, get: function () { return export_utils_1.makeReplacer; } });
46
- Object.defineProperty(exports, "preparePackage", { enumerable: true, get: function () { return export_utils_1.preparePackage; } });
47
- Object.defineProperty(exports, "normalizeOutdir", { enumerable: true, get: function () { return export_utils_1.normalizeOutdir; } });
48
- Object.defineProperty(exports, "detectMissingModules", { enumerable: true, get: function () { return export_utils_1.detectMissingModules; } });
49
- Object.defineProperty(exports, "installMissingModules", { enumerable: true, get: function () { return export_utils_1.installMissingModules; } });
50
- var type_map_1 = require("./type-map");
51
- Object.defineProperty(exports, "PG_TYPE_MAP", { enumerable: true, get: function () { return type_map_1.PG_TYPE_MAP; } });
52
- Object.defineProperty(exports, "lookupByPgUdt", { enumerable: true, get: function () { return type_map_1.lookupByPgUdt; } });
53
- Object.defineProperty(exports, "lookupByGqlType", { enumerable: true, get: function () { return type_map_1.lookupByGqlType; } });
54
46
  var interval_utils_1 = require("./interval-utils");
55
47
  Object.defineProperty(exports, "intervalToPostgres", { enumerable: true, get: function () { return interval_utils_1.intervalToPostgres; } });
56
48
  Object.defineProperty(exports, "parsePgInterval", { enumerable: true, get: function () { return interval_utils_1.parsePgInterval; } });
49
+ var type_map_1 = require("./type-map");
50
+ Object.defineProperty(exports, "lookupByGqlType", { enumerable: true, get: function () { return type_map_1.lookupByGqlType; } });
51
+ Object.defineProperty(exports, "lookupByPgUdt", { enumerable: true, get: function () { return type_map_1.lookupByPgUdt; } });
52
+ Object.defineProperty(exports, "PG_TYPE_MAP", { enumerable: true, get: function () { return type_map_1.PG_TYPE_MAP; } });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pgpmjs/export",
3
- "version": "1.14.2",
3
+ "version": "1.15.0",
4
4
  "author": "Constructive <developers@constructive.io>",
5
5
  "description": "PGPM export tools for SQL and GraphQL database migration extraction",
6
6
  "main": "index.js",
@@ -41,22 +41,22 @@
41
41
  "@pgsql/types": "^18.0.0",
42
42
  "@types/pg": "^8.20.0",
43
43
  "makage": "^0.3.0",
44
- "pgsql-test": "^5.9.7"
44
+ "pgsql-test": "^5.9.8"
45
45
  },
46
46
  "dependencies": {
47
- "@constructive-io/query-builder": "^3.9.4",
48
- "@pgpmjs/core": "^7.21.2",
49
- "@pgpmjs/migrate-client": "^1.10.7",
47
+ "@constructive-io/query-builder": "^3.9.5",
48
+ "@pgpmjs/core": "^7.21.3",
49
+ "@pgpmjs/migrate-client": "^1.10.8",
50
50
  "@pgpmjs/naming-spec": "^0.1.2",
51
- "@pgpmjs/transform": "^0.18.0",
52
- "@pgpmjs/types": "^2.49.2",
53
- "csv-to-pg": "^4.9.3",
51
+ "@pgpmjs/transform": "^0.19.0",
52
+ "@pgpmjs/types": "^2.49.3",
53
+ "csv-to-pg": "^4.9.4",
54
54
  "glob": "^13.0.6",
55
55
  "inflekt": "^0.7.1",
56
56
  "inquirerer": "^4.9.1",
57
57
  "pg": "^8.21.0",
58
- "pg-cache": "^3.24.2",
58
+ "pg-cache": "^3.24.3",
59
59
  "pg-env": "^1.27.2"
60
60
  },
61
- "gitHead": "6d3e25dc5c9f93421dba49ddedf93239f87213a8"
61
+ "gitHead": "c20b6be96b39bf71c0e2df0f4b51e05deacd006b"
62
62
  }
@@ -1,53 +0,0 @@
1
- import { alterationPathFor } from '@pgpmjs/naming-spec';
2
- import { loadModule, restructureChanges } from '@pgpmjs/transform';
3
- export const EXPORT_GRANULARITIES = ['atomic', 'object', 'consolidated'];
4
- export const isExportGranularity = (value) => typeof value === 'string' && EXPORT_GRANULARITIES.includes(value);
5
- /**
6
- * Restructure exported migration rows to the target granularity.
7
- *
8
- * The rows' deploy SQL is flattened in export order, restructured, and
9
- * re-sliced into per-object changes whose paths come from the naming spec and
10
- * whose deps come from the statement graph. When two changes derive the same
11
- * path (the same object altered again), later occurrences get the spec's
12
- * alteration convention via `alterationPathFor` (monotonic per-parent
13
- * counter), with the parent added to their deps.
14
- *
15
- * Revert/verify scripts are not carried through from the original rows (the
16
- * restructured grouping no longer matches them one-to-one); instead each
17
- * change gets scripts generated from its own statements via
18
- * `revertFor`/`verifyFor` (drops in reverse topological order, one existence
19
- * check per created object).
20
- */
21
- export const restructureExportRows = async (rows, granularity) => {
22
- await loadModule();
23
- const { changes, warnings } = restructureChanges(rows.map(row => ({
24
- name: row.deploy,
25
- dependencies: row.deps ?? [],
26
- deploy: row.content
27
- })), { granularity });
28
- const counters = new Map();
29
- const taken = new Set();
30
- const restructured = changes.map(change => {
31
- let deploy = change.name;
32
- const deps = [...change.dependencies];
33
- if (taken.has(deploy)) {
34
- let n = (counters.get(change.name) ?? 0) + 1;
35
- while (taken.has(alterationPathFor(change.name, n)))
36
- n++;
37
- counters.set(change.name, n);
38
- deploy = alterationPathFor(change.name, n);
39
- if (!deps.includes(change.name))
40
- deps.push(change.name);
41
- }
42
- taken.add(deploy);
43
- return {
44
- name: deploy,
45
- deploy,
46
- deps,
47
- content: change.deploy,
48
- revert: change.revert,
49
- verify: change.verify
50
- };
51
- });
52
- return { rows: restructured, warnings };
53
- };
package/restructure.d.ts DELETED
@@ -1,33 +0,0 @@
1
- /**
2
- * Granularity dial for `pgpm export`: route exported sql_actions rows through
3
- * the dials pipeline (`restructureChanges` in `@pgpmjs/transform`) so change
4
- * paths are derived from the naming spec (`identityOf` + `pathFor`) and
5
- * `requires` come from the statement dependency graph instead of the
6
- * hand-chained deps recorded at action time.
7
- */
8
- import { PgpmRow } from '@pgpmjs/core';
9
- import { Granularity } from '@pgpmjs/transform';
10
- export type ExportGranularity = Granularity;
11
- export declare const EXPORT_GRANULARITIES: readonly ExportGranularity[];
12
- export declare const isExportGranularity: (value: unknown) => value is ExportGranularity;
13
- export interface RestructureExportRowsResult {
14
- rows: PgpmRow[];
15
- warnings: string[];
16
- }
17
- /**
18
- * Restructure exported migration rows to the target granularity.
19
- *
20
- * The rows' deploy SQL is flattened in export order, restructured, and
21
- * re-sliced into per-object changes whose paths come from the naming spec and
22
- * whose deps come from the statement graph. When two changes derive the same
23
- * path (the same object altered again), later occurrences get the spec's
24
- * alteration convention via `alterationPathFor` (monotonic per-parent
25
- * counter), with the parent added to their deps.
26
- *
27
- * Revert/verify scripts are not carried through from the original rows (the
28
- * restructured grouping no longer matches them one-to-one); instead each
29
- * change gets scripts generated from its own statements via
30
- * `revertFor`/`verifyFor` (drops in reverse topological order, one existence
31
- * check per created object).
32
- */
33
- export declare const restructureExportRows: (rows: PgpmRow[], granularity: ExportGranularity) => Promise<RestructureExportRowsResult>;
package/restructure.js DELETED
@@ -1,58 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.restructureExportRows = exports.isExportGranularity = exports.EXPORT_GRANULARITIES = void 0;
4
- const naming_spec_1 = require("@pgpmjs/naming-spec");
5
- const transform_1 = require("@pgpmjs/transform");
6
- exports.EXPORT_GRANULARITIES = ['atomic', 'object', 'consolidated'];
7
- const isExportGranularity = (value) => typeof value === 'string' && exports.EXPORT_GRANULARITIES.includes(value);
8
- exports.isExportGranularity = isExportGranularity;
9
- /**
10
- * Restructure exported migration rows to the target granularity.
11
- *
12
- * The rows' deploy SQL is flattened in export order, restructured, and
13
- * re-sliced into per-object changes whose paths come from the naming spec and
14
- * whose deps come from the statement graph. When two changes derive the same
15
- * path (the same object altered again), later occurrences get the spec's
16
- * alteration convention via `alterationPathFor` (monotonic per-parent
17
- * counter), with the parent added to their deps.
18
- *
19
- * Revert/verify scripts are not carried through from the original rows (the
20
- * restructured grouping no longer matches them one-to-one); instead each
21
- * change gets scripts generated from its own statements via
22
- * `revertFor`/`verifyFor` (drops in reverse topological order, one existence
23
- * check per created object).
24
- */
25
- const restructureExportRows = async (rows, granularity) => {
26
- await (0, transform_1.loadModule)();
27
- const { changes, warnings } = (0, transform_1.restructureChanges)(rows.map(row => ({
28
- name: row.deploy,
29
- dependencies: row.deps ?? [],
30
- deploy: row.content
31
- })), { granularity });
32
- const counters = new Map();
33
- const taken = new Set();
34
- const restructured = changes.map(change => {
35
- let deploy = change.name;
36
- const deps = [...change.dependencies];
37
- if (taken.has(deploy)) {
38
- let n = (counters.get(change.name) ?? 0) + 1;
39
- while (taken.has((0, naming_spec_1.alterationPathFor)(change.name, n)))
40
- n++;
41
- counters.set(change.name, n);
42
- deploy = (0, naming_spec_1.alterationPathFor)(change.name, n);
43
- if (!deps.includes(change.name))
44
- deps.push(change.name);
45
- }
46
- taken.add(deploy);
47
- return {
48
- name: deploy,
49
- deploy,
50
- deps,
51
- content: change.deploy,
52
- revert: change.revert,
53
- verify: change.verify
54
- };
55
- });
56
- return { rows: restructured, warnings };
57
- };
58
- exports.restructureExportRows = restructureExportRows;