======================================================================
STALE REGIONALIZATION MIGRATIONS (removed 2026-10-08 19:13:57)
Replaced by 0006_regions_and_superadmins.sql (squashed 0006-0009).
Contents below are the verbatim pre-removal files.
======================================================================

======================================================================
FILE: 0006_regions_and_superadmins.sql
======================================================================
-- Migration: regions, region bounds, per-bound settings, and superadmin identities.
--
-- Region "Mahabubnagar" is created and all current data will be backfilled to it
-- in a later migration (backend phase). This migration only creates the new
-- tables and seeds the region, its two bounds, per-bound settings, and the
-- bootstrap superadmin account.
--
-- NOTE: SQLite has no `ADD COLUMN IF NOT EXISTS`; tables use IF NOT EXISTS so the
-- file is safe if re-applied, but rely on the migration journal to run it once.

CREATE TABLE IF NOT EXISTS `regions` (
	`id` integer PRIMARY KEY AUTOINCREMENT NOT NULL,
	`name` text NOT NULL,
	`description` text,
	`created_at` text DEFAULT (CURRENT_TIMESTAMP) NOT NULL
);
--> statement-breakpoint
CREATE TABLE IF NOT EXISTS `region_bounds` (
	`id` integer PRIMARY KEY AUTOINCREMENT NOT NULL,
	`region_id` integer NOT NULL,
	`description` text,
	`geojson` text NOT NULL,
	`priority` text NOT NULL,
	FOREIGN KEY (`region_id`) REFERENCES `regions`(`id`) ON UPDATE no action ON DELETE no action
);
--> statement-breakpoint
CREATE UNIQUE INDEX IF NOT EXISTS `unique_region_bound_priority` ON `region_bounds` (`region_id`, `priority`);
--> statement-breakpoint
CREATE TABLE IF NOT EXISTS `region_bound_settings` (
	`id` integer PRIMARY KEY AUTOINCREMENT NOT NULL,
	`region_id` integer NOT NULL,
	`bound_id` integer NOT NULL,
	`key` text NOT NULL,
	`value` text,
	FOREIGN KEY (`region_id`) REFERENCES `regions`(`id`) ON UPDATE no action ON DELETE no action,
	FOREIGN KEY (`bound_id`) REFERENCES `region_bounds`(`id`) ON UPDATE no action ON DELETE no action
);
--> statement-breakpoint
CREATE UNIQUE INDEX IF NOT EXISTS `unique_region_bound_setting_key` ON `region_bound_settings` (`bound_id`, `key`);
--> statement-breakpoint
CREATE TABLE IF NOT EXISTS `superadmins` (
	`id` integer PRIMARY KEY AUTOINCREMENT NOT NULL,
	`username` text NOT NULL,
	`password_hash` text NOT NULL,
	`created_at` text DEFAULT (CURRENT_TIMESTAMP) NOT NULL
);
--> statement-breakpoint
CREATE UNIQUE INDEX IF NOT EXISTS `unique_superadmin_username` ON `superadmins` (`username`);
--> statement-breakpoint
-- Seed region.
INSERT INTO `regions` (`name`, `description`)
SELECT 'Mahabubnagar', 'Default region (all pre-existing data backfilled here)'
WHERE NOT EXISTS (SELECT 1 FROM `regions` WHERE `name` = 'Mahabubnagar');
--> statement-breakpoint
-- Seed bounds (exact GeoJSON from apps/backend/src/map-assets/Mbnr-1.geojson / Mbnr-2.geojson).
INSERT INTO `region_bounds` (`region_id`, `description`, `geojson`, `priority`)
SELECT id, 'Mbnr-1', '{"type":"FeatureCollection","features":[{"type":"Feature","properties":{"name":"Polygon 4"},"geometry":{"type":"Polygon","coordinates":[[[77.9802307,16.7224827],[77.9832452,16.7186281],[77.9885003,16.7191643],[78.0027771,16.7184524],[78.0167635,16.717821],[78.0176657,16.7225884],[78.0245685,16.7301059],[78.0346065,16.7366045],[78.0483126,16.7469324],[78.0558892,16.7506815],[78.0533183,16.7547082],[78.0497798,16.7596596],[78.0456892,16.7640471],[78.0383339,16.7641426],[78.0343234,16.758687],[78.021375,16.7570244],[78.0163704,16.758279],[78.0128516,16.7590231],[78.0120219,16.7648881],[78.0095524,16.7664414],[77.99363,16.7660548],[77.9720422,16.7596124],[77.9679337,16.7449906],[77.9802307,16.7224827]]]}}]}', '1'
FROM `regions` WHERE `name` = 'Mahabubnagar'
AND NOT EXISTS (SELECT 1 FROM `region_bounds` WHERE `priority` = '1' AND `region_id` = (SELECT id FROM `regions` WHERE `name` = 'Mahabubnagar'));
--> statement-breakpoint
INSERT INTO `region_bounds` (`region_id`, `description`, `geojson`, `priority`)
SELECT id, 'Mbnr-2', '{"type":"FeatureCollection","features":[{"type":"Feature","properties":{"name":"Mbnr-2"},"geometry":{"type":"Polygon","coordinates":[[[77.9773739,16.7127921],[77.9950359,16.7080928],[78.0187929,16.70999],[78.0289444,16.7211582],[78.0570875,16.7502883],[78.0662922,16.7519867],[78.0652719,16.7684088],[78.0204341,16.7689639],[78.0023161,16.770186],[77.9984809,16.7850848],[77.983305,16.782843],[77.971574,16.7646028],[77.9686551,16.7454905],[77.9773739,16.7127921]]]}}]}', '2'
FROM `regions` WHERE `name` = 'Mahabubnagar'
AND NOT EXISTS (SELECT 1 FROM `region_bounds` WHERE `priority` = '2' AND `region_id` = (SELECT id FROM `regions` WHERE `name` = 'Mahabubnagar'));
--> statement-breakpoint
-- Seed per-bound business settings (same values as the former global constants) for every bound.
INSERT INTO `region_bound_settings` (`region_id`, `bound_id`, `key`, `value`)
SELECT r.id, b.id, 'minRegularOrderValue', '300' FROM `regions` r JOIN `region_bounds` b ON b.region_id = r.id WHERE r.name = 'Mahabubnagar';
--> statement-breakpoint
INSERT INTO `region_bound_settings` (`region_id`, `bound_id`, `key`, `value`)
SELECT r.id, b.id, 'freeDeliveryThreshold', '300' FROM `regions` r JOIN `region_bounds` b ON b.region_id = r.id WHERE r.name = 'Mahabubnagar';
--> statement-breakpoint
INSERT INTO `region_bound_settings` (`region_id`, `bound_id`, `key`, `value`)
SELECT r.id, b.id, 'deliveryCharge', '20' FROM `regions` r JOIN `region_bounds` b ON b.region_id = r.id WHERE r.name = 'Mahabubnagar';
--> statement-breakpoint
INSERT INTO `region_bound_settings` (`region_id`, `bound_id`, `key`, `value`)
SELECT r.id, b.id, 'flashFreeDeliveryThreshold', '500' FROM `regions` r JOIN `region_bounds` b ON b.region_id = r.id WHERE r.name = 'Mahabubnagar';
--> statement-breakpoint
INSERT INTO `region_bound_settings` (`region_id`, `bound_id`, `key`, `value`)
SELECT r.id, b.id, 'flashDeliveryCharge', '69' FROM `regions` r JOIN `region_bounds` b ON b.region_id = r.id WHERE r.name = 'Mahabubnagar';
--> statement-breakpoint
INSERT INTO `region_bound_settings` (`region_id`, `bound_id`, `key`, `value`)
SELECT r.id, b.id, 'popularItems', '[]' FROM `regions` r JOIN `region_bounds` b ON b.region_id = r.id WHERE r.name = 'Mahabubnagar';
--> statement-breakpoint
INSERT INTO `region_bound_settings` (`region_id`, `bound_id`, `key`, `value`)
SELECT r.id, b.id, 'allItemsOrder', '[]' FROM `regions` r JOIN `region_bounds` b ON b.region_id = r.id WHERE r.name = 'Mahabubnagar';
--> statement-breakpoint
INSERT INTO `region_bound_settings` (`region_id`, `bound_id`, `key`, `value`)
SELECT r.id, b.id, 'isFlashDeliveryEnabled', 'false' FROM `regions` r JOIN `region_bounds` b ON b.region_id = r.id WHERE r.name = 'Mahabubnagar';
--> statement-breakpoint
INSERT INTO `region_bound_settings` (`region_id`, `bound_id`, `key`, `value`)
SELECT r.id, b.id, 'supportMobile', '8688182552' FROM `regions` r JOIN `region_bounds` b ON b.region_id = r.id WHERE r.name = 'Mahabubnagar';
--> statement-breakpoint
INSERT INTO `region_bound_settings` (`region_id`, `bound_id`, `key`, `value`)
SELECT r.id, b.id, 'supportEmail', 'qushammohd@gmail.com' FROM `regions` r JOIN `region_bounds` b ON b.region_id = r.id WHERE r.name = 'Mahabubnagar';
--> statement-breakpoint
INSERT INTO `region_bound_settings` (`region_id`, `bound_id`, `key`, `value`)
SELECT r.id, b.id, 'welcomeMessage', 'Hi! Thank you for your first order at Freshyo. We''re glad to have you.' FROM `regions` r JOIN `region_bounds` b ON b.region_id = r.id WHERE r.name = 'Mahabubnagar';
--> statement-breakpoint
-- Bootstrap superadmin (password hash supplied by the product owner; stored as-is, never re-hashed).
INSERT INTO `superadmins` (`username`, `password_hash`)
SELECT 'superadmin', '$2a$12$pDL1ooXdvmdpCFpeXgEnB.7eRot4ygtL/Z9lSmW8ApjRLAhTiuOCu'
WHERE NOT EXISTS (SELECT 1 FROM `superadmins` WHERE `username` = 'superadmin');

======================================================================
FILE: 0007_region_columns_backfill.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`);

======================================================================
FILE: 0008_region_cache_versions.sql
======================================================================
-- Migration: per-region storefront cache versions.
--
-- One row per region in `region_cache_versions` drives the versioned per-region
-- cache files (products/stores/banners/essential-consts, availability, slots).
-- Rows for regions created later are auto-created on first use
-- (getRegionCacheVersions), so the seed below only covers existing regions.
--
-- NOTE: SQLite has no `ADD COLUMN IF NOT EXISTS`; tables use IF NOT EXISTS so the
-- file is safe if re-applied, but rely on the migration journal to run it once.

CREATE TABLE IF NOT EXISTS `region_cache_versions` (
	`region_id` integer NOT NULL,
	`cache_version` integer NOT NULL DEFAULT 1,
	`availability_version_num` integer NOT NULL DEFAULT 1,
	`slots_version_num` integer NOT NULL DEFAULT 1,
	FOREIGN KEY (`region_id`) REFERENCES `regions`(`id`) ON UPDATE no action ON DELETE no action
);
--> statement-breakpoint
CREATE UNIQUE INDEX IF NOT EXISTS `unique_region_cache_version_region` ON `region_cache_versions` (`region_id`);
--> statement-breakpoint
-- Seed one row per existing region.
INSERT INTO `region_cache_versions` (`region_id`, `cache_version`, `availability_version_num`, `slots_version_num`)
SELECT `id`, 1, 1, 1 FROM `regions`
WHERE NOT EXISTS (SELECT 1 FROM `region_cache_versions` WHERE `region_cache_versions`.`region_id` = `regions`.`id`);

======================================================================
FILE: 0009_three_const_tables.sql
======================================================================
-- Migration: split the consts model into global_consts / region_consts / region_bound_consts.
--
-- Replaces the single key_val_store constants table (kept for the remaining
-- leftover keys), the per-region region_cache_versions table, and the
-- per-bound region_bound_settings table.
--
-- Data is copied first, then the superseded tables are dropped.
-- NOTE: SQLite has no ADD COLUMN IF NOT EXISTS; the CREATEs use IF NOT EXISTS so the
-- file is partially re-runnable, but rely on the migration journal to run it once.

CREATE TABLE IF NOT EXISTS `global_consts` (
	`key` text PRIMARY KEY NOT NULL,
	`value` text
);
--> statement-breakpoint
CREATE TABLE IF NOT EXISTS `region_consts` (
	`region_id` integer NOT NULL,
	`key` text NOT NULL,
	`value` text,
	PRIMARY KEY (`region_id`, `key`),
	FOREIGN KEY (`region_id`) REFERENCES `regions`(`id`) ON UPDATE no action ON DELETE no action
);
--> statement-breakpoint
CREATE TABLE IF NOT EXISTS `region_bound_consts` (
	`bound_id` integer NOT NULL,
	`region_id` integer NOT NULL,
	`key` text NOT NULL,
	`value` text,
	PRIMARY KEY (`bound_id`, `key`),
	FOREIGN KEY (`bound_id`) REFERENCES `region_bounds`(`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
-- Copy global rows.
INSERT OR REPLACE INTO `global_consts` (`key`, `value`)
SELECT `key`, `value` FROM `key_val_store`
WHERE `key` IN ('versionNum','playStoreUrl','appStoreUrl','internalAppVersion');
--> statement-breakpoint
-- Copy per-region version counters.
INSERT OR REPLACE INTO `region_consts` (`region_id`, `key`, `value`)
SELECT `region_id`, 'products_version_num', CAST(`cache_version` AS TEXT) FROM `region_cache_versions`;
--> statement-breakpoint
INSERT OR REPLACE INTO `region_consts` (`region_id`, `key`, `value`)
SELECT `region_id`, 'availability_version_num', CAST(`availability_version_num` AS TEXT) FROM `region_cache_versions`;
--> statement-breakpoint
INSERT OR REPLACE INTO `region_consts` (`region_id`, `key`, `value`)
SELECT `region_id`, 'slots_version_num', CAST(`slots_version_num` AS TEXT) FROM `region_cache_versions`;
--> statement-breakpoint
-- Copy region-level business keys from each region's effective (lowest-priority) bound.
INSERT OR REPLACE INTO `region_consts` (`region_id`, `key`, `value`)
SELECT rbs.`region_id`, rbs.`key`, rbs.`value`
FROM `region_bound_settings` rbs
JOIN `region_bounds` rb ON rb.`id` = rbs.`bound_id`
WHERE rbs.`key` IN ('popularItems','allItemsOrder','isFlashDeliveryEnabled','supportMobile','supportEmail','welcomeMessage')
  AND rb.`id` = (
    SELECT b2.`id` FROM `region_bounds` b2
    WHERE b2.`region_id` = rbs.`region_id`
    ORDER BY CAST(b2.`priority` AS REAL) ASC LIMIT 1
  );
--> statement-breakpoint
-- Fallback region-level values from the old global rows for regions with no bound rows.
INSERT OR IGNORE INTO `region_consts` (`region_id`, `key`, `value`)
SELECT r.`id`, kv.`key`, kv.`value`
FROM `regions` r, `key_val_store` kv
WHERE kv.`key` IN ('popularItems','allItemsOrder','tagsOrder','isFlashDeliveryEnabled','supportMobile','supportEmail','welcomeMessage');
--> statement-breakpoint
-- Copy per-bound rows.
INSERT OR REPLACE INTO `region_bound_consts` (`bound_id`, `region_id`, `key`, `value`)
SELECT `bound_id`, `region_id`, `key`, `value` FROM `region_bound_settings`
WHERE `key` IN ('minRegularOrderValue','freeDeliveryThreshold','deliveryCharge','flashFreeDeliveryThreshold','flashDeliveryCharge');
--> statement-breakpoint
-- Keep only the leftover keys in key_val_store.
DELETE FROM `key_val_store` WHERE `key` IN (
  'versionNum','playStoreUrl','appStoreUrl','internalAppVersion',
  'popularItems','allItemsOrder','tagsOrder','isFlashDeliveryEnabled','supportMobile','supportEmail','welcomeMessage',
  'cache_version','availability_version_num','slots_version_num'
);
--> statement-breakpoint
DROP TABLE IF EXISTS `region_bound_settings`;
--> statement-breakpoint
DROP TABLE IF EXISTS `region_cache_versions`;
