|
| 1 | +// Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * ISchemaDiffService - Schema Introspection & Diff Contract |
| 5 | + * |
| 6 | + * Compares the desired metadata state (ObjectStack object definitions) |
| 7 | + * against the current database schema and generates DDL migration plans. |
| 8 | + * |
| 9 | + * Pipeline: Introspect Current → Diff vs Desired → Generate Migrations → Apply |
| 10 | + * |
| 11 | + * This service is dialect-aware and generates DDL appropriate for the |
| 12 | + * target database (PostgreSQL, SQLite/Turso, MySQL, etc.). |
| 13 | + */ |
| 14 | + |
| 15 | +import type { DeployDiff, MigrationPlan } from '../system/deploy-bundle.zod.js'; |
| 16 | +import type { SQLDialect } from '../data/driver-sql.zod.js'; |
| 17 | + |
| 18 | +// ========================================================================== |
| 19 | +// Types |
| 20 | +// ========================================================================== |
| 21 | + |
| 22 | +/** |
| 23 | + * Introspected schema representation of the current database state. |
| 24 | + */ |
| 25 | +export interface IntrospectedSchema { |
| 26 | + /** Object/table names and their column definitions */ |
| 27 | + tables: Record<string, IntrospectedTable>; |
| 28 | + /** Database dialect */ |
| 29 | + dialect: string; |
| 30 | + /** Introspection timestamp (ISO 8601) */ |
| 31 | + introspectedAt: string; |
| 32 | +} |
| 33 | + |
| 34 | +/** |
| 35 | + * Introspected table (current database state). |
| 36 | + */ |
| 37 | +export interface IntrospectedTable { |
| 38 | + /** Table name */ |
| 39 | + name: string; |
| 40 | + /** Column definitions */ |
| 41 | + columns: IntrospectedColumn[]; |
| 42 | + /** Index definitions */ |
| 43 | + indexes: IntrospectedIndex[]; |
| 44 | +} |
| 45 | + |
| 46 | +/** |
| 47 | + * Introspected column definition. |
| 48 | + */ |
| 49 | +export interface IntrospectedColumn { |
| 50 | + /** Column name */ |
| 51 | + name: string; |
| 52 | + /** SQL data type */ |
| 53 | + type: string; |
| 54 | + /** Whether the column is nullable */ |
| 55 | + nullable: boolean; |
| 56 | + /** Default value expression */ |
| 57 | + defaultValue?: string; |
| 58 | + /** Whether this column is a primary key */ |
| 59 | + primaryKey: boolean; |
| 60 | +} |
| 61 | + |
| 62 | +/** |
| 63 | + * Introspected index definition. |
| 64 | + */ |
| 65 | +export interface IntrospectedIndex { |
| 66 | + /** Index name */ |
| 67 | + name: string; |
| 68 | + /** Columns included in the index */ |
| 69 | + columns: string[]; |
| 70 | + /** Whether the index enforces uniqueness */ |
| 71 | + unique: boolean; |
| 72 | +} |
| 73 | + |
| 74 | +/** |
| 75 | + * Migration apply result. |
| 76 | + */ |
| 77 | +export interface MigrationApplyResult { |
| 78 | + /** Whether all migrations applied successfully */ |
| 79 | + success: boolean; |
| 80 | + /** Number of statements executed */ |
| 81 | + statementsExecuted: number; |
| 82 | + /** Total execution duration in milliseconds */ |
| 83 | + durationMs: number; |
| 84 | + /** Error message if a statement failed */ |
| 85 | + error?: string; |
| 86 | + /** Index of the failed statement (if any) */ |
| 87 | + failedAtIndex?: number; |
| 88 | +} |
| 89 | + |
| 90 | +// ========================================================================== |
| 91 | +// Service Interface |
| 92 | +// ========================================================================== |
| 93 | + |
| 94 | +export interface ISchemaDiffService { |
| 95 | + /** |
| 96 | + * Introspect the current database schema. |
| 97 | + * Reads table definitions, columns, indexes from the live database. |
| 98 | + * |
| 99 | + * @param driver - Data driver to introspect |
| 100 | + * @returns Current schema representation |
| 101 | + */ |
| 102 | + introspect(driver: unknown): Promise<IntrospectedSchema>; |
| 103 | + |
| 104 | + /** |
| 105 | + * Compute the diff between current schema and desired object definitions. |
| 106 | + * |
| 107 | + * @param current - Introspected current schema |
| 108 | + * @param desired - Desired ObjectStack object definitions |
| 109 | + * @returns Schema diff describing all changes |
| 110 | + */ |
| 111 | + diff(current: IntrospectedSchema, desired: Record<string, unknown>[]): DeployDiff; |
| 112 | + |
| 113 | + /** |
| 114 | + * Generate SQL migration statements from a schema diff. |
| 115 | + * Output is dialect-specific (PostgreSQL, SQLite, etc.). |
| 116 | + * |
| 117 | + * @param diff - Schema diff to generate migrations for |
| 118 | + * @param dialect - Target SQL dialect |
| 119 | + * @returns Ordered migration plan |
| 120 | + */ |
| 121 | + generateMigrations(diff: DeployDiff, dialect: SQLDialect): MigrationPlan; |
| 122 | + |
| 123 | + /** |
| 124 | + * Apply a migration plan to the database. |
| 125 | + * Executes statements in order within a transaction (when supported). |
| 126 | + * |
| 127 | + * @param driver - Data driver to apply migrations to |
| 128 | + * @param plan - Migration plan to execute |
| 129 | + * @returns Apply result with success status and timing |
| 130 | + */ |
| 131 | + applyMigrations(driver: unknown, plan: MigrationPlan): Promise<MigrationApplyResult>; |
| 132 | +} |
0 commit comments