-
Notifications
You must be signed in to change notification settings - Fork 1
/
migration-utils-v018.ts
180 lines (172 loc) · 5.86 KB
/
migration-utils-v018.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
import { QueryRunner } from "typeorm";
export async function migrateOrderItemPromotionsAndTaxes(
queryRunner: QueryRunner
) {
const items = await q(
queryRunner,
{
mysql: "SELECT `id`, `adjustments`, `taxRate` from `order_item`",
postgres: 'SELECT "id", "adjustments", "taxRate" from "order_item"',
},
[]
);
for (const item of items) {
const adjustments = JSON.parse(item.adjustments);
const promotions = [];
const taxLines = [];
for (const adjustment of adjustments) {
if (adjustment.type === "TAX") {
taxLines.push({ description: "tax", taxRate: item.taxRate });
}
if (adjustment.type === "PROMOTION") {
promotions.push(adjustment);
}
}
await q(
queryRunner,
{
mysql: "UPDATE `order_item` SET `adjustments` = ? WHERE `id` = ?",
postgres: 'UPDATE "order_item" SET "adjustments" = $1 WHERE "id" = $2',
},
[JSON.stringify(promotions), item.id]
);
await q(
queryRunner,
{
mysql: "UPDATE `order_item` SET `taxLines` = ? WHERE `id` = ?",
postgres: 'UPDATE "order_item" SET "taxLines" = $1 WHERE "id" = $2',
},
[JSON.stringify(taxLines), item.id]
);
}
}
export async function migrateOrderShippingToShippingLines(
queryRunner: QueryRunner
) {
const orders = await q(
queryRunner,
{
mysql:
"SELECT `id`, `shipping`, `shippingWithTax`, `shippingMethodId` from `order` WHERE `shippingMethodId` IS NOT NULL",
postgres:
'SELECT "id", "shipping", "shippingWithTax", "shippingMethodId" from "order" WHERE "shippingMethodId" IS NOT NULL',
},
[]
);
for (const order of orders) {
const shippingTax = (order.shippingWithTax / order.shipping) * 100 - 100;
const taxLines = JSON.stringify([
{ description: "shipping tax", taxRate: shippingTax.toFixed(2) },
]);
await q(
queryRunner,
{
mysql:
"INSERT INTO `shipping_line` (`listPrice`, `listPriceIncludesTax`, `adjustments`, `taxLines`, `shippingMethodId`, `orderId`) " +
" VALUES (?, ?, ?, ?, ?, ?)",
postgres:
'INSERT INTO "shipping_line" ("listPrice", "listPriceIncludesTax", "adjustments", "taxLines", "shippingMethodId", "orderId") ' +
" VALUES ($1, $2, $3, $4, $5, $6)",
},
[order.shipping, false, "[]", taxLines, order.shippingMethodId, order.id]
);
}
}
export async function addProductVariantsToProductChannels(
queryRunner: QueryRunner
) {
const variants = await q(
queryRunner,
{
mysql:
"SELECT `v`.`id` AS productVariantId, `pcc`.`productId` AS productId, `pcc`.`channelId` as channelId FROM " +
"`product_variant` AS `v` INNER JOIN `product_channels_channel` AS `pcc` ON `pcc`.`productId` = `v`.`productId`",
postgres:
'SELECT "v"."id" AS "productVariantId", "pcc"."productId" AS "productId", "pcc"."channelId" as "channelId" FROM ' +
'"product_variant" AS "v" INNER JOIN "product_channels_channel" AS "pcc" ON "pcc"."productId" = "v"."productId"',
},
[]
);
for (const variant of variants) {
await q(
queryRunner,
{
mysql:
"INSERT INTO `product_variant_channels_channel` (`productVariantId`, `channelId`) VALUES (?, ?)",
postgres:
'INSERT INTO "product_variant_channels_channel" ("productVariantId", "channelId") VALUES ($1, $2)',
},
[variant.productVariantId, variant.channelId]
);
}
}
export async function migrateOrderAdjustmentsToSurcharges(
queryRunner: QueryRunner
) {
const orders = await q(queryRunner, {
mysql: "SELECT `id`, `pendingAdjustments` from `order`",
postgres: 'SELECT "id", "pendingAdjustments" from "order"',
});
for (const order of orders) {
const adjustments = JSON.parse(order.pendingAdjustments);
for (const adjustment of adjustments) {
await q(
queryRunner,
{
mysql:
"INSERT INTO `surcharge` (`description`, `sku`, `listPrice`, `listPriceIncludesTax`, `taxLines`, `orderId`) VALUES (?, ?, ?, ?, ?, ?)",
postgres:
'INSERT INTO "surcharge" ("description", "sku", "listPrice", "listPriceIncludesTax", "taxLines", "orderId") VALUES ($1, $2, $3, $4, $5, $6)',
},
[adjustment.description, "", adjustment.amount, true, "[]", order.id]
);
await q(
queryRunner,
{
mysql:
"UPDATE `order` SET `subTotal` = `subTotal` + ?, `subTotalWithTax` = `subTotalWithTax` + ? WHERE `id` = ?",
postgres:
'UPDATE "order" SET "subTotal" = "subTotal" + $1, "subTotalWithTax" = "subTotalWithTax" + $2 WHERE "id" = $3',
},
[adjustment.amount, adjustment.amount, order.id]
);
}
}
}
export async function migrateDefaultShippingCalculatorArgs(
queryRunner: QueryRunner
) {
const methods = await q(queryRunner, {
mysql: "SELECT `id`, `calculator` from `shipping_method`",
postgres: 'SELECT "id", "calculator" from "shipping_method"',
});
for (const method of methods) {
const calculator = JSON.parse(method.calculator);
if (calculator.code === "default-shipping-calculator") {
calculator.args.push({ name: "includesTax", value: "auto" });
await q(
queryRunner,
{
mysql: "UPDATE `shipping_method` SET `calculator` = ? WHERE `id` = ?",
postgres:
'UPDATE "shipping_method" SET "calculator" = $1 WHERE "id" = $2',
},
[JSON.stringify(calculator), method.id]
);
}
}
}
function q(
queryRunner: QueryRunner,
query: { mysql: string; postgres: string },
params: any[] = []
) {
return queryRunner.query(
isPostgres(queryRunner) ? query.postgres : query.mysql,
params
);
}
function isPostgres(queryRunner: QueryRunner): boolean {
const { type } = queryRunner.connection.options;
return type === "postgres" || type === "cockroachdb";
}