Skip to content

Commit

Permalink
fix(core): Fix indexing of long descriptions in postgres
Browse files Browse the repository at this point in the history
Fixes #745
  • Loading branch information
michaelbromley committed Mar 11, 2021
1 parent 305727e commit 9efd7db
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 2 deletions.
46 changes: 46 additions & 0 deletions packages/core/e2e/default-search-plugin.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -883,6 +883,52 @@ describe('Default search plugin', () => {
{ productId: 'T_3', enabled: false },
]);
});

// https://github.com/vendure-ecommerce/vendure/issues/745
it('very long Product descriptions no not cause indexing to fail', async () => {
// We generate this long string out of random chars because Postgres uses compression
// when storing the string value, so e.g. a long series of a single character will not
// reproduce the error.
const description = Array.from({ length: 220 })
.map(() => Math.random().toString(36))
.join(' ');

const { createProduct } = await adminClient.query<
CreateProduct.Mutation,
CreateProduct.Variables
>(CREATE_PRODUCT, {
input: {
translations: [
{
languageCode: LanguageCode.en,
name: 'Very long description aabbccdd',
slug: 'very-long-description',
description,
},
],
},
});
await adminClient.query<CreateProductVariants.Mutation, CreateProductVariants.Variables>(
CREATE_PRODUCT_VARIANTS,
{
input: [
{
productId: createProduct.id,
sku: 'VLD01',
price: 100,
translations: [
{ languageCode: LanguageCode.en, name: 'Very long description variant' },
],
},
],
},
);
await awaitRunningJobs(adminClient);
const result = await doAdminSearchQuery({ term: 'aabbccdd' });
expect(result.search.items.map(i => i.productName)).toEqual([
'Very long description aabbccdd',
]);
});
});

// https://github.com/vendure-ecommerce/vendure/issues/609
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ export class IndexerController {
slug: productTranslation.slug,
productId: variant.product.id,
productName: productTranslation.name,
description: productTranslation.description,
description: this.constrainDescription(productTranslation.description),
productVariantName: variantTranslation.name,
productAssetId: variant.product.featuredAsset
? variant.product.featuredAsset.id
Expand Down Expand Up @@ -377,7 +377,7 @@ export class IndexerController {
slug: productTranslation.slug,
productId: product.id,
productName: productTranslation.name,
description: productTranslation.description,
description: this.constrainDescription(productTranslation.description),
productVariantName: productTranslation.name,
productAssetId: product.featuredAsset?.id ?? null,
productPreviewFocalPoint: product.featuredAsset?.focalPoint ?? null,
Expand Down Expand Up @@ -444,4 +444,17 @@ export class IndexerController {
})) as any[];
await this.queue.push(() => this.connection.getRepository(SearchIndexItem).delete(compositeKeys));
}

/**
* Prevent postgres errors from too-long indices
* https://github.com/vendure-ecommerce/vendure/issues/745
*/
private constrainDescription(description: string): string {
const { type } = this.connection.rawConnection.options;
const isPostgresLike = type === 'postgres' || type === 'aurora-data-api-pg' || type === 'cockroachdb';
if (isPostgresLike) {
return description.substring(0, 2600);
}
return description;
}
}

0 comments on commit 9efd7db

Please sign in to comment.