103 lines
7.3 KiB
SQL
103 lines
7.3 KiB
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');
|