GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Merge pull request #69 from cdsiats/bug/68 · stackpress/stackpress@825d82e

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • docs
    • cli-reference.md
  • packages
    • stackpress-schema
      • src/transform
        • helpers.ts
        • index.ts
      • tests
        • transform.test.ts
    • stackpress-server
      • src
        • Terminal.ts
      • tests
        • Terminal.test.ts
    • stackpress-sql
      • src
        • helpers.ts
        • scripts
          • migrate.ts
          • upgrade.ts
        • types.ts
      • tests
        • helpers.test.ts
        • upgrade.test.ts
  • specs
    • api
      • config-reference.md
    • guides
      • generate-and-build.md
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎docs/cli-reference.md‎

Copy file name to clipboardExpand all lines: docs/cli-reference.md
+6Lines changed: 6 additions & 0 deletions
  • Display the source diff
  • Display the rich diff
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,12 @@ stackpress push --b config -v
4545

4646
Creates or updates database structure from the current schema and database config.
4747

48+
- Preserves data for clear one-to-one column renames by issuing a rename instead
49+
of a drop-and-add migration.
50+
- Stops with an ambiguity error when multiple same-shape rename candidates exist.
51+
- Use `--force` only when you intentionally want to accept the destructive diff
52+
for an unresolved rename-like change.
53+
4854
## `populate`
4955

5056
```bash
Collapse file

‎packages/stackpress-schema/src/transform/helpers.ts‎

Copy file name to clipboardExpand all lines: packages/stackpress-schema/src/transform/helpers.ts
+95-1Lines changed: 95 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import path from 'node:path';
66
import type { Project, Directory } from 'ts-morph';
77
import Nest from '@stackpress/lib/Nest';
88
import { TemplateEngine, helpers } from '@stackpress/lib/Template';
9+
//stackpress-schema
10+
import type Schema from '../Schema.js';
911

1012
export const cwd = process.cwd();
1113

@@ -69,10 +71,102 @@ export function renderCode(template: string, data: Record<string, any> = {}) {
6971
return engine.render(template, data);
7072
};
7173

74+
/**
75+
* Remove stale generated schema files that no longer match the current schema.
76+
*/
77+
export async function pruneGeneratedSchemaFiles(root: string, schema: Schema) {
78+
//if the generated root does not exist yet, there is nothing to prune
79+
if (!fs.existsSync(root)) {
80+
return;
81+
}
82+
83+
//build the exact list of folders and files the current schema still expects
84+
const expectedDirectories = new Map<string, {
85+
columns: Set<string>,
86+
tests: Set<string>
87+
}>();
88+
const modelsAndFieldsets = [
89+
...schema.fieldsets.values(),
90+
...schema.models.values()
91+
];
92+
93+
for (const fieldset of modelsAndFieldsets) {
94+
//generated folders are keyed by the fieldset/model path name
95+
const dirname = fieldset.name.toPathName();
96+
//model-backed columns are generated elsewhere, so this pass only tracks
97+
//the direct column artifacts for the fieldset or model folder itself
98+
const columns = fieldset.columns.filter(
99+
column => !column.type.model
100+
);
101+
//record every column file that should still exist after regeneration
102+
const columnFiles = new Set(columns.map(
103+
column => column.name.toPathName('%sColumn.ts')
104+
).toArray());
105+
//only primitive columns get individual test files in this folder layout
106+
const testFiles = new Set(columns
107+
.filter(column => !column.type.fieldset)
108+
.map(column => column.name.toPathName('%sColumn.test.ts'))
109+
.toArray());
110+
expectedDirectories.set(dirname, { columns: columnFiles, tests: testFiles });
111+
}
112+
113+
for (const entry of await fsp.readdir(root, { withFileTypes: true })) {
114+
//this cleaner only manages generated directories
115+
if (!entry.isDirectory()) {
116+
continue;
117+
}
118+
//generated fieldset/model folders should mirror the live schema exactly.
119+
if (!expectedDirectories.has(entry.name)) {
120+
await fsp.rm(path.join(root, entry.name), { recursive: true, force: true });
121+
continue;
122+
}
123+
124+
//once the folder is confirmed valid, prune only the stale files inside it
125+
const expected = expectedDirectories.get(entry.name)!;
126+
const directory = path.join(root, entry.name);
127+
await pruneGeneratedGroup(
128+
path.join(directory, 'columns'),
129+
expected.columns,
130+
new Set([ 'index.ts' ])
131+
);
132+
await pruneGeneratedGroup(
133+
path.join(directory, 'tests', 'columns'),
134+
expected.tests,
135+
new Set()
136+
);
137+
}
138+
};
139+
140+
/**
141+
* Remove generated files from one folder when they are no longer expected.
142+
*/
143+
async function pruneGeneratedGroup(
144+
directory: string,
145+
expectedFiles: Set<string>,
146+
preservedFiles: Set<string>
147+
) {
148+
//skip missing folders because generation may not have created them yet
149+
if (!fs.existsSync(directory)) {
150+
return;
151+
}
152+
153+
for (const entry of await fsp.readdir(directory, { withFileTypes: true })) {
154+
//only generated files are deleted here, not nested directories
155+
if (!entry.isFile()) {
156+
continue;
157+
}
158+
//keep aggregate generated files like index.ts, but prune stale column artifacts.
159+
if (preservedFiles.has(entry.name) || expectedFiles.has(entry.name)) {
160+
continue;
161+
}
162+
await fsp.rm(path.join(directory, entry.name), { force: true });
163+
}
164+
}
165+
72166
export function savePackageJsonNest(pwd: string, nest: Nest) {
73167
if (!fs.existsSync(pwd)) {
74168
fs.mkdirSync(pwd, { recursive: true });
75169
}
76170
const filepath = path.resolve(pwd, 'package.json');
77171
fs.writeFileSync(filepath, JSON.stringify(nest.get(), null, 2));
78-
}
172+
}
Collapse file

‎packages/stackpress-schema/src/transform/index.ts‎

Copy file name to clipboardExpand all lines: packages/stackpress-schema/src/transform/index.ts
+6-1Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,10 @@ import {
1515
import generateEnums from './enums.js';
1616
import generatePackage from './package.js';
1717
import generateSchemaTests from './tests/schema.js';
18-
import { loadProjectFile } from './helpers.js';
18+
import {
19+
loadProjectFile,
20+
pruneGeneratedSchemaFiles
21+
} from './helpers.js';
1922

2023
export default async function generate(props: ClientPluginProps) {
2124
//------------------------------------------------------------------//
@@ -27,6 +30,8 @@ export default async function generate(props: ClientPluginProps) {
2730
const loader = server.loader;
2831
const revisionPath = server.config.path<string>('client.revisions');
2932
const packageName = server.config.path('client.package', 'stackpress-client');
33+
//prune stale generated files first so renamed fields do not leave orphaned output.
34+
await pruneGeneratedSchemaFiles(directory.getPath(), schema);
3035

3136
//------------------------------------------------------------------//
3237
// 2. Generators
Collapse file

‎packages/stackpress-schema/tests/transform.test.ts‎

Copy file name to clipboard
+70Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
//node
2+
import fsp from 'node:fs/promises';
3+
import os from 'node:os';
4+
import path from 'node:path';
5+
//tests
6+
import { describe, it } from 'mocha';
7+
import { expect } from 'chai';
8+
//stackpress-schema
9+
import Schema from '../src/Schema.js';
10+
import { pruneGeneratedSchemaFiles } from '../src/transform/helpers.js';
11+
12+
describe('schema/transform/helpers', () => {
13+
it('should remove stale generated files left behind by renamed fields', async () => {
14+
const root = await fsp.mkdtemp(path.join(os.tmpdir(), 'stackpress-schema-'));
15+
const articleColumns = path.join(root, 'Article', 'columns');
16+
const articleTestColumns = path.join(root, 'Article', 'tests', 'columns');
17+
const profileColumns = path.join(root, 'Profile', 'columns');
18+
19+
try {
20+
await fsp.mkdir(articleColumns, { recursive: true });
21+
await fsp.mkdir(articleTestColumns, { recursive: true });
22+
await fsp.mkdir(profileColumns, { recursive: true });
23+
await fsp.writeFile(path.join(articleColumns, 'SummaryColumn.ts'), '');
24+
await fsp.writeFile(path.join(articleColumns, 'SeoSummaryColumn.ts'), '');
25+
await fsp.writeFile(path.join(articleColumns, 'index.ts'), '');
26+
await fsp.writeFile(path.join(articleTestColumns, 'SummaryColumn.test.ts'), '');
27+
await fsp.writeFile(path.join(articleTestColumns, 'SeoSummaryColumn.test.ts'), '');
28+
await fsp.writeFile(path.join(profileColumns, 'NameColumn.ts'), '');
29+
30+
const schema = Schema.make({
31+
model: {
32+
Article: {
33+
name: 'Article',
34+
mutable: true,
35+
attributes: {},
36+
columns: [
37+
{
38+
name: 'seoSummary',
39+
type: 'String',
40+
required: false,
41+
multiple: false,
42+
attributes: {}
43+
}
44+
]
45+
}
46+
}
47+
});
48+
49+
await pruneGeneratedSchemaFiles(root, schema);
50+
51+
expect(await pathExists(path.join(articleColumns, 'SeoSummaryColumn.ts'))).to.equal(true);
52+
expect(await pathExists(path.join(articleColumns, 'SummaryColumn.ts'))).to.equal(false);
53+
expect(await pathExists(path.join(articleColumns, 'index.ts'))).to.equal(true);
54+
expect(await pathExists(path.join(articleTestColumns, 'SeoSummaryColumn.test.ts'))).to.equal(true);
55+
expect(await pathExists(path.join(articleTestColumns, 'SummaryColumn.test.ts'))).to.equal(false);
56+
expect(await pathExists(path.join(root, 'Profile'))).to.equal(false);
57+
} finally {
58+
await fsp.rm(root, { recursive: true, force: true });
59+
}
60+
});
61+
});
62+
63+
async function pathExists(filepath: string) {
64+
try {
65+
await fsp.stat(filepath);
66+
return true;
67+
} catch {
68+
return false;
69+
}
70+
}
Collapse file

‎packages/stackpress-server/src/Terminal.ts‎

Copy file name to clipboardExpand all lines: packages/stackpress-server/src/Terminal.ts
+7-1Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ export default class StackpressTerminal extends Terminal {
99
public readonly server: Server<any, any, any>;
1010
//whether to show output
1111
public readonly verbose: boolean;
12+
//whether to bypass destructive safety checks
13+
public readonly force: boolean;
1214
//the config pathname
1315
public readonly config: string|null;
1416

@@ -35,6 +37,10 @@ export default class StackpressTerminal extends Terminal {
3537
this.server = server;
3638
//set flags
3739
this.verbose = this.expect<boolean>([ 'verbose', 'v' ], false);
40+
//handle both the parser-based flag value and bare boolean CLI usage.
41+
this.force = args.includes('--force')
42+
|| args.includes('-f')
43+
|| this.expect<boolean>([ 'force', 'f' ], false);
3844
this.config = this.expect<string|null>([ 'bootstrap', 'b' ], null);
3945
//register terminal plugin
4046
this.server.register('terminal', this);
@@ -89,4 +95,4 @@ export default class StackpressTerminal extends Terminal {
8995
return status;
9096
}
9197
}
92-
}
98+
}
Collapse file

‎packages/stackpress-server/tests/Terminal.test.ts‎

Copy file name to clipboardExpand all lines: packages/stackpress-server/tests/Terminal.test.ts
+2-4Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,11 @@ describe('server/Terminal', () => {
1919
}
2020
};
2121

22-
const terminal = new Terminal(
23-
[ 'emit', '-v' ],
24-
server as any
25-
);
22+
const terminal = new Terminal([ 'emit', '-v', '--force' ], server as any);
2623

2724
expect(terminal.server).to.equal(server);
2825
expect(terminal.verbose).to.equal(true);
26+
expect(terminal.force).to.equal(true);
2927
expect(terminal.config).to.equal(null);
3028
expect(terminal.brand).to.equal('Stackpress');
3129
expect(terminal.cwd).to.equal('/workspace/app');

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.