GeezerVison is showing github.com — Merge pull request #69 from cdsiats/bug/68 · stackpress/stackpress@825d82e
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file 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 number | Diff line number | Diff line change |
|---|
@@ -45,6 +45,12 @@ stackpress push --b config -v | 45 | 45 |
| 46 | 46 | Creates or updates database structure from the current schema and database config. | 47 | 47 |
| | 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 | + | 48 | 54 | ## `populate` | 49 | 55 |
| 50 | 56 | ```bash | |
Collapse file Copy file name to clipboardExpand all lines: packages/stackpress-schema/src/transform/helpers.ts +95-1Lines changed: 95 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -6,6 +6,8 @@ import path from 'node:path'; | 6 | 6 | import type { Project, Directory } from 'ts-morph'; | 7 | 7 | import Nest from '@stackpress/lib/Nest'; | 8 | 8 | import { TemplateEngine, helpers } from '@stackpress/lib/Template'; | | 9 | +//stackpress-schema | | 10 | +import type Schema from '../Schema.js'; | 9 | 11 |
| 10 | 12 | export const cwd = process.cwd(); | 11 | 13 |
| @@ -69,10 +71,102 @@ export function renderCode(template: string, data: Record<string, any> = {}) { | 69 | 71 | return engine.render(template, data); | 70 | 72 | }; | 71 | 73 |
| | 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 | + | 72 | 166 | export function savePackageJsonNest(pwd: string, nest: Nest) { | 73 | 167 | if (!fs.existsSync(pwd)) { | 74 | 168 | fs.mkdirSync(pwd, { recursive: true }); | 75 | 169 | } | 76 | 170 | const filepath = path.resolve(pwd, 'package.json'); | 77 | 171 | fs.writeFileSync(filepath, JSON.stringify(nest.get(), null, 2)); | 78 | | -} | | 172 | +} |
Collapse file Copy file name to clipboardExpand all lines: packages/stackpress-schema/src/transform/index.ts +6-1Lines changed: 6 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -15,7 +15,10 @@ import { | 15 | 15 | import generateEnums from './enums.js'; | 16 | 16 | import generatePackage from './package.js'; | 17 | 17 | import generateSchemaTests from './tests/schema.js'; | 18 | | -import { loadProjectFile } from './helpers.js'; | | 18 | +import { | | 19 | + loadProjectFile, | | 20 | + pruneGeneratedSchemaFiles | | 21 | +} from './helpers.js'; | 19 | 22 |
| 20 | 23 | export default async function generate(props: ClientPluginProps) { | 21 | 24 | //------------------------------------------------------------------// | @@ -27,6 +30,8 @@ export default async function generate(props: ClientPluginProps) { | 27 | 30 | const loader = server.loader; | 28 | 31 | const revisionPath = server.config.path<string>('client.revisions'); | 29 | 32 | 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); | 30 | 35 |
| 31 | 36 | //------------------------------------------------------------------// | 32 | 37 | // 2. Generators | |
Collapse file Copy file name to clipboard +70Lines changed: 70 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 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 Copy file name to clipboardExpand all lines: packages/stackpress-server/src/Terminal.ts +7-1Lines changed: 7 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -9,6 +9,8 @@ export default class StackpressTerminal extends Terminal { | 9 | 9 | public readonly server: Server<any, any, any>; | 10 | 10 | //whether to show output | 11 | 11 | public readonly verbose: boolean; | | 12 | + //whether to bypass destructive safety checks | | 13 | + public readonly force: boolean; | 12 | 14 | //the config pathname | 13 | 15 | public readonly config: string|null; | 14 | 16 |
| @@ -35,6 +37,10 @@ export default class StackpressTerminal extends Terminal { | 35 | 37 | this.server = server; | 36 | 38 | //set flags | 37 | 39 | 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); | 38 | 44 | this.config = this.expect<string|null>([ 'bootstrap', 'b' ], null); | 39 | 45 | //register terminal plugin | 40 | 46 | this.server.register('terminal', this); | @@ -89,4 +95,4 @@ export default class StackpressTerminal extends Terminal { | 89 | 95 | return status; | 90 | 96 | } | 91 | 97 | } | 92 | | -} | | 98 | +} |
Collapse file Copy file name to clipboardExpand all lines: packages/stackpress-server/tests/Terminal.test.ts +2-4Lines changed: 2 additions & 4 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -19,13 +19,11 @@ describe('server/Terminal', () => { | 19 | 19 | } | 20 | 20 | }; | 21 | 21 |
| 22 | | - const terminal = new Terminal( | 23 | | - [ 'emit', '-v' ], | 24 | | - server as any | 25 | | - ); | | 22 | + const terminal = new Terminal([ 'emit', '-v', '--force' ], server as any); | 26 | 23 |
| 27 | 24 | expect(terminal.server).to.equal(server); | 28 | 25 | expect(terminal.verbose).to.equal(true); | | 26 | + expect(terminal.force).to.equal(true); | 29 | 27 | expect(terminal.config).to.equal(null); | 30 | 28 | expect(terminal.brand).to.equal('Stackpress'); | 31 | 29 | expect(terminal.cwd).to.equal('/workspace/app'); | |
|