@balena/abstract-sql-compiler
Advanced tools
Comparing version 7.8.1 to 7.8.2-fix-schema-optimizations-46704031fbde45fd0e36b0963f8cf524df53398b
@@ -7,2 +7,6 @@ # Change Log | ||
## 7.8.2 - 2021-01-15 | ||
* Fix schema optimizations when table and resource names are different [Pagan Gazzard] | ||
## 7.8.1 - 2021-01-04 | ||
@@ -9,0 +13,0 @@ |
@@ -12,2 +12,3 @@ "use strict"; | ||
const sbvrTypes = require("@balena/sbvr-types"); | ||
const _ = require("lodash"); | ||
const AbstractSQLCompiler_1 = require("./AbstractSQLCompiler"); | ||
@@ -32,3 +33,2 @@ const countFroms = (n) => { | ||
var _a; | ||
var _b; | ||
const ruleBodyNode = rule.find((r) => r[0] === 'Body'); | ||
@@ -86,9 +86,12 @@ if (ruleBodyNode == null || typeof ruleBodyNode === 'string') { | ||
const sha = sbvrTypes.SHA.validateSync(`${tableName}$${JSON.stringify(ruleBody)}`).replace(/^\$sha256\$/, ''); | ||
(_a = (_b = abstractSqlModel.tables[tableName]).checks) !== null && _a !== void 0 ? _a : (_b.checks = []); | ||
abstractSqlModel.tables[tableName].checks.push({ | ||
description: ruleSE, | ||
name: `${tableName.slice(0, 30)}$${sha}`.slice(0, 63), | ||
abstractSql: whereNode, | ||
}); | ||
return; | ||
const table = _.find(abstractSqlModel.tables, (t) => t.name === tableName); | ||
if (table) { | ||
(_a = table.checks) !== null && _a !== void 0 ? _a : (table.checks = []); | ||
table.checks.push({ | ||
description: ruleSE, | ||
name: `${tableName.slice(0, 30)}$${sha}`.slice(0, 63), | ||
abstractSql: whereNode, | ||
}); | ||
return; | ||
} | ||
} | ||
@@ -95,0 +98,0 @@ } |
{ | ||
"name": "@balena/abstract-sql-compiler", | ||
"version": "7.8.1", | ||
"version": "7.8.2-fix-schema-optimizations-46704031fbde45fd0e36b0963f8cf524df53398b", | ||
"description": "A translator for abstract sql into sql.", | ||
@@ -5,0 +5,0 @@ "main": "out/AbstractSQLCompiler.js", |
@@ -112,10 +112,16 @@ export const enum Engines { | ||
abstractSqlModel.tables[tableName].checks ??= []; | ||
abstractSqlModel.tables[tableName].checks!.push({ | ||
description: ruleSE, | ||
// Trim the trigger to a max of 63 characters, reserving at least 32 characters for the hash | ||
name: `${tableName.slice(0, 30)}$${sha}`.slice(0, 63), | ||
abstractSql: whereNode, | ||
}); | ||
return; | ||
const table = _.find( | ||
abstractSqlModel.tables, | ||
(t) => t.name === tableName, | ||
); | ||
if (table) { | ||
table.checks ??= []; | ||
table.checks!.push({ | ||
description: ruleSE, | ||
// Trim the trigger to a max of 63 characters, reserving at least 32 characters for the hash | ||
name: `${tableName.slice(0, 30)}$${sha}`.slice(0, 63), | ||
abstractSql: whereNode, | ||
}); | ||
return; | ||
} | ||
} | ||
@@ -122,0 +128,0 @@ } |
@@ -173,1 +173,74 @@ import * as AbstractSQLCompiler from '../..'; | ||
}); | ||
it('should work with differing table/resource names', () => { | ||
expect( | ||
generateSchema({ | ||
synonyms: {}, | ||
relationships: {}, | ||
tables: { | ||
some_other_resource_name: { | ||
name: 'test', | ||
resourceName: 'some_other_resource_name', | ||
idField: 'id', | ||
fields: [ | ||
{ | ||
fieldName: 'id', | ||
dataType: 'Integer', | ||
index: 'PRIMARY KEY', | ||
}, | ||
], | ||
indexes: [], | ||
primitive: false, | ||
}, | ||
}, | ||
rules: [ | ||
[ | ||
'Rule', | ||
[ | ||
'Body', | ||
[ | ||
'Not', | ||
[ | ||
'Exists', | ||
[ | ||
'SelectQuery', | ||
['Select', []], | ||
['From', ['test', 'test.0']], | ||
[ | ||
'Where', | ||
[ | ||
'Not', | ||
[ | ||
'And', | ||
[ | ||
'LessThan', | ||
['Integer', 0], | ||
['ReferencedField', 'test.0', 'id'], | ||
], | ||
['Exists', ['ReferencedField', 'test.0', 'id']], | ||
], | ||
], | ||
], | ||
], | ||
], | ||
], | ||
] as AbstractSQLCompiler.AbstractSqlQuery, | ||
[ | ||
'StructuredEnglish', | ||
'It is necessary that each test has an id that is greater than 0.', | ||
], | ||
], | ||
], | ||
}), | ||
) | ||
.to.have.property('createSchema') | ||
.that.deep.equals([ | ||
`\ | ||
CREATE TABLE IF NOT EXISTS "test" ( | ||
"id" INTEGER NULL PRIMARY KEY | ||
, -- It is necessary that each test has an id that is greater than 0. | ||
CONSTRAINT "test$hkEwz3pzAqalNu6crijhhdWJ0ffUvqRGK8rMkQbViPg=" CHECK (0 < "id" | ||
AND "id" IS NOT NULL) | ||
);`, | ||
]); | ||
}); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
583867
12893
2