Skip to content

Commit 8e58c9f

Browse files
committed
fixed export metaschema handler
1 parent fa04d91 commit 8e58c9f

1 file changed

Lines changed: 34 additions & 4 deletions

File tree

functions/export-metaschema/handler.ts

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type { PgpmFunctionContext, PgpmFunctionHandler } from '@constructive-io/fn-pgpm-runtime';
22
import { DEFAULT_DATABASE_NAME } from '@constructive-io/fn-core';
33
import { exportMigrations } from '@pgpmjs/core';
4-
import { getPgPool } from 'pg-cache';
4+
import { getPgPool, pgCache } from 'pg-cache';
55
import { resolve } from 'path';
66

77
type ExportMetaschemaParams = {
@@ -13,6 +13,8 @@ type ExportMetaschemaParams = {
1313
schema_names?: string[];
1414
outdir?: string;
1515
skipSchemaRenaming?: boolean;
16+
username?: string;
17+
repoName?: string;
1618
};
1719

1820
const handler: PgpmFunctionHandler<ExportMetaschemaParams> = async (
@@ -48,6 +50,24 @@ const handler: PgpmFunctionHandler<ExportMetaschemaParams> = async (
4850
const databaseName = targetRow.name;
4951
const database_ids = [targetRow.id];
5052

53+
// Check that sql_actions exist for this database before exporting
54+
const actionsResult = await pgPool.query(
55+
'SELECT count(*)::int AS cnt FROM db_migrate.sql_actions WHERE database_id = $1',
56+
[database_ids[0]]
57+
);
58+
const actionCount = actionsResult.rows[0]?.cnt ?? 0;
59+
60+
if (actionCount === 0) {
61+
log.info('[export-metaschema] No sql_actions found, nothing to export', {
62+
databaseName,
63+
database_id: database_ids[0]
64+
});
65+
return {
66+
complete: false,
67+
reason: `No sql_actions found for database '${databaseName}' (${database_ids[0]}). The database may have been deployed from pre-built packages.`
68+
};
69+
}
70+
5171
// Discover schemas if not provided
5272
let schema_names = params.schema_names;
5373
if (!schema_names?.length) {
@@ -65,13 +85,17 @@ const handler: PgpmFunctionHandler<ExportMetaschemaParams> = async (
6585
const author = params.author || 'Constructive <developers@constructive.io>';
6686
const extensionName = params.extensionName || databaseName;
6787
const metaExtensionName = params.metaExtensionName || `${databaseName}-service`;
88+
// Default username/repoName to avoid interactive prompts from scaffoldTemplate
89+
const username = params.username || 'constructive-io';
90+
const repoName = params.repoName || extensionName;
6891

6992
log.info('[export-metaschema] Starting export', {
7093
dbname,
7194
databaseName,
7295
database_ids,
7396
extensionName,
74-
schema_names
97+
schema_names,
98+
actionCount
7599
});
76100

77101
project.ensureWorkspace();
@@ -92,12 +116,18 @@ const handler: PgpmFunctionHandler<ExportMetaschemaParams> = async (
92116
schema_names,
93117
extensionName,
94118
metaExtensionName,
119+
username,
120+
repoName,
95121
skipSchemaRenaming: params.skipSchemaRenaming
96122
});
97123

98-
log.info('[export-metaschema] Export complete');
124+
// exportMigrationsToDisk calls pgPool.end() which kills the cached pool.
125+
// Evict the dead pool from pg-cache so the next request gets a fresh one.
126+
pgCache.delete(dbname);
127+
128+
log.info('[export-metaschema] Export complete', { outdir });
99129

100-
return { complete: true };
130+
return { complete: true, outdir, extensionName, metaExtensionName, actionCount };
101131
};
102132

103133
export default handler;

0 commit comments

Comments
 (0)