fix(tax): Add indexes to enforce unique constraint on tax region (#8067)

This commit is contained in:
Oli Juhl authored and GitHub committed 2024-07-10 20:09:19 +02:00
1 parent 74c19308a0
commit 256912f392
4 files changed
+226 -3

No files matched your search

@@ -168,7 +168,15 @@
"composite": false,
"primary": false,
"unique": false,
"expression": "CREATE UNIQUE INDEX IF NOT EXISTS \"IDX_tax_region_unique_country_province\" ON \"tax_region\" (country_code, province_code)"
"expression": "CREATE UNIQUE INDEX IF NOT EXISTS \"IDX_tax_region_unique_country_province\" ON \"tax_region\" (country_code, province_code) WHERE deleted_at IS NULL"
},
{
"keyName": "IDX_tax_region_unique_country_nullable_province",
"columnNames": [],
"composite": false,
"primary": false,
"unique": false,
"expression": "CREATE UNIQUE INDEX IF NOT EXISTS \"IDX_tax_region_unique_country_nullable_province\" ON \"tax_region\" (country_code) WHERE province_code IS NULL AND deleted_at IS NULL"
},
{
"keyName": "tax_region_pkey",
@@ -0,0 +1,17 @@
import { Migration } from '@mikro-orm/migrations';
export class Migration20240710135844 extends Migration {
async up(): Promise<void> {
this.addSql('drop index if exists "IDX_tax_region_unique_country_province";');
this.addSql('CREATE UNIQUE INDEX IF NOT EXISTS "IDX_tax_region_unique_country_province" ON "tax_region" (country_code, province_code) WHERE deleted_at IS NULL;');
this.addSql('CREATE UNIQUE INDEX IF NOT EXISTS "IDX_tax_region_unique_country_nullable_province" ON "tax_region" (country_code) WHERE province_code IS NULL AND deleted_at IS NULL;');
}
async down(): Promise<void> {
this.addSql('drop index if exists "IDX_tax_region_unique_country_province";');
this.addSql('drop index if exists "IDX_tax_region_unique_country_nullable_province";');
this.addSql('CREATE UNIQUE INDEX IF NOT EXISTS "IDX_tax_region_unique_country_province" ON "tax_region" (country_code, province_code);');
}
}
@@ -27,6 +27,8 @@ type OptionalTaxRegionProps = DAL.SoftDeletableModelDateColumns
const TABLE_NAME = "tax_region"
export const countryCodeNullProvinceIndexName =
"IDX_tax_region_unique_country_nullable_province"
export const countryCodeProvinceIndexName =
"IDX_tax_region_unique_country_province"
const countryCodeProvinceIndexStatement = createPsqlIndexStatementHelper({
@@ -34,8 +36,18 @@ const countryCodeProvinceIndexStatement = createPsqlIndexStatementHelper({
tableName: TABLE_NAME,
columns: ["country_code", "province_code"],
unique: true,
where: "deleted_at IS NULL",
})
const countryCodeNullableProvinceIndexStatement =
createPsqlIndexStatementHelper({
name: countryCodeNullProvinceIndexName,
tableName: TABLE_NAME,
columns: ["country_code"],
unique: true,
where: "province_code IS NULL AND deleted_at IS NULL",
})
export const taxRegionProviderTopLevelCheckName =
"CK_tax_region_provider_top_level"
export const taxRegionCountryTopLevelCheckName =
@@ -49,6 +61,7 @@ export const taxRegionCountryTopLevelCheckName =
name: taxRegionCountryTopLevelCheckName,
expression: `parent_id IS NULL OR province_code IS NOT NULL`,
})
@countryCodeNullableProvinceIndexStatement.MikroORMIndex()
@countryCodeProvinceIndexStatement.MikroORMIndex()
@Entity({ tableName: TABLE_NAME })
@Filter(DALUtils.mikroOrmSoftDeletableFilterOptions)