fix(medusa): Sales Channel migration script (#2521)

This commit is contained in:
Oliver Windall Juhl authored and GitHub committed 2022-11-02 13:37:46 +01:00
1 parent 144ce0e42c
commit 684f68ce12
1 file changed
+19 -10
@@ -1,8 +1,8 @@
import dotenv from "dotenv" import dotenv from "dotenv"
import { createConnection } from "typeorm" import { createConnection } from "typeorm"
import Logger from "../loaders/logger" import Logger from "../loaders/logger"
import { Product } from "../models/product"; import { Product } from "../models/product"
import { Store } from "../models/store"; import { Store } from "../models/store"
dotenv.config() dotenv.config()
@@ -23,16 +23,17 @@ const migrate = async function ({ typeormConfig }): Promise<void> {
const BATCH_SIZE = 1000 const BATCH_SIZE = 1000
await connection.transaction(async (manager) => { await connection.transaction(async (manager) => {
const store = await manager const store: Store | undefined = await manager
.createQueryBuilder() .createQueryBuilder()
.from(Store, "store") .from(Store, "store")
.select("store.default_sales_channel_id") .select("store.default_sales_channel_id")
.getOne() as Store .getRawOne()
if (!store.default_sales_channel_id) { if (!store?.default_sales_channel_id) {
Logger.error(`The default sales channel does not exists yet. Run your project and then re run the migration.`) Logger.error(
`The default sales channel does not exists yet. Run your project and then re run the migration.`
)
process.exit(1) process.exit(1)
return
} }
let shouldContinue = true let shouldContinue = true
@@ -40,7 +41,11 @@ const migrate = async function ({ typeormConfig }): Promise<void> {
const products = await manager const products = await manager
.createQueryBuilder() .createQueryBuilder()
.from(Product, "product") .from(Product, "product")
.leftJoin("product_sales_channel", "product_sc", "product_sc.product_id = product.id") .leftJoin(
"product_sales_channel",
"product_sc",
"product_sc.product_id = product.id"
)
.andWhere("product_sc.product_id IS NULL") .andWhere("product_sc.product_id IS NULL")
.select("product.id as id") .select("product.id as id")
.distinct(true) .distinct(true)
@@ -54,8 +59,8 @@ const migrate = async function ({ typeormConfig }): Promise<void> {
.into("product_sales_channel") .into("product_sales_channel")
.values( .values(
products.map((product) => ({ products.map((product) => ({
product_id: product.id,
sales_channel_id: store.default_sales_channel_id, sales_channel_id: store.default_sales_channel_id,
product_id: product.id
})) }))
) )
.orIgnore() .orIgnore()
@@ -65,7 +70,11 @@ const migrate = async function ({ typeormConfig }): Promise<void> {
const danglingProductCount = await manager const danglingProductCount = await manager
.createQueryBuilder() .createQueryBuilder()
.from(Product, "product") .from(Product, "product")
.leftJoin("product_sales_channel", "product_sc", "product_sc.product_id = product.id") .leftJoin(
"product_sales_channel",
"product_sc",
"product_sc.product_id = product.id"
)
.andWhere("product_sc.product_id IS NULL") .andWhere("product_sc.product_id IS NULL")
.getCount() .getCount()
shouldContinue = !!danglingProductCount shouldContinue = !!danglingProductCount