MDM Migration Plan
Objective: To refactor the existing product-centric database schema to a flexible, Multi-Domain Master Data Management (MDM) model, starting with the products table.
Guiding Principles:
- No Data Loss: Existing data in the
productstable will be migrated. - Maintain Integrity: Foreign key relationships will be preserved and updated.
- Sequential & Reversible: The process will be broken into a series of Knex migrations, each with a clear
upanddownmethod.
Phase 1: Create the New MDM Core Tables
This phase establishes the foundational tables of the new architecture. We will create three new tables.
-
domain_schemas- Purpose: To govern the structure and data types of the flexible JSONB attributes in
core_entities. - Action: Create a migration file for a
domain_schemastable with columns likeentity_type,attribute_name,data_type, andis_required.
- Purpose: To govern the structure and data types of the flexible JSONB attributes in
-
core_entities- Purpose: The new central table for all master data, replacing the
productstable. - Action: Create a migration file for the
core_entitiestable. It will use the "Hybrid Model":- Native Columns: Critical, frequently-queried fields like
entity_id,tenant_id,entity_type,entity_name,sku, andunit_cost. - JSONB Column: A
domain_attributescolumn for other, less critical product fields (e.g.,product_family).
- Native Columns: Critical, frequently-queried fields like
- A database trigger will be included to validate the JSONB data against the
domain_schemastable.
- Purpose: The new central table for all master data, replacing the
-
entity_relationships- Purpose: A generic table to model all hierarchies and connections, such as Bill of Materials (BOMs).
- Action: Create a migration file for an
entity_relationshipstable with columns likesource_entity_id,target_entity_id, andrelationship_type.
Phase 2: Migrate Existing Product Data
This phase moves the data from the old products table into the new core_entities table.
- Data Migration Script
- Purpose: To transfer all records from
productsintocore_entities. - Action: Create a data-only migration file that will:
- Read every row from the
productstable. - For each product, create a new row in the
core_entitiestable, settingentity_typeto'PRODUCT'. - Map the columns from
productsto their corresponding new columns or into thedomain_attributesJSONB field incore_entities.
- Read every row from the
- Purpose: To transfer all records from
Phase 3: Update Foreign Key Relationships
This phase ensures that other tables in the database correctly point to the new core_entities table instead of the old products table.
- Update Foreign Keys
- Purpose: To maintain referential integrity.
- Action: Create a migration file that uses
alterTableto update the foreign key constraints on the following tables:sop_plan_datainsights
- Their
product_idforeign keys will be modified to point tocore_entities(entity_id).
Phase 4: Decommission the Old products Table
This is the final cleanup step.
- Drop
productsTable- Purpose: To remove the now-redundant
productstable. - Action: Create a final migration file that drops the
productstable. Thedownmethod in this migration will be written to fully re-create the table and its data for safe rollbacks.
- Purpose: To remove the now-redundant