|
| 1 | +# Schema Migration Instructions - Implementation Summary |
| 2 | + |
| 3 | +## Problem Statement |
| 4 | + |
| 5 | +**如何表达对象和字段的更新与删除指令** (How to express object and field update and delete instructions) |
| 6 | + |
| 7 | +The ObjectQL protocol needed a declarative way to express schema evolution operations - specifically updates and deletions of objects and fields. |
| 8 | + |
| 9 | +## Solution |
| 10 | + |
| 11 | +Added a comprehensive set of TypeScript type definitions to `@objectql/types` that enable declarative expression of schema changes. |
| 12 | + |
| 13 | +### Architecture |
| 14 | + |
| 15 | +Following ObjectQL's "Constitution" principle, all new types were added to `@objectql/types` with: |
| 16 | +- ✅ **Zero dependencies** - Pure TypeScript interfaces |
| 17 | +- ✅ **Universal compatibility** - Works in Node.js, Browser, and Edge |
| 18 | +- ✅ **AI-native** - Declarative, YAML-friendly structure |
| 19 | +- ✅ **Backward compatible** - No breaking changes |
| 20 | + |
| 21 | +### Core Types Added |
| 22 | + |
| 23 | +#### 1. Schema Change Instructions |
| 24 | + |
| 25 | +**Base Type:** |
| 26 | +```typescript |
| 27 | +SchemaChangeType = 'field_update' | 'field_delete' | 'object_update' | 'object_delete' |
| 28 | +``` |
| 29 | + |
| 30 | +**Instruction Types:** |
| 31 | + |
| 32 | +1. **`FieldUpdateInstruction`** - Modify field properties |
| 33 | + - Rename fields |
| 34 | + - Change field types |
| 35 | + - Update validation rules |
| 36 | + - Includes data migration strategies |
| 37 | + |
| 38 | +2. **`FieldDeleteInstruction`** - Remove fields |
| 39 | + - Deletion strategies (drop, archive, soft) |
| 40 | + - Archive location specification |
| 41 | + - Reversibility tracking |
| 42 | + |
| 43 | +3. **`ObjectUpdateInstruction`** - Modify object properties |
| 44 | + - Rename objects |
| 45 | + - Update metadata (label, icon, description) |
| 46 | + - Change datasource assignment |
| 47 | + |
| 48 | +4. **`ObjectDeleteInstruction`** - Remove objects |
| 49 | + - Deletion strategies (drop, archive, soft) |
| 50 | + - Cascade strategies (cascade, fail, nullify) |
| 51 | + - Dependency handling |
| 52 | + |
| 53 | +#### 2. Migration Management |
| 54 | + |
| 55 | +**`MigrationConfig`** - Complete migration definition |
| 56 | +- Version tracking |
| 57 | +- Step dependencies |
| 58 | +- Reversibility flags |
| 59 | +- Tags and metadata |
| 60 | + |
| 61 | +**`MigrationStep`** - Individual migration step |
| 62 | +- Unique identifier |
| 63 | +- Schema change instruction |
| 64 | +- Dependency specification |
| 65 | +- Rollback capability |
| 66 | + |
| 67 | +**`MigrationStatus`** - Execution tracking |
| 68 | +- Status states (pending, running, completed, failed, rolled_back) |
| 69 | +- Progress tracking |
| 70 | +- Error reporting |
| 71 | + |
| 72 | +### Key Features |
| 73 | + |
| 74 | +#### Data Migration Strategies |
| 75 | +```typescript |
| 76 | +type DataMigrationStrategy = 'auto' | 'manual' | 'preserve' | 'clear'; |
| 77 | +``` |
| 78 | + |
| 79 | +- **auto**: Automatic conversion (safe for renames, simple type changes) |
| 80 | +- **manual**: Requires custom transformation script |
| 81 | +- **preserve**: Keep existing data as-is |
| 82 | +- **clear**: Reset to null/default values |
| 83 | + |
| 84 | +#### Deletion Strategies |
| 85 | +```typescript |
| 86 | +type DeletionStrategy = 'drop' | 'archive' | 'soft'; |
| 87 | +``` |
| 88 | + |
| 89 | +- **drop**: Permanent removal (irreversible) |
| 90 | +- **archive**: Backup to specified location (reversible) |
| 91 | +- **soft**: Mark as deleted but keep data (reversible) |
| 92 | + |
| 93 | +#### Cascade Strategies (for objects) |
| 94 | +```typescript |
| 95 | +type CascadeStrategy = 'cascade' | 'fail' | 'nullify'; |
| 96 | +``` |
| 97 | + |
| 98 | +- **cascade**: Delete dependent records |
| 99 | +- **fail**: Prevent deletion if dependencies exist |
| 100 | +- **nullify**: Set foreign key references to null |
| 101 | + |
| 102 | +### Usage Patterns |
| 103 | + |
| 104 | +#### 1. Field Rename (Simple) |
| 105 | +```yaml |
| 106 | +type: field_update |
| 107 | +object_name: users |
| 108 | +field_name: username |
| 109 | +new_field_name: user_name |
| 110 | +changes: |
| 111 | + label: User Name |
| 112 | +data_migration_strategy: auto |
| 113 | +``` |
| 114 | +
|
| 115 | +#### 2. Field Type Change (Complex) |
| 116 | +```yaml |
| 117 | +type: field_update |
| 118 | +object_name: products |
| 119 | +field_name: price |
| 120 | +changes: |
| 121 | + type: currency |
| 122 | +data_migration_strategy: manual |
| 123 | +transform_script: | |
| 124 | + function transform(oldValue) { |
| 125 | + return typeof oldValue === 'number' ? oldValue : 0; |
| 126 | + } |
| 127 | +``` |
| 128 | +
|
| 129 | +#### 3. Field Deletion with Archive |
| 130 | +```yaml |
| 131 | +type: field_delete |
| 132 | +object_name: users |
| 133 | +field_name: legacy_id |
| 134 | +deletion_strategy: archive |
| 135 | +archive_location: backups/users_legacy_id |
| 136 | +``` |
| 137 | +
|
| 138 | +#### 4. Object Rename |
| 139 | +```yaml |
| 140 | +type: object_update |
| 141 | +object_name: customers |
| 142 | +new_object_name: accounts |
| 143 | +changes: |
| 144 | + label: Account |
| 145 | + icon: standard:account |
| 146 | +``` |
| 147 | +
|
| 148 | +#### 5. Object Deletion with Cascade |
| 149 | +```yaml |
| 150 | +type: object_delete |
| 151 | +object_name: temp_imports |
| 152 | +deletion_strategy: archive |
| 153 | +archive_location: backups/temp_imports |
| 154 | +cascade_strategy: nullify |
| 155 | +``` |
| 156 | +
|
| 157 | +### File Naming Convention |
| 158 | +
|
| 159 | +Following ObjectQL's metadata standard: |
| 160 | +
|
| 161 | +``` |
| 162 | +<migration_id>.migration.yml |
| 163 | +``` |
| 164 | + |
| 165 | +Examples: |
| 166 | +- `v1.1_rename_username.migration.yml` |
| 167 | +- `v2.0_cleanup_legacy_fields.migration.yml` |
| 168 | +- `v1.3_refactor_crm_objects.migration.yml` |
| 169 | + |
| 170 | +### Documentation |
| 171 | + |
| 172 | +1. **Package README** - Updated with migration types and usage examples |
| 173 | +2. **TypeScript Example** - Comprehensive example with 6 migration patterns |
| 174 | +3. **YAML Examples** - Three complete migration files demonstrating: |
| 175 | + - Field operations |
| 176 | + - Object operations |
| 177 | + - Multi-step migrations |
| 178 | + |
| 179 | +### Benefits |
| 180 | + |
| 181 | +1. **Declarative** - Express "what" not "how" |
| 182 | +2. **Versioned** - Track schema evolution over time |
| 183 | +3. **Reversible** - Built-in rollback support |
| 184 | +4. **Safe** - Multiple safety strategies (archive, soft delete) |
| 185 | +5. **Auditable** - Include reason, author, timestamp |
| 186 | +6. **AI-Friendly** - Perfect for LLM code generation |
| 187 | +7. **Type-Safe** - Full TypeScript support |
| 188 | + |
| 189 | +### Future Implementation (Out of Scope) |
| 190 | + |
| 191 | +This PR provides the **type definitions only**. Future work includes: |
| 192 | + |
| 193 | +1. **Migration Executor** - Implement in `@objectql/core` |
| 194 | + - Parse migration files |
| 195 | + - Execute schema changes |
| 196 | + - Handle data transformations |
| 197 | + - Track migration state |
| 198 | + |
| 199 | +2. **CLI Commands** - Add to `@objectql/cli` |
| 200 | + - `objectql migrate up` - Apply migrations |
| 201 | + - `objectql migrate down` - Rollback migrations |
| 202 | + - `objectql migrate status` - View migration state |
| 203 | + - `objectql migrate create` - Generate new migration |
| 204 | + |
| 205 | +3. **Migration State Management** |
| 206 | + - Track applied migrations |
| 207 | + - Prevent duplicate execution |
| 208 | + - Handle dependencies |
| 209 | + - Store execution history |
| 210 | + |
| 211 | +4. **Driver Integration** |
| 212 | + - SQL DDL generation (ALTER TABLE, DROP COLUMN, etc.) |
| 213 | + - MongoDB schema updates |
| 214 | + - Data migration execution |
| 215 | + - Backup creation |
| 216 | + |
| 217 | +### Testing |
| 218 | + |
| 219 | +- ✅ Types package builds successfully |
| 220 | +- ✅ No breaking changes to existing APIs |
| 221 | +- ✅ Full monorepo builds without errors |
| 222 | +- ✅ CodeQL security scan passed (0 alerts) |
| 223 | +- ✅ Example files demonstrate proper usage |
| 224 | + |
| 225 | +## Conclusion |
| 226 | + |
| 227 | +This implementation provides a solid foundation for declarative schema evolution in ObjectQL. The types are: |
| 228 | + |
| 229 | +- **Minimal** - Only what's needed for the use case |
| 230 | +- **Extensible** - Easy to add new instruction types |
| 231 | +- **Production-ready** - Safety features built-in |
| 232 | +- **Standards-compliant** - Follows ObjectQL architecture principles |
| 233 | + |
| 234 | +The solution enables both human developers and AI agents to safely express and track schema changes in a consistent, type-safe manner. |
0 commit comments