# MF919 Seed Files - Pre-Deployment Checklist

## 🎯 Changes Summary

### ✅ Completed Updates

1. **Category Integration** - MF919 categories now nest under existing "Device Setup" (id=20)
2. **Template Alignment** - Uses existing "MF919 Default Configuration" template (id=3)  
3. **Schema Migration** - Created migration to align parameter_definitions table fields
4. **Documentation** - Comprehensive alignment guide and summary created

### 📁 Files Changed

**Seed Files:**
- ✅ `apps/da_product_app/priv/repo/seeds/mf919_parameter_categories.exs`
- ✅ `apps/da_product_app/priv/repo/seeds/mf919_default_template.exs`
- ⚪ `apps/da_product_app/priv/repo/seeds/mf919_parameter_definitions.exs` (no changes needed)
- ⚪ `apps/da_product_app/priv/repo/seeds/mf919_merchant_overlay.exs` (no changes needed)

**New Files:**
- ✅ `priv/repo/migrations/20260310000001_align_parameter_definitions_schema.exs`
- ✅ `apps/da_product_app/priv/repo/migrations/20260310000001_align_parameter_definitions_schema.exs`
- ✅ `docs/MF919_SEED_ALIGNMENT.md`
- ✅ `docs/MF919_SEED_ALIGNMENT_SUMMARY.md`
- ✅ `docs/MF919_SEED_DEPLOYMENT_CHECKLIST.md` (this file)

## 🔍 Pre-Deployment Verification

### Database Compatibility Checks

- [ ] Confirm current DB has `parameter_templates` table with MF919 entry (id=3)
- [ ] Confirm current DB has `parameter_categories` table with Device Setup entry (id=20)
- [ ] Check if parameter_definitions table has `key`, `is_active`, etc. fields
  - If YES: Migration will skip adding them
  - If NO: Migration will add them safely

### Code Review Checks

- [x] All seed files have no syntax errors
- [x] Migration uses safe `add_if_not_exists` / `remove_if_exists`
- [x] Parameter definitions use correct field names per schema
- [x] Category codes follow naming convention (`mf919_*`)
- [x] Template query matches current DB template name exactly

### Documentation Checks

- [x] Execution order documented clearly
- [x] Category hierarchy diagram included
- [x] Migration rationale explained
- [x] Verification steps provided

## 🚀 Deployment Steps

### Step 1: Run Migration
```bash
cd /home/prem/mercurypay/tms_new_version/tmsuat_apps
mix ecto.migrate
```

**Expected Output:**
```
[info] == Running DaProductApp.Repo.Migrations.AlignParameterDefinitionsSchema.up/0 forward
[info] alter table parameter_definitions
[info] == Migrated in 0.XYZs
```

**Verify:**
```bash
mix ecto.migrations
```
Should show migration `20260310000001` as "up".

### Step 2: Seed Categories
```bash
mix run apps/da_product_app/priv/repo/seeds/mf919_parameter_categories.exs
```

**Expected Output:**
```
[info] Using Device Setup parent category ID: 20
[info] Using MF919 root category ID: XX
[info] Created category: mf919_base (ID: YY)
[info] Created category: mf919_merchant (ID: YY)
...
[info] MF919 parameter categories seeding completed!
```

**Verify:** Check that 9 new categories were created (or logged as existing).

### Step 3: Seed Definitions
```bash
mix run apps/da_product_app/priv/repo/seeds/mf919_parameter_definitions.exs
```

**Expected Output:**
```
[info] Created parameter definition: MERCHANT_ID (ID: XX)
[info] Created parameter definition: TERMINAL_ID (ID: XX)
...
[info] MF919 parameter definitions seeding completed!
```

**Verify:** Check that 47 parameter definitions were created (or logged as existing).

### Step 4: Seed Template Values
```bash
mix run apps/da_product_app/priv/repo/seeds/mf919_default_template.exs
```

**Expected Output:**
```
[info] Using template ID: 3 – MF919 Default Configuration
[info] Created template value: BASE_MERCHANT_NAME = Demo Merchant (ID: XX)
...
[info] MF919 default template seeding completed!
```

**Verify:** Check that ~42 template values were created for template ID 3.

### Step 5 (Optional): Apply Merchant Overlay
```bash
SAMPLE_TERMINAL_ID=<your_terminal_id> mix run apps/da_product_app/priv/repo/seeds/mf919_merchant_overlay.exs
```

**Expected Output:**
```
[info] Created override: terminal=XX MERCHANT_ID=123456789012345 (ID: YY)
...
[info] MF919 merchant overlay seeding completed for terminal ID: XX
```

## 🧪 Post-Deployment Verification

### Database Verification

```sql
-- 1. Verify parameter_definitions table structure
DESCRIBE parameter_definitions;
-- Should have: key, is_required, is_system, is_encrypted, display_order, is_active

-- 2. Verify MF919 category hierarchy
SELECT id, name, code, parent_id FROM parameter_categories 
WHERE code LIKE 'mf919%' OR code = 'device_setup'
ORDER BY parent_id NULLS FIRST, sort_order;
-- Should show device_setup (parent_id=NULL) and mf919_* (parent_id=20)

-- 3. Verify parameter definitions created
SELECT COUNT(*) FROM parameter_definitions WHERE key LIKE 'MERCHANT_ID' OR key LIKE 'BASE_%' OR key LIKE 'COMM_%';
-- Should return 47

-- 4. Verify template values populated
SELECT COUNT(*) FROM parameter_template_values WHERE template_id = 3;
-- Should return ~42

-- 5. (If overlay was run) Verify device overrides
SELECT * FROM device_parameter_overrides WHERE terminal_id = <your_terminal_id>;
-- Should show 3 rows: MERCHANT_ID, TERMINAL_ID, BATCH_NO
```

### Application Verification

- [ ] Open Parameter Management UI
- [ ] Navigate to Categories section
- [ ] Confirm "Device Setup" → "MF919 Device Setup" hierarchy visible
- [ ] Open MF919 Default Configuration template
- [ ] Confirm ~42 parameter values displayed
- [ ] (If overlay was run) Check terminal parameters show overrides

## ⚠️ Rollback Plan

If issues occur:

### Rollback Migration
```bash
mix ecto.rollback --step 1
```

This will:
- Remove added fields (key, is_required, etc.)
- Restore old 'active' field if it existed
- Remove unique index on key

### Remove Seeded Data
```sql
-- Remove template values for MF919 template
DELETE FROM parameter_template_values WHERE template_id = 3 AND parameter_definition_id IN (
  SELECT id FROM parameter_definitions WHERE key LIKE 'BASE_%' OR key LIKE 'COMM_%' ...
);

-- Remove parameter definitions
DELETE FROM parameter_definitions WHERE category_id IN (
  SELECT id FROM parameter_categories WHERE code LIKE 'mf919%'
);

-- Remove categories
DELETE FROM parameter_categories WHERE code LIKE 'mf919%';
```

## 📊 Expected Metrics

After successful deployment:

| Metric | Expected Value |
|--------|----------------|
| New categories created | 9 (1 root + 8 children) |
| New parameter definitions | 47 |
| Template values added | ~42 |
| MF919 categories parent | Device Setup (id=20) |
| Template used | MF919 Default Configuration (id=3) |
| Migration status | UP |

## 🔗 References

- Full Alignment Guide: `docs/MF919_SEED_ALIGNMENT.md`
- Summary: `docs/MF919_SEED_ALIGNMENT_SUMMARY.md`
- Current DB Data: `docs/tms_mf919_parameter_samples/currentdb-data.txt`

## ✅ Sign-Off

- [ ] Migration tested in development
- [ ] Seed scripts executed successfully
- [ ] Database verification queries passed
- [ ] UI displays correct hierarchy
- [ ] No errors in logs
- [ ] Documentation reviewed
- [ ] Rollback plan understood

**Deployment Date:** _______________  
**Deployed By:** _______________  
**Environment:** [ ] Dev [ ] Staging [ ] Production
