Skip to content

Commit f23c8fb

Browse files
authored
Merge pull request #501 from constructive-io/devin/1766785649-improve-export-ux
feat(pgpm): improve export command UX with smart defaults
2 parents c2e68f9 + ad96963 commit f23c8fb

File tree

2 files changed

+47
-25
lines changed

2 files changed

+47
-25
lines changed

pgpm/cli/src/commands/export.ts

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { exportMigrations,PgpmPackage } from '@pgpmjs/core';
22
import { getEnvOptions } from '@pgpmjs/env';
33
import { getGitConfigInfo } from '@pgpmjs/types';
4-
import { CLIOptions, Inquirerer, OptionValue } from 'inquirerer';
4+
import { CLIOptions, Inquirerer } from 'inquirerer';
55
import { resolve } from 'path';
66
import { getPgPool } from 'pg-cache';
77

@@ -52,18 +52,15 @@ export default async (
5252
AND datname !~ '^pg_';
5353
`);
5454

55-
let databases: OptionValue[];
56-
({ databases } = await prompter.prompt(argv, [
55+
const { databases: dbname } = await prompter.prompt(argv, [
5756
{
58-
type: 'checkbox',
57+
type: 'list',
5958
name: 'databases',
6059
message: 'Select a database',
6160
options: databasesResult.rows.map(row => row.datname),
6261
required: true
6362
}
64-
]));
65-
66-
const dbname = databases.filter(d=>d.selected).map(d=>d.value)[0];
63+
]);
6764
const selectedDb = await getPgPool({
6865
database: dbname
6966
});
@@ -72,26 +69,22 @@ export default async (
7269
SELECT id, name FROM collections_public.database;
7370
`);
7471

75-
let database_ids: OptionValue[];
76-
({ database_ids } = await prompter.prompt({} as any, [
72+
const { database_ids: selectedDatabaseName } = await prompter.prompt({} as any, [
7773
{
78-
type: 'checkbox',
74+
type: 'list',
7975
name: 'database_ids',
80-
message: 'Select database_id(s)',
76+
message: 'Select database_id',
8177
options: dbsResult.rows.map(db => db.name),
8278
required: true
8379
}
84-
]));
80+
]);
8581

86-
const selectedDatabaseNames = database_ids
87-
.filter(did => did.selected)
88-
.map(did => did.name);
82+
const selectedDatabase = dbsResult.rows.find(db => db.name === selectedDatabaseName);
8983

9084
const dbInfo = {
9185
dbname,
92-
database_ids: database_ids.map(did =>
93-
dbsResult.rows.find(db => db.name === did.name)!.id
94-
)
86+
databaseName: selectedDatabaseName,
87+
database_ids: [selectedDatabase!.id]
9588
};
9689

9790
const { author, extensionName, metaExtensionName } = await prompter.prompt(argv, [
@@ -106,14 +99,14 @@ export default async (
10699
type: 'text',
107100
name: 'extensionName',
108101
message: 'Extension name',
109-
default: selectedDatabaseNames[0] || dbname,
102+
default: selectedDatabaseName || dbname,
110103
required: true
111104
},
112105
{
113106
type: 'text',
114107
name: 'metaExtensionName',
115108
message: 'Meta extension name',
116-
default: 'svc',
109+
default: `${selectedDatabaseName || dbname}-service`,
117110
required: true
118111
}
119112
]);

pgpm/core/src/export/export-migrations.ts

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,37 +14,46 @@ interface ExportMigrationsToDiskOptions {
1414
options: PgpmOptions;
1515
database: string;
1616
databaseId: string;
17+
databaseName: string;
1718
author: string;
1819
outdir: string;
1920
schema_names: string[];
2021
extensionName?: string;
22+
extensionDesc?: string;
2123
metaExtensionName: string;
24+
metaExtensionDesc?: string;
2225
}
2326

2427
interface ExportOptions {
2528
project: PgpmPackage;
2629
options: PgpmOptions;
2730
dbInfo: {
2831
dbname: string;
32+
databaseName: string;
2933
database_ids: string[];
3034
};
3135
author: string;
3236
outdir: string;
3337
schema_names: string[];
3438
extensionName?: string;
39+
extensionDesc?: string;
3540
metaExtensionName: string;
41+
metaExtensionDesc?: string;
3642
}
3743

3844
const exportMigrationsToDisk = async ({
3945
project,
4046
options,
4147
database,
4248
databaseId,
49+
databaseName,
4350
author,
4451
outdir,
4552
schema_names,
4653
extensionName,
47-
metaExtensionName
54+
extensionDesc,
55+
metaExtensionName,
56+
metaExtensionDesc
4857
}: ExportMigrationsToDiskOptions): Promise<void> => {
4958
outdir = outdir + '/';
5059

@@ -93,12 +102,16 @@ const exportMigrationsToDisk = async ({
93102
author
94103
};
95104

105+
// Build description for the database extension package
106+
const dbExtensionDesc = extensionDesc || `${name} database schema for ${databaseName}`;
107+
96108
if (results?.rows?.length > 0) {
97109
await preparePackage({
98110
project,
99111
author,
100112
outdir,
101113
name,
114+
description: dbExtensionDesc,
102115
extensions: [
103116
'plpgsql',
104117
'uuid-ossp',
@@ -131,12 +144,16 @@ const exportMigrationsToDisk = async ({
131144

132145
meta = replacer(meta);
133146

147+
// Build description for the meta/service extension package
148+
const metaDesc = metaExtensionDesc || `${metaExtensionName} service utilities for managing domains, APIs, and services`;
149+
134150
await preparePackage({
135151
project,
136152
author,
137153
outdir,
138-
extensions: ['plpgsql', 'db-meta-schema', 'db-meta-modules'],
139-
name: metaExtensionName
154+
name: metaExtensionName,
155+
description: metaDesc,
156+
extensions: ['plpgsql', 'db-meta-schema', 'db-meta-modules']
140157
});
141158

142159
const metaReplacer = makeReplacer({
@@ -197,16 +214,21 @@ export const exportMigrations = async ({
197214
outdir,
198215
schema_names,
199216
extensionName,
200-
metaExtensionName
217+
extensionDesc,
218+
metaExtensionName,
219+
metaExtensionDesc
201220
}: ExportOptions): Promise<void> => {
202221
for (let v = 0; v < dbInfo.database_ids.length; v++) {
203222
const databaseId = dbInfo.database_ids[v];
204223
await exportMigrationsToDisk({
205224
project,
206225
options,
207226
extensionName,
227+
extensionDesc,
208228
metaExtensionName,
229+
metaExtensionDesc,
209230
database: dbInfo.dbname,
231+
databaseName: dbInfo.databaseName,
210232
databaseId,
211233
schema_names,
212234
author,
@@ -221,6 +243,7 @@ interface PreparePackageOptions {
221243
author: string;
222244
outdir: string;
223245
name: string;
246+
description: string;
224247
extensions: string[];
225248
}
226249

@@ -247,6 +270,7 @@ const preparePackage = async ({
247270
author,
248271
outdir,
249272
name,
273+
description,
250274
extensions
251275
}: PreparePackageOptions): Promise<void> => {
252276
const curDir = process.cwd();
@@ -258,9 +282,14 @@ const preparePackage = async ({
258282
if (!plan.length) {
259283
await project.initModule({
260284
name,
261-
description: name,
285+
description,
262286
author,
263287
extensions,
288+
answers: {
289+
moduleName: name,
290+
moduleDesc: description,
291+
access: 'restricted'
292+
}
264293
});
265294
} else {
266295
rmSync(path.resolve(sqitchDir, 'deploy'), { recursive: true, force: true });

0 commit comments

Comments
 (0)