-- ============================================================
-- Migration History Fixer
-- Run this ONCE on databases that were created with the OLD
-- migration names (20260223125953_init_mysql, etc.)
-- ============================================================
-- This replaces old migration entries with our new standard names
-- NO data is modified, only the _prisma_migrations tracking table

SET FOREIGN_KEY_CHECKS = 0;

-- Remove old migration entries (from original setup)
DELETE FROM `_prisma_migrations`
WHERE `migration_name` IN (
  '20260223125953_init_mysql',
  '20260223232338_extend_qrimagedata_to_text'
);

-- Remove any existing entries for our migrations (avoid duplicates)
DELETE FROM `_prisma_migrations`
WHERE `migration_name` IN (
  '0001_baseline',
  '0002_add_unit_sale_rule'
);

-- Insert baseline migration as already-applied
-- checksum can be anything since we use resolve mode
INSERT INTO `_prisma_migrations`
  (`id`, `checksum`, `finished_at`, `migration_name`, `logs`, `rolled_back_at`, `started_at`, `applied_steps_count`)
VALUES
  (UUID(), 'resolved-baseline', NOW(3), '0001_baseline', NULL, NULL, NOW(3), 1);

-- NOTE: 0002_add_unit_sale_rule is NOT inserted here.
-- deploy.sh will either:
--   a) mark it applied via `prisma migrate resolve --applied` (if unitSaleRule column already exists), or
--   b) let `prisma migrate deploy` run the SQL and add the column
-- This keeps the script MySQL 5.7 + MariaDB compatible (no ADD COLUMN IF NOT EXISTS).

SET FOREIGN_KEY_CHECKS = 1;

SELECT 'Migration history fixed successfully' AS status;
SELECT migration_name, finished_at FROM `_prisma_migrations` ORDER BY started_at;
