@memberjunction/metadata-sync 2.50.0 → 2.51.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -29,16 +29,20 @@ Object.defineProperty(exports, "__esModule", { value: true });
29
29
  const core_1 = require("@oclif/core");
30
30
  const fs_extra_1 = __importDefault(require("fs-extra"));
31
31
  const path_1 = __importDefault(require("path"));
32
+ const prompts_1 = require("@inquirer/prompts");
32
33
  const ora_classic_1 = __importDefault(require("ora-classic"));
33
34
  const fast_glob_1 = __importDefault(require("fast-glob"));
35
+ const chalk_1 = __importDefault(require("chalk"));
34
36
  const config_1 = require("../../config");
35
37
  const provider_utils_1 = require("../../lib/provider-utils");
36
- const provider_utils_2 = require("../../lib/provider-utils");
38
+ const core_2 = require("@memberjunction/core");
37
39
  const config_manager_1 = require("../../lib/config-manager");
38
40
  const singleton_manager_1 = require("../../lib/singleton-manager");
39
41
  const global_1 = require("@memberjunction/global");
40
42
  class Push extends core_1.Command {
41
43
  static description = 'Push local file changes to the database';
44
+ warnings = [];
45
+ errors = [];
42
46
  static examples = [
43
47
  `<%= config.bin %> <%= command.id %>`,
44
48
  `<%= config.bin %> <%= command.id %> --dry-run`,
@@ -50,11 +54,19 @@ class Push extends core_1.Command {
50
54
  'dry-run': core_1.Flags.boolean({ description: 'Show what would be pushed without actually pushing' }),
51
55
  ci: core_1.Flags.boolean({ description: 'CI mode - no prompts, fail on issues' }),
52
56
  verbose: core_1.Flags.boolean({ char: 'v', description: 'Show detailed field-level output' }),
57
+ 'no-validate': core_1.Flags.boolean({ description: 'Skip validation before push' }),
53
58
  };
59
+ // Override warn to collect warnings
60
+ warn(input) {
61
+ const message = typeof input === 'string' ? input : input.message;
62
+ this.warnings.push(message);
63
+ return super.warn(input);
64
+ }
54
65
  async run() {
55
66
  const { flags } = await this.parse(Push);
56
67
  const spinner = (0, ora_classic_1.default)();
57
68
  let sqlLogger = null;
69
+ const startTime = Date.now();
58
70
  try {
59
71
  // Load configurations
60
72
  spinner.start('Loading configuration');
@@ -72,7 +84,12 @@ class Push extends core_1.Command {
72
84
  // Initialize sync engine using singleton pattern
73
85
  const syncEngine = await (0, singleton_manager_1.getSyncEngine)((0, provider_utils_1.getSystemUser)());
74
86
  // Show success after all initialization is complete
75
- spinner.succeed('Configuration and metadata loaded');
87
+ if (flags.verbose) {
88
+ spinner.succeed('Configuration and metadata loaded');
89
+ }
90
+ else {
91
+ spinner.stop();
92
+ }
76
93
  // Initialize SQL logging AFTER provider setup is complete
77
94
  if (syncConfig?.sqlLogging?.enabled) {
78
95
  const outputDir = syncConfig.sqlLogging.outputDirectory || './sql_logging';
@@ -96,18 +113,20 @@ class Push extends core_1.Command {
96
113
  // Import and access the data provider from the provider utils
97
114
  const { getDataProvider } = await Promise.resolve().then(() => __importStar(require('../../lib/provider-utils')));
98
115
  const dataProvider = getDataProvider();
99
- if (dataProvider && typeof dataProvider.createSqlLogger === 'function') {
100
- sqlLogger = await dataProvider.createSqlLogger(logFilePath, {
116
+ if (dataProvider && typeof dataProvider.CreateSqlLogger === 'function') {
117
+ sqlLogger = await dataProvider.CreateSqlLogger(logFilePath, {
101
118
  formatAsMigration,
102
119
  description: 'MetadataSync Push Operation',
103
120
  statementTypes: 'mutations', // Only log mutations (data changes)
104
121
  batchSeparator: 'GO', // Add GO statements for SQL Server batch processing
105
122
  prettyPrint: true // Enable pretty printing for readable output
106
123
  });
107
- this.log(`šŸ“ SQL logging enabled: ${path_1.default.relative(process.cwd(), logFilePath)}`);
124
+ if (flags.verbose) {
125
+ this.log(`šŸ“ SQL logging enabled: ${path_1.default.relative(process.cwd(), logFilePath)}`);
126
+ }
108
127
  }
109
128
  else {
110
- this.warn('SQL logging requested but data provider does not support createSqlLogger');
129
+ this.warn('SQL logging requested but data provider does not support CreateSqlLogger');
111
130
  }
112
131
  }
113
132
  // Find entity directories to process
@@ -115,7 +134,63 @@ class Push extends core_1.Command {
115
134
  if (entityDirs.length === 0) {
116
135
  this.error('No entity directories found');
117
136
  }
118
- this.log(`Found ${entityDirs.length} entity ${entityDirs.length === 1 ? 'directory' : 'directories'} to process`);
137
+ if (flags.verbose) {
138
+ this.log(`Found ${entityDirs.length} entity ${entityDirs.length === 1 ? 'directory' : 'directories'} to process`);
139
+ }
140
+ // Run validation unless disabled
141
+ if (!flags['no-validate']) {
142
+ const { ValidationService } = await Promise.resolve().then(() => __importStar(require('../../services/ValidationService')));
143
+ const { FormattingService } = await Promise.resolve().then(() => __importStar(require('../../services/FormattingService')));
144
+ spinner.start('Validating metadata...');
145
+ const validator = new ValidationService({ verbose: flags.verbose });
146
+ const formatter = new FormattingService();
147
+ const targetDir = flags.dir ? path_1.default.resolve(config_manager_1.configManager.getOriginalCwd(), flags.dir) : config_manager_1.configManager.getOriginalCwd();
148
+ const validationResult = await validator.validateDirectory(targetDir);
149
+ spinner.stop();
150
+ if (!validationResult.isValid || validationResult.warnings.length > 0) {
151
+ // Show validation results
152
+ this.log('\n' + formatter.formatValidationResult(validationResult, flags.verbose));
153
+ if (!validationResult.isValid) {
154
+ // In CI mode, fail immediately
155
+ if (flags.ci) {
156
+ this.error('Validation failed. Cannot proceed with push.');
157
+ }
158
+ // Otherwise, ask for confirmation
159
+ const shouldContinue = await (0, prompts_1.confirm)({
160
+ message: 'Validation failed with errors. Do you want to continue anyway?',
161
+ default: false
162
+ });
163
+ if (!shouldContinue) {
164
+ this.error('Push cancelled due to validation errors.');
165
+ }
166
+ }
167
+ }
168
+ else {
169
+ this.log(chalk_1.default.green('āœ“ Validation passed'));
170
+ }
171
+ }
172
+ // Start a database transaction for the entire push operation (unless in dry-run mode)
173
+ // IMPORTANT: We start the transaction AFTER metadata loading and validation to avoid
174
+ // transaction conflicts with background refresh operations
175
+ if (!flags['dry-run']) {
176
+ const { getDataProvider } = await Promise.resolve().then(() => __importStar(require('../../lib/provider-utils')));
177
+ const dataProvider = getDataProvider();
178
+ if (dataProvider && typeof dataProvider.BeginTransaction === 'function') {
179
+ try {
180
+ await dataProvider.BeginTransaction();
181
+ if (flags.verbose) {
182
+ this.log('šŸ”„ Transaction started - all changes will be committed or rolled back as a unit');
183
+ }
184
+ }
185
+ catch (error) {
186
+ this.warn('Failed to start transaction - changes will be committed individually');
187
+ this.warn(`Transaction error: ${error instanceof Error ? error.message : String(error)}`);
188
+ }
189
+ }
190
+ else {
191
+ this.warn('Transaction support not available - changes will be committed individually');
192
+ }
193
+ }
119
194
  // Process each entity directory
120
195
  let totalCreated = 0;
121
196
  let totalUpdated = 0;
@@ -126,23 +201,110 @@ class Push extends core_1.Command {
126
201
  this.warn(`Skipping ${entityDir} - no valid entity configuration`);
127
202
  continue;
128
203
  }
129
- this.log(`\nProcessing ${entityConfig.entity} in ${entityDir}`);
204
+ if (flags.verbose) {
205
+ this.log(`\nProcessing ${entityConfig.entity} in ${entityDir}`);
206
+ }
130
207
  const result = await this.processEntityDirectory(entityDir, entityConfig, syncEngine, flags, syncConfig);
131
208
  totalCreated += result.created;
132
209
  totalUpdated += result.updated;
133
210
  totalErrors += result.errors;
134
211
  }
135
- // Summary
136
- this.log('\n=== Push Summary ===');
137
- this.log(`Created: ${totalCreated}`);
138
- this.log(`Updated: ${totalUpdated}`);
139
- this.log(`Errors: ${totalErrors}`);
140
- if (totalErrors > 0 && flags.ci) {
141
- this.error('Push failed with errors in CI mode');
212
+ // Summary using FormattingService
213
+ const endTime = Date.now();
214
+ const { FormattingService } = await Promise.resolve().then(() => __importStar(require('../../services/FormattingService')));
215
+ const formatter = new FormattingService();
216
+ this.log('\n' + formatter.formatSyncSummary('push', {
217
+ created: totalCreated,
218
+ updated: totalUpdated,
219
+ deleted: 0,
220
+ skipped: 0,
221
+ errors: totalErrors,
222
+ duration: endTime - startTime
223
+ }));
224
+ // Handle transaction commit/rollback
225
+ if (!flags['dry-run']) {
226
+ const dataProvider = core_2.Metadata.Provider;
227
+ // Check if we have an active transaction
228
+ if (dataProvider) {
229
+ let shouldCommit = true;
230
+ // If there are any errors, always rollback
231
+ if (totalErrors > 0 || this.errors.length > 0) {
232
+ shouldCommit = false;
233
+ this.log('\nāŒ Errors detected - rolling back all changes');
234
+ }
235
+ // If there are warnings, ask user (unless in CI mode)
236
+ else if (this.warnings.length > 0) {
237
+ // Filter out transaction-related warnings since we're now using transactions
238
+ const nonTransactionWarnings = this.warnings.filter(w => !w.includes('Transaction support not available') &&
239
+ !w.includes('Failed to start transaction'));
240
+ if (nonTransactionWarnings.length > 0) {
241
+ if (flags.ci) {
242
+ // In CI mode, rollback on warnings
243
+ shouldCommit = false;
244
+ this.log('\nāš ļø Warnings detected in CI mode - rolling back all changes');
245
+ }
246
+ else {
247
+ // Show warnings to user
248
+ this.log('\nāš ļø The following warnings were encountered:');
249
+ for (const warning of nonTransactionWarnings) {
250
+ this.log(` - ${warning}`);
251
+ }
252
+ // Ask user whether to commit or rollback
253
+ shouldCommit = await (0, prompts_1.confirm)({
254
+ message: 'Do you want to commit these changes despite the warnings?',
255
+ default: false // Default to rollback
256
+ });
257
+ }
258
+ }
259
+ }
260
+ try {
261
+ if (shouldCommit) {
262
+ await dataProvider.CommitTransaction();
263
+ this.log('\nāœ… All changes committed successfully');
264
+ }
265
+ else {
266
+ // User chose to rollback or errors/warnings in CI mode
267
+ this.log('\nšŸ”™ Rolling back all changes...');
268
+ await dataProvider.RollbackTransaction();
269
+ this.log('āœ… Rollback completed - no changes were made to the database');
270
+ }
271
+ }
272
+ catch (error) {
273
+ // Try to rollback on any error
274
+ this.log('\nāŒ Transaction error - attempting to roll back changes');
275
+ try {
276
+ await dataProvider.RollbackTransaction();
277
+ this.log('āœ… Rollback completed');
278
+ }
279
+ catch (rollbackError) {
280
+ this.log('āŒ Rollback failed: ' + (rollbackError instanceof Error ? rollbackError.message : String(rollbackError)));
281
+ }
282
+ throw error;
283
+ }
284
+ }
285
+ }
286
+ // Exit with error if there were errors in CI mode
287
+ if ((totalErrors > 0 || this.errors.length > 0 || (this.warnings.length > 0 && flags.ci)) && flags.ci) {
288
+ this.error('Push failed in CI mode');
142
289
  }
143
290
  }
144
291
  catch (error) {
145
292
  spinner.fail('Push failed');
293
+ // Try to rollback the transaction if one is active
294
+ if (!flags['dry-run']) {
295
+ const { getDataProvider } = await Promise.resolve().then(() => __importStar(require('../../lib/provider-utils')));
296
+ const dataProvider = getDataProvider();
297
+ if (dataProvider && typeof dataProvider.RollbackTransaction === 'function') {
298
+ try {
299
+ this.log('\nšŸ”™ Rolling back transaction due to error...');
300
+ await dataProvider.RollbackTransaction();
301
+ this.log('āœ… Rollback completed - no changes were made to the database');
302
+ }
303
+ catch (rollbackError) {
304
+ this.log('āŒ Rollback failed: ' + (rollbackError instanceof Error ? rollbackError.message : String(rollbackError)));
305
+ }
306
+ }
307
+ }
146
308
  // Enhanced error logging for debugging
147
309
  this.log('\n=== Push Error Details ===');
148
310
  this.log(`Error type: ${error?.constructor?.name || 'Unknown'}`);
@@ -176,7 +338,9 @@ class Push extends core_1.Command {
176
338
  if (sqlLogger) {
177
339
  try {
178
340
  await sqlLogger.dispose();
179
- this.log('āœ… SQL logging session closed');
341
+ if (flags.verbose) {
342
+ this.log('āœ… SQL logging session closed');
343
+ }
180
344
  }
181
345
  catch (error) {
182
346
  this.warn(`Failed to close SQL logging session: ${error}`);
@@ -184,9 +348,8 @@ class Push extends core_1.Command {
184
348
  }
185
349
  // Reset sync engine singleton
186
350
  (0, singleton_manager_1.resetSyncEngine)();
187
- // Clean up database connection
188
- await (0, provider_utils_2.cleanupProvider)();
189
351
  // Exit process to prevent background MJ tasks from throwing errors
352
+ // We don't explicitly close the connection - let the process termination handle it
190
353
  process.exit(0);
191
354
  }
192
355
  }
@@ -200,7 +363,9 @@ class Push extends core_1.Command {
200
363
  dot: true, // Include dotfiles (files starting with .)
201
364
  onlyFiles: true
202
365
  });
203
- this.log(`Processing ${jsonFiles.length} records in ${path_1.default.relative(process.cwd(), entityDir) || '.'}`);
366
+ if (flags.verbose) {
367
+ this.log(`Processing ${jsonFiles.length} records in ${path_1.default.relative(process.cwd(), entityDir) || '.'}`);
368
+ }
204
369
  // First, process all JSON files in this directory
205
370
  await this.processJsonFiles(jsonFiles, entityDir, entityConfig, syncEngine, flags, result);
206
371
  // Then, recursively process subdirectories
@@ -278,10 +443,17 @@ class Push extends core_1.Command {
278
443
  catch (error) {
279
444
  result.errors++;
280
445
  const errorMessage = error instanceof Error ? error.message : String(error);
281
- this.error(`Failed to process ${file}: ${errorMessage}`, { exit: false });
446
+ const fullErrorMessage = `Failed to process ${file}: ${errorMessage}`;
447
+ this.errors.push(fullErrorMessage);
448
+ this.error(fullErrorMessage, { exit: false });
282
449
  }
283
450
  }
284
- spinner.succeed(`Processed ${totalRecords} records from ${jsonFiles.length} files`);
451
+ if (flags.verbose) {
452
+ spinner.succeed(`Processed ${totalRecords} records from ${jsonFiles.length} files`);
453
+ }
454
+ else {
455
+ spinner.stop();
456
+ }
285
457
  }
286
458
  async pushRecord(recordData, entityName, baseDir, fileName, defaults, syncEngine, dryRun, verbose = false, arrayIndex) {
287
459
  // Load or create entity
@@ -394,7 +566,9 @@ class Push extends core_1.Command {
394
566
  async processRelatedEntities(relatedEntities, parentEntity, rootEntity, baseDir, syncEngine, verbose = false, indentLevel = 1) {
395
567
  const indent = ' '.repeat(indentLevel);
396
568
  for (const [entityName, records] of Object.entries(relatedEntities)) {
397
- this.log(`${indent}↳ Processing ${records.length} related ${entityName} records`);
569
+ if (verbose) {
570
+ this.log(`${indent}↳ Processing ${records.length} related ${entityName} records`);
571
+ }
398
572
  for (const relatedRecord of records) {
399
573
  try {
400
574
  // Load or create entity
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/commands/push/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,sCAA6C;AAC7C,wDAA0B;AAC1B,gDAAwB;AAExB,8DAA8B;AAC9B,0DAAiC;AACjC,yCAA8E;AAE9E,6DAAoG;AAEpG,6DAA2D;AAC3D,6DAAyD;AACzD,mEAA6E;AAE7E,mDAA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;oBACf,MAAM,IAAI,KAAK,CAAC,6BAA6B,UAAU,KAAK,KAAK,EAAE,CAAC,CAAC;gBACvE,CAAC;YACH,CAAC;QACH,CAAC;IACH,CAAC;;AA1lBH,uBA2lBC","sourcesContent":["import { Command, Flags } from '@oclif/core';\nimport fs from 'fs-extra';\nimport path from 'path';\nimport { confirm } from '@inquirer/prompts';\nimport ora from 'ora-classic';\nimport fastGlob from 'fast-glob';\nimport { loadMJConfig, loadSyncConfig, loadEntityConfig } from '../../config';\nimport { SyncEngine, RecordData } from '../../lib/sync-engine';\nimport { initializeProvider, findEntityDirectories, getSystemUser } from '../../lib/provider-utils';\nimport { BaseEntity } from '@memberjunction/core';\nimport { cleanupProvider } from '../../lib/provider-utils';\nimport { configManager } from '../../lib/config-manager';\nimport { getSyncEngine, resetSyncEngine } from '../../lib/singleton-manager';\nimport type { SqlLoggingSession } from '@memberjunction/sqlserver-dataprovider';\nimport { uuidv4 } from '@memberjunction/global';\n\nexport default class Push extends Command {\n static description = 'Push local file changes to the database';\n \n static examples = [\n `<%= config.bin %> <%= command.id %>`,\n `<%= config.bin %> <%= command.id %> --dry-run`,\n `<%= config.bin %> <%= command.id %> --dir=\"ai-prompts\"`,\n `<%= config.bin %> <%= command.id %> --ci`,\n ];\n \n static flags = {\n dir: Flags.string({ description: 'Specific entity directory to push' }),\n 'dry-run': Flags.boolean({ description: 'Show what would be pushed without actually pushing' }),\n ci: Flags.boolean({ description: 'CI mode - no prompts, fail on issues' }),\n verbose: Flags.boolean({ char: 'v', description: 'Show detailed field-level output' }),\n };\n \n async run(): Promise<void> {\n const { flags } = await this.parse(Push);\n const spinner = ora();\n let sqlLogger: SqlLoggingSession | null = null;\n \n try {\n // Load configurations\n spinner.start('Loading configuration');\n const mjConfig = loadMJConfig();\n if (!mjConfig) {\n this.error('No mj.config.cjs found in current directory or parent directories');\n }\n \n // Load sync config from target directory if --dir is specified, otherwise from current directory\n const syncConfigDir = flags.dir ? path.resolve(configManager.getOriginalCwd(), flags.dir) : configManager.getOriginalCwd();\n const syncConfig = await loadSyncConfig(syncConfigDir);\n \n // Stop spinner before provider initialization (which logs to console)\n spinner.stop();\n \n // Initialize data provider\n await initializeProvider(mjConfig);\n \n // Initialize sync engine using singleton pattern\n const syncEngine = await getSyncEngine(getSystemUser());\n \n // Show success after all initialization is complete\n spinner.succeed('Configuration and metadata loaded');\n \n // Initialize SQL logging AFTER provider setup is complete\n if (syncConfig?.sqlLogging?.enabled) {\n const outputDir = syncConfig.sqlLogging.outputDirectory || './sql_logging';\n const formatAsMigration = syncConfig.sqlLogging.formatAsMigration || false;\n \n // Ensure output directory exists\n const fullOutputDir = path.resolve(outputDir);\n await fs.ensureDir(fullOutputDir);\n \n // Generate filename with timestamp and directory name\n const now = new Date();\n const humanReadableTimestamp = now.toISOString()\n .replace('T', '_')\n .replace(/:/g, '-')\n .slice(0, -5); // Remove milliseconds and Z\n \n // Get directory name for filename\n const targetDir = flags.dir ? path.resolve(configManager.getOriginalCwd(), flags.dir) : configManager.getOriginalCwd();\n const dirName = path.basename(targetDir);\n \n const filename = formatAsMigration \n ? `V${now.toISOString().replace(/[:.T-]/g, '').slice(0, -5)}__MetadataSync_Push.sql`\n : `metadata-sync-push_${dirName}_${humanReadableTimestamp}.sql`;\n const logFilePath = path.join(fullOutputDir, filename);\n \n // Import and access the data provider from the provider utils\n const { getDataProvider } = await import('../../lib/provider-utils');\n const dataProvider = getDataProvider();\n \n if (dataProvider && typeof dataProvider.createSqlLogger === 'function') {\n sqlLogger = await dataProvider.createSqlLogger(logFilePath, {\n formatAsMigration,\n description: 'MetadataSync Push Operation',\n statementTypes: 'mutations', // Only log mutations (data changes)\n batchSeparator: 'GO', // Add GO statements for SQL Server batch processing\n prettyPrint: true // Enable pretty printing for readable output\n });\n \n this.log(`šŸ“ SQL logging enabled: ${path.relative(process.cwd(), logFilePath)}`);\n } else {\n this.warn('SQL logging requested but data provider does not support createSqlLogger');\n }\n }\n \n // Find entity directories to process\n const entityDirs = findEntityDirectories(configManager.getOriginalCwd(), flags.dir, syncConfig?.directoryOrder);\n \n if (entityDirs.length === 0) {\n this.error('No entity directories found');\n }\n \n this.log(`Found ${entityDirs.length} entity ${entityDirs.length === 1 ? 'directory' : 'directories'} to process`);\n \n // Process each entity directory\n let totalCreated = 0;\n let totalUpdated = 0;\n let totalErrors = 0;\n \n for (const entityDir of entityDirs) {\n const entityConfig = await loadEntityConfig(entityDir);\n if (!entityConfig) {\n this.warn(`Skipping ${entityDir} - no valid entity configuration`);\n continue;\n }\n \n this.log(`\\nProcessing ${entityConfig.entity} in ${entityDir}`);\n \n const result = await this.processEntityDirectory(\n entityDir,\n entityConfig,\n syncEngine,\n flags,\n syncConfig\n );\n \n totalCreated += result.created;\n totalUpdated += result.updated;\n totalErrors += result.errors;\n }\n \n // Summary\n this.log('\\n=== Push Summary ===');\n this.log(`Created: ${totalCreated}`);\n this.log(`Updated: ${totalUpdated}`);\n this.log(`Errors: ${totalErrors}`);\n \n if (totalErrors > 0 && flags.ci) {\n this.error('Push failed with errors in CI mode');\n }\n \n } catch (error) {\n spinner.fail('Push failed');\n \n // Enhanced error logging for debugging\n this.log('\\n=== Push Error Details ===');\n this.log(`Error type: ${error?.constructor?.name || 'Unknown'}`);\n this.log(`Error message: ${error instanceof Error ? error.message : String(error)}`);\n \n if (error instanceof Error && error.stack) {\n this.log(`\\nStack trace:`);\n this.log(error.stack);\n }\n \n // Log context information\n this.log(`\\nContext:`);\n this.log(`- Working directory: ${configManager.getOriginalCwd()}`);\n this.log(`- Flags: ${JSON.stringify(flags, null, 2)}`);\n \n // Check if error is related to common issues\n const errorMessage = error instanceof Error ? error.message : String(error);\n if (errorMessage.includes('entity directories')) {\n this.log(`\\nHint: This appears to be an entity directory configuration issue.`);\n this.log(`Make sure each entity directory has a .mj-sync.json file.`);\n } else if (errorMessage.includes('database') || errorMessage.includes('connection')) {\n this.log(`\\nHint: This appears to be a database connectivity issue.`);\n this.log(`Check your mj.config.cjs configuration and database connectivity.`);\n } else if (errorMessage.includes('config') || errorMessage.includes('mj.config.cjs')) {\n this.log(`\\nHint: This appears to be a configuration file issue.`);\n this.log(`Make sure mj.config.cjs exists and is properly configured.`);\n }\n \n this.error(error as Error);\n } finally {\n // Dispose SQL logging session if active\n if (sqlLogger) {\n try {\n await sqlLogger.dispose();\n this.log('āœ… SQL logging session closed');\n } catch (error) {\n this.warn(`Failed to close SQL logging session: ${error}`);\n }\n }\n \n // Reset sync engine singleton\n resetSyncEngine();\n // Clean up database connection\n await cleanupProvider();\n \n // Exit process to prevent background MJ tasks from throwing errors\n process.exit(0);\n }\n }\n \n private async processEntityDirectory(\n entityDir: string,\n entityConfig: any,\n syncEngine: SyncEngine,\n flags: any,\n syncConfig: any\n ): Promise<{ created: number; updated: number; errors: number }> {\n const result = { created: 0, updated: 0, errors: 0 };\n \n // Find files matching the configured pattern\n const pattern = entityConfig.filePattern || '*.json';\n const jsonFiles = await fastGlob(pattern, {\n cwd: entityDir,\n ignore: ['.mj-sync.json', '.mj-folder.json', '**/*.backup'],\n dot: true, // Include dotfiles (files starting with .)\n onlyFiles: true\n });\n \n this.log(`Processing ${jsonFiles.length} records in ${path.relative(process.cwd(), entityDir) || '.'}`);\n \n // First, process all JSON files in this directory\n await this.processJsonFiles(jsonFiles, entityDir, entityConfig, syncEngine, flags, result);\n \n // Then, recursively process subdirectories\n const entries = await fs.readdir(entityDir, { withFileTypes: true });\n for (const entry of entries) {\n if (entry.isDirectory() && !entry.name.startsWith('.')) {\n const subDir = path.join(entityDir, entry.name);\n \n // Load subdirectory config and merge with parent config\n let subEntityConfig = { ...entityConfig };\n const subDirConfig = await loadEntityConfig(subDir);\n \n if (subDirConfig) {\n // Check if this is a new entity type (has different entity name)\n if (subDirConfig.entity && subDirConfig.entity !== entityConfig.entity) {\n // This is a different entity type, skip it (will be processed separately)\n continue;\n }\n \n // Merge defaults: parent defaults + subdirectory overrides\n subEntityConfig = {\n ...entityConfig,\n ...subDirConfig,\n defaults: {\n ...entityConfig.defaults,\n ...(subDirConfig.defaults || {})\n }\n };\n }\n \n // Process subdirectory with merged config\n const subResult = await this.processEntityDirectory(\n subDir,\n subEntityConfig,\n syncEngine,\n flags,\n syncConfig\n );\n \n result.created += subResult.created;\n result.updated += subResult.updated;\n result.errors += subResult.errors;\n }\n }\n \n return result;\n }\n \n private async processJsonFiles(\n jsonFiles: string[],\n entityDir: string,\n entityConfig: any,\n syncEngine: SyncEngine,\n flags: any,\n result: { created: number; updated: number; errors: number }\n ): Promise<void> {\n if (jsonFiles.length === 0) {\n return;\n }\n \n const spinner = ora();\n spinner.start('Processing records');\n \n let totalRecords = 0;\n \n for (const file of jsonFiles) {\n try {\n const filePath = path.join(entityDir, file);\n const fileContent = await fs.readJson(filePath);\n \n // Process templates in the loaded content\n const processedContent = await syncEngine.processTemplates(fileContent, entityDir);\n \n // Check if the file contains a single record or an array of records\n const isArray = Array.isArray(processedContent);\n const records: RecordData[] = isArray ? processedContent : [processedContent];\n totalRecords += records.length;\n \n // Build and process defaults (including lookups)\n const defaults = await syncEngine.buildDefaults(filePath, entityConfig);\n \n // Process each record in the file\n for (let i = 0; i < records.length; i++) {\n const recordData = records[i];\n \n // Process the record\n const isNew = await this.pushRecord(\n recordData,\n entityConfig.entity,\n path.dirname(filePath),\n file,\n defaults,\n syncEngine,\n flags['dry-run'],\n flags.verbose,\n isArray ? i : undefined\n );\n \n if (!flags['dry-run']) {\n if (isNew) {\n result.created++;\n } else {\n result.updated++;\n }\n }\n \n spinner.text = `Processing records (${result.created + result.updated + result.errors}/${totalRecords})`;\n }\n \n // Write back the entire file if it's an array\n if (isArray && !flags['dry-run']) {\n await fs.writeJson(filePath, records, { spaces: 2 });\n }\n \n } catch (error) {\n result.errors++;\n const errorMessage = error instanceof Error ? error.message : String(error);\n this.error(`Failed to process ${file}: ${errorMessage}`, { exit: false });\n }\n }\n \n spinner.succeed(`Processed ${totalRecords} records from ${jsonFiles.length} files`);\n }\n \n private async pushRecord(\n recordData: RecordData,\n entityName: string,\n baseDir: string,\n fileName: string,\n defaults: Record<string, any>,\n syncEngine: SyncEngine,\n dryRun: boolean,\n verbose: boolean = false,\n arrayIndex?: number\n ): Promise<boolean> {\n // Load or create entity\n let entity: BaseEntity | null = null;\n let isNew = false;\n \n if (recordData.primaryKey) {\n entity = await syncEngine.loadEntity(entityName, recordData.primaryKey);\n }\n \n if (!entity) {\n // New record\n entity = await syncEngine.createEntityObject(entityName);\n entity.NewRecord();\n isNew = true;\n \n // Handle primary keys for new records\n const entityInfo = syncEngine.getEntityInfo(entityName);\n if (entityInfo) {\n for (const pk of entityInfo.PrimaryKeys) {\n if (!pk.AutoIncrement) {\n // Check if we have a value in primaryKey object\n if (recordData.primaryKey?.[pk.Name]) {\n // User specified a primary key for new record, set it on entity directly\n // Don't add to fields as it will be in primaryKey section\n (entity as any)[pk.Name] = recordData.primaryKey[pk.Name];\n if (verbose) {\n this.log(` Using specified primary key ${pk.Name}: ${recordData.primaryKey[pk.Name]}`);\n }\n } else if (pk.Type.toLowerCase() === 'uniqueidentifier' && !recordData.fields[pk.Name]) {\n // Generate UUID for this primary key and set it on entity directly\n const uuid = uuidv4();\n // Don't add to fields as it will be in primaryKey section after save\n if (verbose) {\n this.log(` Generated UUID for primary key ${pk.Name}: ${uuid}`);\n }\n // Set the generated UUID on the entity\n (entity as any)[pk.Name] = uuid;\n }\n }\n }\n }\n }\n \n // Apply defaults first\n for (const [field, value] of Object.entries(defaults)) {\n if (field in entity) {\n (entity as any)[field] = value;\n }\n }\n \n // Apply record fields\n for (const [field, value] of Object.entries(recordData.fields)) {\n if (field in entity) {\n try {\n const processedValue = await syncEngine.processFieldValue(value, baseDir, null, null);\n if (verbose) {\n this.log(` Setting ${field}: ${JSON.stringify(value)} -> ${JSON.stringify(processedValue)}`);\n }\n (entity as any)[field] = processedValue;\n } catch (error) {\n throw new Error(`Failed to process field '${field}': ${error}`);\n }\n } else {\n this.warn(`Field '${field}' does not exist on entity '${entityName}'`);\n }\n }\n \n if (dryRun) {\n this.log(`Would ${isNew ? 'create' : 'update'} ${entityName} record`);\n return isNew;\n }\n \n // Save the record\n const saved = await entity.Save();\n if (!saved) {\n const message = entity.LatestResult?.Message;\n if (message) {\n throw new Error(`Failed to save record: ${message}`);\n }\n \n const errors = entity.LatestResult?.Errors?.map(err => \n typeof err === 'string' ? err : (err?.message || JSON.stringify(err))\n )?.join(', ') || 'Unknown error';\n throw new Error(`Failed to save record: ${errors}`);\n }\n \n // Process related entities after saving parent\n if (recordData.relatedEntities && !dryRun) {\n await this.processRelatedEntities(\n recordData.relatedEntities,\n entity,\n entity, // root is same as parent for top level\n baseDir,\n syncEngine,\n verbose\n );\n }\n \n // Update the local file with new primary key if created\n if (isNew) {\n const entityInfo = syncEngine.getEntityInfo(entityName);\n if (entityInfo) {\n const newPrimaryKey: Record<string, any> = {};\n for (const pk of entityInfo.PrimaryKeys) {\n newPrimaryKey[pk.Name] = entity.Get(pk.Name);\n }\n recordData.primaryKey = newPrimaryKey;\n }\n }\n \n // Always update sync metadata\n // This ensures related entities are persisted with their metadata\n recordData.sync = {\n lastModified: new Date().toISOString(),\n checksum: syncEngine.calculateChecksum(recordData.fields)\n };\n \n // Write back to file only if it's a single record (not part of an array)\n // Array records are written back in bulk after all records are processed\n if (arrayIndex === undefined) {\n const filePath = path.join(baseDir, fileName);\n await fs.writeJson(filePath, recordData, { spaces: 2 });\n }\n \n return isNew;\n }\n \n private async processRelatedEntities(\n relatedEntities: Record<string, RecordData[]>,\n parentEntity: BaseEntity,\n rootEntity: BaseEntity,\n baseDir: string,\n syncEngine: SyncEngine,\n verbose: boolean = false,\n indentLevel: number = 1\n ): Promise<void> {\n const indent = ' '.repeat(indentLevel);\n \n for (const [entityName, records] of Object.entries(relatedEntities)) {\n this.log(`${indent}↳ Processing ${records.length} related ${entityName} records`);\n \n for (const relatedRecord of records) {\n try {\n // Load or create entity\n let entity = null;\n let isNew = false;\n \n if (relatedRecord.primaryKey) {\n entity = await syncEngine.loadEntity(entityName, relatedRecord.primaryKey);\n }\n \n if (!entity) {\n entity = await syncEngine.createEntityObject(entityName);\n entity.NewRecord();\n isNew = true;\n \n // Handle primary keys for new related entity records\n const entityInfo = syncEngine.getEntityInfo(entityName);\n if (entityInfo) {\n for (const pk of entityInfo.PrimaryKeys) {\n if (!pk.AutoIncrement) {\n // Check if we have a value in primaryKey object\n if (relatedRecord.primaryKey?.[pk.Name]) {\n // User specified a primary key for new record, set it on entity directly\n // Don't add to fields as it will be in primaryKey section\n (entity as any)[pk.Name] = relatedRecord.primaryKey[pk.Name];\n if (verbose) {\n this.log(`${indent} Using specified primary key ${pk.Name}: ${relatedRecord.primaryKey[pk.Name]}`);\n }\n } else if (pk.Type.toLowerCase() === 'uniqueidentifier' && !relatedRecord.fields[pk.Name]) {\n // Generate UUID for this primary key and set it on entity directly\n const uuid = uuidv4();\n // Don't add to fields as it will be in primaryKey section after save\n (entity as any)[pk.Name] = uuid;\n if (verbose) {\n this.log(`${indent} Generated UUID for primary key ${pk.Name}: ${uuid}`);\n }\n }\n }\n }\n }\n }\n \n // Apply fields with parent/root context\n for (const [field, value] of Object.entries(relatedRecord.fields)) {\n if (field in entity) {\n try {\n const processedValue = await syncEngine.processFieldValue(\n value, \n baseDir, \n parentEntity, \n rootEntity\n );\n if (verbose) {\n this.log(`${indent} Setting ${field}: ${JSON.stringify(value)} -> ${JSON.stringify(processedValue)}`);\n }\n (entity as any)[field] = processedValue;\n } catch (error) {\n throw new Error(`Failed to process field '${field}' in ${entityName}: ${error}`);\n }\n } else {\n this.warn(`${indent} Field '${field}' does not exist on entity '${entityName}'`);\n }\n }\n \n // Save the related entity\n const saved = await entity.Save();\n if (!saved) {\n const message = entity.LatestResult?.Message;\n if (message) {\n throw new Error(`Failed to save related ${entityName}: ${message}`);\n }\n \n const errors = entity.LatestResult?.Errors?.map(err => \n typeof err === 'string' ? err : (err?.message || JSON.stringify(err))\n )?.join(', ') || 'Unknown error';\n throw new Error(`Failed to save related ${entityName}: ${errors}`);\n }\n \n if (verbose) {\n this.log(`${indent} āœ“ ${isNew ? 'Created' : 'Updated'} ${entityName} record`);\n }\n \n // Update the related record with primary key and sync metadata\n const entityInfo = syncEngine.getEntityInfo(entityName);\n if (entityInfo) {\n // Update primary key if new\n if (isNew) {\n relatedRecord.primaryKey = {};\n for (const pk of entityInfo.PrimaryKeys) {\n relatedRecord.primaryKey[pk.Name] = entity.Get(pk.Name);\n }\n }\n \n // Always update sync metadata\n relatedRecord.sync = {\n lastModified: new Date().toISOString(),\n checksum: syncEngine.calculateChecksum(relatedRecord.fields)\n };\n }\n \n // Process nested related entities if any\n if (relatedRecord.relatedEntities) {\n await this.processRelatedEntities(\n relatedRecord.relatedEntities,\n entity,\n rootEntity,\n baseDir,\n syncEngine,\n verbose,\n indentLevel + 1\n );\n }\n } catch (error) {\n throw new Error(`Failed to process related ${entityName}: ${error}`);\n }\n }\n }\n }\n}"]}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/commands/push/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,sCAA6C;AAC7C,wDAA0B;AAC1B,gDAAwB;AACxB,+CAA4C;AAC5C,8DAA8B;AAC9B,0DAAiC;AACjC,kDAA0B;AAC1B,yCAA8E;AAE9E,6DAAoG;AACpG,+CAAuE;AACvE,6DAAyD;AACzD,mEAA6E;AAE7E,mDAAgD;AAEhD,MAAqB,IAAK,SAAQ,cAAO;IACvC,MAAM,CAAC,WAAW,GAAG,yCAAyC,CAAC;IAEvD,QAAQ,GAAa,EAAE,CAAC;IACxB,MAAM,GAAa,EAAE,CAAC;IAE9B,MAAM,CAAC,QAAQ,GAAG;QAChB,qCAAqC;QACrC,+CAA+C;QAC/C,wDAAwD;QACxD,0CAA0C;KAC3C,CAAC;IAEF,MAAM,CAAC,KAAK,GAAG;QACb,GAAG,EAAE,YAAK,CAAC,MAAM,CAAC,EAAE,WAAW,EAAE,mCAAmC,EAAE,CAAC;QACvE,SAAS,EAAE,YAAK,CA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UAAU,EACV,OAAO,EACP,WAAW,GAAG,CAAC,CAChB,CAAC;oBACJ,CAAC;gBACH,CAAC;gBAAC,OAAO,KAAK,EAAE,CAAC;oBACf,MAAM,IAAI,KAAK,CAAC,6BAA6B,UAAU,KAAK,KAAK,EAAE,CAAC,CAAC;gBACvE,CAAC;YACH,CAAC;QACH,CAAC;IACH,CAAC;;AAjxBH,uBAkxBC","sourcesContent":["import { Command, Flags } from '@oclif/core';\nimport fs from 'fs-extra';\nimport path from 'path';\nimport { confirm } from '@inquirer/prompts';\nimport ora from 'ora-classic';\nimport fastGlob from 'fast-glob';\nimport chalk from 'chalk';\nimport { loadMJConfig, loadSyncConfig, loadEntityConfig } from '../../config';\nimport { SyncEngine, RecordData } from '../../lib/sync-engine';\nimport { initializeProvider, findEntityDirectories, getSystemUser } from '../../lib/provider-utils';\nimport { BaseEntity, LogStatus, Metadata } from '@memberjunction/core';\nimport { configManager } from '../../lib/config-manager';\nimport { getSyncEngine, resetSyncEngine } from '../../lib/singleton-manager';\nimport { SQLServerDataProvider, type SqlLoggingSession } from '@memberjunction/sqlserver-dataprovider';\nimport { uuidv4 } from '@memberjunction/global';\n\nexport default class Push extends Command {\n static description = 'Push local file changes to the database';\n \n private warnings: string[] = [];\n private errors: string[] = [];\n \n static examples = [\n `<%= config.bin %> <%= command.id %>`,\n `<%= config.bin %> <%= command.id %> --dry-run`,\n `<%= config.bin %> <%= command.id %> --dir=\"ai-prompts\"`,\n `<%= config.bin %> <%= command.id %> --ci`,\n ];\n \n static flags = {\n dir: Flags.string({ description: 'Specific entity directory to push' }),\n 'dry-run': Flags.boolean({ description: 'Show what would be pushed without actually pushing' }),\n ci: Flags.boolean({ description: 'CI mode - no prompts, fail on issues' }),\n verbose: Flags.boolean({ char: 'v', description: 'Show detailed field-level output' }),\n 'no-validate': Flags.boolean({ description: 'Skip validation before push' }),\n };\n \n // Override warn to collect warnings\n warn(input: string | Error): string | Error {\n const message = typeof input === 'string' ? input : input.message;\n this.warnings.push(message);\n return super.warn(input);\n }\n \n async run(): Promise<void> {\n const { flags } = await this.parse(Push);\n const spinner = ora();\n let sqlLogger: SqlLoggingSession | null = null;\n const startTime = Date.now();\n \n try {\n // Load configurations\n spinner.start('Loading configuration');\n const mjConfig = loadMJConfig();\n if (!mjConfig) {\n this.error('No mj.config.cjs found in current directory or parent directories');\n }\n \n // Load sync config from target directory if --dir is specified, otherwise from current directory\n const syncConfigDir = flags.dir ? path.resolve(configManager.getOriginalCwd(), flags.dir) : configManager.getOriginalCwd();\n const syncConfig = await loadSyncConfig(syncConfigDir);\n \n // Stop spinner before provider initialization (which logs to console)\n spinner.stop();\n \n // Initialize data provider\n await initializeProvider(mjConfig);\n \n // Initialize sync engine using singleton pattern\n const syncEngine = await getSyncEngine(getSystemUser());\n \n // Show success after all initialization is complete\n if (flags.verbose) {\n spinner.succeed('Configuration and metadata loaded');\n } else {\n spinner.stop();\n }\n \n // Initialize SQL logging AFTER provider setup is complete\n if (syncConfig?.sqlLogging?.enabled) {\n const outputDir = syncConfig.sqlLogging.outputDirectory || './sql_logging';\n const formatAsMigration = syncConfig.sqlLogging.formatAsMigration || false;\n \n // Ensure output directory exists\n const fullOutputDir = path.resolve(outputDir);\n await fs.ensureDir(fullOutputDir);\n \n // Generate filename with timestamp and directory name\n const now = new Date();\n const humanReadableTimestamp = now.toISOString()\n .replace('T', '_')\n .replace(/:/g, '-')\n .slice(0, -5); // Remove milliseconds and Z\n \n // Get directory name for filename\n const targetDir = flags.dir ? path.resolve(configManager.getOriginalCwd(), flags.dir) : configManager.getOriginalCwd();\n const dirName = path.basename(targetDir);\n \n const filename = formatAsMigration \n ? `V${now.toISOString().replace(/[:.T-]/g, '').slice(0, -5)}__MetadataSync_Push.sql`\n : `metadata-sync-push_${dirName}_${humanReadableTimestamp}.sql`;\n const logFilePath = path.join(fullOutputDir, filename);\n \n // Import and access the data provider from the provider utils\n const { getDataProvider } = await import('../../lib/provider-utils');\n const dataProvider = getDataProvider();\n \n if (dataProvider && typeof dataProvider.CreateSqlLogger === 'function') {\n sqlLogger = await dataProvider.CreateSqlLogger(logFilePath, {\n formatAsMigration,\n description: 'MetadataSync Push Operation',\n statementTypes: 'mutations', // Only log mutations (data changes)\n batchSeparator: 'GO', // Add GO statements for SQL Server batch processing\n prettyPrint: true // Enable pretty printing for readable output\n });\n \n if (flags.verbose) {\n this.log(`šŸ“ SQL logging enabled: ${path.relative(process.cwd(), logFilePath)}`);\n }\n } else {\n this.warn('SQL logging requested but data provider does not support CreateSqlLogger');\n }\n }\n \n // Find entity directories to process\n const entityDirs = findEntityDirectories(configManager.getOriginalCwd(), flags.dir, syncConfig?.directoryOrder);\n \n if (entityDirs.length === 0) {\n this.error('No entity directories found');\n }\n \n if (flags.verbose) {\n this.log(`Found ${entityDirs.length} entity ${entityDirs.length === 1 ? 'directory' : 'directories'} to process`);\n }\n \n // Run validation unless disabled\n if (!flags['no-validate']) {\n const { ValidationService } = await import('../../services/ValidationService');\n const { FormattingService } = await import('../../services/FormattingService');\n \n spinner.start('Validating metadata...');\n const validator = new ValidationService({ verbose: flags.verbose });\n const formatter = new FormattingService();\n \n const targetDir = flags.dir ? path.resolve(configManager.getOriginalCwd(), flags.dir) : configManager.getOriginalCwd();\n const validationResult = await validator.validateDirectory(targetDir);\n spinner.stop();\n \n if (!validationResult.isValid || validationResult.warnings.length > 0) {\n // Show validation results\n this.log('\\n' + formatter.formatValidationResult(validationResult, flags.verbose));\n \n if (!validationResult.isValid) {\n // In CI mode, fail immediately\n if (flags.ci) {\n this.error('Validation failed. Cannot proceed with push.');\n }\n \n // Otherwise, ask for confirmation\n const shouldContinue = await confirm({\n message: 'Validation failed with errors. Do you want to continue anyway?',\n default: false\n });\n \n if (!shouldContinue) {\n this.error('Push cancelled due to validation errors.');\n }\n }\n } else {\n this.log(chalk.green('āœ“ Validation passed'));\n }\n }\n \n // Start a database transaction for the entire push operation (unless in dry-run mode)\n // IMPORTANT: We start the transaction AFTER metadata loading and validation to avoid\n // transaction conflicts with background refresh operations\n if (!flags['dry-run']) {\n const { getDataProvider } = await import('../../lib/provider-utils');\n const dataProvider = getDataProvider();\n if (dataProvider && typeof dataProvider.BeginTransaction === 'function') {\n try {\n await dataProvider.BeginTransaction();\n if (flags.verbose) {\n this.log('šŸ”„ Transaction started - all changes will be committed or rolled back as a unit');\n }\n } catch (error) {\n this.warn('Failed to start transaction - changes will be committed individually');\n this.warn(`Transaction error: ${error instanceof Error ? error.message : String(error)}`);\n }\n } else {\n this.warn('Transaction support not available - changes will be committed individually');\n }\n }\n \n // Process each entity directory\n let totalCreated = 0;\n let totalUpdated = 0;\n let totalErrors = 0;\n \n for (const entityDir of entityDirs) {\n const entityConfig = await loadEntityConfig(entityDir);\n if (!entityConfig) {\n this.warn(`Skipping ${entityDir} - no valid entity configuration`);\n continue;\n }\n \n if (flags.verbose) {\n this.log(`\\nProcessing ${entityConfig.entity} in ${entityDir}`);\n }\n \n const result = await this.processEntityDirectory(\n entityDir,\n entityConfig,\n syncEngine,\n flags,\n syncConfig\n );\n \n totalCreated += result.created;\n totalUpdated += result.updated;\n totalErrors += result.errors;\n }\n \n // Summary using FormattingService\n const endTime = Date.now();\n const { FormattingService } = await import('../../services/FormattingService');\n const formatter = new FormattingService();\n \n this.log('\\n' + formatter.formatSyncSummary('push', {\n created: totalCreated,\n updated: totalUpdated,\n deleted: 0,\n skipped: 0,\n errors: totalErrors,\n duration: endTime - startTime\n }));\n \n // Handle transaction commit/rollback\n if (!flags['dry-run']) {\n const dataProvider = Metadata.Provider as SQLServerDataProvider;\n \n // Check if we have an active transaction\n if (dataProvider ) {\n let shouldCommit = true;\n \n // If there are any errors, always rollback\n if (totalErrors > 0 || this.errors.length > 0) {\n shouldCommit = false;\n this.log('\\nāŒ Errors detected - rolling back all changes');\n }\n // If there are warnings, ask user (unless in CI mode)\n else if (this.warnings.length > 0) {\n // Filter out transaction-related warnings since we're now using transactions\n const nonTransactionWarnings = this.warnings.filter(w => \n !w.includes('Transaction support not available') && \n !w.includes('Failed to start transaction')\n );\n \n if (nonTransactionWarnings.length > 0) {\n if (flags.ci) {\n // In CI mode, rollback on warnings\n shouldCommit = false;\n this.log('\\nāš ļø Warnings detected in CI mode - rolling back all changes');\n } else {\n // Show warnings to user\n this.log('\\nāš ļø The following warnings were encountered:');\n for (const warning of nonTransactionWarnings) {\n this.log(` - ${warning}`);\n }\n \n // Ask user whether to commit or rollback\n shouldCommit = await confirm({\n message: 'Do you want to commit these changes despite the warnings?',\n default: false // Default to rollback\n });\n }\n }\n }\n \n try {\n if (shouldCommit) {\n await dataProvider.CommitTransaction();\n this.log('\\nāœ… All changes committed successfully');\n } else {\n // User chose to rollback or errors/warnings in CI mode\n this.log('\\nšŸ”™ Rolling back all changes...');\n await dataProvider.RollbackTransaction();\n this.log('āœ… Rollback completed - no changes were made to the database');\n }\n } catch (error) {\n // Try to rollback on any error\n this.log('\\nāŒ Transaction error - attempting to roll back changes');\n try {\n await dataProvider.RollbackTransaction();\n this.log('āœ… Rollback completed');\n } catch (rollbackError) {\n this.log('āŒ Rollback failed: ' + (rollbackError instanceof Error ? rollbackError.message : String(rollbackError)));\n }\n throw error;\n }\n }\n }\n \n // Exit with error if there were errors in CI mode\n if ((totalErrors > 0 || this.errors.length > 0 || (this.warnings.length > 0 && flags.ci)) && flags.ci) {\n this.error('Push failed in CI mode');\n }\n \n } catch (error) {\n spinner.fail('Push failed');\n \n // Try to rollback the transaction if one is active\n if (!flags['dry-run']) {\n const { getDataProvider } = await import('../../lib/provider-utils');\n const dataProvider = getDataProvider();\n \n if (dataProvider && typeof dataProvider.RollbackTransaction === 'function') {\n try {\n this.log('\\nšŸ”™ Rolling back transaction due to error...');\n await dataProvider.RollbackTransaction();\n this.log('āœ… Rollback completed - no changes were made to the database');\n } catch (rollbackError) {\n this.log('āŒ Rollback failed: ' + (rollbackError instanceof Error ? rollbackError.message : String(rollbackError)));\n }\n }\n }\n \n // Enhanced error logging for debugging\n this.log('\\n=== Push Error Details ===');\n this.log(`Error type: ${error?.constructor?.name || 'Unknown'}`);\n this.log(`Error message: ${error instanceof Error ? error.message : String(error)}`);\n \n if (error instanceof Error && error.stack) {\n this.log(`\\nStack trace:`);\n this.log(error.stack);\n }\n \n // Log context information\n this.log(`\\nContext:`);\n this.log(`- Working directory: ${configManager.getOriginalCwd()}`);\n this.log(`- Flags: ${JSON.stringify(flags, null, 2)}`);\n \n // Check if error is related to common issues\n const errorMessage = error instanceof Error ? error.message : String(error);\n if (errorMessage.includes('entity directories')) {\n this.log(`\\nHint: This appears to be an entity directory configuration issue.`);\n this.log(`Make sure each entity directory has a .mj-sync.json file.`);\n } else if (errorMessage.includes('database') || errorMessage.includes('connection')) {\n this.log(`\\nHint: This appears to be a database connectivity issue.`);\n this.log(`Check your mj.config.cjs configuration and database connectivity.`);\n } else if (errorMessage.includes('config') || errorMessage.includes('mj.config.cjs')) {\n this.log(`\\nHint: This appears to be a configuration file issue.`);\n this.log(`Make sure mj.config.cjs exists and is properly configured.`);\n }\n \n this.error(error as Error);\n } finally {\n // Dispose SQL logging session if active\n if (sqlLogger) {\n try {\n await sqlLogger.dispose();\n if (flags.verbose) {\n this.log('āœ… SQL logging session closed');\n }\n } catch (error) {\n this.warn(`Failed to close SQL logging session: ${error}`);\n }\n }\n \n // Reset sync engine singleton\n resetSyncEngine();\n \n // Exit process to prevent background MJ tasks from throwing errors\n // We don't explicitly close the connection - let the process termination handle it\n process.exit(0);\n }\n }\n \n private async processEntityDirectory(\n entityDir: string,\n entityConfig: any,\n syncEngine: SyncEngine,\n flags: any,\n syncConfig: any\n ): Promise<{ created: number; updated: number; errors: number }> {\n const result = { created: 0, updated: 0, errors: 0 };\n \n // Find files matching the configured pattern\n const pattern = entityConfig.filePattern || '*.json';\n const jsonFiles = await fastGlob(pattern, {\n cwd: entityDir,\n ignore: ['.mj-sync.json', '.mj-folder.json', '**/*.backup'],\n dot: true, // Include dotfiles (files starting with .)\n onlyFiles: true\n });\n \n if (flags.verbose) {\n this.log(`Processing ${jsonFiles.length} records in ${path.relative(process.cwd(), entityDir) || '.'}`);\n }\n \n // First, process all JSON files in this directory\n await this.processJsonFiles(jsonFiles, entityDir, entityConfig, syncEngine, flags, result);\n \n // Then, recursively process subdirectories\n const entries = await fs.readdir(entityDir, { withFileTypes: true });\n for (const entry of entries) {\n if (entry.isDirectory() && !entry.name.startsWith('.')) {\n const subDir = path.join(entityDir, entry.name);\n \n // Load subdirectory config and merge with parent config\n let subEntityConfig = { ...entityConfig };\n const subDirConfig = await loadEntityConfig(subDir);\n \n if (subDirConfig) {\n // Check if this is a new entity type (has different entity name)\n if (subDirConfig.entity && subDirConfig.entity !== entityConfig.entity) {\n // This is a different entity type, skip it (will be processed separately)\n continue;\n }\n \n // Merge defaults: parent defaults + subdirectory overrides\n subEntityConfig = {\n ...entityConfig,\n ...subDirConfig,\n defaults: {\n ...entityConfig.defaults,\n ...(subDirConfig.defaults || {})\n }\n };\n }\n \n // Process subdirectory with merged config\n const subResult = await this.processEntityDirectory(\n subDir,\n subEntityConfig,\n syncEngine,\n flags,\n syncConfig\n );\n \n result.created += subResult.created;\n result.updated += subResult.updated;\n result.errors += subResult.errors;\n }\n }\n \n return result;\n }\n \n private async processJsonFiles(\n jsonFiles: string[],\n entityDir: string,\n entityConfig: any,\n syncEngine: SyncEngine,\n flags: any,\n result: { created: number; updated: number; errors: number }\n ): Promise<void> {\n if (jsonFiles.length === 0) {\n return;\n }\n \n const spinner = ora();\n spinner.start('Processing records');\n \n let totalRecords = 0;\n \n for (const file of jsonFiles) {\n try {\n const filePath = path.join(entityDir, file);\n const fileContent = await fs.readJson(filePath);\n \n // Process templates in the loaded content\n const processedContent = await syncEngine.processTemplates(fileContent, entityDir);\n \n // Check if the file contains a single record or an array of records\n const isArray = Array.isArray(processedContent);\n const records: RecordData[] = isArray ? processedContent : [processedContent];\n totalRecords += records.length;\n \n // Build and process defaults (including lookups)\n const defaults = await syncEngine.buildDefaults(filePath, entityConfig);\n \n // Process each record in the file\n for (let i = 0; i < records.length; i++) {\n const recordData = records[i];\n \n // Process the record\n const isNew = await this.pushRecord(\n recordData,\n entityConfig.entity,\n path.dirname(filePath),\n file,\n defaults,\n syncEngine,\n flags['dry-run'],\n flags.verbose,\n isArray ? i : undefined\n );\n \n if (!flags['dry-run']) {\n if (isNew) {\n result.created++;\n } else {\n result.updated++;\n }\n }\n \n spinner.text = `Processing records (${result.created + result.updated + result.errors}/${totalRecords})`;\n }\n \n // Write back the entire file if it's an array\n if (isArray && !flags['dry-run']) {\n await fs.writeJson(filePath, records, { spaces: 2 });\n }\n \n } catch (error) {\n result.errors++;\n const errorMessage = error instanceof Error ? error.message : String(error);\n const fullErrorMessage = `Failed to process ${file}: ${errorMessage}`;\n this.errors.push(fullErrorMessage);\n this.error(fullErrorMessage, { exit: false });\n }\n }\n \n if (flags.verbose) {\n spinner.succeed(`Processed ${totalRecords} records from ${jsonFiles.length} files`);\n } else {\n spinner.stop();\n }\n }\n \n private async pushRecord(\n recordData: RecordData,\n entityName: string,\n baseDir: string,\n fileName: string,\n defaults: Record<string, any>,\n syncEngine: SyncEngine,\n dryRun: boolean,\n verbose: boolean = false,\n arrayIndex?: number\n ): Promise<boolean> {\n // Load or create entity\n let entity: BaseEntity | null = null;\n let isNew = false;\n \n if (recordData.primaryKey) {\n entity = await syncEngine.loadEntity(entityName, recordData.primaryKey);\n }\n \n if (!entity) {\n // New record\n entity = await syncEngine.createEntityObject(entityName);\n entity.NewRecord();\n isNew = true;\n \n // Handle primary keys for new records\n const entityInfo = syncEngine.getEntityInfo(entityName);\n if (entityInfo) {\n for (const pk of entityInfo.PrimaryKeys) {\n if (!pk.AutoIncrement) {\n // Check if we have a value in primaryKey object\n if (recordData.primaryKey?.[pk.Name]) {\n // User specified a primary key for new record, set it on entity directly\n // Don't add to fields as it will be in primaryKey section\n (entity as any)[pk.Name] = recordData.primaryKey[pk.Name];\n if (verbose) {\n this.log(` Using specified primary key ${pk.Name}: ${recordData.primaryKey[pk.Name]}`);\n }\n } else if (pk.Type.toLowerCase() === 'uniqueidentifier' && !recordData.fields[pk.Name]) {\n // Generate UUID for this primary key and set it on entity directly\n const uuid = uuidv4();\n // Don't add to fields as it will be in primaryKey section after save\n if (verbose) {\n this.log(` Generated UUID for primary key ${pk.Name}: ${uuid}`);\n }\n // Set the generated UUID on the entity\n (entity as any)[pk.Name] = uuid;\n }\n }\n }\n }\n }\n \n // Apply defaults first\n for (const [field, value] of Object.entries(defaults)) {\n if (field in entity) {\n (entity as any)[field] = value;\n }\n }\n \n // Apply record fields\n for (const [field, value] of Object.entries(recordData.fields)) {\n if (field in entity) {\n try {\n const processedValue = await syncEngine.processFieldValue(value, baseDir, null, null);\n if (verbose) {\n this.log(` Setting ${field}: ${JSON.stringify(value)} -> ${JSON.stringify(processedValue)}`);\n }\n (entity as any)[field] = processedValue;\n } catch (error) {\n throw new Error(`Failed to process field '${field}': ${error}`);\n }\n } else {\n this.warn(`Field '${field}' does not exist on entity '${entityName}'`);\n }\n }\n \n if (dryRun) {\n this.log(`Would ${isNew ? 'create' : 'update'} ${entityName} record`);\n return isNew;\n }\n \n // Save the record\n const saved = await entity.Save();\n if (!saved) {\n const message = entity.LatestResult?.Message;\n if (message) {\n throw new Error(`Failed to save record: ${message}`);\n }\n \n const errors = entity.LatestResult?.Errors?.map(err => \n typeof err === 'string' ? err : (err?.message || JSON.stringify(err))\n )?.join(', ') || 'Unknown error';\n throw new Error(`Failed to save record: ${errors}`);\n }\n \n // Process related entities after saving parent\n if (recordData.relatedEntities && !dryRun) {\n await this.processRelatedEntities(\n recordData.relatedEntities,\n entity,\n entity, // root is same as parent for top level\n baseDir,\n syncEngine,\n verbose\n );\n }\n \n // Update the local file with new primary key if created\n if (isNew) {\n const entityInfo = syncEngine.getEntityInfo(entityName);\n if (entityInfo) {\n const newPrimaryKey: Record<string, any> = {};\n for (const pk of entityInfo.PrimaryKeys) {\n newPrimaryKey[pk.Name] = entity.Get(pk.Name);\n }\n recordData.primaryKey = newPrimaryKey;\n }\n }\n \n // Always update sync metadata\n // This ensures related entities are persisted with their metadata\n recordData.sync = {\n lastModified: new Date().toISOString(),\n checksum: syncEngine.calculateChecksum(recordData.fields)\n };\n \n // Write back to file only if it's a single record (not part of an array)\n // Array records are written back in bulk after all records are processed\n if (arrayIndex === undefined) {\n const filePath = path.join(baseDir, fileName);\n await fs.writeJson(filePath, recordData, { spaces: 2 });\n }\n \n return isNew;\n }\n \n private async processRelatedEntities(\n relatedEntities: Record<string, RecordData[]>,\n parentEntity: BaseEntity,\n rootEntity: BaseEntity,\n baseDir: string,\n syncEngine: SyncEngine,\n verbose: boolean = false,\n indentLevel: number = 1\n ): Promise<void> {\n const indent = ' '.repeat(indentLevel);\n \n for (const [entityName, records] of Object.entries(relatedEntities)) {\n if (verbose) {\n this.log(`${indent}↳ Processing ${records.length} related ${entityName} records`);\n }\n \n for (const relatedRecord of records) {\n try {\n // Load or create entity\n let entity = null;\n let isNew = false;\n \n if (relatedRecord.primaryKey) {\n entity = await syncEngine.loadEntity(entityName, relatedRecord.primaryKey);\n }\n \n if (!entity) {\n entity = await syncEngine.createEntityObject(entityName);\n entity.NewRecord();\n isNew = true;\n \n // Handle primary keys for new related entity records\n const entityInfo = syncEngine.getEntityInfo(entityName);\n if (entityInfo) {\n for (const pk of entityInfo.PrimaryKeys) {\n if (!pk.AutoIncrement) {\n // Check if we have a value in primaryKey object\n if (relatedRecord.primaryKey?.[pk.Name]) {\n // User specified a primary key for new record, set it on entity directly\n // Don't add to fields as it will be in primaryKey section\n (entity as any)[pk.Name] = relatedRecord.primaryKey[pk.Name];\n if (verbose) {\n this.log(`${indent} Using specified primary key ${pk.Name}: ${relatedRecord.primaryKey[pk.Name]}`);\n }\n } else if (pk.Type.toLowerCase() === 'uniqueidentifier' && !relatedRecord.fields[pk.Name]) {\n // Generate UUID for this primary key and set it on entity directly\n const uuid = uuidv4();\n // Don't add to fields as it will be in primaryKey section after save\n (entity as any)[pk.Name] = uuid;\n if (verbose) {\n this.log(`${indent} Generated UUID for primary key ${pk.Name}: ${uuid}`);\n }\n }\n }\n }\n }\n }\n \n // Apply fields with parent/root context\n for (const [field, value] of Object.entries(relatedRecord.fields)) {\n if (field in entity) {\n try {\n const processedValue = await syncEngine.processFieldValue(\n value, \n baseDir, \n parentEntity, \n rootEntity\n );\n if (verbose) {\n this.log(`${indent} Setting ${field}: ${JSON.stringify(value)} -> ${JSON.stringify(processedValue)}`);\n }\n (entity as any)[field] = processedValue;\n } catch (error) {\n throw new Error(`Failed to process field '${field}' in ${entityName}: ${error}`);\n }\n } else {\n this.warn(`${indent} Field '${field}' does not exist on entity '${entityName}'`);\n }\n }\n \n // Save the related entity\n const saved = await entity.Save();\n if (!saved) {\n const message = entity.LatestResult?.Message;\n if (message) {\n throw new Error(`Failed to save related ${entityName}: ${message}`);\n }\n \n const errors = entity.LatestResult?.Errors?.map(err => \n typeof err === 'string' ? err : (err?.message || JSON.stringify(err))\n )?.join(', ') || 'Unknown error';\n throw new Error(`Failed to save related ${entityName}: ${errors}`);\n }\n \n if (verbose) {\n this.log(`${indent} āœ“ ${isNew ? 'Created' : 'Updated'} ${entityName} record`);\n }\n \n // Update the related record with primary key and sync metadata\n const entityInfo = syncEngine.getEntityInfo(entityName);\n if (entityInfo) {\n // Update primary key if new\n if (isNew) {\n relatedRecord.primaryKey = {};\n for (const pk of entityInfo.PrimaryKeys) {\n relatedRecord.primaryKey[pk.Name] = entity.Get(pk.Name);\n }\n }\n \n // Always update sync metadata\n relatedRecord.sync = {\n lastModified: new Date().toISOString(),\n checksum: syncEngine.calculateChecksum(relatedRecord.fields)\n };\n }\n \n // Process nested related entities if any\n if (relatedRecord.relatedEntities) {\n await this.processRelatedEntities(\n relatedRecord.relatedEntities,\n entity,\n rootEntity,\n baseDir,\n syncEngine,\n verbose,\n indentLevel + 1\n );\n }\n } catch (error) {\n throw new Error(`Failed to process related ${entityName}: ${error}`);\n }\n }\n }\n }\n}"]}
@@ -0,0 +1,15 @@
1
+ import { Command } from '@oclif/core';
2
+ export default class Validate extends Command {
3
+ static description: string;
4
+ static examples: string[];
5
+ static flags: {
6
+ dir: import("@oclif/core/lib/interfaces").OptionFlag<string, import("@oclif/core/lib/interfaces").CustomOptions>;
7
+ verbose: import("@oclif/core/lib/interfaces").BooleanFlag<boolean>;
8
+ format: import("@oclif/core/lib/interfaces").OptionFlag<string, import("@oclif/core/lib/interfaces").CustomOptions>;
9
+ 'max-depth': import("@oclif/core/lib/interfaces").OptionFlag<number, import("@oclif/core/lib/interfaces").CustomOptions>;
10
+ 'no-best-practices': import("@oclif/core/lib/interfaces").BooleanFlag<boolean>;
11
+ 'no-report': import("@oclif/core/lib/interfaces").BooleanFlag<boolean>;
12
+ 'output-file': import("@oclif/core/lib/interfaces").OptionFlag<string | undefined, import("@oclif/core/lib/interfaces").CustomOptions>;
13
+ };
14
+ run(): Promise<void>;
15
+ }