@kronor/dtv 9.4.1 → 9.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.
@@ -58,16 +58,21 @@ export function registerDefinitionsCommand(program) {
58
58
  });
59
59
  withCommonOptions(definitions
60
60
  .command('check')
61
- .description('Fail when a view definition changed without a migration, or a migration is incomplete'))
62
- .action(async (opts) => {
61
+ .description('Fail when a view definition changed without a migration, or a migration is incomplete')
62
+ .argument('[definition]', 'Only this definition'))
63
+ .action(async (definition, opts) => {
63
64
  try {
64
- const report = await withProject(opts, (project, options) => runCheck(options, project));
65
+ const { report, views } = await withProject(opts, async (project, options) => ({ report: await runCheck({ ...options, definition }, project), views: Object.keys(project.definitions).length }));
65
66
  report.warnings.forEach(warning => console.warn(`warning: ${warning}`));
66
67
  report.problems.forEach(problem => console.error(problem));
67
68
  if (report.problems.length > 0)
68
69
  process.exitCode = 1;
69
70
  else
70
- console.log('View migrations are complete.');
71
+ console.log(definition !== undefined
72
+ ? `${definition}: matches its latest snapshot, and its migrations cover every change between snapshots.`
73
+ : views === 1
74
+ ? '1 view matches its latest snapshot, and its migrations cover every change between snapshots.'
75
+ : `${views} views match their latest snapshots, and their migrations cover every change between snapshots.`);
71
76
  }
72
77
  catch (e) {
73
78
  fail(e);
@@ -67,6 +67,11 @@ function operatorsOf(filter) {
67
67
  function controlOperators(control) {
68
68
  return control.type === 'customOperator' ? [...control.operators, ...controlOperators(control.valueControl)] : [];
69
69
  }
70
+ /** `from`'s values that `to` does not have. */
71
+ function removedValues(from, to) {
72
+ return from.filter(value => !to.some(other => deepEqual(other, value)));
73
+ }
74
+ const describeValues = (values) => values.map(value => JSON.stringify(value)).join(', ');
70
75
  /* The operations of a migration */
71
76
  /** The operations a migration from `from` to `to` needs, as far as the diff can tell. */
72
77
  export function generateOps(from, to) {
@@ -121,6 +126,8 @@ export function generateOps(from, to) {
121
126
  `Removed: m.drop(${quote(removed)})`
122
127
  ]));
123
128
  }
129
+ /** Filters whose stored state is reset anyway: their item values do not matter. */
130
+ const reset = new Set();
124
131
  for (const filterId of Object.keys(from.filters)) {
125
132
  const toFilter = to.filters[filterId];
126
133
  if (toFilter === undefined || deepEqual(from.filters[filterId], toFilter))
@@ -128,6 +135,7 @@ export function generateOps(from, to) {
128
135
  const change = { structure: null, operatorsChanged: false, meaning: [] };
129
136
  compareFilters(from.filters[filterId], toFilter, change);
130
137
  if (change.structure !== null) {
138
+ reset.add(filterId);
131
139
  ops.push(op(`m.reset(${quote(filterId)})`, [
132
140
  `Stored state of "${filterId}" no longer fits: ${change.structure}.`,
133
141
  `To carry it over instead: m.custom(${quote(filterId)}, ${quote(filterId)}, state => …)`
@@ -157,6 +165,31 @@ export function generateOps(from, to) {
157
165
  ]));
158
166
  }
159
167
  }
168
+ // Item values a filter no longer has: what is stored for it may be one of them. Compared
169
+ // where both snapshots have the filter's items, as `check` does.
170
+ for (const [filterId, values] of Object.entries(from.items ?? {})) {
171
+ const renamed = renames.get(filterId);
172
+ const target = renamed ?? filterId;
173
+ const toValues = to.items?.[target];
174
+ if (toValues === undefined || to.filters[target] === undefined || reset.has(filterId))
175
+ continue;
176
+ const removed = removedValues(values, toValues);
177
+ if (removed.length === 0)
178
+ continue;
179
+ if (renamed !== undefined) {
180
+ ops.push(todo(filterId, `renamed to "${renamed}", and its items no longer have ${describeValues(removed)}`, [
181
+ `Rename it and map the values, each to one of ${describeValues(toValues)}, or to DSL.RESET to reset it:`,
182
+ `m.custom(${quote(filterId)}, ${quote(renamed)}, state => …) in place of the m.renameFilter above`
183
+ ]));
184
+ continue;
185
+ }
186
+ const kept = values.length > removed.length;
187
+ ops.push(todo(filterId, `its items no longer have ${describeValues(removed)}`, [
188
+ `Map each to one of ${describeValues(toValues)}, or to DSL.DROP to clear it:`,
189
+ `m.mapValues(${quote(filterId)}, value => ${removed.map(value => `value === ${toLiteral(value)} ? …`).join(' : ')} : ${kept ? 'value' : 'DSL.DROP'})`,
190
+ `Otherwise: m.reset(${quote(filterId)})`
191
+ ]));
192
+ }
160
193
  const removedColumns = Object.keys(from.columns).filter(id => to.columns[id] === undefined);
161
194
  const addedColumns = Object.keys(to.columns).filter(id => from.columns[id] === undefined);
162
195
  for (const removed of removedColumns) {
@@ -43,11 +43,23 @@ export function resolveDefinitions(built, project) {
43
43
  }
44
44
  return { definitions, problems };
45
45
  }
46
- /** The projection of a definition: its instances' filters (which agree) and all their columns. */
46
+ /**
47
+ * The projection of a definition: its instances' filters (which agree), all their columns,
48
+ * and the items that are the same in all of them.
49
+ */
47
50
  export function projectDefinition(definition, id, revision, project) {
48
51
  const [first, ...rest] = definition.instances.map(view => project(view, id, revision));
49
- return rest.reduce((merged, projection) => ({
52
+ const columns = rest.reduce((merged, projection) => ({
50
53
  ...merged,
51
- columns: { ...merged.columns, ...Object.fromEntries(Object.entries(projection.columns).filter(([column]) => merged.columns[column] === undefined)) }
52
- }), first);
54
+ ...Object.fromEntries(Object.entries(projection.columns).filter(([column]) => merged[column] === undefined))
55
+ }), first.columns);
56
+ const items = Object.fromEntries(Object.entries(first.items ?? {})
57
+ .filter(([filterId, values]) => rest.every(projection => deepEqual(projection.items?.[filterId], values))));
58
+ return {
59
+ definition: first.definition,
60
+ revision: first.revision,
61
+ filters: first.filters,
62
+ columns,
63
+ ...(Object.keys(items).length > 0 ? { items } : {})
64
+ };
53
65
  }
@@ -3,6 +3,12 @@ import * as path from 'node:path';
3
3
  import { deepEqual, generateOps, migrationFile, MIGRATION_FILE, renderIndex, renderMigration, renderSnapshot, snapshotFile } from './generate.js';
4
4
  import { renameMigrationsImport, wireMigrations } from './wire.js';
5
5
  import { MIGRATIONS_FOLDER } from './types.js';
6
+ /** Throws unless the project has the definition `definition`. */
7
+ function requireDefinition(project, definition) {
8
+ if (project.definitions[definition] === undefined) {
9
+ throw new Error(`No view definition "${definition}"; there are ${Object.keys(project.definitions).join(', ')}`);
10
+ }
11
+ }
6
12
  async function loadChains(project) {
7
13
  const folders = await project.findChainFolders();
8
14
  return new Map(await Promise.all(folders.map(async (folder) => [folder, await project.loadChain(folder)])));
@@ -26,8 +32,16 @@ async function locate(options, project, chains, definition, view) {
26
32
  const parent = source === null ? path.resolve(options.cwd, options.migrationsDir) : path.join(path.dirname(source), MIGRATIONS_FOLDER);
27
33
  return { kind: 'unwired', folder: path.join(parent, definition), chain: null };
28
34
  }
35
+ /**
36
+ * Whether the definition is as its latest snapshot has it. A snapshot without items was
37
+ * written before snapshots had them, or when no filter's items were fixed: its items are not
38
+ * compared (`runMigrate` adds them).
39
+ */
29
40
  function sameDefinition(live, latest) {
30
- return live.definition === latest.definition && deepEqual(live.filters, latest.filters) && deepEqual(live.columns, latest.columns);
41
+ return live.definition === latest.definition
42
+ && deepEqual(live.filters, latest.filters)
43
+ && deepEqual(live.columns, latest.columns)
44
+ && (latest.items === undefined || deepEqual(live.items, latest.items));
31
45
  }
32
46
  /** The migration revisions a folder has files for, oldest first. */
33
47
  async function migrationRevisions(folder) {
@@ -80,9 +94,8 @@ async function writeFile(file, content) {
80
94
  */
81
95
  export async function runMigrate(options, project) {
82
96
  const definitions = options.definition === undefined ? Object.keys(project.definitions) : [options.definition];
83
- if (options.definition !== undefined && project.definitions[options.definition] === undefined) {
84
- throw new Error(`No view definition "${options.definition}"; there are ${Object.keys(project.definitions).join(', ')}`);
85
- }
97
+ if (options.definition !== undefined)
98
+ requireDefinition(project, options.definition);
86
99
  if ((options.manual !== undefined || options.renamedFrom !== undefined) && options.definition === undefined) {
87
100
  throw new Error('--manual and --renamed-from need the definition they are for');
88
101
  }
@@ -117,6 +130,8 @@ export async function runMigrate(options, project) {
117
130
  let todos = 0;
118
131
  /** Definitions without changes; counted rather than listed when all definitions are snapshotted. */
119
132
  let upToDate = 0;
133
+ /** Definitions whose latest snapshot got the items it was written without. */
134
+ let itemsAdded = 0;
120
135
  for (const definition of definitions) {
121
136
  const view = project.definitions[definition].instances[0];
122
137
  const renamedChain = renamedFolder === null ? null : chains.get(renamedFolder) ?? null;
@@ -139,6 +154,14 @@ export async function runMigrate(options, project) {
139
154
  const latest = chain.latest;
140
155
  const revision = latest.revision + 1;
141
156
  const live = project.projectDefinition(definition, revision);
157
+ if (sameDefinition(live, latest) && latest.items === undefined && live.items !== undefined && options.manual === undefined) {
158
+ // Items say nothing about what was stored before: no revision for them.
159
+ const file = path.join(folder, snapshotFile(latest.revision));
160
+ if (await fs.access(file).then(() => true, () => false)) {
161
+ await writeFile(file, renderSnapshot({ ...latest, items: live.items }, options.dtvImport));
162
+ itemsAdded++;
163
+ }
164
+ }
142
165
  if (sameDefinition(live, latest) && options.manual === undefined) {
143
166
  // Only worth a line of its own when asked for, or when it is wired up now.
144
167
  if (options.definition === undefined && located.kind !== 'unwired') {
@@ -171,6 +194,8 @@ export async function runMigrate(options, project) {
171
194
  if (located.kind === 'unwired')
172
195
  (await wireUp(options, project, definition, folder)).forEach(options.log);
173
196
  }
197
+ if (itemsAdded > 0)
198
+ options.log(`${itemsAdded} ${itemsAdded === 1 ? 'view' : 'views'}: added the values of fixed dropdown and multiselect items to the latest snapshot`);
174
199
  if (upToDate > 0)
175
200
  options.log(`${upToDate} ${upToDate === 1 ? 'view' : 'views'} up to date`);
176
201
  return { todos, problems: project.problems };
@@ -185,8 +210,13 @@ export async function runCheck(options, project) {
185
210
  const warnings = [];
186
211
  const chains = await loadChains(project);
187
212
  const used = new Set();
188
- problems.push(...project.problems);
213
+ if (options.definition !== undefined)
214
+ requireDefinition(project, options.definition);
215
+ else
216
+ problems.push(...project.problems);
189
217
  for (const [definition, { instances: [view] }] of Object.entries(project.definitions)) {
218
+ if (options.definition !== undefined && definition !== options.definition)
219
+ continue;
190
220
  const located = await locate(options, project, chains, definition, view);
191
221
  if (located.kind === 'problem') {
192
222
  problems.push(located.message);
@@ -215,7 +245,7 @@ export async function runCheck(options, project) {
215
245
  }
216
246
  }
217
247
  for (const folder of chains.keys()) {
218
- if (!used.has(folder)) {
248
+ if (options.definition === undefined && !used.has(folder)) {
219
249
  warnings.push(`${relative(options, folder)}: migrations no view uses (renamed? \`dtv definitions snapshot <new id> --renamed-from ${path.basename(folder)}\`)`);
220
250
  }
221
251
  }