Skip to content

Commit

Permalink
feat(arrays): define join tables without redundant prefix to avoid re…
Browse files Browse the repository at this point in the history
…source name length issues

since, postgres has a 64 char resource name len limit
  • Loading branch information
uladkasach committed Jun 9, 2024
1 parent 6b1928d commit df945ce
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -199,4 +199,54 @@ describe('generateAndRecordEntitySchema', () => {
);
expect(viewCurrentSql).toContain('CREATE OR REPLACE VIEW');
});
it('should record all resources for an entity with array properties that have the same prefix', async () => {
const habitat = new Literal({
name: 'sea_turtle_habitat',
properties: {
name: prop.VARCHAR(255),
},
});
const turtle = new Entity({
name: 'sea_turtle',
properties: {
name: prop.VARCHAR(255),
favorite_habitat_ids: prop.ARRAY_OF(prop.REFERENCES(habitat)),
},
unique: ['name', 'favorite_habitat_ids'],
});
await generateAndRecordEntitySchema({
targetDirPath,
entity: turtle,
});

// check static table was created
const tableStaticSql = await readFile(
`${targetDirPath}/tables/${turtle.name}.sql`,
'utf8',
);
expect(tableStaticSql).toContain('CREATE TABLE');

// check that the mapping table was created
const mappingTableOne = await readFile(
`${targetDirPath}/tables/${turtle.name}_to_habitat.sql`, // !: the repeated suffix is not included
'utf8',
);
expect(mappingTableOne).toContain('CREATE TABLE');

// check that the upsert function was created
const upsertSql = await readFile(
`${targetDirPath}/functions/upsert_${turtle.name}.sql`,
'utf8',
);
expect(upsertSql).toContain('CREATE OR REPLACE FUNCTION');

// check that the getFromDelimiterSplitString function was created
const getFromDelimiterSplitStringSql = await readFile(
`${targetDirPath}/functions/upsert_${turtle.name}.sql`,
'utf8',
);
expect(getFromDelimiterSplitStringSql).toContain(
'CREATE OR REPLACE FUNCTION',
);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,13 @@ export const defineMappingTableKeysForEntityProperty = ({
// handle case where we are mapping directly to an entity, within this database, by fk
if (referenceType === EntityReferenceType.DIRECT_FK_REFERENCE) {
const mappedEntityReferenceTableName = propertyDefinition.references;
const mappedEntityReferenceTableNameWithPrefixDeduped =
mappedEntityReferenceTableName?.replace(
new RegExp(`^${entityReferenceTableName}_`), // replace the prefix, if it matches, to avoid having table names that are too long
'',
);
return {
tableName: `${entityReferenceTableName}_to_${mappedEntityReferenceTableName}`,
tableName: `${entityReferenceTableName}_to_${mappedEntityReferenceTableNameWithPrefixDeduped}`,
entityReferenceColumnName: `${entityReferenceTableName}_id`,
mappedEntityReferenceColumnName: `${mappedEntityReferenceTableName}_id`,
mappedEntityReferenceColumnType: 'bigint',
Expand Down

0 comments on commit df945ce

Please sign in to comment.