Skip to content

Commit

Permalink
Export the generateRuleHashAcronym helper
Browse files Browse the repository at this point in the history
Change-type: minor
  • Loading branch information
thgreasi committed Mar 21, 2024
1 parent d266662 commit 2998222
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 7 deletions.
3 changes: 2 additions & 1 deletion src/AbstractSQLCompiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
export { Binding, SqlResult } from './AbstractSQLRules2SQL';
import sbvrTypes from '@balena/sbvr-types';
import * as _ from 'lodash';
import { optimizeSchema } from './AbstractSQLSchemaOptimiser';
import { optimizeSchema, generateRuleSlug } from './AbstractSQLSchemaOptimiser';
import {
getReferencedFields,
getRuleReferencedFields,
Expand Down Expand Up @@ -1082,6 +1082,7 @@ CREATE TABLE ${ifNotExistsStr}"${table.name}" (
const generateExport = (engine: Engines, ifNotExists: boolean) => {
return {
optimizeSchema,
generateRuleSlug,
compileSchema: (abstractSqlModel: AbstractSqlModel) =>
compileSchema(abstractSqlModel, engine, ifNotExists),
compileRule: (abstractSQL: AbstractSqlQuery) =>
Expand Down
18 changes: 12 additions & 6 deletions src/AbstractSQLSchemaOptimiser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,17 @@ const countFroms = (n: AbstractSqlType[]) => {
return count;
};

export const generateRuleSlug = (
tableName: string,
ruleBody: AbstractSqlType,
) => {
const sha = sbvrTypes.SHA.validateSync(
`${tableName}$${JSON.stringify(ruleBody)}`,
).replace(/^\$sha256\$/, '');
// Trim the trigger to a max of 63 characters, reserving at least 32 characters for the hash
return `${tableName.slice(0, 30)}$${sha}`.slice(0, 63);
};

export const optimizeSchema = (
abstractSqlModel: AbstractSqlModel,
createCheckConstraints: boolean = true,
Expand Down Expand Up @@ -110,10 +121,6 @@ export const optimizeSchema = (
convertReferencedFieldsToFields(whereNode);

const tableName = fromNode[1];
const sha = sbvrTypes.SHA.validateSync(
`${tableName}$${JSON.stringify(ruleBody)}`,
).replace(/^\$sha256\$/, '');

const table = _.find(
abstractSqlModel.tables,
(t) => t.name === tableName,
Expand All @@ -122,8 +129,7 @@ export const optimizeSchema = (
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),
name: generateRuleSlug(tableName, ruleBody),
abstractSql: whereNode,
});
return;
Expand Down

0 comments on commit 2998222

Please sign in to comment.