@ductape/cli 0.4.0 → 0.4.2
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.
- package/dist/commands/component-actions.d.ts +1 -0
- package/dist/commands/component-actions.js +10 -0
- package/dist/commands/db-schema.js +12 -3
- package/dist/commands/db.js +1 -1
- package/dist/lib/db-types.d.ts +12 -0
- package/dist/lib/migration-files.d.ts +2 -0
- package/dist/lib/migration-files.js +38 -1
- package/package.json +1 -1
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
export type ComponentActionKind = 'graph' | 'vector';
|
|
2
2
|
export type ComponentActionVerb = 'create' | 'update' | 'get' | 'list' | 'delete' | 'execute';
|
|
3
|
+
export declare function assertComponentActionResult(kind: ComponentActionKind, verb: ComponentActionVerb, result: unknown): void;
|
|
3
4
|
export declare function runComponentActions(kind: ComponentActionKind, verb: ComponentActionVerb, opts: {
|
|
4
5
|
resource?: string;
|
|
5
6
|
action?: string;
|
|
@@ -1,6 +1,15 @@
|
|
|
1
1
|
import { readJsonBody } from '../lib/read-body.js';
|
|
2
2
|
import { getSdkProxy, requireSession } from '../lib/proxy/context.js';
|
|
3
3
|
import { printJson } from '../lib/output.js';
|
|
4
|
+
export function assertComponentActionResult(kind, verb, result) {
|
|
5
|
+
if (verb === 'get' && (result === null || result === undefined)) {
|
|
6
|
+
throw new Error(`${kind} action was not found in the canonical product catalogue`);
|
|
7
|
+
}
|
|
8
|
+
if (verb === 'execute' && result && typeof result === 'object' && result.success === false) {
|
|
9
|
+
const message = result.error ?? result.message;
|
|
10
|
+
throw new Error(`${kind} action execution failed${message ? `: ${String(message)}` : ''}`);
|
|
11
|
+
}
|
|
12
|
+
}
|
|
4
13
|
export async function runComponentActions(kind, verb, opts) {
|
|
5
14
|
const session = requireSession();
|
|
6
15
|
const body = readJsonBody(opts.file) ?? {};
|
|
@@ -41,5 +50,6 @@ export async function runComponentActions(kind, verb, opts) {
|
|
|
41
50
|
params = [{ product: session.project.product_tag, env: session.project.env_slug, ...body }];
|
|
42
51
|
}
|
|
43
52
|
const result = await getSdkProxy(session).execute(kind, method, params);
|
|
53
|
+
assertComponentActionResult(kind, verb, result);
|
|
44
54
|
printJson(result, Boolean(opts.json));
|
|
45
55
|
}
|
|
@@ -357,6 +357,10 @@ function adaptColumn(col) {
|
|
|
357
357
|
}
|
|
358
358
|
export async function runDbSchemaPush(opts) {
|
|
359
359
|
const session = requireSession();
|
|
360
|
+
const project = findProjectConfig();
|
|
361
|
+
const localEntry = project && fs.existsSync(getSchemaPath(project.dir))
|
|
362
|
+
? loadSchemaFile(project.dir).find((entry) => entry.db === opts.db)
|
|
363
|
+
: undefined;
|
|
360
364
|
const proxy = getDbProxy(session);
|
|
361
365
|
const envSlug = opts.env;
|
|
362
366
|
const productTag = session.project.product_tag;
|
|
@@ -382,12 +386,17 @@ export async function runDbSchemaPush(opts) {
|
|
|
382
386
|
for (const tableName of tableNames) {
|
|
383
387
|
try {
|
|
384
388
|
const tableSchema = await proxy.execute('schema.describe', [tableName], dbContext);
|
|
385
|
-
|
|
389
|
+
let cols = Array.isArray(tableSchema?.columns)
|
|
386
390
|
? tableSchema.columns
|
|
387
391
|
: Array.isArray(tableSchema) ? tableSchema : [];
|
|
388
392
|
if (cols.length === 0) {
|
|
389
|
-
|
|
390
|
-
|
|
393
|
+
const localDefinition = localEntry?.tables?.[tableName];
|
|
394
|
+
if (!localDefinition) {
|
|
395
|
+
console.warn(` [skip] ${tableName}: no live columns or code-first definition returned`);
|
|
396
|
+
continue;
|
|
397
|
+
}
|
|
398
|
+
cols = parseTableDef(tableName, localDefinition).fields;
|
|
399
|
+
console.log(` ${tableName}: using code-first schema because the collection is empty`);
|
|
391
400
|
}
|
|
392
401
|
const columns = cols
|
|
393
402
|
.filter((c) => c.name)
|
package/dist/commands/db.js
CHANGED
|
@@ -115,7 +115,7 @@ export async function runDbActions(verb, opts, extraArgs) {
|
|
|
115
115
|
if (!databaseTag) {
|
|
116
116
|
throw new Error('list requires --database <product_tag:database_tag>');
|
|
117
117
|
}
|
|
118
|
-
method = 'action.
|
|
118
|
+
method = 'action.list';
|
|
119
119
|
params = [databaseTag];
|
|
120
120
|
break;
|
|
121
121
|
}
|
package/dist/lib/db-types.d.ts
CHANGED
|
@@ -45,7 +45,19 @@ export interface ICreateIndexOp {
|
|
|
45
45
|
order?: 'asc' | 'desc';
|
|
46
46
|
}>;
|
|
47
47
|
unique?: boolean;
|
|
48
|
+
sparse?: boolean;
|
|
48
49
|
ifNotExists?: boolean;
|
|
50
|
+
sqlOptions?: {
|
|
51
|
+
method?: string;
|
|
52
|
+
where?: string;
|
|
53
|
+
include?: string[];
|
|
54
|
+
concurrent?: boolean;
|
|
55
|
+
};
|
|
56
|
+
mongoOptions?: {
|
|
57
|
+
expireAfterSeconds?: number;
|
|
58
|
+
partialFilterExpression?: Record<string, unknown>;
|
|
59
|
+
collation?: Record<string, unknown>;
|
|
60
|
+
};
|
|
49
61
|
}
|
|
50
62
|
export interface IDropIndexOp {
|
|
51
63
|
type: 'dropIndex';
|
|
@@ -1,4 +1,6 @@
|
|
|
1
1
|
import type { IMigration } from './db-types.js';
|
|
2
|
+
export declare function validateEquivalentMigrationIndexes(migrations: IMigration[]): void;
|
|
3
|
+
export declare function validateCanonicalMigrationOperations(migrations: IMigration[]): void;
|
|
2
4
|
export declare function getMigrationsDir(projectDir: string, dbTag: string): string;
|
|
3
5
|
export declare function loadMigrationFiles(projectDir: string, dbTag: string): IMigration[];
|
|
4
6
|
export declare function writeMigrationFile(projectDir: string, dbTag: string, migration: IMigration, counter: number): string;
|
|
@@ -1,6 +1,40 @@
|
|
|
1
1
|
import fs from 'node:fs';
|
|
2
2
|
import path from 'node:path';
|
|
3
3
|
import { PROJECT_CONFIG_DIR } from './config.js';
|
|
4
|
+
function stable(value) {
|
|
5
|
+
if (Array.isArray(value))
|
|
6
|
+
return `[${value.map(stable).join(',')}]`;
|
|
7
|
+
if (value && typeof value === 'object')
|
|
8
|
+
return `{${Object.entries(value).sort(([a], [b]) => a.localeCompare(b)).map(([key, nested]) => `${JSON.stringify(key)}:${stable(nested)}`).join(',')}}`;
|
|
9
|
+
return JSON.stringify(value);
|
|
10
|
+
}
|
|
11
|
+
export function validateEquivalentMigrationIndexes(migrations) {
|
|
12
|
+
const seen = new Map();
|
|
13
|
+
for (const migration of migrations)
|
|
14
|
+
migration.up.forEach((operation, operationIndex) => {
|
|
15
|
+
if (operation.type !== 'createIndex')
|
|
16
|
+
return;
|
|
17
|
+
const fingerprint = stable({ collection: operation.collection, fields: operation.fields.map((field) => ({ name: field.name, order: field.order ?? 'asc' })), unique: Boolean(operation.unique), sparse: Boolean(operation.sparse), sql: operation.sqlOptions ?? {}, mongo: operation.mongoOptions ?? {} });
|
|
18
|
+
const previous = seen.get(fingerprint);
|
|
19
|
+
if (previous && previous.name !== operation.name) {
|
|
20
|
+
throw new Error(`Equivalent index definitions use different names: migration "${previous.migration}" operation ${previous.operation + 1} creates "${previous.name}", while migration "${migration.tag}" operation ${operationIndex + 1} creates "${operation.name}".`);
|
|
21
|
+
}
|
|
22
|
+
seen.set(fingerprint, { migration: migration.tag, operation: operationIndex, name: operation.name });
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
export function validateCanonicalMigrationOperations(migrations) {
|
|
26
|
+
for (const migration of migrations) {
|
|
27
|
+
for (const [direction, operations] of [['up', migration.up], ['down', migration.down]]) {
|
|
28
|
+
operations.forEach((operation, index) => {
|
|
29
|
+
if (operation.type === 'createCollection' && !operation.name) {
|
|
30
|
+
const legacy = operation.collection;
|
|
31
|
+
throw new Error(`Migration "${migration.tag}" ${direction} operation ${index + 1} createCollection requires "name"` +
|
|
32
|
+
(typeof legacy === 'string' ? '; "collection" is not valid for createCollection' : ''));
|
|
33
|
+
}
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
}
|
|
4
38
|
export function getMigrationsDir(projectDir, dbTag) {
|
|
5
39
|
return path.join(projectDir, PROJECT_CONFIG_DIR, 'database', 'migrations', dbTag);
|
|
6
40
|
}
|
|
@@ -12,10 +46,13 @@ export function loadMigrationFiles(projectDir, dbTag) {
|
|
|
12
46
|
.readdirSync(dir)
|
|
13
47
|
.filter((f) => f.endsWith('.json'))
|
|
14
48
|
.sort();
|
|
15
|
-
|
|
49
|
+
const migrations = files.map((file) => {
|
|
16
50
|
const raw = JSON.parse(fs.readFileSync(path.join(dir, file), 'utf8'));
|
|
17
51
|
return raw;
|
|
18
52
|
});
|
|
53
|
+
validateCanonicalMigrationOperations(migrations);
|
|
54
|
+
validateEquivalentMigrationIndexes(migrations);
|
|
55
|
+
return migrations;
|
|
19
56
|
}
|
|
20
57
|
function makeTimestamp(counter) {
|
|
21
58
|
const now = new Date();
|
package/package.json
CHANGED