You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

88 rivejä
2.6 KiB

  1. -- Move demo dates forward to the real UTC+8 today.
  2. -- Run this on the morning of a demo when that day is later than
  3. -- settings DEMO.dateAnchor. Safe to run again on the same day.
  4. --
  5. -- Adds the same number of days to:
  6. -- inventory_lot.expiryDate
  7. -- delivery_order.estimatedArrivalDate
  8. -- purchase_order.estimatedArrivalDate
  9. -- Quantities, statuses, and lot numbers stay. Already-expired lots stay expired.
  10. --
  11. -- Does not anonymize. Run scripts/anonymize_demo_copy.sql once first.
  12. -- Only updates when DATABASE() is fpsms_demo and the guards below are ok.
  13. --
  14. -- Workbench: select fpsms_demo, File → Open SQL Script, run the whole file.
  15. -- No DELIMITER / procedure (Workbench File → Run friendly).
  16. --
  17. -- mysql -h127.0.0.1 -P3306 -uroot -p fpsms_demo < scripts/refresh_demo_dates.sql
  18. SET NAMES utf8mb4;
  19. SET SESSION time_zone = '+08:00';
  20. SET SESSION sql_safe_updates = 0;
  21. SET SESSION net_read_timeout = 7200;
  22. SET SESSION net_write_timeout = 7200;
  23. SET SESSION wait_timeout = 28800;
  24. SELECT CASE
  25. WHEN DATABASE() IS NULL OR DATABASE() <> 'fpsms_demo'
  26. THEN CONCAT('FAIL: current database is ', IFNULL(DATABASE(), 'NULL'), ' — select fpsms_demo')
  27. WHEN (SELECT COUNT(*) FROM settings WHERE name = 'DEMO.anonymized.v1') = 0
  28. THEN 'FAIL: run anonymize_demo_copy.sql first'
  29. WHEN (SELECT COUNT(*) FROM settings WHERE name = 'DEMO.dateAnchor') <> 1
  30. THEN 'FAIL: DEMO.dateAnchor must exist exactly once'
  31. WHEN (
  32. SELECT STR_TO_DATE(value, '%Y-%m-%d')
  33. FROM settings
  34. WHERE name = 'DEMO.dateAnchor'
  35. ) IS NULL
  36. THEN 'FAIL: DEMO.dateAnchor is not YYYY-MM-DD'
  37. ELSE 'ok'
  38. END AS refresh_guard
  39. INTO @refresh_guard;
  40. SET @day_delta = IF(
  41. @refresh_guard = 'ok',
  42. (
  43. SELECT DATEDIFF(CURDATE(), STR_TO_DATE(value, '%Y-%m-%d'))
  44. FROM settings
  45. WHERE name = 'DEMO.dateAnchor'
  46. ),
  47. NULL
  48. );
  49. START TRANSACTION;
  50. UPDATE inventory_lot
  51. SET expiryDate = DATE_ADD(expiryDate, INTERVAL @day_delta DAY)
  52. WHERE @refresh_guard = 'ok'
  53. AND @day_delta > 0
  54. AND expiryDate IS NOT NULL;
  55. UPDATE delivery_order
  56. SET estimatedArrivalDate = DATE_ADD(estimatedArrivalDate, INTERVAL @day_delta DAY)
  57. WHERE @refresh_guard = 'ok'
  58. AND @day_delta > 0
  59. AND estimatedArrivalDate IS NOT NULL;
  60. UPDATE purchase_order
  61. SET estimatedArrivalDate = DATE_ADD(estimatedArrivalDate, INTERVAL @day_delta DAY)
  62. WHERE @refresh_guard = 'ok'
  63. AND @day_delta > 0
  64. AND estimatedArrivalDate IS NOT NULL;
  65. UPDATE settings
  66. SET value = DATE_FORMAT(CURDATE(), '%Y-%m-%d')
  67. WHERE @refresh_guard = 'ok'
  68. AND @day_delta > 0
  69. AND name = 'DEMO.dateAnchor';
  70. COMMIT;
  71. SELECT
  72. @refresh_guard AS refresh_guard,
  73. @day_delta AS days_added,
  74. name,
  75. value
  76. FROM settings
  77. WHERE name = 'DEMO.dateAnchor';