-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(tables): ensure constraint names dont exceed postgres name len l…
…imit
- Loading branch information
1 parent
333df3d
commit 45cc443
Showing
8 changed files
with
147 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
src/__nonpublished_modules__/hash-fns/toHashShake256Sync.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import crypto from 'crypto'; | ||
|
||
/** | ||
* hashes a string w/ shake256, which allows for custom length hashes | ||
* | ||
* usecases | ||
* - custom sized hashing, when you want to control length at the cost of collision-probability | ||
* | ||
* note | ||
* - sync version is only available in node env | ||
* | ||
* ref | ||
* - https://stackoverflow.com/a/67073856/3068233 | ||
*/ | ||
export const toHashShake256Sync = (data: string, length: 2 | 4 | 8 | 16 | 32) => | ||
crypto | ||
.createHash('shake256', { outputLength: length }) | ||
.update(data) | ||
.digest('hex'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
src/logic/generate/entityTables/generateTable/__snapshots__/generateTable.test.ts.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`generateTableConstraint should safely define the constraint names for constraints that would otherwise have names longer than 64 chars 1`] = ` | ||
"CREATE TABLE async_task_predict_train_station_congestion_per_movement_event ( | ||
__COLUMN__, | ||
__COLUMN__, | ||
CONSTRAINT async_task_predict_train_station_congestion_per_mov_50cc24a7_pk PRIMARY KEY (id), | ||
CONSTRAINT async_task_predict_train_station_congestion_per_mo_50cc24a7_ux1 UNIQUE (status), | ||
__FOREIGN_KEY_CONSTRAINT__, | ||
CONSTRAINT async_task_predict_train_station_congesti_50cc24a7_status_check CHECK (status IN ('happy', 'meh', 'sad')) | ||
); | ||
__FOREIGN_KEY_INDEX__" | ||
`; |
29 changes: 29 additions & 0 deletions
29
src/logic/generate/entityTables/generateTable/defineConstraintNameSafely.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { toHashShake256Sync } from '../../../../__nonpublished_modules__/hash-fns/toHashShake256Sync'; | ||
|
||
const POSTGRES_RESOURCE_NAME_LEN_LIMIT = 63; | ||
|
||
/** | ||
* .what = determines an intuitive yet safe constraint name | ||
* .why = postgres has a 63 char resource name limit | ||
* .how = include a hash if the ideal name would be too long | ||
*/ | ||
export const defineConstraintNameSafely = (input: { | ||
tableName: string; | ||
constraintName: string; | ||
}): string => { | ||
const idealConstraintName = [input.tableName, input.constraintName].join('_'); | ||
if (idealConstraintName.length <= POSTGRES_RESOURCE_NAME_LEN_LIMIT) | ||
return idealConstraintName; | ||
const hash = toHashShake256Sync(input.tableName, 4); | ||
const tableNameLimit = | ||
POSTGRES_RESOURCE_NAME_LEN_LIMIT - | ||
hash.length - | ||
input.constraintName.length - | ||
2; | ||
const safeConstraintName = [ | ||
input.tableName.slice(0, tableNameLimit), | ||
hash, | ||
input.constraintName, | ||
].join('_'); | ||
return safeConstraintName; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters