freshyo/packages/db_helper_sqlite/drizzle/0007_region_columns_backfill.sql
2026-10-07 13:36:48 +05:30

744 lines
37 KiB
SQL

-- Migration: rebuild the region-ownership FK subgraph with `region_id NOT NULL`
-- pointing at the Mahabubnagar region created in 0006, and repair legacy orphan
-- rows that were written before FK enforcement (they would fail the copy).
--
-- Single pass — no ALTER ADD COLUMN phase. `region_id` is added by the rebuild
-- itself and its value is computed at DB level in each copy SELECT
-- ((SELECT id FROM regions WHERE name='Mahabubnagar')), never by app defaults.
--
-- Follows docs/recreate-referenced-tables.md: create/copy parent-first, drop
-- leaf-first, rename parent-first, recreate indexes last. D1 enforces FKs and
-- ignores both FK pragmas, so the whole connected subgraph is rebuilt.
--
-- NOTE: re-running without resetting the database will fail (tables already
-- exist / columns already added). Reset the local D1 state and re-apply 0006
-- first when starting over.
-- =====================================================================
-- Repair legacy orphan rows (must run before any copy: the rebuilt tables
-- declare FKs, and inserting live orphan rows into them is a constraint
-- violation).
--
-- Live-DB findings this is sized for:
-- * 11 payments rows whose order no longer exists (all payment rows).
-- * 5 orders whose address no longer exists (their dependents deleted too,
-- per product-owner decision; purchase history for these is dropped).
-- =====================================================================
-- Orphan payments first (also removes payments of the orphan orders below).
DELETE FROM `payments` WHERE `order_id` NOT IN (SELECT `id` FROM `orders`);
--> statement-breakpoint
-- Detach coupon usage / complaints / incidents from the orphan orders before
-- removing the orders themselves (columns are nullable; deleting usage rows
-- entirely would also lose coupon history tied to surviving behavior).
UPDATE `coupon_usage` SET `order_item_id` = NULL
WHERE `order_item_id` IN (SELECT `oi`.`id` FROM `order_items` `oi` JOIN `orders` `o` ON `o`.`id` = `oi`.`order_id` WHERE `o`.`address_id` NOT IN (SELECT `id` FROM `addresses`));
--> statement-breakpoint
UPDATE `coupon_usage` SET `order_id` = NULL
WHERE `order_id` IN (SELECT `id` FROM `orders` WHERE `address_id` NOT IN (SELECT `id` FROM `addresses`));
--> statement-breakpoint
UPDATE `complaints` SET `order_id` = NULL
WHERE `order_id` IN (SELECT `id` FROM `orders` WHERE `address_id` NOT IN (SELECT `id` FROM `addresses`));
--> statement-breakpoint
UPDATE `user_incidents` SET `order_id` = NULL
WHERE `order_id` IN (SELECT `id` FROM `orders` WHERE `address_id` NOT IN (SELECT `id` FROM `addresses`));
--> statement-breakpoint
-- Delete the orphan orders' remaining hard dependents (order_status / refunds
-- included — their copies into the rebuilt tables would otherwise fail first).
DELETE FROM `refunds` WHERE `order_id` IN (SELECT `id` FROM `orders` WHERE `address_id` NOT IN (SELECT `id` FROM `addresses`));
--> statement-breakpoint
DELETE FROM `order_status` WHERE `order_id` IN (SELECT `id` FROM `orders` WHERE `address_id` NOT IN (SELECT `id` FROM `addresses`));
--> statement-breakpoint
DELETE FROM `order_items` WHERE `order_id` IN (SELECT `id` FROM `orders` WHERE `address_id` NOT IN (SELECT `id` FROM `addresses`));
--> statement-breakpoint
DELETE FROM `orders`
WHERE `id` IN (SELECT `id` FROM `orders` WHERE `address_id` NOT IN (SELECT `id` FROM `addresses`))
AND `address_id` NOT IN (SELECT `id` FROM `addresses`);
--> statement-breakpoint
-- Sanity: no more orphan rows against the tables we keep.
-- (Not verifiable in plain SQL execution; validated post-migration with
-- PRAGMA foreign_key_check and row counts.)
-- =====================================================================
-- Rebuild phase. 31 tables: create `__new_*` (parents first), copy all
-- columns, drop originals leaf-first, rename parents first, recreate indexes.
-- =====================================================================
CREATE TABLE `__new_staff_users` (
`id` integer PRIMARY KEY AUTOINCREMENT NOT NULL,
`name` text NOT NULL,
`password` text NOT NULL,
`staff_role_id` integer,
`created_at` text DEFAULT CURRENT_TIMESTAMP NOT NULL,
`region_id` integer NOT NULL,
FOREIGN KEY (`staff_role_id`) REFERENCES `staff_roles`(`id`) ON UPDATE no action ON DELETE no action,
FOREIGN KEY (`region_id`) REFERENCES `regions`(`id`) ON UPDATE no action ON DELETE no action
);
--> statement-breakpoint
INSERT INTO `__new_staff_users` (`id`,`name`,`password`,`staff_role_id`,`created_at`,`region_id`)
SELECT `id`,`name`,`password`,`staff_role_id`,`created_at`,(SELECT `id` FROM `regions` WHERE `name` = 'Mahabubnagar') FROM `staff_users`;
--> statement-breakpoint
CREATE TABLE `__new_staff_notif_creds` (
`id` integer PRIMARY KEY AUTOINCREMENT NOT NULL,
`token` text NOT NULL,
`added_at` text DEFAULT CURRENT_TIMESTAMP NOT NULL,
`staff_user_id` integer NOT NULL,
`last_verified` text,
FOREIGN KEY (`staff_user_id`) REFERENCES `__new_staff_users`(`id`) ON UPDATE no action ON DELETE no action
);
--> statement-breakpoint
INSERT INTO `__new_staff_notif_creds` (`id`,`token`,`added_at`,`staff_user_id`,`last_verified`)
SELECT `id`,`token`,`added_at`,`staff_user_id`,`last_verified` FROM `staff_notif_creds`;
--> statement-breakpoint
CREATE TABLE `__new_store_info` (
`id` integer PRIMARY KEY AUTOINCREMENT NOT NULL,
`name` text NOT NULL,
`description` text,
`image_url` text,
`created_at` text DEFAULT CURRENT_TIMESTAMP NOT NULL,
`owner` integer NOT NULL,
`region_id` integer NOT NULL,
FOREIGN KEY (`owner`) REFERENCES `__new_staff_users`(`id`) ON UPDATE no action ON DELETE no action,
FOREIGN KEY (`region_id`) REFERENCES `regions`(`id`) ON UPDATE no action ON DELETE no action
);
--> statement-breakpoint
INSERT INTO `__new_store_info` (`id`,`name`,`description`,`image_url`,`created_at`,`owner`,`region_id`)
SELECT `id`,`name`,`description`,`image_url`,`created_at`,`owner`,(SELECT `id` FROM `regions` WHERE `name` = 'Mahabubnagar') FROM `store_info`;
--> statement-breakpoint
CREATE TABLE `__new_product_info` (
`id` integer PRIMARY KEY AUTOINCREMENT NOT NULL,
`name` text NOT NULL,
`short_description` text,
`long_description` text,
`store_id` integer,
`increment_step` real DEFAULT 1 NOT NULL,
`created_at` text DEFAULT CURRENT_TIMESTAMP NOT NULL,
`product_type` text DEFAULT 'item' NOT NULL,
`region_id` integer NOT NULL,
FOREIGN KEY (`store_id`) REFERENCES `__new_store_info`(`id`) ON UPDATE no action ON DELETE no action,
FOREIGN KEY (`region_id`) REFERENCES `regions`(`id`) ON UPDATE no action ON DELETE no action
);
--> statement-breakpoint
INSERT INTO `__new_product_info` (`id`,`name`,`short_description`,`long_description`,`store_id`,`increment_step`,`created_at`,`product_type`,`region_id`)
SELECT `id`,`name`,`short_description`,`long_description`,`store_id`,`increment_step`,`created_at`,`product_type`,(SELECT `id` FROM `regions` WHERE `name` = 'Mahabubnagar') FROM `product_info`;
--> statement-breakpoint
CREATE TABLE `__new_product_tag_info` (
`id` integer PRIMARY KEY AUTOINCREMENT NOT NULL,
`tag_name` text NOT NULL,
`tag_description` text,
`image_url` text,
`is_dashboard_tag` integer DEFAULT false NOT NULL,
`related_stores` text,
`sort_order` text DEFAULT '[]',
`created_at` text DEFAULT CURRENT_TIMESTAMP NOT NULL,
`region_id` integer NOT NULL,
FOREIGN KEY (`region_id`) REFERENCES `regions`(`id`) ON UPDATE no action ON DELETE no action
);
--> statement-breakpoint
INSERT INTO `__new_product_tag_info` (`id`,`tag_name`,`tag_description`,`image_url`,`is_dashboard_tag`,`related_stores`,`sort_order`,`created_at`,`region_id`)
SELECT `id`,`tag_name`,`tag_description`,`image_url`,`is_dashboard_tag`,`related_stores`,`sort_order`,`created_at`,(SELECT `id` FROM `regions` WHERE `name` = 'Mahabubnagar') FROM `product_tag_info`;
--> statement-breakpoint
CREATE TABLE `__new_product_group_info` (
`id` integer PRIMARY KEY AUTOINCREMENT NOT NULL,
`group_name` text NOT NULL,
`description` text,
`created_at` text DEFAULT CURRENT_TIMESTAMP NOT NULL,
`region_id` integer NOT NULL,
FOREIGN KEY (`region_id`) REFERENCES `regions`(`id`) ON UPDATE no action ON DELETE no action
);
--> statement-breakpoint
INSERT INTO `__new_product_group_info` (`id`,`group_name`,`description`,`created_at`,`region_id`)
SELECT `id`,`group_name`,`description`,`created_at`,(SELECT `id` FROM `regions` WHERE `name` = 'Mahabubnagar') FROM `product_group_info`;
--> statement-breakpoint
CREATE TABLE `__new_delivery_slot_info` (
`id` integer PRIMARY KEY AUTOINCREMENT NOT NULL,
`delivery_time` text NOT NULL,
`freeze_time` text NOT NULL,
`is_active` integer DEFAULT true NOT NULL,
`is_flash` integer DEFAULT false NOT NULL,
`is_capacity_full` integer DEFAULT false NOT NULL,
`delivery_sequence` text,
`group_ids` text,
`sku_ids` text,
`region_id` integer NOT NULL,
FOREIGN KEY (`region_id`) REFERENCES `regions`(`id`) ON UPDATE no action ON DELETE no action
);
--> statement-breakpoint
INSERT INTO `__new_delivery_slot_info` (`id`,`delivery_time`,`freeze_time`,`is_active`,`is_flash`,`is_capacity_full`,`delivery_sequence`,`group_ids`,`sku_ids`,`region_id`)
SELECT `id`,`delivery_time`,`freeze_time`,`is_active`,`is_flash`,`is_capacity_full`,`delivery_sequence`,`group_ids`,`sku_ids`,(SELECT `id` FROM `regions` WHERE `name` = 'Mahabubnagar') FROM `delivery_slot_info`;
--> statement-breakpoint
CREATE TABLE `__new_vendor_snippets` (
`id` integer PRIMARY KEY AUTOINCREMENT NOT NULL,
`snippet_code` text NOT NULL,
`slot_id` integer,
`is_permanent` integer DEFAULT false NOT NULL,
`sku_ids` text NOT NULL,
`valid_till` text,
`created_at` text DEFAULT CURRENT_TIMESTAMP NOT NULL,
`region_id` integer NOT NULL,
FOREIGN KEY (`slot_id`) REFERENCES `__new_delivery_slot_info`(`id`) ON UPDATE no action ON DELETE no action,
FOREIGN KEY (`region_id`) REFERENCES `regions`(`id`) ON UPDATE no action ON DELETE no action
);
--> statement-breakpoint
INSERT INTO `__new_vendor_snippets` (`id`,`snippet_code`,`slot_id`,`is_permanent`,`sku_ids`,`valid_till`,`created_at`,`region_id`)
SELECT `id`,`snippet_code`,`slot_id`,`is_permanent`,`sku_ids`,`valid_till`,`created_at`,(SELECT `id` FROM `regions` WHERE `name` = 'Mahabubnagar') FROM `vendor_snippets`;
--> statement-breakpoint
CREATE TABLE `__new_home_banners` (
`id` integer PRIMARY KEY AUTOINCREMENT NOT NULL,
`name` text NOT NULL,
`image_url` text NOT NULL,
`description` text,
`sku_ids` text,
`redirect_url` text,
`serial_num` integer,
`is_active` integer DEFAULT false NOT NULL,
`created_at` text DEFAULT CURRENT_TIMESTAMP NOT NULL,
`last_updated` text DEFAULT CURRENT_TIMESTAMP NOT NULL,
`region_id` integer NOT NULL,
FOREIGN KEY (`region_id`) REFERENCES `regions`(`id`) ON UPDATE no action ON DELETE no action
);
--> statement-breakpoint
INSERT INTO `__new_home_banners` (`id`,`name`,`image_url`,`description`,`sku_ids`,`redirect_url`,`serial_num`,`is_active`,`created_at`,`last_updated`,`region_id`)
SELECT `id`,`name`,`image_url`,`description`,`sku_ids`,`redirect_url`,`serial_num`,`is_active`,`created_at`,`last_updated`,(SELECT `id` FROM `regions` WHERE `name` = 'Mahabubnagar') FROM `home_banners`;
--> statement-breakpoint
CREATE TABLE `__new_addresses` (
`id` integer PRIMARY KEY AUTOINCREMENT NOT NULL,
`user_id` integer NOT NULL,
`name` text NOT NULL,
`phone` text NOT NULL,
`address_line1` text NOT NULL,
`address_line2` text,
`city` text NOT NULL,
`state` text NOT NULL,
`pincode` text NOT NULL,
`is_default` integer DEFAULT false NOT NULL,
`latitude` real,
`longitude` real,
`google_maps_url` text,
`admin_latitude` real,
`admin_longitude` real,
`zone_id` integer,
`created_at` text DEFAULT CURRENT_TIMESTAMP NOT NULL,
`region_id` integer NOT NULL,
FOREIGN KEY (`user_id`) REFERENCES `users`(`id`) ON UPDATE no action ON DELETE no action,
FOREIGN KEY (`region_id`) REFERENCES `regions`(`id`) ON UPDATE no action ON DELETE no action
);
--> statement-breakpoint
INSERT INTO `__new_addresses` (`id`,`user_id`,`name`,`phone`,`address_line1`,`address_line2`,`city`,`state`,`pincode`,`is_default`,`latitude`,`longitude`,`google_maps_url`,`admin_latitude`,`admin_longitude`,`zone_id`,`created_at`,`region_id`)
SELECT `id`,`user_id`,`name`,`phone`,`address_line1`,`address_line2`,`city`,`state`,`pincode`,`is_default`,`latitude`,`longitude`,`google_maps_url`,`admin_latitude`,`admin_longitude`,`zone_id`,`created_at`,(SELECT `id` FROM `regions` WHERE `name` = 'Mahabubnagar') FROM `addresses`;
--> statement-breakpoint
CREATE TABLE `__new_coupons` (
`id` integer PRIMARY KEY AUTOINCREMENT NOT NULL,
`coupon_code` text NOT NULL,
`is_user_based` integer DEFAULT false NOT NULL,
`discount_percent` text,
`flat_discount` text,
`min_order` text,
`sku_ids` text,
`created_by` integer NOT NULL,
`max_value` text,
`is_apply_for_all` integer DEFAULT false NOT NULL,
`valid_till` text,
`max_limit_for_user` integer,
`is_invalidated` integer DEFAULT false NOT NULL,
`exclusive_apply` integer DEFAULT false NOT NULL,
`is_first_order_only` integer DEFAULT false NOT NULL,
`created_at` text DEFAULT CURRENT_TIMESTAMP NOT NULL,
`region_id` integer NOT NULL,
FOREIGN KEY (`created_by`) REFERENCES `__new_staff_users`(`id`) ON UPDATE no action ON DELETE no action,
FOREIGN KEY (`region_id`) REFERENCES `regions`(`id`) ON UPDATE no action ON DELETE no action
);
--> statement-breakpoint
INSERT INTO `__new_coupons` (`id`,`coupon_code`,`is_user_based`,`discount_percent`,`flat_discount`,`min_order`,`sku_ids`,`created_by`,`max_value`,`is_apply_for_all`,`valid_till`,`max_limit_for_user`,`is_invalidated`,`exclusive_apply`,`is_first_order_only`,`created_at`,`region_id`)
SELECT `id`,`coupon_code`,`is_user_based`,`discount_percent`,`flat_discount`,`min_order`,`sku_ids`,`created_by`,`max_value`,`is_apply_for_all`,`valid_till`,`max_limit_for_user`,`is_invalidated`,`exclusive_apply`,`is_first_order_only`,`created_at`,(SELECT `id` FROM `regions` WHERE `name` = 'Mahabubnagar') FROM `coupons`;
--> statement-breakpoint
CREATE TABLE `__new_product_skus` (
`id` integer PRIMARY KEY AUTOINCREMENT NOT NULL,
`product_id` integer NOT NULL,
`name` text,
`images` text,
`is_offer` integer DEFAULT false NOT NULL,
`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_offer`,`is_combo_only`,`is_deleted`,`created_at`)
SELECT `id`,`product_id`,`name`,`images`,`is_offer`,`is_combo_only`,`is_deleted`,`created_at` FROM `product_skus`;
--> statement-breakpoint
CREATE TABLE `__new_product_market_stats` (
`id` integer PRIMARY KEY AUTOINCREMENT NOT NULL,
`sku_id` integer NOT NULL,
`market_price` text,
`our_price` text NOT NULL,
`is_flash_available` integer DEFAULT false NOT NULL,
`flash_price` text,
`is_out_of_stock` integer DEFAULT false NOT NULL,
`is_suspended` integer DEFAULT false NOT NULL,
FOREIGN KEY (`sku_id`) REFERENCES `__new_product_skus`(`id`) ON UPDATE no action ON DELETE no action
);
--> statement-breakpoint
INSERT INTO `__new_product_market_stats` (`id`,`sku_id`,`market_price`,`our_price`,`is_flash_available`,`flash_price`,`is_out_of_stock`,`is_suspended`)
SELECT `id`,`sku_id`,`market_price`,`our_price`,`is_flash_available`,`flash_price`,`is_out_of_stock`,`is_suspended` 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
CREATE TABLE `__new_special_deals` (
`id` integer PRIMARY KEY AUTOINCREMENT NOT NULL,
`sku_id` integer NOT NULL,
`quantity` text NOT NULL,
`price` text 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_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_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 `__new_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 `__new_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_orders` (
`id` integer PRIMARY KEY AUTOINCREMENT NOT NULL,
`user_id` integer NOT NULL,
`address_id` integer NOT NULL,
`slot_id` integer,
`is_cod` integer DEFAULT false NOT NULL,
`is_online_payment` integer DEFAULT false NOT NULL,
`payment_info_id` integer,
`total_amount` text NOT NULL,
`delivery_charge` text DEFAULT '0' NOT NULL,
`readable_id` integer NOT NULL,
`admin_notes` text,
`user_notes` text,
`order_group_id` text,
`order_group_proportion` text,
`is_flash_delivery` integer DEFAULT false NOT NULL,
`created_at` text DEFAULT CURRENT_TIMESTAMP NOT NULL,
`delivered_at` text,
`region_id` integer NOT NULL,
FOREIGN KEY (`user_id`) REFERENCES `users`(`id`) ON UPDATE no action ON DELETE no action,
FOREIGN KEY (`address_id`) REFERENCES `__new_addresses`(`id`) ON UPDATE no action ON DELETE no action,
FOREIGN KEY (`slot_id`) REFERENCES `__new_delivery_slot_info`(`id`) ON UPDATE no action ON DELETE no action,
FOREIGN KEY (`payment_info_id`) REFERENCES `payment_info`(`id`) ON UPDATE no action ON DELETE no action,
FOREIGN KEY (`region_id`) REFERENCES `regions`(`id`) ON UPDATE no action ON DELETE no action
);
--> statement-breakpoint
INSERT INTO `__new_orders` (`id`,`user_id`,`address_id`,`slot_id`,`is_cod`,`is_online_payment`,`payment_info_id`,`total_amount`,`delivery_charge`,`readable_id`,`admin_notes`,`user_notes`,`order_group_id`,`order_group_proportion`,`is_flash_delivery`,`created_at`,`delivered_at`,`region_id`)
SELECT `id`,`user_id`,`address_id`,`slot_id`,`is_cod`,`is_online_payment`,`payment_info_id`,`total_amount`,`delivery_charge`,`readable_id`,`admin_notes`,`user_notes`,`order_group_id`,`order_group_proportion`,`is_flash_delivery`,`created_at`,`delivered_at`,(SELECT `id` FROM `regions` WHERE `name` = 'Mahabubnagar') FROM `orders`;
--> 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` text NOT NULL,
`discounted_price` text,
`is_packaged` integer DEFAULT false NOT NULL,
`is_package_verified` integer DEFAULT false NOT NULL,
`packaged_at` text,
FOREIGN KEY (`order_id`) REFERENCES `__new_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_order_status` (
`id` integer PRIMARY KEY AUTOINCREMENT NOT NULL,
`order_time` text DEFAULT CURRENT_TIMESTAMP NOT NULL,
`user_id` integer NOT NULL,
`order_id` integer NOT NULL,
`is_packaged` integer DEFAULT false NOT NULL,
`is_delivered` integer DEFAULT false NOT NULL,
`is_cancelled` integer DEFAULT false NOT NULL,
`cancel_reason` text,
`is_cancelled_by_admin` integer,
`payment_state` text DEFAULT 'pending' NOT NULL,
`cancellation_user_notes` text,
`cancellation_admin_notes` text,
`cancellation_reviewed` integer DEFAULT false NOT NULL,
`cancellation_reviewed_at` text,
`refund_coupon_id` integer,
FOREIGN KEY (`user_id`) REFERENCES `users`(`id`) ON UPDATE no action ON DELETE no action,
FOREIGN KEY (`order_id`) REFERENCES `__new_orders`(`id`) ON UPDATE no action ON DELETE no action,
FOREIGN KEY (`refund_coupon_id`) REFERENCES `__new_coupons`(`id`) ON UPDATE no action ON DELETE no action
);
--> statement-breakpoint
INSERT INTO `__new_order_status` (`id`,`order_time`,`user_id`,`order_id`,`is_packaged`,`is_delivered`,`is_cancelled`,`cancel_reason`,`is_cancelled_by_admin`,`payment_state`,`cancellation_user_notes`,`cancellation_admin_notes`,`cancellation_reviewed`,`cancellation_reviewed_at`,`refund_coupon_id`)
SELECT `id`,`order_time`,`user_id`,`order_id`,`is_packaged`,`is_delivered`,`is_cancelled`,`cancel_reason`,`is_cancelled_by_admin`,`payment_state`,`cancellation_user_notes`,`cancellation_admin_notes`,`cancellation_reviewed`,`cancellation_reviewed_at`,`refund_coupon_id` FROM `order_status`;
--> statement-breakpoint
CREATE TABLE `__new_refunds` (
`id` integer PRIMARY KEY AUTOINCREMENT NOT NULL,
`order_id` integer NOT NULL,
`refund_amount` text,
`refund_status` text DEFAULT 'none',
`merchant_refund_id` text,
`refund_processed_at` text,
`created_at` text DEFAULT CURRENT_TIMESTAMP NOT NULL,
FOREIGN KEY (`order_id`) REFERENCES `__new_orders`(`id`) ON UPDATE no action ON DELETE no action
);
--> statement-breakpoint
INSERT INTO `__new_refunds` (`id`,`order_id`,`refund_amount`,`refund_status`,`merchant_refund_id`,`refund_processed_at`,`created_at`)
SELECT `id`,`order_id`,`refund_amount`,`refund_status`,`merchant_refund_id`,`refund_processed_at`,`created_at` FROM `refunds`;
--> statement-breakpoint
CREATE TABLE `__new_payments` (
`id` integer PRIMARY KEY AUTOINCREMENT NOT NULL,
`status` text NOT NULL,
`gateway` text NOT NULL,
`order_id` integer NOT NULL,
`token` text,
`merchant_order_id` text NOT NULL,
`payload` text,
FOREIGN KEY (`order_id`) REFERENCES `__new_orders`(`id`) ON UPDATE no action ON DELETE no action
);
--> statement-breakpoint
INSERT INTO `__new_payments` (`id`,`status`,`gateway`,`order_id`,`token`,`merchant_order_id`,`payload`)
SELECT `id`,`status`,`gateway`,`order_id`,`token`,`merchant_order_id`,`payload` FROM `payments`;
--> 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 `__new_coupons`(`id`) ON UPDATE no action ON DELETE no action,
FOREIGN KEY (`order_id`) REFERENCES `__new_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_coupon_applicable_users` (
`id` integer PRIMARY KEY AUTOINCREMENT NOT NULL,
`coupon_id` integer NOT NULL,
`user_id` integer NOT NULL,
FOREIGN KEY (`coupon_id`) REFERENCES `__new_coupons`(`id`) ON UPDATE no action ON DELETE no action,
FOREIGN KEY (`user_id`) REFERENCES `users`(`id`) ON UPDATE no action ON DELETE no action
);
--> statement-breakpoint
INSERT INTO `__new_coupon_applicable_users` (`id`,`coupon_id`,`user_id`)
SELECT `id`,`coupon_id`,`user_id` FROM `coupon_applicable_users`;
--> 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 `__new_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_reserved_coupons` (
`id` integer PRIMARY KEY AUTOINCREMENT NOT NULL,
`secret_code` text NOT NULL,
`coupon_code` text NOT NULL,
`discount_percent` text,
`flat_discount` text,
`min_order` text,
`sku_ids` text,
`max_value` text,
`valid_till` text,
`max_limit_for_user` integer,
`exclusive_apply` integer DEFAULT false NOT NULL,
`is_first_order_only` integer DEFAULT false NOT NULL,
`is_redeemed` integer DEFAULT false NOT NULL,
`redeemed_by` integer,
`redeemed_at` text,
`created_by` integer NOT NULL,
`created_at` text DEFAULT CURRENT_TIMESTAMP NOT NULL,
`region_id` integer NOT NULL,
FOREIGN KEY (`redeemed_by`) REFERENCES `users`(`id`) ON UPDATE no action ON DELETE no action,
FOREIGN KEY (`created_by`) REFERENCES `__new_staff_users`(`id`) ON UPDATE no action ON DELETE no action,
FOREIGN KEY (`region_id`) REFERENCES `regions`(`id`) ON UPDATE no action ON DELETE no action
);
--> statement-breakpoint
INSERT INTO `__new_reserved_coupons` (`id`,`secret_code`,`coupon_code`,`discount_percent`,`flat_discount`,`min_order`,`sku_ids`,`max_value`,`valid_till`,`max_limit_for_user`,`exclusive_apply`,`is_first_order_only`,`is_redeemed`,`redeemed_by`,`redeemed_at`,`created_by`,`created_at`,`region_id`)
SELECT `id`,`secret_code`,`coupon_code`,`discount_percent`,`flat_discount`,`min_order`,`sku_ids`,`max_value`,`valid_till`,`max_limit_for_user`,`exclusive_apply`,`is_first_order_only`,`is_redeemed`,`redeemed_by`,`redeemed_at`,`created_by`,`created_at`,(SELECT `id` FROM `regions` WHERE `name` = 'Mahabubnagar') FROM `reserved_coupons`;
--> 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` text 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_complaints` (
`id` integer PRIMARY KEY AUTOINCREMENT NOT NULL,
`user_id` integer NOT NULL,
`order_id` integer,
`complaint_body` text NOT NULL,
`images` text,
`response` text,
`is_resolved` integer DEFAULT false NOT NULL,
`created_at` text DEFAULT CURRENT_TIMESTAMP NOT NULL,
FOREIGN KEY (`user_id`) REFERENCES `users`(`id`) ON UPDATE no action ON DELETE no action,
FOREIGN KEY (`order_id`) REFERENCES `__new_orders`(`id`) ON UPDATE no action ON DELETE no action
);
--> statement-breakpoint
INSERT INTO `__new_complaints` (`id`,`user_id`,`order_id`,`complaint_body`,`images`,`response`,`is_resolved`,`created_at`)
SELECT `id`,`user_id`,`order_id`,`complaint_body`,`images`,`response`,`is_resolved`,`created_at` FROM `complaints`;
--> statement-breakpoint
CREATE TABLE `__new_user_incidents` (
`id` integer PRIMARY KEY AUTOINCREMENT NOT NULL,
`user_id` integer NOT NULL,
`order_id` integer,
`date_added` text DEFAULT CURRENT_TIMESTAMP NOT NULL,
`admin_comment` text,
`added_by` integer,
`negativity_score` integer,
FOREIGN KEY (`user_id`) REFERENCES `users`(`id`) ON UPDATE no action ON DELETE no action,
FOREIGN KEY (`order_id`) REFERENCES `__new_orders`(`id`) ON UPDATE no action ON DELETE no action,
FOREIGN KEY (`added_by`) REFERENCES `__new_staff_users`(`id`) ON UPDATE no action ON DELETE no action
);
--> statement-breakpoint
INSERT INTO `__new_user_incidents` (`id`,`user_id`,`order_id`,`date_added`,`admin_comment`,`added_by`,`negativity_score`)
SELECT `id`,`user_id`,`order_id`,`date_added`,`admin_comment`,`added_by`,`negativity_score` FROM `user_incidents`;
--> statement-breakpoint
-- Drop originals leaf-first (nothing references a table when it is dropped).
DROP TABLE `user_incidents`;
--> statement-breakpoint
DROP TABLE `complaints`;
--> statement-breakpoint
DROP TABLE `cart_items`;
--> statement-breakpoint
DROP TABLE `reserved_coupons`;
--> statement-breakpoint
DROP TABLE `coupon_applicable_products`;
--> statement-breakpoint
DROP TABLE `coupon_applicable_users`;
--> statement-breakpoint
DROP TABLE `coupon_usage`;
--> statement-breakpoint
DROP TABLE `payments`;
--> statement-breakpoint
DROP TABLE `refunds`;
--> statement-breakpoint
DROP TABLE `order_status`;
--> statement-breakpoint
DROP TABLE `order_items`;
--> statement-breakpoint
DROP TABLE `orders`;
--> statement-breakpoint
DROP TABLE `product_group_membership`;
--> statement-breakpoint
DROP TABLE `product_tags`;
--> statement-breakpoint
DROP TABLE `product_reviews`;
--> statement-breakpoint
DROP TABLE `special_deals`;
--> statement-breakpoint
DROP TABLE `product_combos`;
--> statement-breakpoint
DROP TABLE `sku_features`;
--> statement-breakpoint
DROP TABLE `product_market_stats`;
--> statement-breakpoint
DROP TABLE `product_skus`;
--> statement-breakpoint
DROP TABLE `coupons`;
--> statement-breakpoint
DROP TABLE `addresses`;
--> statement-breakpoint
DROP TABLE `home_banners`;
--> statement-breakpoint
DROP TABLE `vendor_snippets`;
--> statement-breakpoint
DROP TABLE `delivery_slot_info`;
--> statement-breakpoint
DROP TABLE `product_group_info`;
--> statement-breakpoint
DROP TABLE `product_tag_info`;
--> statement-breakpoint
DROP TABLE `product_info`;
--> statement-breakpoint
DROP TABLE `store_info`;
--> statement-breakpoint
DROP TABLE `staff_notif_creds`;
--> statement-breakpoint
DROP TABLE `staff_users`;
--> statement-breakpoint
-- Promote replacements parent-first; SQLite rewrites child FK clauses on rename.
ALTER TABLE `__new_staff_users` RENAME TO `staff_users`;
--> statement-breakpoint
ALTER TABLE `__new_staff_notif_creds` RENAME TO `staff_notif_creds`;
--> statement-breakpoint
ALTER TABLE `__new_store_info` RENAME TO `store_info`;
--> statement-breakpoint
ALTER TABLE `__new_product_info` RENAME TO `product_info`;
--> statement-breakpoint
ALTER TABLE `__new_product_tag_info` RENAME TO `product_tag_info`;
--> statement-breakpoint
ALTER TABLE `__new_product_group_info` RENAME TO `product_group_info`;
--> statement-breakpoint
ALTER TABLE `__new_delivery_slot_info` RENAME TO `delivery_slot_info`;
--> statement-breakpoint
ALTER TABLE `__new_vendor_snippets` RENAME TO `vendor_snippets`;
--> statement-breakpoint
ALTER TABLE `__new_home_banners` RENAME TO `home_banners`;
--> statement-breakpoint
ALTER TABLE `__new_addresses` RENAME TO `addresses`;
--> statement-breakpoint
ALTER TABLE `__new_coupons` RENAME TO `coupons`;
--> statement-breakpoint
ALTER TABLE `__new_product_skus` RENAME TO `product_skus`;
--> 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
ALTER TABLE `__new_special_deals` RENAME TO `special_deals`;
--> statement-breakpoint
ALTER TABLE `__new_product_reviews` RENAME TO `product_reviews`;
--> 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_orders` RENAME TO `orders`;
--> statement-breakpoint
ALTER TABLE `__new_order_items` RENAME TO `order_items`;
--> statement-breakpoint
ALTER TABLE `__new_order_status` RENAME TO `order_status`;
--> statement-breakpoint
ALTER TABLE `__new_refunds` RENAME TO `refunds`;
--> statement-breakpoint
ALTER TABLE `__new_payments` RENAME TO `payments`;
--> statement-breakpoint
ALTER TABLE `__new_coupon_usage` RENAME TO `coupon_usage`;
--> statement-breakpoint
ALTER TABLE `__new_coupon_applicable_users` RENAME TO `coupon_applicable_users`;
--> statement-breakpoint
ALTER TABLE `__new_coupon_applicable_products` RENAME TO `coupon_applicable_products`;
--> statement-breakpoint
ALTER TABLE `__new_reserved_coupons` RENAME TO `reserved_coupons`;
--> statement-breakpoint
ALTER TABLE `__new_cart_items` RENAME TO `cart_items`;
--> statement-breakpoint
ALTER TABLE `__new_complaints` RENAME TO `complaints`;
--> statement-breakpoint
ALTER TABLE `__new_user_incidents` RENAME TO `user_incidents`;
--> statement-breakpoint
-- Recreate indexes after original names are released.
CREATE UNIQUE INDEX `staff_notif_creds_token_unique` ON `staff_notif_creds` (`token`);
--> statement-breakpoint
CREATE UNIQUE INDEX `product_tag_info_tag_name_unique` ON `product_tag_info` (`tag_name`);
--> statement-breakpoint
CREATE UNIQUE INDEX `vendor_snippets_snippet_code_unique` ON `vendor_snippets` (`snippet_code`);
--> statement-breakpoint
CREATE UNIQUE INDEX `coupons_coupon_code_unique` ON `coupons` (`coupon_code`);
--> statement-breakpoint
CREATE UNIQUE INDEX `product_market_stats_sku_id_unique` ON `product_market_stats` (`sku_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 `payments_merchant_order_id_unique` ON `payments` (`merchant_order_id`);
--> statement-breakpoint
CREATE UNIQUE INDEX `unique_coupon_user` ON `coupon_applicable_users` (`coupon_id`,`user_id`);
--> statement-breakpoint
CREATE UNIQUE INDEX `unique_coupon_sku` ON `coupon_applicable_products` (`coupon_id`,`sku_id`);
--> statement-breakpoint
CREATE UNIQUE INDEX `reserved_coupons_secret_code_unique` ON `reserved_coupons` (`secret_code`);
--> statement-breakpoint
CREATE UNIQUE INDEX `unique_user_sku` ON `cart_items` (`user_id`,`sku_id`);
--> statement-breakpoint
CREATE INDEX `idx_product_info_region` ON `product_info` (`region_id`);
--> statement-breakpoint
CREATE INDEX `idx_staff_users_region` ON `staff_users` (`region_id`);
--> statement-breakpoint
CREATE INDEX `idx_store_info_region` ON `store_info` (`region_id`);
--> statement-breakpoint
CREATE INDEX `idx_vendor_snippets_region` ON `vendor_snippets` (`region_id`);
--> statement-breakpoint
CREATE INDEX `idx_product_tag_info_region` ON `product_tag_info` (`region_id`);
--> statement-breakpoint
CREATE INDEX `idx_product_group_info_region` ON `product_group_info` (`region_id`);
--> statement-breakpoint
CREATE INDEX `idx_delivery_slot_info_region` ON `delivery_slot_info` (`region_id`);
--> statement-breakpoint
CREATE INDEX `idx_coupons_region` ON `coupons` (`region_id`);
--> statement-breakpoint
CREATE INDEX `idx_reserved_coupons_region` ON `reserved_coupons` (`region_id`);
--> statement-breakpoint
CREATE INDEX `idx_home_banners_region` ON `home_banners` (`region_id`);
--> statement-breakpoint
CREATE INDEX `idx_addresses_region` ON `addresses` (`region_id`);
--> statement-breakpoint
CREATE INDEX `idx_orders_region` ON `orders` (`region_id`);