diff --git a/.changeset/tall-lions-rescue.md b/.changeset/tall-lions-rescue.md new file mode 100644 index 0000000000..33c65c0b3f --- /dev/null +++ b/.changeset/tall-lions-rescue.md @@ -0,0 +1,5 @@ +--- +"@medusajs/medusa": patch +--- + +feat(medusa): remove soft delete from categories + adds indexes for categories diff --git a/packages/generated/client-types/src/lib/models/ProductCategory.ts b/packages/generated/client-types/src/lib/models/ProductCategory.ts index 58a0e95386..541d6db669 100644 --- a/packages/generated/client-types/src/lib/models/ProductCategory.ts +++ b/packages/generated/client-types/src/lib/models/ProductCategory.ts @@ -61,8 +61,4 @@ export interface ProductCategory { * The date with timezone at which the resource was updated. */ updated_at: string - /** - * The date with timezone at which the resource was deleted. - */ - deleted_at: string | null } diff --git a/packages/medusa/src/migrations/1679950221063-category-remove-soft-delete.ts b/packages/medusa/src/migrations/1679950221063-category-remove-soft-delete.ts new file mode 100644 index 0000000000..37fd79f2b0 --- /dev/null +++ b/packages/medusa/src/migrations/1679950221063-category-remove-soft-delete.ts @@ -0,0 +1,20 @@ +import { MigrationInterface, QueryRunner } from "typeorm" + +export class categoryRemoveSoftDelete1679950221063 implements MigrationInterface { + public async up(queryRunner: QueryRunner): Promise { + await queryRunner.query(`DELETE FROM "product_category" WHERE "deleted_at" IS NOT NULL;`) + await queryRunner.query(`ALTER TABLE "product_category" DROP COLUMN "deleted_at";`) + await queryRunner.query(`DROP INDEX IF EXISTS "IDX_product_category_handle";`) + await queryRunner.query( + `CREATE UNIQUE INDEX "IDX_product_category_handle" ON "product_category" ("handle");` + ) + } + + public async down(queryRunner: QueryRunner): Promise { + await queryRunner.query(`DROP INDEX IF EXISTS "IDX_product_category_handle";`) + await queryRunner.query(`ALTER TABLE "product_category" ADD COLUMN "deleted_at" timestamp with time zone;`) + await queryRunner.query( + `CREATE UNIQUE INDEX "IDX_product_category_handle" ON "product_category" ("handle") WHERE deleted_at IS NULL;` + ) + } +} diff --git a/packages/medusa/src/migrations/1679950645253-category-create-indexes.ts b/packages/medusa/src/migrations/1679950645253-category-create-indexes.ts new file mode 100644 index 0000000000..33cea8ba15 --- /dev/null +++ b/packages/medusa/src/migrations/1679950645253-category-create-indexes.ts @@ -0,0 +1,22 @@ +import { MigrationInterface, QueryRunner } from "typeorm" + +export class categoryCreateIndexes1679950645253 implements MigrationInterface { + public async up(queryRunner: QueryRunner): Promise { + await queryRunner.query(` + CREATE INDEX "IDX_product_category_active_public" ON "product_category" ( + "parent_category_id", + "is_active", + "is_internal" + ) WHERE ( + ("is_active" IS TRUE) AND + ("is_internal" IS FALSE) + ); + `) + } + + public async down(queryRunner: QueryRunner): Promise { + await queryRunner.query(` + DROP INDEX "IDX_product_category_active_public"; + `) + } +} diff --git a/packages/medusa/src/models/product-category.ts b/packages/medusa/src/models/product-category.ts index 1617a035ef..43c97afe28 100644 --- a/packages/medusa/src/models/product-category.ts +++ b/packages/medusa/src/models/product-category.ts @@ -1,5 +1,5 @@ import { generateEntityId } from "../utils/generate-entity-id" -import { SoftDeletableEntity } from "../interfaces/models/soft-deletable-entity" +import { BaseEntity } from "../interfaces/models/base-entity" import { kebabCase } from "lodash" import { Product } from "." import { @@ -18,14 +18,14 @@ import { @Entity() @Tree("materialized-path") @Index(["parent_category_id", "rank"], { unique: true }) -export class ProductCategory extends SoftDeletableEntity { +export class ProductCategory extends BaseEntity { static productCategoryProductJoinTable = "product_category_product" static treeRelations = ["parent_category", "category_children"] @Column() name: string - @Index({ unique: true, where: "deleted_at IS NULL" }) + @Index({ unique: true }) @Column({ nullable: false }) handle: string @@ -88,7 +88,6 @@ export class ProductCategory extends SoftDeletableEntity { * required: * - category_children * - created_at - * - deleted_at * - handle * - id * - is_active @@ -154,9 +153,4 @@ export class ProductCategory extends SoftDeletableEntity { * description: The date with timezone at which the resource was updated. * type: string * format: date-time - * deleted_at: - * description: The date with timezone at which the resource was deleted. - * nullable: true - * type: string - * format: date-time */ diff --git a/packages/medusa/src/repositories/product-category.ts b/packages/medusa/src/repositories/product-category.ts index a866ce4282..beb4af8648 100644 --- a/packages/medusa/src/repositories/product-category.ts +++ b/packages/medusa/src/repositories/product-category.ts @@ -109,10 +109,6 @@ export const ProductCategoryRepository = dataSource queryBuilder.leftJoinAndSelect(`${entityName}.${relation}`, relation) }) - if (options_.withDeleted) { - queryBuilder.withDeleted() - } - let [categories, count] = await queryBuilder.getManyAndCount() if (includeTree) {