1 import { Migration } from "@medusajs/framework/mikro-orm/migrations"
2
3 export class Migration20240311145700_InitialSetupMigration extends Migration {
4 async up(): Promise<void> {
5 const shippingOptionTable = await this.execute(
6 `SELECT * FROM information_schema.tables where table_name = 'shipping_option' and table_schema = 'public';`
7 )
8
9 if (!shippingOptionTable.length) {
10 await migrateUpModuleMigration.call(this)
11 } else {
12 await migrateUpBackwardCompatibility.call(this)
13 }
14 }
15 }
16
17 /**
18 * This migration is for the initial setup of the fulfillment module in the case of
19 * an already existing database. It will check if the `shipping_option` table exists and in that case
20 * assume that all other associated tables exists and will migrate them all.
21 *
22 * This migration does not take into account the data migration part which
23 * should be handled separately. Also, after the data migration
24 * the tables should be cleaned up from the old columns.
25 **/
26 async function migrateUpBackwardCompatibility(
27 this: Migration20240311145700_InitialSetupMigration
28 ) {
29 this.addSql(
30 'create table if not exists "fulfillment_address" ("id" text not null, "company" text null, "first_name" text null, "last_name" text null, "address_1" text null, "address_2" text null, "city" text null, "country_code" text null, "province" text null, "postal_code" text null, "phone" text null, "metadata" jsonb null, "created_at" timestamptz not null default now(), "updated_at" timestamptz not null default now(), "deleted_at" timestamptz null, constraint "fulfillment_address_pkey" primary key ("id"));'
31 )
32 this.addSql(
33 'CREATE INDEX IF NOT EXISTS "IDX_fulfillment_address_deleted_at" ON "fulfillment_address" (deleted_at) WHERE deleted_at IS NOT NULL;'
34 )
35
36 this.addSql(`
37 alter table fulfillment_provider
38 alter column id type text using id::text;
39
40 alter table fulfillment_provider
41 add is_enabled boolean default true not null;
42 `)
43
44 this.addSql(
45 'create table if not exists "fulfillment_set" ("id" text not null, "name" text not null, "type" text not null, "metadata" jsonb null, "created_at" timestamptz not null default now(), "updated_at" timestamptz not null default now(), "deleted_at" timestamptz null, constraint "fulfillment_set_pkey" primary key ("id"));'
46 )
47 this.addSql(
48 'CREATE UNIQUE INDEX IF NOT EXISTS "IDX_fulfillment_set_name_unique" ON "fulfillment_set" (name) WHERE deleted_at IS NULL;'
49 )
50 this.addSql(
51 'CREATE INDEX IF NOT EXISTS "IDX_fulfillment_set_deleted_at" ON "fulfillment_set" (deleted_at) WHERE deleted_at IS NOT NULL;'
52 )
53
54 this.addSql(
55 'create table if not exists "service_zone" ("id" text not null, "name" text not null, "metadata" jsonb null, "fulfillment_set_id" text not null, "created_at" timestamptz not null default now(), "updated_at" timestamptz not null default now(), "deleted_at" timestamptz null, constraint "service_zone_pkey" primary key ("id"));'
56 )
57 this.addSql(
58 'CREATE UNIQUE INDEX IF NOT EXISTS "IDX_service_zone_name_unique" ON "service_zone" (name) WHERE deleted_at IS NULL;'
59 )
60 this.addSql(
61 'CREATE INDEX IF NOT EXISTS "IDX_service_zone_fulfillment_set_id" ON "service_zone" (fulfillment_set_id) WHERE deleted_at IS NULL;'
62 )
63 this.addSql(
64 'CREATE INDEX IF NOT EXISTS "IDX_service_zone_deleted_at" ON "service_zone" (deleted_at) WHERE deleted_at IS NOT NULL;'
65 )
66
67 this.addSql(
68 'create table if not exists "geo_zone" ("id" text not null, "type" text check ("type" in (\'country\', \'province\', \'city\', \'zip\')) not null default \'country\', "country_code" text not null, "province_code" text null, "city" text null, "service_zone_id" text not null, "postal_expression" jsonb null, "metadata" jsonb null, "created_at" timestamptz not null default now(), "updated_at" timestamptz not null default now(), "deleted_at" timestamptz null, constraint "geo_zone_pkey" primary key ("id"));'
69 )
70 this.addSql(
71 'CREATE INDEX IF NOT EXISTS "IDX_geo_zone_country_code" ON "geo_zone" (country_code) WHERE deleted_at IS NULL;'
72 )
73 this.addSql(
74 'CREATE INDEX IF NOT EXISTS "IDX_geo_zone_province_code" ON "geo_zone" (province_code) WHERE deleted_at IS NULL AND province_code IS NOT NULL;'
75 )
76 this.addSql(
77 'CREATE INDEX IF NOT EXISTS "IDX_geo_zone_city" ON "geo_zone" (city) WHERE deleted_at IS NULL AND city IS NOT NULL;'
78 )
79 this.addSql(
80 'CREATE INDEX IF NOT EXISTS "IDX_geo_zone_service_zone_id" ON "geo_zone" (service_zone_id) WHERE deleted_at IS NULL;'
81 )
82 this.addSql(
83 'CREATE INDEX IF NOT EXISTS "IDX_geo_zone_deleted_at" ON "geo_zone" (deleted_at) WHERE deleted_at IS NOT NULL;'
84 )
85
86 this.addSql(
87 'create table if not exists "shipping_option_type" ("id" text not null, "label" text not null, "description" text null, "code" text not null, "created_at" timestamptz not null default now(), "updated_at" timestamptz not null default now(), "deleted_at" timestamptz null, constraint "shipping_option_type_pkey" primary key ("id"));'
88 )
89 this.addSql(
90 'CREATE INDEX IF NOT EXISTS "IDX_shipping_option_type_deleted_at" ON "shipping_option_type" (deleted_at) WHERE deleted_at IS NOT NULL;'
91 )
92
93 this.addSql(`
94 alter table shipping_profile
95 alter column id type text using id::text;
96
97 alter table shipping_profile
98 alter column name type text using name::text;
99
100 alter table shipping_profile
101 alter column type type text using type::text;
102
103 create unique index if not exists "IDX_shipping_profile_name_unique"
104 on shipping_profile (name)
105 where (deleted_at IS NULL);
106
107 create index if not exists "IDX_shipping_profile_deleted_at"
108 on shipping_profile (deleted_at)
109 where (deleted_at IS NOT NULL);
110 `)
111
112 this.addSql(`
113 alter table shipping_option
114 alter column id type text using id::text;
115
116 alter table shipping_option
117 alter column name type text using name::text;
118
119 alter table shipping_option
120 add service_zone_id text not null;
121
122 alter table shipping_option
123 RENAME profile_id TO shipping_profile_id;
124
125 alter table shipping_option
126 alter column region_id drop not null;
127
128 alter table shipping_option
129 alter column price_type type text using price_type::text;
130
131 alter table shipping_option
132 alter column price_type set default 'calculated'::text;
133
134 alter table shipping_option
135 alter column data drop not null;
136
137 alter table shipping_option
138 add shipping_option_type_id text not null;
139
140 drop index if exists "IDX_5c58105f1752fca0f4ce69f466";
141
142 drop index if exists "IDX_c951439af4c98bf2bd7fb8726c";
143
144 drop index if exists "IDX_a0e206bfaed3cb63c186091734";
145
146 create index if not exists "IDX_shipping_option_service_zone_id"
147 on shipping_option (service_zone_id)
148 where (deleted_at IS NULL);
149
150 create index if not exists "IDX_shipping_option_shipping_profile_id"
151 on shipping_option (shipping_profile_id)
152 where (deleted_at IS NULL);
153
154 create index if not exists "IDX_shipping_option_provider_id"
155 on shipping_option (provider_id)
156 where (deleted_at IS NULL);
157
158 create index if not exists "IDX_shipping_option_shipping_option_type_id"
159 on shipping_option (shipping_option_type_id)
160 where (deleted_at IS NULL);
161
162 create index if not exists "IDX_shipping_option_deleted_at"
163 on shipping_option (deleted_at)
164 where (deleted_at IS NOT NULL);
165
166 alter table shipping_option
167 add constraint shipping_option_shipping_option_type_id_unique
168 unique (shipping_option_type_id);
169
170 alter table shipping_option
171 drop constraint "FK_5c58105f1752fca0f4ce69f4663";
172
173 alter table shipping_option
174 drop constraint "FK_c951439af4c98bf2bd7fb8726cd";
175
176 alter table shipping_option
177 drop constraint "FK_a0e206bfaed3cb63c1860917347";
178
179 alter table shipping_option
180 add constraint shipping_option_service_zone_id_foreign
181 foreign key (service_zone_id) references service_zone
182 on update cascade on delete cascade;
183
184 alter table shipping_option
185 add constraint shipping_option_shipping_profile_id_foreign
186 foreign key (shipping_profile_id) references shipping_profile
187 on update cascade on delete set null;
188
189 alter table shipping_option
190 add constraint shipping_option_provider_id_foreign
191 foreign key (provider_id) references fulfillment_provider
192 on update cascade on delete set null;
193
194 alter table shipping_option
195 add constraint shipping_option_shipping_option_type_id_foreign
196 foreign key (shipping_option_type_id) references shipping_option_type
197 on update cascade on delete cascade;
198
199 alter table shipping_option
200 drop constraint "CHK_7a367f5901ae0a5b0df75aee38";
201
202 alter table shipping_option
203 add constraint shipping_option_price_type_check
204 check (price_type = ANY (ARRAY ['calculated'::text, 'flat'::text]));
205 `)
206
207 this.addSql(
208 'create table if not exists "shipping_option_rule" ("id" text not null, "attribute" text not null, "operator" text check ("operator" in (\'in\', \'eq\', \'ne\', \'gt\', \'gte\', \'lt\', \'lte\', \'nin\')) not null, "value" jsonb null, "shipping_option_id" text not null, "created_at" timestamptz not null default now(), "updated_at" timestamptz not null default now(), "deleted_at" timestamptz null, constraint "shipping_option_rule_pkey" primary key ("id"));'
209 )
210 this.addSql(
211 'CREATE INDEX IF NOT EXISTS "IDX_shipping_option_rule_shipping_option_id" ON "shipping_option_rule" (shipping_option_id) WHERE deleted_at IS NULL;'
212 )
213 this.addSql(
214 'CREATE INDEX IF NOT EXISTS "IDX_shipping_option_rule_deleted_at" ON "shipping_option_rule" (deleted_at) WHERE deleted_at IS NOT NULL;'
215 )
216
217 this.addSql(`
218 alter table fulfillment
219 alter
220 column id type text using id::text;
221
222 alter table fulfillment
223 add packed_at timestamp with time zone;
224
225 alter table fulfillment
226 alter column data drop not null;
227
228 alter table fulfillment
229 add delivered_at timestamp with time zone;
230
231 alter table fulfillment
232 add shipping_option_id text;
233
234 alter table fulfillment
235 add delivery_address_id text;
236
237 alter table fulfillment
238 alter
239 column provider_id type text using provider_id::text;
240
241 alter table fulfillment
242 add deleted_at timestamp with time zone;
243
244 alter table fulfillment
245 alter
246 column location_id type text using location_id::text;
247
248 alter table fulfillment
249 alter column location_id set not null;
250
251 drop index if exists "IDX_d73e55964e0ff2db8f03807d52";
252
253 drop index if exists "IDX_a52e234f729db789cf473297a5";
254
255 drop index if exists "IDX_f129acc85e346a10eed12b86fc";
256
257 drop index if exists "IDX_beb35a6de60a6c4f91d5ae57e4";
258
259 create index if not exists "IDX_fulfillment_location_id"
260 on fulfillment (location_id) where (deleted_at IS NULL);
261
262 create index if not exists"IDX_fulfillment_provider_id"
263 on fulfillment (provider_id) where (deleted_at IS NULL);
264
265 create index if not exists"IDX_fulfillment_shipping_option_id"
266 on fulfillment (shipping_option_id) where (deleted_at IS NULL);
267
268 create index if not exists"IDX_fulfillment_deleted_at"
269 on fulfillment (deleted_at) where (deleted_at IS NOT NULL);
270
271 alter table fulfillment
272 add constraint fulfillment_delivery_address_id_unique
273 unique (delivery_address_id);
274
275 alter table fulfillment
276 drop
277 constraint "FK_a52e234f729db789cf473297a5c"; /* swap id */
278
279 alter table fulfillment
280 drop
281 constraint "FK_f129acc85e346a10eed12b86fca"; /* order id */
282
283 alter table fulfillment
284 drop
285 constraint "FK_beb35a6de60a6c4f91d5ae57e44"; /* provider id */
286
287 alter table fulfillment
288 drop
289 constraint "FK_d73e55964e0ff2db8f03807d52e"; /* claim id */
290
291 alter table fulfillment
292 add constraint fulfillment_provider_id_foreign
293 foreign key (provider_id) references fulfillment_provider
294 on update cascade on delete set null;
295
296 alter table fulfillment
297 add constraint fulfillment_shipping_option_id_foreign
298 foreign key (shipping_option_id) references shipping_option
299 on update cascade on delete set null;
300
301 alter table fulfillment
302 add constraint fulfillment_delivery_address_id_foreign
303 foreign key (delivery_address_id) references fulfillment_address
304 on update cascade on delete cascade;
305 `)
306
307 this.addSql(
308 'create table if not exists "fulfillment_label" ("id" text not null, "tracking_number" text not null, "tracking_url" text not null, "label_url" text not null, "fulfillment_id" text not null, "created_at" timestamptz not null default now(), "updated_at" timestamptz not null default now(), "deleted_at" timestamptz null, constraint "fulfillment_label_pkey" primary key ("id"));'
309 )
310 this.addSql(
311 'CREATE INDEX IF NOT EXISTS "IDX_fulfillment_label_fulfillment_id" ON "fulfillment_label" (fulfillment_id) WHERE deleted_at IS NULL;'
312 )
313 this.addSql(
314 'CREATE INDEX IF NOT EXISTS "IDX_fulfillment_label_deleted_at" ON "fulfillment_label" (deleted_at) WHERE deleted_at IS NOT NULL;'
315 )
316
317 this.addSql(`
318 alter table fulfillment_item
319 add id text not null;
320
321 alter table fulfillment_item
322 add title text not null;
323
324 alter table fulfillment_item
325 add sku text not null;
326
327 alter table fulfillment_item
328 add barcode text not null;
329
330 alter table fulfillment_item
331 alter column quantity type numeric using quantity::numeric;
332
333 alter table fulfillment_item
334 add raw_quantity jsonb not null;
335
336 alter table fulfillment_item
337 add line_item_id text;
338
339 alter table fulfillment_item
340 add inventory_item_id text;
341
342 alter table fulfillment_item
343 alter column fulfillment_id type text using fulfillment_id::text;
344
345 alter table fulfillment_item
346 add created_at timestamp with time zone default now() not null;
347
348 alter table fulfillment_item
349 add updated_at timestamp with time zone default now() not null;
350
351 alter table fulfillment_item
352 add deleted_at timestamp with time zone;
353
354 create index if not exists "IDX_fulfillment_item_line_item_id"
355 on fulfillment_item (line_item_id)
356 where (deleted_at IS NULL);
357
358 create index if not exists "IDX_fulfillment_item_inventory_item_id"
359 on fulfillment_item (inventory_item_id)
360 where (deleted_at IS NULL);
361
362 create index if not exists "IDX_fulfillment_item_fulfillment_id"
363 on fulfillment_item (fulfillment_id)
364 where (deleted_at IS NULL);
365
366 create index if not exists "IDX_fulfillment_item_deleted_at"
367 on fulfillment_item (deleted_at)
368 where (deleted_at IS NOT NULL);
369
370 alter table fulfillment_item
371 drop constraint "FK_a033f83cc6bd7701a5687ab4b38"; /* fulfillment id */
372
373 alter table fulfillment_item
374 drop constraint "FK_e13ff60e74206b747a1896212d1"; /* item id */
375
376 alter table fulfillment_item
377 drop constraint "PK_bc3e8a388de75db146a249922e0"; /* item id + fulfillment id PK */
378
379 alter table fulfillment_item
380 alter column item_id drop not null;
381
382 alter table fulfillment_item
383 add constraint fulfillment_item_fulfillment_id_foreign
384 foreign key (fulfillment_id) references fulfillment
385 on update cascade on delete cascade;
386
387 alter table "fulfillment_item" add primary key ("id");
388 `)
389
390 this.addSql(
391 'alter table if exists "service_zone" add constraint "service_zone_fulfillment_set_id_foreign" foreign key ("fulfillment_set_id") references "fulfillment_set" ("id") on update cascade on delete cascade;'
392 )
393
394 this.addSql(
395 'alter table if exists "geo_zone" add constraint "geo_zone_service_zone_id_foreign" foreign key ("service_zone_id") references "service_zone" ("id") on update cascade on delete cascade;'
396 )
397
398 this.addSql(
399 'alter table if exists "shipping_option_rule" add constraint "shipping_option_rule_shipping_option_id_foreign" foreign key ("shipping_option_id") references "shipping_option" ("id") on update cascade on delete cascade;'
400 )
401
402 this.addSql(
403 'alter table if exists "fulfillment_label" add constraint "fulfillment_label_fulfillment_id_foreign" foreign key ("fulfillment_id") references "fulfillment" ("id") on update cascade on delete cascade;'
404 )
405 }
406
407 /**
408 * This migration is for be initial setup of the fulfillment module in the case of
409 * a new database. It will create all the necessary tables and indexes.
410 */
411 async function migrateUpModuleMigration(
412 this: Migration20240311145700_InitialSetupMigration
413 ) {
414 this.addSql(
415 'create table if not exists "fulfillment_address" ("id" text not null, "company" text null, "first_name" text null, "last_name" text null, "address_1" text null, "address_2" text null, "city" text null, "country_code" text null, "province" text null, "postal_code" text null, "phone" text null, "metadata" jsonb null, "created_at" timestamptz not null default now(), "updated_at" timestamptz not null default now(), "deleted_at" timestamptz null, constraint "fulfillment_address_pkey" primary key ("id"));'
416 )
417 this.addSql(
418 'CREATE INDEX IF NOT EXISTS "IDX_fulfillment_address_deleted_at" ON "fulfillment_address" (deleted_at) WHERE deleted_at IS NOT NULL;'
419 )
420
421 this.addSql(
422 'create table if not exists "fulfillment_provider" ("id" text not null, "is_enabled" boolean not null default true, constraint "fulfillment_provider_pkey" primary key ("id"));'
423 )
424
425 this.addSql(
426 'create table if not exists "fulfillment_set" ("id" text not null, "name" text not null, "type" text not null, "metadata" jsonb null, "created_at" timestamptz not null default now(), "updated_at" timestamptz not null default now(), "deleted_at" timestamptz null, constraint "fulfillment_set_pkey" primary key ("id"));'
427 )
428 this.addSql(
429 'CREATE UNIQUE INDEX IF NOT EXISTS "IDX_fulfillment_set_name_unique" ON "fulfillment_set" (name) WHERE deleted_at IS NULL;'
430 )
431 this.addSql(
432 'CREATE INDEX IF NOT EXISTS "IDX_fulfillment_set_deleted_at" ON "fulfillment_set" (deleted_at) WHERE deleted_at IS NOT NULL;'
433 )
434
435 this.addSql(
436 'create table if not exists "service_zone" ("id" text not null, "name" text not null, "metadata" jsonb null, "fulfillment_set_id" text not null, "created_at" timestamptz not null default now(), "updated_at" timestamptz not null default now(), "deleted_at" timestamptz null, constraint "service_zone_pkey" primary key ("id"));'
437 )
438 this.addSql(
439 'CREATE UNIQUE INDEX IF NOT EXISTS "IDX_service_zone_name_unique" ON "service_zone" (name) WHERE deleted_at IS NULL;'
440 )
441 this.addSql(
442 'CREATE INDEX IF NOT EXISTS "IDX_service_zone_fulfillment_set_id" ON "service_zone" (fulfillment_set_id) WHERE deleted_at IS NULL;'
443 )
444 this.addSql(
445 'CREATE INDEX IF NOT EXISTS "IDX_service_zone_deleted_at" ON "service_zone" (deleted_at) WHERE deleted_at IS NOT NULL;'
446 )
447
448 this.addSql(
449 'create table if not exists "geo_zone" ("id" text not null, "type" text check ("type" in (\'country\', \'province\', \'city\', \'zip\')) not null default \'country\', "country_code" text not null, "province_code" text null, "city" text null, "service_zone_id" text not null, "postal_expression" jsonb null, "metadata" jsonb null, "created_at" timestamptz not null default now(), "updated_at" timestamptz not null default now(), "deleted_at" timestamptz null, constraint "geo_zone_pkey" primary key ("id"));'
450 )
451 this.addSql(
452 'CREATE INDEX IF NOT EXISTS "IDX_geo_zone_country_code" ON "geo_zone" (country_code) WHERE deleted_at IS NULL;'
453 )
454 this.addSql(
455 'CREATE INDEX IF NOT EXISTS "IDX_geo_zone_province_code" ON "geo_zone" (province_code) WHERE deleted_at IS NULL AND province_code IS NOT NULL;'
456 )
457 this.addSql(
458 'CREATE INDEX IF NOT EXISTS "IDX_geo_zone_city" ON "geo_zone" (city) WHERE deleted_at IS NULL AND city IS NOT NULL;'
459 )
460 this.addSql(
461 'CREATE INDEX IF NOT EXISTS "IDX_geo_zone_service_zone_id" ON "geo_zone" (service_zone_id) WHERE deleted_at IS NULL;'
462 )
463 this.addSql(
464 'CREATE INDEX IF NOT EXISTS "IDX_geo_zone_deleted_at" ON "geo_zone" (deleted_at) WHERE deleted_at IS NOT NULL;'
465 )
466
467 this.addSql(
468 'create table if not exists "shipping_option_type" ("id" text not null, "label" text not null, "description" text null, "code" text not null, "created_at" timestamptz not null default now(), "updated_at" timestamptz not null default now(), "deleted_at" timestamptz null, constraint "shipping_option_type_pkey" primary key ("id"));'
469 )
470 this.addSql(
471 'CREATE INDEX IF NOT EXISTS "IDX_shipping_option_type_deleted_at" ON "shipping_option_type" (deleted_at) WHERE deleted_at IS NOT NULL;'
472 )
473
474 this.addSql(
475 'create table if not exists "shipping_profile" ("id" text not null, "name" text not null, "type" text not null, "metadata" jsonb null, "created_at" timestamptz not null default now(), "updated_at" timestamptz not null default now(), "deleted_at" timestamptz null, constraint "shipping_profile_pkey" primary key ("id"));'
476 )
477 this.addSql(
478 'CREATE UNIQUE INDEX IF NOT EXISTS "IDX_shipping_profile_name_unique" ON "shipping_profile" (name) WHERE deleted_at IS NULL;'
479 )
480 this.addSql(
481 'CREATE INDEX IF NOT EXISTS "IDX_shipping_profile_deleted_at" ON "shipping_profile" (deleted_at) WHERE deleted_at IS NOT NULL;'
482 )
483
484 this.addSql(
485 'create table if not exists "shipping_option" ("id" text not null, "name" text not null, "price_type" text check ("price_type" in (\'calculated\', \'flat\')) not null default \'flat\', "service_zone_id" text not null, "shipping_profile_id" text null, "provider_id" text null, "data" jsonb null, "metadata" jsonb null, "shipping_option_type_id" text not null, "created_at" timestamptz not null default now(), "updated_at" timestamptz not null default now(), "deleted_at" timestamptz null, constraint "shipping_option_pkey" primary key ("id"));'
486 )
487 this.addSql(
488 'alter table if exists "shipping_option" add constraint "shipping_option_shipping_option_type_id_unique" unique ("shipping_option_type_id");'
489 )
490 this.addSql(
491 'CREATE INDEX IF NOT EXISTS "IDX_shipping_option_service_zone_id" ON "shipping_option" (service_zone_id) WHERE deleted_at IS NULL;'
492 )
493 this.addSql(
494 'CREATE INDEX IF NOT EXISTS "IDX_shipping_option_shipping_profile_id" ON "shipping_option" (shipping_profile_id) WHERE deleted_at IS NULL;'
495 )
496 this.addSql(
497 'CREATE INDEX IF NOT EXISTS "IDX_shipping_option_provider_id" ON "shipping_option" (provider_id) WHERE deleted_at IS NULL;'
498 )
499 this.addSql(
500 'CREATE INDEX IF NOT EXISTS "IDX_shipping_option_shipping_option_type_id" ON "shipping_option" (shipping_option_type_id) WHERE deleted_at IS NULL;'
501 )
502 this.addSql(
503 'CREATE INDEX IF NOT EXISTS "IDX_shipping_option_deleted_at" ON "shipping_option" (deleted_at) WHERE deleted_at IS NOT NULL;'
504 )
505
506 this.addSql(
507 'create table if not exists "shipping_option_rule" ("id" text not null, "attribute" text not null, "operator" text check ("operator" in (\'in\', \'eq\', \'ne\', \'gt\', \'gte\', \'lt\', \'lte\', \'nin\')) not null, "value" jsonb null, "shipping_option_id" text not null, "created_at" timestamptz not null default now(), "updated_at" timestamptz not null default now(), "deleted_at" timestamptz null, constraint "shipping_option_rule_pkey" primary key ("id"));'
508 )
509 this.addSql(
510 'CREATE INDEX IF NOT EXISTS "IDX_shipping_option_rule_shipping_option_id" ON "shipping_option_rule" (shipping_option_id) WHERE deleted_at IS NULL;'
511 )
512 this.addSql(
513 'CREATE INDEX IF NOT EXISTS "IDX_shipping_option_rule_deleted_at" ON "shipping_option_rule" (deleted_at) WHERE deleted_at IS NOT NULL;'
514 )
515
516 this.addSql(
517 'create table if not exists "fulfillment" ("id" text not null, "location_id" text not null, "packed_at" timestamptz null, "shipped_at" timestamptz null, "delivered_at" timestamptz null, "canceled_at" timestamptz null, "data" jsonb null, "provider_id" text null, "shipping_option_id" text null, "metadata" jsonb null, "delivery_address_id" text null, "created_at" timestamptz not null default now(), "updated_at" timestamptz not null default now(), "deleted_at" timestamptz null, constraint "fulfillment_pkey" primary key ("id"));'
518 )
519 this.addSql(
520 'alter table if exists "fulfillment" add constraint "fulfillment_delivery_address_id_unique" unique ("delivery_address_id");'
521 )
522 this.addSql(
523 'CREATE INDEX IF NOT EXISTS "IDX_fulfillment_location_id" ON "fulfillment" (location_id) WHERE deleted_at IS NULL;'
524 )
525 this.addSql(
526 'CREATE INDEX IF NOT EXISTS "IDX_fulfillment_provider_id" ON "fulfillment" (provider_id) WHERE deleted_at IS NULL;'
527 )
528 this.addSql(
529 'CREATE INDEX IF NOT EXISTS "IDX_fulfillment_shipping_option_id" ON "fulfillment" (shipping_option_id) WHERE deleted_at IS NULL;'
530 )
531 this.addSql(
532 'CREATE INDEX IF NOT EXISTS "IDX_fulfillment_deleted_at" ON "fulfillment" (deleted_at) WHERE deleted_at IS NOT NULL;'
533 )
534
535 this.addSql(
536 'create table if not exists "fulfillment_label" ("id" text not null, "tracking_number" text not null, "tracking_url" text not null, "label_url" text not null, "fulfillment_id" text not null, "created_at" timestamptz not null default now(), "updated_at" timestamptz not null default now(), "deleted_at" timestamptz null, constraint "fulfillment_label_pkey" primary key ("id"));'
537 )
538 this.addSql(
539 'CREATE INDEX IF NOT EXISTS "IDX_fulfillment_label_fulfillment_id" ON "fulfillment_label" (fulfillment_id) WHERE deleted_at IS NULL;'
540 )
541 this.addSql(
542 'CREATE INDEX IF NOT EXISTS "IDX_fulfillment_label_deleted_at" ON "fulfillment_label" (deleted_at) WHERE deleted_at IS NOT NULL;'
543 )
544
545 this.addSql(
546 'create table if not exists "fulfillment_item" ("id" text not null, "title" text not null, "sku" text not null, "barcode" text not null, "quantity" numeric not null, "raw_quantity" jsonb not null, "line_item_id" text null, "inventory_item_id" text null, "fulfillment_id" text not null, "created_at" timestamptz not null default now(), "updated_at" timestamptz not null default now(), "deleted_at" timestamptz null, constraint "fulfillment_item_pkey" primary key ("id"));'
547 )
548 this.addSql(
549 'CREATE INDEX IF NOT EXISTS "IDX_fulfillment_item_line_item_id" ON "fulfillment_item" (line_item_id) WHERE deleted_at IS NULL;'
550 )
551 this.addSql(
552 'CREATE INDEX IF NOT EXISTS "IDX_fulfillment_item_inventory_item_id" ON "fulfillment_item" (inventory_item_id) WHERE deleted_at IS NULL;'
553 )
554 this.addSql(
555 'CREATE INDEX IF NOT EXISTS "IDX_fulfillment_item_fulfillment_id" ON "fulfillment_item" (fulfillment_id) WHERE deleted_at IS NULL;'
556 )
557 this.addSql(
558 'CREATE INDEX IF NOT EXISTS "IDX_fulfillment_item_deleted_at" ON "fulfillment_item" (deleted_at) WHERE deleted_at IS NOT NULL;'
559 )
560
561 this.addSql(
562 'alter table if exists "service_zone" add constraint "service_zone_fulfillment_set_id_foreign" foreign key ("fulfillment_set_id") references "fulfillment_set" ("id") on update cascade on delete cascade;'
563 )
564
565 this.addSql(
566 'alter table if exists "geo_zone" add constraint "geo_zone_service_zone_id_foreign" foreign key ("service_zone_id") references "service_zone" ("id") on update cascade on delete cascade;'
567 )
568
569 this.addSql(
570 'alter table if exists "shipping_option" add constraint "shipping_option_service_zone_id_foreign" foreign key ("service_zone_id") references "service_zone" ("id") on update cascade on delete cascade;'
571 )
572 this.addSql(
573 'alter table if exists "shipping_option" add constraint "shipping_option_shipping_profile_id_foreign" foreign key ("shipping_profile_id") references "shipping_profile" ("id") on update cascade on delete set null;'
574 )
575 this.addSql(
576 'alter table if exists "shipping_option" add constraint "shipping_option_provider_id_foreign" foreign key ("provider_id") references "fulfillment_provider" ("id") on update cascade on delete set null;'
577 )
578 this.addSql(
579 'alter table if exists "shipping_option" add constraint "shipping_option_shipping_option_type_id_foreign" foreign key ("shipping_option_type_id") references "shipping_option_type" ("id") on update cascade on delete cascade;'
580 )
581
582 this.addSql(
583 'alter table if exists "shipping_option_rule" add constraint "shipping_option_rule_shipping_option_id_foreign" foreign key ("shipping_option_id") references "shipping_option" ("id") on update cascade on delete cascade;'
584 )
585
586 this.addSql(
587 'alter table if exists "fulfillment" add constraint "fulfillment_provider_id_foreign" foreign key ("provider_id") references "fulfillment_provider" ("id") on update cascade on delete set null;'
588 )
589 this.addSql(
590 'alter table if exists "fulfillment" add constraint "fulfillment_shipping_option_id_foreign" foreign key ("shipping_option_id") references "shipping_option" ("id") on update cascade on delete set null;'
591 )
592 this.addSql(
593 'alter table if exists "fulfillment" add constraint "fulfillment_delivery_address_id_foreign" foreign key ("delivery_address_id") references "fulfillment_address" ("id") on update cascade on delete cascade;'
594 )
595
596 this.addSql(
597 'alter table if exists "fulfillment_label" add constraint "fulfillment_label_fulfillment_id_foreign" foreign key ("fulfillment_id") references "fulfillment" ("id") on update cascade on delete cascade;'
598 )
599
600 this.addSql(
601 'alter table if exists "fulfillment_item" add constraint "fulfillment_item_fulfillment_id_foreign" foreign key ("fulfillment_id") references "fulfillment" ("id") on update cascade on delete cascade;'
602 )
603 }