Skip to content

Commit e7ed70f

Browse files
fix(cds-build): retain more build relevant options in the deployer app (#1206)
with an allow list approach for build relevant `cds` options, we are more flexible. Moreover, almost all `cdsc` options are build relevant, so we now copy over the whole section, minus some selected options which are set implicitly. --> Would be nice if every option would be controlled through `cds.features`, but e.g. `defaultStringLength`/`defaultBinaryLength` are only available through `cdsc`. Those `cdsc` options are also documented in [capire](https://cap.cloud.sap/docs/cds/cdl#built-in-types) fixes #1167 --- follow up #1032
1 parent 8975490 commit e7ed70f

File tree

3 files changed

+46
-17
lines changed

3 files changed

+46
-17
lines changed

postgres/cds-plugin.js

Lines changed: 33 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,22 @@ if (!cds.env.fiori.lean_draft) {
55
throw new Error('"@cap-js/postgres" only works if cds.fiori.lean_draft is enabled. Please adapt your configuration.')
66
}
77

8+
// copy over build relevant cds options to the package.json of the deployer app
9+
const CDS_BUILD_OPTIONS = ['assert_integrity']
10+
11+
// cdsc options are build relevant too, but we need to filter out some
12+
const CDSC_DISALLOW = ['moduleLookupDirectories']
13+
814
// requires @sap/cds-dk version >= 7.5.0
915
cds.build?.register?.('postgres', class PostgresBuildPlugin extends cds.build.Plugin {
10-
1116
static taskDefaults = { src: cds.env.folders.db }
12-
13-
static hasTask() { return cds.requires.db?.kind === 'postgres' }
14-
15-
init() {
17+
static hasTask () { return cds.requires.db?.kind === 'postgres' }
18+
init () {
1619
// different from the default build output structure
1720
this.task.dest = path.join(cds.root, cds.env.build.target !== '.' ? cds.env.build.target : 'gen', 'pg')
1821
}
1922

20-
async build() {
23+
async build () {
2124
const model = await this.model()
2225
if (!model) return
2326

@@ -27,23 +30,36 @@ cds.build?.register?.('postgres', class PostgresBuildPlugin extends cds.build.Pl
2730
} else {
2831
const packageJson = {
2932
dependencies: {
30-
'@sap/cds': '^8',
31-
'@cap-js/postgres': '^1'
33+
'@sap/cds': '^9',
34+
'@cap-js/postgres': '^2'
3235
},
33-
scripts: {
34-
start: 'cds-deploy'
36+
scripts: { start: 'cds-deploy' }
37+
}
38+
39+
// propagate cds.env.features (allow-listed)
40+
const envFeatures = cds.env?.features ?? {}
41+
for (const name of CDS_BUILD_OPTIONS) {
42+
const val = envFeatures[name]
43+
if (val !== undefined) {
44+
packageJson.cds ??= {}
45+
packageJson.cds.features ??= {}
46+
packageJson.cds.features[name] = val
3547
}
3648
}
37-
const assertIntegrity = cds.env?.features?.assert_integrity
38-
if (assertIntegrity) {
49+
50+
// propagate cds.env.cdsc (minus disallowed)
51+
const envCdsc = cds.env?.cdsc ?? {}
52+
const cdscClean = Object.fromEntries(
53+
Object.entries(envCdsc).filter(([key]) => !CDSC_DISALLOW.includes(key))
54+
)
55+
if (Object.keys(cdscClean).length) {
3956
packageJson.cds ??= {}
40-
packageJson.cds.features ??= {}
41-
packageJson.cds.features.assert_integrity = assertIntegrity
57+
packageJson.cds.cdsc = cdscClean
4258
}
43-
promises.push(
44-
this.write(packageJson).to('package.json')
45-
)
59+
60+
promises.push(this.write(packageJson).to('package.json'))
4661
}
62+
4763
promises.push(this.write(cds.compile.to.json(model)).to(path.join('db', 'csn.json')))
4864

4965
let data

postgres/test/cds-build.test.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,13 @@ describe('cds build plugin', () => {
3636
const ddl = String(execSync('npx cds deploy --dry', { cwd: workDir }))
3737
expect(ddl).to.contain('REFERENCES')
3838
})
39+
40+
test('should retain cdsc settings', () => {
41+
execSync('npx cds build --production', { cwd: workDir })
42+
const packageJson = JSON.parse(fs.readFileSync(path.join(pgDest, 'package.json'), 'utf8'))
43+
expect(packageJson.cds?.cdsc?.defaultStringLength).to.equal(1000)
44+
expect(packageJson.cds?.cdsc?.standardDatabaseFunctions).to.be.true
45+
// this is excluded from being copied over
46+
expect(packageJson.cds?.cdsc?.moduleLookupDirectories).to.be.undefined
47+
})
3948
})

postgres/test/tiny-sample/package.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@
88
"cds": {
99
"features": {
1010
"assert_integrity": "db"
11+
},
12+
"cdsc": {
13+
"defaultStringLength": 1000,
14+
"standardDatabaseFunctions": true
1115
}
1216
}
1317
}

0 commit comments

Comments
 (0)