Ver Fonte

fix(core): Define cascade behaviour for featured assets

BREAKING CHANGE: Some ON DELETE behaviour was incorrectly defined in the database schema, and has how been fixed. This will require a non-destructive migration.
Michael Bromley há 5 anos atrás
pai
commit
3f0c79bbc0

+ 6 - 16
packages/core/src/entity/collection/collection.entity.ts

@@ -56,32 +56,22 @@ export class Collection extends VendureEntity
 
     slug: LocaleString;
 
-    @OneToMany(
-        type => CollectionTranslation,
-        translation => translation.base,
-        { eager: true },
-    )
+    @OneToMany((type) => CollectionTranslation, (translation) => translation.base, { eager: true })
     translations: Array<Translation<Collection>>;
 
-    @ManyToOne(type => Asset)
+    @ManyToOne((type) => Asset, { onDelete: 'SET NULL' })
     featuredAsset: Asset;
 
-    @OneToMany(
-        type => CollectionAsset,
-        collectionAsset => collectionAsset.collection,
-    )
+    @OneToMany((type) => CollectionAsset, (collectionAsset) => collectionAsset.collection)
     assets: CollectionAsset[];
 
     @Column('simple-json') filters: ConfigurableOperation[];
 
-    @ManyToMany(
-        type => ProductVariant,
-        productVariant => productVariant.collections,
-    )
+    @ManyToMany((type) => ProductVariant, (productVariant) => productVariant.collections)
     @JoinTable()
     productVariants: ProductVariant[];
 
-    @Column(type => CustomCollectionFields)
+    @Column((type) => CustomCollectionFields)
     customFields: CustomCollectionFields;
 
     @TreeChildren()
@@ -90,7 +80,7 @@ export class Collection extends VendureEntity
     @TreeParent()
     parent: Collection;
 
-    @ManyToMany(type => Channel)
+    @ManyToMany((type) => Channel)
     @JoinTable()
     channels: Channel[];
 }

+ 1 - 1
packages/core/src/entity/product-variant/product-variant-asset.entity.ts

@@ -13,6 +13,6 @@ export class ProductVariantAsset extends OrderableAsset {
     @Column()
     productVariantId: ID;
 
-    @ManyToOne(type => ProductVariant, variant => variant.assets)
+    @ManyToOne((type) => ProductVariant, (variant) => variant.assets, { onDelete: 'CASCADE' })
     productVariant: ProductVariant;
 }

+ 4 - 2
packages/core/src/entity/product-variant/product-variant.entity.ts

@@ -77,10 +77,12 @@ export class ProductVariant extends VendureEntity implements Translatable, HasCu
      */
     taxRateApplied: TaxRate;
 
-    @ManyToOne((type) => Asset)
+    @ManyToOne((type) => Asset, { onDelete: 'SET NULL' })
     featuredAsset: Asset;
 
-    @OneToMany((type) => ProductVariantAsset, (productVariantAsset) => productVariantAsset.productVariant)
+    @OneToMany((type) => ProductVariantAsset, (productVariantAsset) => productVariantAsset.productVariant, {
+        onDelete: 'SET NULL',
+    })
     assets: ProductVariantAsset[];
 
     @ManyToOne((type) => TaxCategory)

+ 1 - 1
packages/core/src/entity/product/product-asset.entity.ts

@@ -13,6 +13,6 @@ export class ProductAsset extends OrderableAsset {
     @Column()
     productId: ID;
 
-    @ManyToOne(type => Product, product => product.assets)
+    @ManyToOne((type) => Product, (product) => product.assets, { onDelete: 'CASCADE' })
     product: Product;
 }

+ 1 - 1
packages/core/src/entity/product/product.entity.ts

@@ -44,7 +44,7 @@ export class Product extends VendureEntity
     @ManyToOne((type) => Asset, { onDelete: 'SET NULL' })
     featuredAsset: Asset;
 
-    @OneToMany((type) => ProductAsset, (productAsset) => productAsset.product, { onDelete: 'SET NULL' })
+    @OneToMany((type) => ProductAsset, (productAsset) => productAsset.product)
     assets: ProductAsset[];
 
     @OneToMany((type) => ProductTranslation, (translation) => translation.base, { eager: true })