freshyo/packages/db_helper_sqlite/drizzle/0007_region_scoping.sql
2026-10-05 20:43:25 +05:30

404 lines
No EOL
17 KiB
SQL

-- Migration: region scoping.
-- Adds region ownership across the platform and moves a product's store to the
-- per-region availability table. Existing rows are backfilled to region id 1.
--
-- product_info is rebuilt (create-copy-drop-rename) instead of using
-- ALTER TABLE DROP COLUMN, because store_id is part of a FOREIGN KEY definition
-- and SQLite refuses to drop columns used in FK constraints. The rebuild follows
-- the same pattern as 0002_sku_split.sql.
PRAGMA defer_foreign_keys = on;
--> statement-breakpoint
-- 1) region_id columns (backfill to region 1)
ALTER TABLE `staff_users` ADD COLUMN `region_id` integer NOT NULL DEFAULT 1;
--> statement-breakpoint
ALTER TABLE `addresses` ADD COLUMN `region_id` integer NOT NULL DEFAULT 1;
--> statement-breakpoint
ALTER TABLE `vendor_snippets` ADD COLUMN `region_id` integer NOT NULL DEFAULT 1;
--> statement-breakpoint
ALTER TABLE `store_info` ADD COLUMN `region_id` integer NOT NULL DEFAULT 1;
--> statement-breakpoint
ALTER TABLE `product_tag_info` ADD COLUMN `region_id` integer NOT NULL DEFAULT 1;
--> statement-breakpoint
ALTER TABLE `product_group_info` ADD COLUMN `region_id` integer NOT NULL DEFAULT 1;
--> statement-breakpoint
ALTER TABLE `delivery_slot_info` ADD COLUMN `region_id` integer NOT NULL DEFAULT 1;
--> statement-breakpoint
ALTER TABLE `home_banners` ADD COLUMN `region_id` integer NOT NULL DEFAULT 1;
--> statement-breakpoint
-- 2) move storeId from product_info to product_market_stats
ALTER TABLE `product_market_stats` ADD COLUMN `region_id` integer NOT NULL DEFAULT 1;
--> statement-breakpoint
ALTER TABLE `product_market_stats` ADD COLUMN `store_id` integer;
--> statement-breakpoint
-- 3) backfill market-stats store_id from the old product_info.store_id (before the rebuild drops it)
UPDATE `product_market_stats`
SET `store_id` = (
SELECT `product_info`.`store_id`
FROM `product_skus`
JOIN `product_info` ON `product_info`.`id` = `product_skus`.`product_id`
WHERE `product_skus`.`id` = `product_market_stats`.`sku_id`
);
--> statement-breakpoint
-- 3b) move is_offer from product_skus to product_market_stats (per-region flag)
ALTER TABLE `product_market_stats` ADD COLUMN `is_offer` integer NOT NULL DEFAULT 0;
--> statement-breakpoint
UPDATE `product_market_stats`
SET `is_offer` = (
SELECT `product_skus`.`is_offer`
FROM `product_skus`
WHERE `product_skus`.`id` = `product_market_stats`.`sku_id`
);
--> statement-breakpoint
ALTER TABLE `product_skus` DROP COLUMN `is_offer`;
--> statement-breakpoint
-- 4) rebuild product_info: drops store_id (with its FK), adds region_id + available_in_all_regions
--
-- D1 enforces foreign keys and ignores defer_foreign_keys / foreign_keys=OFF,
-- so a plain create-copy-drop-rename of product_info dies at DROP TABLE while
-- product_skus / product_reviews hold rows — no matter which executor is used.
-- Instead the whole FK subgraph is rebuilt (13 tables), leaves first:
-- every step below is individually FK-legal, so per-statement autocommit is fine.
-- An audit of the schema showed coupon_usage.order_item_id -> order_items(id),
-- so order_items is NOT a leaf and coupon_usage is rebuilt too.
-- Two deliberate deviations from drizzle-kit rendering: the rating_check CHECK
-- is unqualified (RENAME TABLE does not rewrite CHECK expressions, a qualified
-- name would dangle after the rename), and __new_ FKs point at __new_ names
-- (rewritten to the final names by the renames). Indexes are created last,
-- after the drops free their names. All NOT NULLs are preserved.
CREATE TABLE `__new_product_info` (
`id` integer PRIMARY KEY AUTOINCREMENT NOT NULL,
`name` text NOT NULL,
`short_description` text,
`long_description` text,
-- NULL when the product is available in all regions. Existing rows belong to region 1.
`region_id` integer,
`available_in_all_regions` integer NOT NULL DEFAULT 0,
`increment_step` real DEFAULT 1 NOT NULL,
`created_at` text DEFAULT CURRENT_TIMESTAMP NOT NULL,
`product_type` text DEFAULT 'item' NOT NULL
);
--> statement-breakpoint
INSERT INTO `__new_product_info` (
`id`, `name`, `short_description`, `long_description`, `region_id`, `available_in_all_regions`, `increment_step`, `created_at`, `product_type`
)
SELECT
`id`, `name`, `short_description`, `long_description`, 1, 0, `increment_step`, `created_at`, `product_type`
FROM `product_info`;
--> statement-breakpoint
CREATE TABLE `__new_product_skus` (
`id` integer PRIMARY KEY AUTOINCREMENT NOT NULL,
`product_id` integer NOT NULL,
`name` text,
`images` text,
`is_combo_only` integer DEFAULT false NOT NULL,
`is_deleted` integer DEFAULT false NOT NULL,
`created_at` text DEFAULT CURRENT_TIMESTAMP NOT NULL,
FOREIGN KEY (`product_id`) REFERENCES `__new_product_info`(`id`) ON UPDATE no action ON DELETE no action
);
--> statement-breakpoint
INSERT INTO `__new_product_skus` (
`id`, `product_id`, `name`, `images`, `is_combo_only`, `is_deleted`, `created_at`
)
SELECT
`id`, `product_id`, `name`, `images`, `is_combo_only`, `is_deleted`, `created_at`
FROM `product_skus`;
--> statement-breakpoint
CREATE TABLE `__new_product_reviews` (
`id` integer PRIMARY KEY AUTOINCREMENT NOT NULL,
`user_id` integer NOT NULL,
`product_id` integer NOT NULL,
`review_body` text NOT NULL,
`image_urls` text,
`review_time` text DEFAULT CURRENT_TIMESTAMP NOT NULL,
`ratings` real NOT NULL,
`admin_response` text,
`admin_response_images` text,
FOREIGN KEY (`user_id`) REFERENCES `users`(`id`) ON UPDATE no action ON DELETE no action,
FOREIGN KEY (`product_id`) REFERENCES `__new_product_info`(`id`) ON UPDATE no action ON DELETE no action,
CONSTRAINT "rating_check" CHECK(`ratings` >= 1 AND `ratings` <= 5)
);
--> statement-breakpoint
INSERT INTO `__new_product_reviews` (
`id`, `user_id`, `product_id`, `review_body`, `image_urls`, `review_time`, `ratings`, `admin_response`, `admin_response_images`
)
SELECT
`id`, `user_id`, `product_id`, `review_body`, `image_urls`, `review_time`, `ratings`, `admin_response`, `admin_response_images`
FROM `product_reviews`;
--> statement-breakpoint
CREATE TABLE `__new_order_items` (
`id` integer PRIMARY KEY AUTOINCREMENT NOT NULL,
`order_id` integer NOT NULL,
`sku_id` integer NOT NULL,
`quantity` text NOT NULL,
`price` numeric NOT NULL,
`discounted_price` numeric,
`is_packaged` integer DEFAULT false NOT NULL,
`is_package_verified` integer DEFAULT false NOT NULL,
`packaged_at` text,
FOREIGN KEY (`order_id`) REFERENCES `orders`(`id`) ON UPDATE no action ON DELETE no action,
FOREIGN KEY (`sku_id`) REFERENCES `__new_product_skus`(`id`) ON UPDATE no action ON DELETE no action
);
--> statement-breakpoint
INSERT INTO `__new_order_items` (
`id`, `order_id`, `sku_id`, `quantity`, `price`, `discounted_price`, `is_packaged`, `is_package_verified`, `packaged_at`
)
SELECT
`id`, `order_id`, `sku_id`, `quantity`, `price`, `discounted_price`, `is_packaged`, `is_package_verified`, `packaged_at`
FROM `order_items`;
--> statement-breakpoint
CREATE TABLE `__new_coupon_usage` (
`id` integer PRIMARY KEY AUTOINCREMENT NOT NULL,
`user_id` integer NOT NULL,
`coupon_id` integer NOT NULL,
`order_id` integer,
`order_item_id` integer,
`used_at` text DEFAULT CURRENT_TIMESTAMP NOT NULL,
FOREIGN KEY (`user_id`) REFERENCES `users`(`id`) ON UPDATE no action ON DELETE no action,
FOREIGN KEY (`coupon_id`) REFERENCES `coupons`(`id`) ON UPDATE no action ON DELETE no action,
FOREIGN KEY (`order_id`) REFERENCES `orders`(`id`) ON UPDATE no action ON DELETE no action,
FOREIGN KEY (`order_item_id`) REFERENCES `__new_order_items`(`id`) ON UPDATE no action ON DELETE no action
);
--> statement-breakpoint
INSERT INTO `__new_coupon_usage` (
`id`, `user_id`, `coupon_id`, `order_id`, `order_item_id`, `used_at`
)
SELECT
`id`, `user_id`, `coupon_id`, `order_id`, `order_item_id`, `used_at`
FROM `coupon_usage`;
--> statement-breakpoint
CREATE TABLE `__new_cart_items` (
`id` integer PRIMARY KEY AUTOINCREMENT NOT NULL,
`user_id` integer NOT NULL,
`sku_id` integer NOT NULL,
`quantity` numeric NOT NULL,
`added_at` text DEFAULT CURRENT_TIMESTAMP NOT NULL,
FOREIGN KEY (`user_id`) REFERENCES `users`(`id`) ON UPDATE no action ON DELETE no action,
FOREIGN KEY (`sku_id`) REFERENCES `__new_product_skus`(`id`) ON UPDATE no action ON DELETE no action
);
--> statement-breakpoint
INSERT INTO `__new_cart_items` (`id`, `user_id`, `sku_id`, `quantity`, `added_at`)
SELECT `id`, `user_id`, `sku_id`, `quantity`, `added_at` FROM `cart_items`;
--> statement-breakpoint
CREATE TABLE `__new_special_deals` (
`id` integer PRIMARY KEY AUTOINCREMENT NOT NULL,
`sku_id` integer NOT NULL,
`quantity` numeric NOT NULL,
`price` numeric NOT NULL,
`valid_till` text NOT NULL,
FOREIGN KEY (`sku_id`) REFERENCES `__new_product_skus`(`id`) ON UPDATE no action ON DELETE no action
);
--> statement-breakpoint
INSERT INTO `__new_special_deals` (`id`, `sku_id`, `quantity`, `price`, `valid_till`)
SELECT `id`, `sku_id`, `quantity`, `price`, `valid_till` FROM `special_deals`;
--> statement-breakpoint
CREATE TABLE `__new_coupon_applicable_products` (
`id` integer PRIMARY KEY AUTOINCREMENT NOT NULL,
`coupon_id` integer NOT NULL,
`sku_id` integer NOT NULL,
FOREIGN KEY (`coupon_id`) REFERENCES `coupons`(`id`) ON UPDATE no action ON DELETE no action,
FOREIGN KEY (`sku_id`) REFERENCES `__new_product_skus`(`id`) ON UPDATE no action ON DELETE no action
);
--> statement-breakpoint
INSERT INTO `__new_coupon_applicable_products` (`id`, `coupon_id`, `sku_id`)
SELECT `id`, `coupon_id`, `sku_id` FROM `coupon_applicable_products`;
--> statement-breakpoint
CREATE TABLE `__new_product_tags` (
`id` integer PRIMARY KEY AUTOINCREMENT NOT NULL,
`product_id` integer NOT NULL,
`tag_id` integer NOT NULL,
`assigned_at` text DEFAULT CURRENT_TIMESTAMP NOT NULL,
FOREIGN KEY (`product_id`) REFERENCES `__new_product_skus`(`id`) ON UPDATE no action ON DELETE no action,
FOREIGN KEY (`tag_id`) REFERENCES `product_tag_info`(`id`) ON UPDATE no action ON DELETE no action
);
--> statement-breakpoint
INSERT INTO `__new_product_tags` (`id`, `product_id`, `tag_id`, `assigned_at`)
SELECT `id`, `product_id`, `tag_id`, `assigned_at` FROM `product_tags`;
--> statement-breakpoint
CREATE TABLE `__new_product_group_membership` (
`product_id` integer NOT NULL,
`group_id` integer NOT NULL,
`added_at` text DEFAULT CURRENT_TIMESTAMP NOT NULL,
PRIMARY KEY(`product_id`, `group_id`),
FOREIGN KEY (`product_id`) REFERENCES `__new_product_skus`(`id`) ON UPDATE no action ON DELETE no action,
FOREIGN KEY (`group_id`) REFERENCES `product_group_info`(`id`) ON UPDATE no action ON DELETE no action
);
--> statement-breakpoint
INSERT INTO `__new_product_group_membership` (`product_id`, `group_id`, `added_at`)
SELECT `product_id`, `group_id`, `added_at` FROM `product_group_membership`;
--> statement-breakpoint
CREATE TABLE `__new_product_market_stats` (
`id` integer PRIMARY KEY AUTOINCREMENT NOT NULL,
`sku_id` integer NOT NULL,
`region_id` integer NOT NULL,
`store_id` integer,
`market_price` numeric,
`our_price` numeric NOT NULL,
`is_flash_available` integer DEFAULT false NOT NULL,
`flash_price` numeric,
`is_out_of_stock` integer DEFAULT false NOT NULL,
`is_suspended` integer DEFAULT false NOT NULL,
`is_offer` integer DEFAULT false NOT NULL,
FOREIGN KEY (`sku_id`) REFERENCES `__new_product_skus`(`id`) ON UPDATE no action ON DELETE no action,
FOREIGN KEY (`region_id`) REFERENCES `regions`(`id`) ON UPDATE no action ON DELETE no action,
FOREIGN KEY (`store_id`) REFERENCES `store_info`(`id`) ON UPDATE no action ON DELETE no action
);
--> statement-breakpoint
INSERT INTO `__new_product_market_stats` (
`id`, `sku_id`, `region_id`, `store_id`, `market_price`, `our_price`, `is_flash_available`, `flash_price`, `is_out_of_stock`, `is_suspended`, `is_offer`
)
SELECT
`id`, `sku_id`, `region_id`, `store_id`, `market_price`, `our_price`, `is_flash_available`, `flash_price`, `is_out_of_stock`, `is_suspended`, `is_offer`
FROM `product_market_stats`;
--> statement-breakpoint
CREATE TABLE `__new_sku_features` (
`id` integer PRIMARY KEY AUTOINCREMENT NOT NULL,
`sku_id` integer NOT NULL,
`feature_name` text,
`feature_value` text NOT NULL,
FOREIGN KEY (`sku_id`) REFERENCES `__new_product_skus`(`id`) ON UPDATE no action ON DELETE no action
);
--> statement-breakpoint
INSERT INTO `__new_sku_features` (`id`, `sku_id`, `feature_name`, `feature_value`)
SELECT `id`, `sku_id`, `feature_name`, `feature_value` FROM `sku_features`;
--> statement-breakpoint
CREATE TABLE `__new_product_combos` (
`id` integer PRIMARY KEY AUTOINCREMENT NOT NULL,
`combo_sku_id` integer NOT NULL,
`sku_id` integer NOT NULL,
FOREIGN KEY (`combo_sku_id`) REFERENCES `__new_product_skus`(`id`) ON UPDATE no action ON DELETE no action,
FOREIGN KEY (`sku_id`) REFERENCES `__new_product_skus`(`id`) ON UPDATE no action ON DELETE no action
);
--> statement-breakpoint
INSERT INTO `__new_product_combos` (`id`, `combo_sku_id`, `sku_id`)
SELECT `id`, `combo_sku_id`, `sku_id` FROM `product_combos`;
--> statement-breakpoint
-- 4b) drop the originals, leaves first. Each is unreferenced at drop time.
DROP TABLE `coupon_usage`;
--> statement-breakpoint
DROP TABLE `cart_items`;
--> statement-breakpoint
DROP TABLE `special_deals`;
--> statement-breakpoint
DROP TABLE `coupon_applicable_products`;
--> statement-breakpoint
DROP TABLE `product_tags`;
--> statement-breakpoint
DROP TABLE `product_group_membership`;
--> statement-breakpoint
DROP TABLE `product_market_stats`;
--> statement-breakpoint
DROP TABLE `sku_features`;
--> statement-breakpoint
DROP TABLE `product_combos`;
--> statement-breakpoint
DROP TABLE `order_items`;
--> statement-breakpoint
DROP TABLE `product_reviews`;
--> statement-breakpoint
DROP TABLE `product_skus`;
--> statement-breakpoint
DROP TABLE `product_info`;
--> statement-breakpoint
-- 4c) promote the replacements. Child FK clauses auto-rewrite to final names.
ALTER TABLE `__new_product_info` RENAME TO `product_info`;
--> statement-breakpoint
ALTER TABLE `__new_product_skus` RENAME TO `product_skus`;
--> statement-breakpoint
ALTER TABLE `__new_product_reviews` RENAME TO `product_reviews`;
--> statement-breakpoint
ALTER TABLE `__new_order_items` RENAME TO `order_items`;
--> statement-breakpoint
ALTER TABLE `__new_coupon_usage` RENAME TO `coupon_usage`;
--> statement-breakpoint
ALTER TABLE `__new_cart_items` RENAME TO `cart_items`;
--> statement-breakpoint
ALTER TABLE `__new_special_deals` RENAME TO `special_deals`;
--> statement-breakpoint
ALTER TABLE `__new_coupon_applicable_products` RENAME TO `coupon_applicable_products`;
--> statement-breakpoint
ALTER TABLE `__new_product_tags` RENAME TO `product_tags`;
--> statement-breakpoint
ALTER TABLE `__new_product_group_membership` RENAME TO `product_group_membership`;
--> statement-breakpoint
ALTER TABLE `__new_product_market_stats` RENAME TO `product_market_stats`;
--> statement-breakpoint
ALTER TABLE `__new_sku_features` RENAME TO `sku_features`;
--> statement-breakpoint
ALTER TABLE `__new_product_combos` RENAME TO `product_combos`;
--> statement-breakpoint
-- 4d) recreate the indexes (names were freed by the drops above).
CREATE UNIQUE INDEX `unique_sku_region` ON `product_market_stats` (`sku_id`, `region_id`);
--> statement-breakpoint
CREATE UNIQUE INDEX `unique_sku_feature_name` ON `sku_features` (`sku_id`,`feature_name`);
--> statement-breakpoint
CREATE UNIQUE INDEX `unique_combo_sku_item` ON `product_combos` (`combo_sku_id`,`sku_id`);
--> statement-breakpoint
CREATE UNIQUE INDEX `unique_product_tag` ON `product_tags` (`product_id`,`tag_id`);
--> statement-breakpoint
CREATE UNIQUE INDEX `unique_user_sku` ON `cart_items` (`user_id`,`sku_id`);
--> statement-breakpoint
CREATE UNIQUE INDEX `unique_coupon_sku` ON `coupon_applicable_products` (`coupon_id`,`sku_id`);
--> statement-breakpoint
-- 6) region- and bound-scoped app constants.
-- New region_consts table holds the admin customize-app visible fields per
-- region (bound_id NULL = region-level fallback) and per bound. Existing
-- values are backfilled from key_val_store for every region, then removed
-- from key_val_store so the global table keeps global-only keys.
CREATE TABLE `region_consts` (
`id` integer PRIMARY KEY AUTOINCREMENT NOT NULL,
`region_id` integer NOT NULL REFERENCES `regions`(`id`),
`bound_id` integer REFERENCES `region_bounds`(`id`),
`key` text NOT NULL,
`value` text
);
--> statement-breakpoint
CREATE UNIQUE INDEX `region_consts_region_key_bound_unique`
ON `region_consts` (`region_id`, `key`, `bound_id`);
--> statement-breakpoint
INSERT INTO `region_consts` (`region_id`, `bound_id`, `key`, `value`)
SELECT `regions`.`id`, NULL, `key_val_store`.`key`, `key_val_store`.`value`
FROM `regions`
CROSS JOIN `key_val_store`
WHERE `key_val_store`.`key` IN (
'minRegularOrderValue',
'freeDeliveryThreshold',
'deliveryCharge',
'flashFreeDeliveryThreshold',
'flashDeliveryCharge',
'allItemsOrder',
'isFlashDeliveryEnabled',
'supportMobile',
'supportEmail',
'welcomeMessage'
);
--> statement-breakpoint
DELETE FROM `key_val_store`
WHERE `key` IN (
'minRegularOrderValue',
'freeDeliveryThreshold',
'deliveryCharge',
'flashFreeDeliveryThreshold',
'flashDeliveryCharge',
'allItemsOrder',
'isFlashDeliveryEnabled',
'supportMobile',
'supportEmail',
'welcomeMessage'
);
--> statement-breakpoint
-- 7) tag names are unique per region, not globally.
DROP INDEX IF EXISTS `product_tag_info_tag_name_unique`;
--> statement-breakpoint
CREATE UNIQUE INDEX IF NOT EXISTS `unique_tag_name_region`
ON `product_tag_info` (`tag_name`, `region_id`);
--> statement-breakpoint
PRAGMA defer_foreign_keys = off;