# Database Migration Design ## Overview This document describes the approach for migrating legacy SQL Server objects to DbUp migration scripts. ## Script Naming Convention Scripts follow the pattern: `NNN_Description.sql` ``` 001_CreateSearchTable.sql (existing) 002_CreateDataUpdateTable.sql (existing) 003_CreateBranchTable.sql ... 025_CreateLotUsageHistTable.sql 026_CreateWorkOrderView.sql ... 032_CreateLastDataUpdatesView.sql 033_CreateWorkOrderFilterParameterType.sql ... 039_CreateItemOperationMisFilterParameterType.sql 040_CreateSubmitSearchProcedure.sql ... 043_CreateResetPartialSearchesProcedure.sql 044_CreateMatchMisFunction.sql ``` ### Number Ranges | Range | Category | Count | |-------|----------|-------| | 001-002 | Already done (Search, DataUpdate) | 2 | | 003-025 | Tables | 23 | | 026-032 | Views | 7 | | 033-039 | Table-valued parameter types | 7 | | 040-043 | Stored procedures | 4 | | 044 | Functions | 1 | ## Execution Order Objects must be created in dependency order: ``` 1. Reference tables (no FK dependencies) - Branch, StatusCode, FunctionCode, ProfitCenter, WorkCenter - Item, JdeUser, OrgHierarchy, RouteMaster, MisData 2. Core tables (depend on reference tables) - Lot, LotLocation, WorkOrder_Curr, WorkOrder_Hist - WorkOrderStep_Curr, WorkOrderStep_Hist - WorkOrderTime_Curr, WorkOrderTime_Hist - WorkOrderComponent_Curr, WorkOrderComponent_Hist - LotUsage_Curr, LotUsage_Hist 3. Views (depend on tables) - Union views: WorkOrder, WorkOrderStep, WorkOrderTime, WorkOrderComponent, LotUsage - Aggregation views: WorkOrderTotalScrap, LastDataUpdates 4. Types (no dependencies, but used by procedures) - All 7 TVP types 5. Procedures and Functions (depend on tables, views, types) - SubmitSearch, StartSearch, CompleteSearch, ResetPartialSearches - MatchMis function ``` ## Schema Mapping ### Data Type Decisions | Legacy | New | Rationale | |--------|-----|-----------| | DATETIME | DATETIME2(7) | Better precision, recommended for new development | | VARCHAR | VARCHAR | Keep as-is for JDE/CMS compatibility | | NVARCHAR | NVARCHAR | Keep as-is | | VARBINARY(MAX) | VARBINARY(MAX) | Keep for Excel storage (per user decision) | **Note:** The DATETIME → DATETIME2(7) conversion requires updating the database-schema spec to reflect this decision. This is a deliberate modernization choice. ### Index Strategy - Primary keys: Defined in table creation scripts - Foreign keys: NOT created (legacy doesn't have them, cache tables) - Clustered indexes: On primary keys - Non-clustered indexes: Included in table creation scripts (match legacy exactly) ## DbUp Configuration The existing `DatabaseMigrator.cs` configuration is appropriate: - Uses `WithTransaction()` for atomic migrations - Uses `WithScriptsEmbeddedInAssembly()` for embedded resources - Uses `EnsureDatabase.For.SqlDatabase()` to create DB if needed No changes needed to the migrator itself. ## Verification Approach 1. **Script syntax**: Run against local SQL Server container 2. **Object existence**: Query sys.tables, sys.views, sys.procedures 3. **Schema accuracy**: Compare column definitions to legacy 4. **Codex review**: Cross-reference with specs ## File Structure ``` NEW/src/JdeScoping.Database/ ├── Scripts/ │ ├── 001_CreateSearchTable.sql (existing) │ ├── 002_CreateDataUpdateTable.sql (existing) │ ├── 003_CreateBranchTable.sql (new) │ ├── ... │ └── 046_CreateMatchMisFunction.sql (new) ├── DatabaseMigrator.cs (existing, no changes) └── JdeScoping.Database.csproj (existing, no changes) ```