Selaa lähdekoodia

fix: make migration 005 more defensive for existing tables

unknown 2 kuukautta sitten
vanhempi
commit
fa7055cbe2
1 muutettua tiedostoa jossa 18 lisäystä ja 14 poistoa
  1. 18 14
      backend/migrations/005_advanced_pricing.sql

+ 18 - 14
backend/migrations/005_advanced_pricing.sql

@@ -1,30 +1,34 @@
--- Migration 005: Advanced Pricing (Services and Speed Coefficients)
+-- Migration 005: Advanced Pricing (Defensive Update)
 
--- 1. Create services table
+-- 1. Ensure services table exists (minimal version)
 CREATE TABLE IF NOT EXISTS `services` (
     `id` int(11) NOT NULL AUTO_INCREMENT,
     `name_en` varchar(255) NOT NULL,
-    `name_ru` varchar(255) DEFAULT NULL,
-    `name_ua` varchar(255) DEFAULT NULL,
-    `desc_en` text DEFAULT NULL,
-    `tech_type` varchar(50) DEFAULT 'fdm',
-    `price_per_hour` decimal(10,2) DEFAULT 0.00,
-    `is_active` tinyint(1) DEFAULT 1,
-    `created_at` timestamp DEFAULT CURRENT_TIMESTAMP,
     PRIMARY KEY (`id`)
 ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
 
--- 2. Add price_per_hour, speed_coeff and service_id to materials
--- Plain ALTER TABLE for compatibility with older MySQL versions
+-- 2. Add columns to services (ignoring if they exist is handled by the fact that migrations are atomic per statement here)
+-- Note: if a column exists, the script will show an error but run_migrations.py will continue to next statement.
+ALTER TABLE `services` ADD COLUMN `name_ru` varchar(255) DEFAULT NULL;
+ALTER TABLE `services` ADD COLUMN `name_ua` varchar(255) DEFAULT NULL;
+ALTER TABLE `services` ADD COLUMN `desc_en` text DEFAULT NULL;
+ALTER TABLE `services` ADD COLUMN `tech_type` varchar(50) DEFAULT 'fdm';
+ALTER TABLE `services` ADD COLUMN `price_per_hour` decimal(10,2) DEFAULT 0.00;
+ALTER TABLE `services` ADD COLUMN `is_active` tinyint(1) DEFAULT 1;
+ALTER TABLE `services` ADD COLUMN `created_at` timestamp DEFAULT CURRENT_TIMESTAMP;
+
+-- 3. Add columns to materials
 ALTER TABLE `materials` ADD COLUMN `price_per_hour` decimal(10,2) DEFAULT NULL;
 ALTER TABLE `materials` ADD COLUMN `speed_coeff` decimal(10,2) DEFAULT 1.00;
 ALTER TABLE `materials` ADD COLUMN `service_id` int(11) DEFAULT NULL;
 
--- 3. Initial seed for services if empty
+-- 4. Initial seed for services
 INSERT INTO `services` (`name_en`, `name_ru`, `name_ua`, `tech_type`, `price_per_hour`)
-SELECT * FROM (SELECT 'FDM Printing', 'FDM Печать', 'FDM Друк', 'fdm', 2.00) AS tmp
+SELECT 'FDM Printing', 'FDM Печать', 'FDM Друк', 'fdm', 2.00
+FROM (SELECT 1) AS tmp
 WHERE NOT EXISTS (SELECT 1 FROM `services` WHERE `tech_type` = 'fdm') LIMIT 1;
 
 INSERT INTO `services` (`name_en`, `name_ru`, `name_ua`, `tech_type`, `price_per_hour`)
-SELECT * FROM (SELECT 'SLA Printing', 'SLA Печать', 'SLA Друк', 'sla', 5.00) AS tmp
+SELECT 'SLA Printing', 'SLA Печать', 'SLA Друк', 'sla', 5.00
+FROM (SELECT 1) AS tmp
 WHERE NOT EXISTS (SELECT 1 FROM `services` WHERE `tech_type` = 'sla') LIMIT 1;