appwrite-utils-cli 1.7.9 → 1.8.2

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.
Files changed (70) hide show
  1. package/CHANGELOG.md +14 -199
  2. package/README.md +87 -30
  3. package/dist/adapters/AdapterFactory.js +5 -25
  4. package/dist/adapters/DatabaseAdapter.d.ts +17 -2
  5. package/dist/adapters/LegacyAdapter.d.ts +2 -1
  6. package/dist/adapters/LegacyAdapter.js +212 -16
  7. package/dist/adapters/TablesDBAdapter.d.ts +2 -12
  8. package/dist/adapters/TablesDBAdapter.js +261 -57
  9. package/dist/cli/commands/databaseCommands.js +4 -3
  10. package/dist/cli/commands/functionCommands.js +17 -8
  11. package/dist/collections/attributes.js +447 -125
  12. package/dist/collections/methods.js +197 -186
  13. package/dist/collections/tableOperations.d.ts +86 -0
  14. package/dist/collections/tableOperations.js +434 -0
  15. package/dist/collections/transferOperations.d.ts +3 -2
  16. package/dist/collections/transferOperations.js +93 -12
  17. package/dist/config/yamlConfig.d.ts +221 -88
  18. package/dist/examples/yamlTerminologyExample.d.ts +1 -1
  19. package/dist/examples/yamlTerminologyExample.js +6 -3
  20. package/dist/functions/fnConfigDiscovery.d.ts +3 -0
  21. package/dist/functions/fnConfigDiscovery.js +108 -0
  22. package/dist/interactiveCLI.js +18 -15
  23. package/dist/main.js +211 -73
  24. package/dist/migrations/appwriteToX.d.ts +88 -23
  25. package/dist/migrations/comprehensiveTransfer.d.ts +2 -0
  26. package/dist/migrations/comprehensiveTransfer.js +83 -6
  27. package/dist/migrations/dataLoader.d.ts +227 -69
  28. package/dist/migrations/dataLoader.js +3 -3
  29. package/dist/migrations/importController.js +3 -3
  30. package/dist/migrations/relationships.d.ts +8 -2
  31. package/dist/migrations/services/ImportOrchestrator.js +3 -3
  32. package/dist/migrations/transfer.js +159 -37
  33. package/dist/shared/attributeMapper.d.ts +20 -0
  34. package/dist/shared/attributeMapper.js +203 -0
  35. package/dist/shared/selectionDialogs.js +8 -4
  36. package/dist/storage/schemas.d.ts +354 -92
  37. package/dist/utils/configDiscovery.js +4 -3
  38. package/dist/utils/versionDetection.d.ts +0 -4
  39. package/dist/utils/versionDetection.js +41 -173
  40. package/dist/utils/yamlConverter.js +89 -16
  41. package/dist/utils/yamlLoader.d.ts +1 -1
  42. package/dist/utils/yamlLoader.js +6 -2
  43. package/dist/utilsController.js +56 -19
  44. package/package.json +4 -4
  45. package/src/adapters/AdapterFactory.ts +119 -143
  46. package/src/adapters/DatabaseAdapter.ts +18 -3
  47. package/src/adapters/LegacyAdapter.ts +236 -105
  48. package/src/adapters/TablesDBAdapter.ts +773 -643
  49. package/src/cli/commands/databaseCommands.ts +13 -12
  50. package/src/cli/commands/functionCommands.ts +23 -14
  51. package/src/collections/attributes.ts +2054 -1611
  52. package/src/collections/methods.ts +208 -293
  53. package/src/collections/tableOperations.ts +506 -0
  54. package/src/collections/transferOperations.ts +218 -144
  55. package/src/examples/yamlTerminologyExample.ts +10 -5
  56. package/src/functions/fnConfigDiscovery.ts +103 -0
  57. package/src/interactiveCLI.ts +25 -20
  58. package/src/main.ts +549 -194
  59. package/src/migrations/comprehensiveTransfer.ts +126 -50
  60. package/src/migrations/dataLoader.ts +3 -3
  61. package/src/migrations/importController.ts +3 -3
  62. package/src/migrations/services/ImportOrchestrator.ts +3 -3
  63. package/src/migrations/transfer.ts +148 -131
  64. package/src/shared/attributeMapper.ts +229 -0
  65. package/src/shared/selectionDialogs.ts +29 -25
  66. package/src/utils/configDiscovery.ts +9 -3
  67. package/src/utils/versionDetection.ts +74 -228
  68. package/src/utils/yamlConverter.ts +94 -17
  69. package/src/utils/yamlLoader.ts +11 -4
  70. package/src/utilsController.ts +80 -30
@@ -1,1611 +1,2054 @@
1
- import { Query, type Databases, type Models } from "node-appwrite";
2
- import {
3
- attributeSchema,
4
- parseAttribute,
5
- type Attribute,
6
- } from "appwrite-utils";
7
- import {
8
- nameToIdMapping,
9
- enqueueOperation,
10
- markAttributeProcessed,
11
- isAttributeProcessed
12
- } from "../shared/operationQueue.js";
13
- import { delay, tryAwaitWithRetry, calculateExponentialBackoff } from "../utils/helperFunctions.js";
14
- import chalk from "chalk";
15
- import type { DatabaseAdapter, CreateAttributeParams, UpdateAttributeParams, DeleteAttributeParams } from "../adapters/DatabaseAdapter.js";
16
- import { logger } from "../shared/logging.js";
17
- import { MessageFormatter } from "../shared/messageFormatter.js";
18
- import { isDatabaseAdapter } from "../utils/typeGuards.js";
19
-
20
- // Threshold for treating min/max values as undefined (1 trillion)
21
- const MIN_MAX_THRESHOLD = 1_000_000_000_000;
22
-
23
- // Extreme values that Appwrite may return, which should be treated as undefined
24
- const EXTREME_MIN_INTEGER = -9223372036854776000;
25
- const EXTREME_MAX_INTEGER = 9223372036854776000;
26
- const EXTREME_MIN_FLOAT = -1.7976931348623157e+308;
27
- const EXTREME_MAX_FLOAT = 1.7976931348623157e+308;
28
-
29
- /**
30
- * Type guard to check if an attribute has min/max properties
31
- */
32
- const hasMinMaxProperties = (attribute: Attribute): attribute is Attribute & { min?: number; max?: number } => {
33
- return attribute.type === 'integer' || attribute.type === 'double' || attribute.type === 'float';
34
- };
35
-
36
- /**
37
- * Normalizes min/max values for integer and float attributes
38
- * Sets values to undefined if they exceed the threshold or are extreme values from database
39
- */
40
- const normalizeMinMaxValues = (attribute: Attribute): { min?: number; max?: number } => {
41
- if (!hasMinMaxProperties(attribute)) {
42
- logger.debug(`Attribute '${attribute.key}' does not have min/max properties`, {
43
- type: attribute.type,
44
- operation: 'normalizeMinMaxValues'
45
- });
46
- return {};
47
- }
48
-
49
- const { type, min, max } = attribute;
50
- let normalizedMin = min;
51
- let normalizedMax = max;
52
-
53
- logger.debug(`Normalizing min/max values for attribute '${attribute.key}'`, {
54
- type,
55
- originalMin: min,
56
- originalMax: max,
57
- operation: 'normalizeMinMaxValues'
58
- });
59
-
60
- // Handle min value
61
- if (normalizedMin !== undefined && normalizedMin !== null) {
62
- const minValue = Number(normalizedMin);
63
- const originalMin = normalizedMin;
64
-
65
- // Check if it exceeds threshold or is an extreme database value
66
- if (type === 'integer') {
67
- if (Math.abs(minValue) >= MIN_MAX_THRESHOLD || minValue === EXTREME_MIN_INTEGER) {
68
- logger.debug(`Min value normalized to undefined for attribute '${attribute.key}'`, {
69
- type,
70
- originalValue: originalMin,
71
- numericValue: minValue,
72
- reason: Math.abs(minValue) >= MIN_MAX_THRESHOLD ? 'exceeds_threshold' : 'extreme_database_value',
73
- threshold: MIN_MAX_THRESHOLD,
74
- extremeValue: EXTREME_MIN_INTEGER,
75
- operation: 'normalizeMinMaxValues'
76
- });
77
- normalizedMin = undefined;
78
- }
79
- } else { // float/double
80
- if (Math.abs(minValue) >= MIN_MAX_THRESHOLD || minValue === EXTREME_MIN_FLOAT) {
81
- logger.debug(`Min value normalized to undefined for attribute '${attribute.key}'`, {
82
- type,
83
- originalValue: originalMin,
84
- numericValue: minValue,
85
- reason: Math.abs(minValue) >= MIN_MAX_THRESHOLD ? 'exceeds_threshold' : 'extreme_database_value',
86
- threshold: MIN_MAX_THRESHOLD,
87
- extremeValue: EXTREME_MIN_FLOAT,
88
- operation: 'normalizeMinMaxValues'
89
- });
90
- normalizedMin = undefined;
91
- }
92
- }
93
- }
94
-
95
- // Handle max value
96
- if (normalizedMax !== undefined && normalizedMax !== null) {
97
- const maxValue = Number(normalizedMax);
98
- const originalMax = normalizedMax;
99
-
100
- // Check if it exceeds threshold or is an extreme database value
101
- if (type === 'integer') {
102
- if (Math.abs(maxValue) >= MIN_MAX_THRESHOLD || maxValue === EXTREME_MAX_INTEGER) {
103
- logger.debug(`Max value normalized to undefined for attribute '${attribute.key}'`, {
104
- type,
105
- originalValue: originalMax,
106
- numericValue: maxValue,
107
- reason: Math.abs(maxValue) >= MIN_MAX_THRESHOLD ? 'exceeds_threshold' : 'extreme_database_value',
108
- threshold: MIN_MAX_THRESHOLD,
109
- extremeValue: EXTREME_MAX_INTEGER,
110
- operation: 'normalizeMinMaxValues'
111
- });
112
- normalizedMax = undefined;
113
- }
114
- } else { // float/double
115
- if (Math.abs(maxValue) >= MIN_MAX_THRESHOLD || maxValue === EXTREME_MAX_FLOAT) {
116
- logger.debug(`Max value normalized to undefined for attribute '${attribute.key}'`, {
117
- type,
118
- originalValue: originalMax,
119
- numericValue: maxValue,
120
- reason: Math.abs(maxValue) >= MIN_MAX_THRESHOLD ? 'exceeds_threshold' : 'extreme_database_value',
121
- threshold: MIN_MAX_THRESHOLD,
122
- extremeValue: EXTREME_MAX_FLOAT,
123
- operation: 'normalizeMinMaxValues'
124
- });
125
- normalizedMax = undefined;
126
- }
127
- }
128
- }
129
-
130
- const result = { min: normalizedMin, max: normalizedMax };
131
- logger.debug(`Min/max normalization complete for attribute '${attribute.key}'`, {
132
- type,
133
- result,
134
- operation: 'normalizeMinMaxValues'
135
- });
136
-
137
- return result;
138
- };
139
-
140
- /**
141
- * Normalizes an attribute for comparison by handling extreme database values
142
- * This is used when comparing database attributes with config attributes
143
- */
144
- const normalizeAttributeForComparison = (attribute: Attribute): Attribute => {
145
- const normalized: any = { ...attribute };
146
-
147
- // Normalize min/max for numeric types
148
- if (hasMinMaxProperties(attribute)) {
149
- const { min, max } = normalizeMinMaxValues(attribute);
150
- normalized.min = min;
151
- normalized.max = max;
152
- }
153
-
154
- // Remove xdefault if null/undefined to ensure consistent comparison
155
- // Appwrite sets xdefault: null for required attributes, but config files omit it
156
- if ('xdefault' in normalized && (normalized.xdefault === null || normalized.xdefault === undefined)) {
157
- delete normalized.xdefault;
158
- }
159
-
160
- return normalized;
161
- };
162
-
163
- /**
164
- * Helper function to create an attribute using either the adapter or legacy API
165
- */
166
- const createAttributeViaAdapter = async (
167
- db: Databases | DatabaseAdapter,
168
- dbId: string,
169
- collectionId: string,
170
- attribute: Attribute
171
- ): Promise<void> => {
172
- const startTime = Date.now();
173
- const adapterType = isDatabaseAdapter(db) ? 'adapter' : 'legacy';
174
-
175
- logger.info(`Creating attribute '${attribute.key}' via ${adapterType}`, {
176
- type: attribute.type,
177
- dbId,
178
- collectionId,
179
- adapterType,
180
- operation: 'createAttributeViaAdapter'
181
- });
182
-
183
- if (isDatabaseAdapter(db)) {
184
- // Use the adapter's unified createAttribute method
185
- const params: CreateAttributeParams = {
186
- databaseId: dbId,
187
- tableId: collectionId,
188
- key: attribute.key,
189
- type: attribute.type,
190
- required: attribute.required || false,
191
- array: attribute.array || false,
192
- ...((attribute as any).size && { size: (attribute as any).size }),
193
- ...((attribute as any).xdefault !== undefined && !attribute.required && { default: (attribute as any).xdefault }),
194
- ...((attribute as any).encrypted && { encrypt: (attribute as any).encrypted }),
195
- ...((attribute as any).min !== undefined && { min: (attribute as any).min }),
196
- ...((attribute as any).max !== undefined && { max: (attribute as any).max }),
197
- ...((attribute as any).elements && { elements: (attribute as any).elements }),
198
- ...((attribute as any).relatedCollection && { relatedCollection: (attribute as any).relatedCollection }),
199
- ...((attribute as any).relationType && { relationType: (attribute as any).relationType }),
200
- ...((attribute as any).twoWay !== undefined && { twoWay: (attribute as any).twoWay }),
201
- ...((attribute as any).onDelete && { onDelete: (attribute as any).onDelete }),
202
- ...((attribute as any).twoWayKey && { twoWayKey: (attribute as any).twoWayKey })
203
- };
204
-
205
- logger.debug(`Adapter create parameters for '${attribute.key}'`, {
206
- params,
207
- operation: 'createAttributeViaAdapter'
208
- });
209
-
210
- await db.createAttribute(params);
211
-
212
- const duration = Date.now() - startTime;
213
- logger.info(`Successfully created attribute '${attribute.key}' via adapter`, {
214
- duration,
215
- operation: 'createAttributeViaAdapter'
216
- });
217
- } else {
218
- // Use legacy type-specific methods
219
- logger.debug(`Using legacy creation for attribute '${attribute.key}'`, {
220
- operation: 'createAttributeViaAdapter'
221
- });
222
- await createLegacyAttribute(db, dbId, collectionId, attribute);
223
-
224
- const duration = Date.now() - startTime;
225
- logger.info(`Successfully created attribute '${attribute.key}' via legacy`, {
226
- duration,
227
- operation: 'createAttributeViaAdapter'
228
- });
229
- }
230
- };
231
-
232
- /**
233
- * Helper function to update an attribute using either the adapter or legacy API
234
- */
235
- const updateAttributeViaAdapter = async (
236
- db: Databases | DatabaseAdapter,
237
- dbId: string,
238
- collectionId: string,
239
- attribute: Attribute
240
- ): Promise<void> => {
241
- if (isDatabaseAdapter(db)) {
242
- // Use the adapter's unified updateAttribute method
243
- const params: UpdateAttributeParams = {
244
- databaseId: dbId,
245
- tableId: collectionId,
246
- key: attribute.key,
247
- required: attribute.required || false,
248
- ...((attribute as any).xdefault !== undefined && !attribute.required && { default: (attribute as any).xdefault })
249
- };
250
- await db.updateAttribute(params);
251
- } else {
252
- // Use legacy type-specific methods
253
- await updateLegacyAttribute(db, dbId, collectionId, attribute);
254
- }
255
- };
256
-
257
- /**
258
- * Legacy attribute creation using type-specific methods
259
- */
260
- const createLegacyAttribute = async (
261
- db: Databases,
262
- dbId: string,
263
- collectionId: string,
264
- attribute: Attribute
265
- ): Promise<void> => {
266
- const startTime = Date.now();
267
- const { min: normalizedMin, max: normalizedMax } = normalizeMinMaxValues(attribute);
268
-
269
- logger.info(`Creating legacy attribute '${attribute.key}'`, {
270
- type: attribute.type,
271
- dbId,
272
- collectionId,
273
- normalizedMin,
274
- normalizedMax,
275
- operation: 'createLegacyAttribute'
276
- });
277
-
278
- switch (attribute.type) {
279
- case "string":
280
- const stringParams = {
281
- size: (attribute as any).size || 255,
282
- required: attribute.required || false,
283
- defaultValue: (attribute as any).xdefault !== undefined && !attribute.required ? (attribute as any).xdefault : undefined,
284
- array: attribute.array || false,
285
- encrypted: (attribute as any).encrypted
286
- };
287
- logger.debug(`Creating string attribute '${attribute.key}'`, {
288
- ...stringParams,
289
- operation: 'createLegacyAttribute'
290
- });
291
- await db.createStringAttribute(
292
- dbId,
293
- collectionId,
294
- attribute.key,
295
- stringParams.size,
296
- stringParams.required,
297
- stringParams.defaultValue,
298
- stringParams.array,
299
- stringParams.encrypted
300
- );
301
- break;
302
- case "integer":
303
- const integerParams = {
304
- required: attribute.required || false,
305
- min: normalizedMin !== undefined ? parseInt(String(normalizedMin)) : undefined,
306
- max: normalizedMax !== undefined ? parseInt(String(normalizedMax)) : undefined,
307
- defaultValue: (attribute as any).xdefault !== undefined && !attribute.required ? (attribute as any).xdefault : undefined,
308
- array: attribute.array || false
309
- };
310
- logger.debug(`Creating integer attribute '${attribute.key}'`, {
311
- ...integerParams,
312
- operation: 'createLegacyAttribute'
313
- });
314
- await db.createIntegerAttribute(
315
- dbId,
316
- collectionId,
317
- attribute.key,
318
- integerParams.required,
319
- integerParams.min,
320
- integerParams.max,
321
- integerParams.defaultValue,
322
- integerParams.array
323
- );
324
- break;
325
- case "double":
326
- case "float":
327
- await db.createFloatAttribute(
328
- dbId,
329
- collectionId,
330
- attribute.key,
331
- attribute.required || false,
332
- normalizedMin !== undefined ? Number(normalizedMin) : undefined,
333
- normalizedMax !== undefined ? Number(normalizedMax) : undefined,
334
- (attribute as any).xdefault !== undefined && !attribute.required ? (attribute as any).xdefault : undefined,
335
- attribute.array || false
336
- );
337
- break;
338
- case "boolean":
339
- await db.createBooleanAttribute(
340
- dbId,
341
- collectionId,
342
- attribute.key,
343
- attribute.required || false,
344
- (attribute as any).xdefault !== undefined && !attribute.required ? (attribute as any).xdefault : undefined,
345
- attribute.array || false
346
- );
347
- break;
348
- case "datetime":
349
- await db.createDatetimeAttribute(
350
- dbId,
351
- collectionId,
352
- attribute.key,
353
- attribute.required || false,
354
- (attribute as any).xdefault !== undefined && !attribute.required ? (attribute as any).xdefault : undefined,
355
- attribute.array || false
356
- );
357
- break;
358
- case "email":
359
- await db.createEmailAttribute(
360
- dbId,
361
- collectionId,
362
- attribute.key,
363
- attribute.required || false,
364
- (attribute as any).xdefault !== undefined && !attribute.required ? (attribute as any).xdefault : undefined,
365
- attribute.array || false
366
- );
367
- break;
368
- case "ip":
369
- await db.createIpAttribute(
370
- dbId,
371
- collectionId,
372
- attribute.key,
373
- attribute.required || false,
374
- (attribute as any).xdefault !== undefined && !attribute.required ? (attribute as any).xdefault : undefined,
375
- attribute.array || false
376
- );
377
- break;
378
- case "url":
379
- await db.createUrlAttribute(
380
- dbId,
381
- collectionId,
382
- attribute.key,
383
- attribute.required || false,
384
- (attribute as any).xdefault !== undefined && !attribute.required ? (attribute as any).xdefault : undefined,
385
- attribute.array || false
386
- );
387
- break;
388
- case "enum":
389
- await db.createEnumAttribute(
390
- dbId,
391
- collectionId,
392
- attribute.key,
393
- (attribute as any).elements || [],
394
- attribute.required || false,
395
- (attribute as any).xdefault !== undefined && !attribute.required ? (attribute as any).xdefault : undefined,
396
- attribute.array || false
397
- );
398
- break;
399
- case "relationship":
400
- await db.createRelationshipAttribute(
401
- dbId,
402
- collectionId,
403
- (attribute as any).relatedCollection!,
404
- (attribute as any).relationType!,
405
- (attribute as any).twoWay,
406
- attribute.key,
407
- (attribute as any).twoWayKey,
408
- (attribute as any).onDelete
409
- );
410
- break;
411
- default:
412
- const error = new Error(`Unsupported attribute type: ${(attribute as any).type}`);
413
- logger.error(`Unsupported attribute type for '${(attribute as any).key}'`, {
414
- type: (attribute as any).type,
415
- supportedTypes: ['string', 'integer', 'double', 'float', 'boolean', 'datetime', 'email', 'ip', 'url', 'enum', 'relationship'],
416
- operation: 'createLegacyAttribute'
417
- });
418
- throw error;
419
- }
420
-
421
- const duration = Date.now() - startTime;
422
- logger.info(`Successfully created legacy attribute '${attribute.key}'`, {
423
- type: attribute.type,
424
- duration,
425
- operation: 'createLegacyAttribute'
426
- });
427
- };
428
-
429
- /**
430
- * Legacy attribute update using type-specific methods
431
- */
432
- const updateLegacyAttribute = async (
433
- db: Databases,
434
- dbId: string,
435
- collectionId: string,
436
- attribute: Attribute
437
- ): Promise<void> => {
438
- const { min: normalizedMin, max: normalizedMax } = normalizeMinMaxValues(attribute);
439
-
440
- switch (attribute.type) {
441
- case "string":
442
- await db.updateStringAttribute(
443
- dbId,
444
- collectionId,
445
- attribute.key,
446
- attribute.required || false,
447
- !attribute.required && (attribute as any).xdefault !== undefined ? (attribute as any).xdefault : null,
448
- attribute.size
449
- );
450
- break;
451
- case "integer":
452
- await db.updateIntegerAttribute(
453
- dbId,
454
- collectionId,
455
- attribute.key,
456
- attribute.required || false,
457
- !attribute.required && (attribute as any).xdefault !== undefined ? (attribute as any).xdefault : null,
458
- normalizedMin !== undefined ? parseInt(String(normalizedMin)) : undefined,
459
- normalizedMax !== undefined ? parseInt(String(normalizedMax)) : undefined
460
- );
461
- break;
462
- case "double":
463
- case "float":
464
- await db.updateFloatAttribute(
465
- dbId,
466
- collectionId,
467
- attribute.key,
468
- attribute.required || false,
469
- normalizedMin !== undefined ? Number(normalizedMin) : undefined,
470
- normalizedMax !== undefined ? Number(normalizedMax) : undefined,
471
- !attribute.required && (attribute as any).xdefault !== undefined ? (attribute as any).xdefault : null
472
- );
473
- break;
474
- case "boolean":
475
- await db.updateBooleanAttribute(
476
- dbId,
477
- collectionId,
478
- attribute.key,
479
- attribute.required || false,
480
- !attribute.required && (attribute as any).xdefault !== undefined ? (attribute as any).xdefault : null
481
- );
482
- break;
483
- case "datetime":
484
- await db.updateDatetimeAttribute(
485
- dbId,
486
- collectionId,
487
- attribute.key,
488
- attribute.required || false,
489
- !attribute.required && (attribute as any).xdefault !== undefined ? (attribute as any).xdefault : null
490
- );
491
- break;
492
- case "email":
493
- await db.updateEmailAttribute(
494
- dbId,
495
- collectionId,
496
- attribute.key,
497
- attribute.required || false,
498
- !attribute.required && (attribute as any).xdefault !== undefined ? (attribute as any).xdefault : null
499
- );
500
- break;
501
- case "ip":
502
- await db.updateIpAttribute(
503
- dbId,
504
- collectionId,
505
- attribute.key,
506
- attribute.required || false,
507
- !attribute.required && (attribute as any).xdefault !== undefined ? (attribute as any).xdefault : null
508
- );
509
- break;
510
- case "url":
511
- await db.updateUrlAttribute(
512
- dbId,
513
- collectionId,
514
- attribute.key,
515
- attribute.required || false,
516
- !attribute.required && (attribute as any).xdefault !== undefined ? (attribute as any).xdefault : null
517
- );
518
- break;
519
- case "enum":
520
- await db.updateEnumAttribute(
521
- dbId,
522
- collectionId,
523
- attribute.key,
524
- (attribute as any).elements || [],
525
- attribute.required || false,
526
- !attribute.required && (attribute as any).xdefault !== undefined ? (attribute as any).xdefault : null
527
- );
528
- break;
529
- case "relationship":
530
- await db.updateRelationshipAttribute(
531
- dbId,
532
- collectionId,
533
- attribute.key,
534
- (attribute as any).onDelete
535
- );
536
- break;
537
- default:
538
- throw new Error(`Unsupported attribute type for update: ${(attribute as any).type}`);
539
- }
540
- };
541
-
542
- // Interface for attribute with status (fixing the type issue)
543
- interface AttributeWithStatus {
544
- key: string;
545
- type: string;
546
- status: "available" | "processing" | "deleting" | "stuck" | "failed";
547
- error: string;
548
- required: boolean;
549
- array?: boolean;
550
- $createdAt: string;
551
- $updatedAt: string;
552
- [key: string]: any; // For type-specific fields
553
- }
554
-
555
- /**
556
- * Wait for attribute to become available, with retry logic for stuck attributes and exponential backoff
557
- */
558
- const waitForAttributeAvailable = async (
559
- db: Databases | DatabaseAdapter,
560
- dbId: string,
561
- collectionId: string,
562
- attributeKey: string,
563
- maxWaitTime: number = 60000, // 1 minute
564
- retryCount: number = 0,
565
- maxRetries: number = 5
566
- ): Promise<boolean> => {
567
- const startTime = Date.now();
568
- let checkInterval = 2000; // Start with 2 seconds
569
-
570
- logger.info(`Waiting for attribute '${attributeKey}' to become available`, {
571
- dbId,
572
- collectionId,
573
- maxWaitTime,
574
- retryCount,
575
- maxRetries,
576
- operation: 'waitForAttributeAvailable'
577
- });
578
-
579
- // Calculate exponential backoff: 2s, 4s, 8s, 16s, 30s (capped at 30s)
580
- if (retryCount > 0) {
581
- const exponentialDelay = calculateExponentialBackoff(retryCount);
582
- await delay(exponentialDelay);
583
- }
584
-
585
- while (Date.now() - startTime < maxWaitTime) {
586
- try {
587
- const collection = isDatabaseAdapter(db)
588
- ? (await db.getTable({ databaseId: dbId, tableId: collectionId })).data
589
- : await db.getCollection(dbId, collectionId);
590
- const attribute = (collection.attributes as any[]).find(
591
- (attr: AttributeWithStatus) => attr.key === attributeKey
592
- ) as AttributeWithStatus | undefined;
593
-
594
- if (!attribute) {
595
- MessageFormatter.error(`Attribute '${attributeKey}' not found`);
596
- return false;
597
- }
598
-
599
- const statusInfo = {
600
- attributeKey,
601
- status: attribute.status,
602
- error: attribute.error,
603
- dbId,
604
- collectionId,
605
- waitTime: Date.now() - startTime,
606
- operation: 'waitForAttributeAvailable'
607
- };
608
-
609
- switch (attribute.status) {
610
- case "available":
611
- logger.info(`Attribute '${attributeKey}' became available`, statusInfo);
612
- return true;
613
-
614
- case "failed":
615
- logger.error(`Attribute '${attributeKey}' failed`, statusInfo);
616
- return false;
617
-
618
- case "stuck":
619
- logger.warn(`Attribute '${attributeKey}' is stuck`, statusInfo);
620
- return false;
621
-
622
- case "processing":
623
- // Continue waiting
624
- logger.debug(`Attribute '${attributeKey}' still processing`, statusInfo);
625
- break;
626
-
627
- case "deleting":
628
- MessageFormatter.info(
629
- chalk.yellow(`Attribute '${attributeKey}' is being deleted`)
630
- );
631
- logger.warn(`Attribute '${attributeKey}' is being deleted`, statusInfo);
632
- break;
633
-
634
- default:
635
- MessageFormatter.info(
636
- chalk.yellow(
637
- `Unknown status '${attribute.status}' for attribute '${attributeKey}'`
638
- )
639
- );
640
- logger.warn(`Unknown status for attribute '${attributeKey}'`, statusInfo);
641
- break;
642
- }
643
-
644
- await delay(checkInterval);
645
- } catch (error) {
646
- const errorMessage = error instanceof Error ? error.message : String(error);
647
- MessageFormatter.error(`Error checking attribute status: ${errorMessage}`);
648
-
649
- logger.error('Error checking attribute status', {
650
- attributeKey,
651
- dbId,
652
- collectionId,
653
- error: errorMessage,
654
- waitTime: Date.now() - startTime,
655
- operation: 'waitForAttributeAvailable'
656
- });
657
-
658
- return false;
659
- }
660
- }
661
-
662
- // Timeout reached
663
- MessageFormatter.info(
664
- chalk.yellow(
665
- `⏰ Timeout waiting for attribute '${attributeKey}' (${maxWaitTime}ms)`
666
- )
667
- );
668
-
669
- // If we have retries left and this isn't the last retry, try recreating
670
- if (retryCount < maxRetries) {
671
- MessageFormatter.info(
672
- chalk.yellow(
673
- `🔄 Retrying attribute creation (attempt ${
674
- retryCount + 1
675
- }/${maxRetries})`
676
- )
677
- );
678
- return false; // Signal that we need to retry
679
- }
680
-
681
- return false;
682
- };
683
-
684
- /**
685
- * Wait for all attributes in a collection to become available
686
- */
687
- const waitForAllAttributesAvailable = async (
688
- db: Databases | DatabaseAdapter,
689
- dbId: string,
690
- collectionId: string,
691
- attributeKeys: string[],
692
- maxWaitTime: number = 60000
693
- ): Promise<string[]> => {
694
- MessageFormatter.info(
695
- chalk.blue(
696
- `Waiting for ${attributeKeys.length} attributes to become available...`
697
- )
698
- );
699
-
700
- const failedAttributes: string[] = [];
701
-
702
- for (const attributeKey of attributeKeys) {
703
- const success = await waitForAttributeAvailable(
704
- db,
705
- dbId,
706
- collectionId,
707
- attributeKey,
708
- maxWaitTime
709
- );
710
- if (!success) {
711
- failedAttributes.push(attributeKey);
712
- }
713
- }
714
-
715
- return failedAttributes;
716
- };
717
-
718
- /**
719
- * Delete collection and recreate with retry logic
720
- */
721
- const deleteAndRecreateCollection = async (
722
- db: Databases | DatabaseAdapter,
723
- dbId: string,
724
- collection: Models.Collection,
725
- retryCount: number
726
- ): Promise<Models.Collection | null> => {
727
- try {
728
- MessageFormatter.info(
729
- chalk.yellow(
730
- `🗑️ Deleting collection '${collection.name}' for retry ${retryCount}`
731
- )
732
- );
733
-
734
- // Delete the collection
735
- if (isDatabaseAdapter(db)) {
736
- await db.deleteTable({ databaseId: dbId, tableId: collection.$id });
737
- } else {
738
- await db.deleteCollection(dbId, collection.$id);
739
- }
740
- MessageFormatter.warning(`Deleted collection '${collection.name}'`);
741
-
742
- // Wait a bit before recreating
743
- await delay(2000);
744
-
745
- // Recreate the collection
746
- MessageFormatter.info(`🔄 Recreating collection '${collection.name}'`);
747
- const newCollection = isDatabaseAdapter(db)
748
- ? (await db.createTable({
749
- databaseId: dbId,
750
- id: collection.$id,
751
- name: collection.name,
752
- permissions: collection.$permissions,
753
- documentSecurity: collection.documentSecurity,
754
- enabled: collection.enabled
755
- })).data
756
- : await db.createCollection(
757
- dbId,
758
- collection.$id,
759
- collection.name,
760
- collection.$permissions,
761
- collection.documentSecurity,
762
- collection.enabled
763
- );
764
-
765
- MessageFormatter.success(`✅ Recreated collection '${collection.name}'`);
766
- return newCollection;
767
- } catch (error) {
768
- MessageFormatter.info(
769
- chalk.red(
770
- `Failed to delete/recreate collection '${collection.name}': ${error}`
771
- )
772
- );
773
- return null;
774
- }
775
- };
776
-
777
- /**
778
- * Get the fields that should be compared for a specific attribute type
779
- * Only returns fields that are valid for the given type to avoid false positives
780
- */
781
- const getComparableFields = (type: string): string[] => {
782
- const baseFields = ["key", "type", "array", "required", "xdefault"];
783
-
784
- switch (type) {
785
- case "string":
786
- return [...baseFields, "size", "encrypted"];
787
-
788
- case "integer":
789
- case "double":
790
- case "float":
791
- return [...baseFields, "min", "max"];
792
-
793
- case "enum":
794
- return [...baseFields, "elements"];
795
-
796
- case "relationship":
797
- return [...baseFields, "relationType", "twoWay", "twoWayKey", "onDelete", "relatedCollection"];
798
-
799
- case "boolean":
800
- case "datetime":
801
- case "email":
802
- case "ip":
803
- case "url":
804
- return baseFields;
805
-
806
- default:
807
- // Fallback to all fields for unknown types
808
- return [
809
- "key", "type", "array", "encrypted", "required", "size",
810
- "min", "max", "xdefault", "elements", "relationType",
811
- "twoWay", "twoWayKey", "onDelete", "relatedCollection"
812
- ];
813
- }
814
- };
815
-
816
- const attributesSame = (
817
- databaseAttribute: Attribute,
818
- configAttribute: Attribute
819
- ): boolean => {
820
- // Normalize both attributes for comparison (handle extreme database values)
821
- const normalizedDbAttr = normalizeAttributeForComparison(databaseAttribute);
822
- const normalizedConfigAttr = normalizeAttributeForComparison(configAttribute);
823
-
824
- // Use type-specific field list to avoid false positives from irrelevant fields
825
- const attributesToCheck = getComparableFields(normalizedConfigAttr.type);
826
-
827
- const differences: string[] = [];
828
-
829
- const result = attributesToCheck.every((attr) => {
830
- // Check if both objects have the attribute
831
- const dbHasAttr = attr in normalizedDbAttr;
832
- const configHasAttr = attr in normalizedConfigAttr;
833
-
834
- // If both have the attribute, compare values
835
- if (dbHasAttr && configHasAttr) {
836
- const dbValue = normalizedDbAttr[attr as keyof typeof normalizedDbAttr];
837
- const configValue = normalizedConfigAttr[attr as keyof typeof normalizedConfigAttr];
838
-
839
- // Consider undefined and null as equivalent
840
- if (
841
- (dbValue === undefined || dbValue === null) &&
842
- (configValue === undefined || configValue === null)
843
- ) {
844
- return true;
845
- }
846
-
847
- // Normalize booleans: treat undefined and false as equivalent
848
- if (typeof dbValue === "boolean" || typeof configValue === "boolean") {
849
- const boolMatch = Boolean(dbValue) === Boolean(configValue);
850
- if (!boolMatch) {
851
- differences.push(`${attr}: db=${dbValue} config=${configValue}`);
852
- }
853
- return boolMatch;
854
- }
855
- // For numeric comparisons, compare numbers if both are numeric-like
856
- if (
857
- (typeof dbValue === "number" || (typeof dbValue === "string" && dbValue !== "" && !isNaN(Number(dbValue)))) &&
858
- (typeof configValue === "number" || (typeof configValue === "string" && configValue !== "" && !isNaN(Number(configValue))))
859
- ) {
860
- const numMatch = Number(dbValue) === Number(configValue);
861
- if (!numMatch) {
862
- differences.push(`${attr}: db=${dbValue} config=${configValue}`);
863
- }
864
- return numMatch;
865
- }
866
-
867
- // For array comparisons (e.g., enum elements), use order-independent equality
868
- if (Array.isArray(dbValue) && Array.isArray(configValue)) {
869
- const arrayMatch =
870
- dbValue.length === configValue.length &&
871
- dbValue.every((val) => configValue.includes(val));
872
- if (!arrayMatch) {
873
- differences.push(`${attr}: db=${JSON.stringify(dbValue)} config=${JSON.stringify(configValue)}`);
874
- }
875
- return arrayMatch;
876
- }
877
-
878
- const match = dbValue === configValue;
879
- if (!match) {
880
- differences.push(`${attr}: db=${JSON.stringify(dbValue)} config=${JSON.stringify(configValue)}`);
881
- }
882
- return match;
883
- }
884
-
885
- // If neither has the attribute, consider it the same
886
- if (!dbHasAttr && !configHasAttr) {
887
- return true;
888
- }
889
-
890
- // If one has the attribute and the other doesn't, check if it's undefined or null
891
- if (dbHasAttr && !configHasAttr) {
892
- const dbValue = normalizedDbAttr[attr as keyof typeof normalizedDbAttr];
893
- // Consider default-false booleans as equal to missing in config
894
- if (typeof dbValue === "boolean") {
895
- const match = dbValue === false; // missing in config equals false in db
896
- if (!match) {
897
- differences.push(`${attr}: db=${dbValue} config=<missing>`);
898
- }
899
- return match;
900
- }
901
- const match = dbValue === undefined || dbValue === null;
902
- if (!match) {
903
- differences.push(`${attr}: db=${JSON.stringify(dbValue)} config=<missing>`);
904
- }
905
- return match;
906
- }
907
-
908
- if (!dbHasAttr && configHasAttr) {
909
- const configValue = normalizedConfigAttr[attr as keyof typeof normalizedConfigAttr];
910
- // Consider default-false booleans as equal to missing in db
911
- if (typeof configValue === "boolean") {
912
- const match = configValue === false; // missing in db equals false in config
913
- if (!match) {
914
- differences.push(`${attr}: db=<missing> config=${configValue}`);
915
- }
916
- return match;
917
- }
918
- const match = configValue === undefined || configValue === null;
919
- if (!match) {
920
- differences.push(`${attr}: db=<missing> config=${JSON.stringify(configValue)}`);
921
- }
922
- return match;
923
- }
924
-
925
- // If we reach here, the attributes are different
926
- differences.push(`${attr}: unexpected comparison state`);
927
- return false;
928
- });
929
-
930
- // Log differences if any were found
931
- if (differences.length > 0) {
932
- logger.debug(`Attribute '${normalizedDbAttr.key}' comparison found differences:`, {
933
- differences,
934
- operation: 'attributesSame'
935
- });
936
- }
937
-
938
- // Log differences if comparison failed (for debugging)
939
- if (!result && differences.length > 0) {
940
- MessageFormatter.debug(
941
- `Attribute '${configAttribute.key}' differences detected:`,
942
- { prefix: "Attributes" }
943
- );
944
- differences.forEach(diff => {
945
- MessageFormatter.debug(` ${diff}`, { prefix: "Attributes" });
946
- });
947
- }
948
-
949
- return result;
950
- };
951
-
952
- /**
953
- * Enhanced attribute creation with proper status monitoring and retry logic
954
- */
955
- export const createOrUpdateAttributeWithStatusCheck = async (
956
- db: Databases | DatabaseAdapter,
957
- dbId: string,
958
- collection: Models.Collection,
959
- attribute: Attribute,
960
- retryCount: number = 0,
961
- maxRetries: number = 5
962
- ): Promise<boolean> => {
963
- try {
964
- // First, try to create/update the attribute using existing logic
965
- const result = await createOrUpdateAttribute(db, dbId, collection, attribute);
966
-
967
- // If the attribute was queued (relationship dependency unresolved),
968
- // skip status polling and retry logic — the queue will handle it later.
969
- if (result === "queued") {
970
- MessageFormatter.info(
971
- chalk.yellow(
972
- `⏭️ Deferred relationship attribute '${attribute.key}' — queued for later once dependencies are available`
973
- )
974
- );
975
- return true;
976
- }
977
-
978
- // If collection creation failed, return false to indicate failure
979
- if (result === "error") {
980
- MessageFormatter.error(`Failed to create collection for attribute '${attribute.key}'`);
981
- return false;
982
- }
983
-
984
- // Now wait for the attribute to become available
985
- const success = await waitForAttributeAvailable(
986
- db,
987
- dbId,
988
- collection.$id,
989
- attribute.key,
990
- 60000, // 1 minute timeout
991
- retryCount,
992
- maxRetries
993
- );
994
-
995
- if (success) {
996
- return true;
997
- }
998
-
999
- // If not successful and we have retries left, delete specific attribute and try again
1000
- if (retryCount < maxRetries) {
1001
- MessageFormatter.info(
1002
- chalk.yellow(
1003
- `Attribute '${attribute.key}' failed/stuck, deleting and retrying...`
1004
- )
1005
- );
1006
-
1007
- // Try to delete the specific stuck attribute instead of the entire collection
1008
- try {
1009
- if (isDatabaseAdapter(db)) {
1010
- await db.deleteAttribute({ databaseId: dbId, tableId: collection.$id, key: attribute.key });
1011
- } else {
1012
- await db.deleteAttribute(dbId, collection.$id, attribute.key);
1013
- }
1014
- MessageFormatter.info(
1015
- chalk.yellow(
1016
- `Deleted stuck attribute '${attribute.key}', will retry creation`
1017
- )
1018
- );
1019
-
1020
- // Wait a bit before retry
1021
- await delay(3000);
1022
-
1023
- // Get fresh collection data
1024
- const freshCollection = isDatabaseAdapter(db)
1025
- ? (await db.getTable({ databaseId: dbId, tableId: collection.$id })).data
1026
- : await db.getCollection(dbId, collection.$id);
1027
-
1028
- // Retry with the same collection (attribute should be gone now)
1029
- return await createOrUpdateAttributeWithStatusCheck(
1030
- db,
1031
- dbId,
1032
- freshCollection,
1033
- attribute,
1034
- retryCount + 1,
1035
- maxRetries
1036
- );
1037
- } catch (deleteError) {
1038
- MessageFormatter.info(
1039
- chalk.red(
1040
- `Failed to delete stuck attribute '${attribute.key}': ${deleteError}`
1041
- )
1042
- );
1043
-
1044
- // If attribute deletion fails, only then try collection recreation as last resort
1045
- if (retryCount >= maxRetries - 1) {
1046
- MessageFormatter.info(
1047
- chalk.yellow(
1048
- `Last resort: Recreating collection for attribute '${attribute.key}'`
1049
- )
1050
- );
1051
-
1052
- // Get fresh collection data
1053
- const freshCollection = isDatabaseAdapter(db)
1054
- ? (await db.getTable({ databaseId: dbId, tableId: collection.$id })).data
1055
- : await db.getCollection(dbId, collection.$id);
1056
-
1057
- // Delete and recreate collection
1058
- const newCollection = await deleteAndRecreateCollection(
1059
- db,
1060
- dbId,
1061
- freshCollection,
1062
- retryCount + 1
1063
- );
1064
-
1065
- if (newCollection) {
1066
- // Retry with the new collection
1067
- return await createOrUpdateAttributeWithStatusCheck(
1068
- db,
1069
- dbId,
1070
- newCollection,
1071
- attribute,
1072
- retryCount + 1,
1073
- maxRetries
1074
- );
1075
- }
1076
- } else {
1077
- // Continue to next retry without collection recreation
1078
- return await createOrUpdateAttributeWithStatusCheck(
1079
- db,
1080
- dbId,
1081
- collection,
1082
- attribute,
1083
- retryCount + 1,
1084
- maxRetries
1085
- );
1086
- }
1087
- }
1088
- }
1089
-
1090
- MessageFormatter.info(
1091
- chalk.red(
1092
- `❌ Failed to create attribute '${attribute.key}' after ${
1093
- maxRetries + 1
1094
- } attempts`
1095
- )
1096
- );
1097
- return false;
1098
- } catch (error) {
1099
- MessageFormatter.info(
1100
- chalk.red(`Error creating attribute '${attribute.key}': ${error}`)
1101
- );
1102
-
1103
- if (retryCount < maxRetries) {
1104
- MessageFormatter.info(
1105
- chalk.yellow(`Retrying attribute '${attribute.key}' due to error...`)
1106
- );
1107
-
1108
- // Wait a bit before retry
1109
- await delay(2000);
1110
-
1111
- return await createOrUpdateAttributeWithStatusCheck(
1112
- db,
1113
- dbId,
1114
- collection,
1115
- attribute,
1116
- retryCount + 1,
1117
- maxRetries
1118
- );
1119
- }
1120
-
1121
- return false;
1122
- }
1123
- };
1124
-
1125
- export const createOrUpdateAttribute = async (
1126
- db: Databases | DatabaseAdapter,
1127
- dbId: string,
1128
- collection: Models.Collection,
1129
- attribute: Attribute
1130
- ): Promise<"queued" | "processed" | "error"> => {
1131
- let action = "create";
1132
- let foundAttribute: Attribute | undefined;
1133
- const updateEnabled = true;
1134
- let finalAttribute: any = attribute;
1135
- try {
1136
- const collectionAttr = collection.attributes.find(
1137
- (attr: any) => attr.key === attribute.key
1138
- ) as unknown as any;
1139
- foundAttribute = parseAttribute(collectionAttr);
1140
-
1141
- } catch (error) {
1142
- foundAttribute = undefined;
1143
- }
1144
-
1145
- if (
1146
- foundAttribute &&
1147
- attributesSame(foundAttribute, attribute) &&
1148
- updateEnabled
1149
- ) {
1150
- // No need to do anything, they are the same
1151
- return "processed";
1152
- } else if (
1153
- foundAttribute &&
1154
- !attributesSame(foundAttribute, attribute) &&
1155
- updateEnabled
1156
- ) {
1157
- // MessageFormatter.info(
1158
- // `Updating attribute with same key ${attribute.key} but different values`
1159
- // );
1160
- finalAttribute = {
1161
- ...foundAttribute,
1162
- ...attribute,
1163
- };
1164
- action = "update";
1165
- } else if (
1166
- !updateEnabled &&
1167
- foundAttribute &&
1168
- !attributesSame(foundAttribute, attribute)
1169
- ) {
1170
- if (isDatabaseAdapter(db)) {
1171
- await db.deleteAttribute({ databaseId: dbId, tableId: collection.$id, key: attribute.key });
1172
- } else {
1173
- await db.deleteAttribute(dbId, collection.$id, attribute.key);
1174
- }
1175
- MessageFormatter.info(
1176
- `Deleted attribute: ${attribute.key} to recreate it because they diff (update disabled temporarily)`
1177
- );
1178
- return "processed";
1179
- }
1180
-
1181
-
1182
-
1183
- // Relationship attribute logic with adjustments
1184
- let collectionFoundViaRelatedCollection: Models.Collection | undefined;
1185
- let relatedCollectionId: string | undefined;
1186
- if (
1187
- finalAttribute.type === "relationship" &&
1188
- finalAttribute.relatedCollection
1189
- ) {
1190
- // First try treating relatedCollection as an ID directly
1191
- try {
1192
- const byIdCollection = isDatabaseAdapter(db)
1193
- ? (await db.getTable({ databaseId: dbId, tableId: finalAttribute.relatedCollection })).data
1194
- : await db.getCollection(dbId, finalAttribute.relatedCollection);
1195
- collectionFoundViaRelatedCollection = byIdCollection;
1196
- relatedCollectionId = byIdCollection.$id;
1197
- // Cache by name for subsequent lookups
1198
- nameToIdMapping.set(byIdCollection.name, byIdCollection.$id);
1199
- } catch (_) {
1200
- // Not an ID or not found — fall back to name-based resolution below
1201
- }
1202
-
1203
- if (!collectionFoundViaRelatedCollection && nameToIdMapping.has(finalAttribute.relatedCollection)) {
1204
- relatedCollectionId = nameToIdMapping.get(
1205
- finalAttribute.relatedCollection
1206
- );
1207
- try {
1208
- collectionFoundViaRelatedCollection = isDatabaseAdapter(db)
1209
- ? (await db.getTable({ databaseId: dbId, tableId: relatedCollectionId! })).data
1210
- : await db.getCollection(dbId, relatedCollectionId!);
1211
- } catch (e) {
1212
- // MessageFormatter.info(
1213
- // `Collection not found: ${finalAttribute.relatedCollection} when nameToIdMapping was set`
1214
- // );
1215
- collectionFoundViaRelatedCollection = undefined;
1216
- }
1217
- } else if (!collectionFoundViaRelatedCollection) {
1218
- const collectionsPulled = isDatabaseAdapter(db)
1219
- ? await db.listTables({ databaseId: dbId, queries: [Query.equal("name", finalAttribute.relatedCollection)] })
1220
- : await db.listCollections(dbId, [Query.equal("name", finalAttribute.relatedCollection)]);
1221
- if (collectionsPulled.total && collectionsPulled.total > 0) {
1222
- collectionFoundViaRelatedCollection = isDatabaseAdapter(db)
1223
- ? (collectionsPulled as any).tables?.[0]
1224
- : (collectionsPulled as any).collections?.[0];
1225
- relatedCollectionId = collectionFoundViaRelatedCollection?.$id;
1226
- if (relatedCollectionId) {
1227
- nameToIdMapping.set(
1228
- finalAttribute.relatedCollection,
1229
- relatedCollectionId
1230
- );
1231
- }
1232
- }
1233
- }
1234
- // ONLY queue relationship attributes that have actual unresolved dependencies
1235
- if (!(relatedCollectionId && collectionFoundViaRelatedCollection)) {
1236
- MessageFormatter.info(
1237
- chalk.yellow(
1238
- `⏳ Queueing relationship attribute '${finalAttribute.key}' - related collection '${finalAttribute.relatedCollection}' not found yet`
1239
- )
1240
- );
1241
- enqueueOperation({
1242
- type: "attribute",
1243
- collectionId: collection.$id,
1244
- collection: collection,
1245
- attribute,
1246
- dependencies: [finalAttribute.relatedCollection],
1247
- });
1248
- return "queued";
1249
- }
1250
- }
1251
- finalAttribute = parseAttribute(finalAttribute);
1252
-
1253
- // Ensure collection/table exists - create it if it doesn't
1254
- try {
1255
- await (isDatabaseAdapter(db)
1256
- ? db.getTable({ databaseId: dbId, tableId: collection.$id })
1257
- : db.getCollection(dbId, collection.$id));
1258
- } catch (error) {
1259
- // Collection doesn't exist - create it
1260
- if ((error as any).code === 404 ||
1261
- (error instanceof Error && (
1262
- error.message.includes('collection_not_found') ||
1263
- error.message.includes('Collection with the requested ID could not be found')
1264
- ))) {
1265
-
1266
- MessageFormatter.info(`Collection '${collection.name}' doesn't exist, creating it first...`);
1267
-
1268
- try {
1269
- if (isDatabaseAdapter(db)) {
1270
- await db.createTable({
1271
- databaseId: dbId,
1272
- id: collection.$id,
1273
- name: collection.name,
1274
- permissions: collection.$permissions || [],
1275
- documentSecurity: collection.documentSecurity ?? false,
1276
- enabled: collection.enabled ?? true
1277
- });
1278
- } else {
1279
- await db.createCollection(
1280
- dbId,
1281
- collection.$id,
1282
- collection.name,
1283
- collection.$permissions || [],
1284
- collection.documentSecurity ?? false,
1285
- collection.enabled ?? true
1286
- );
1287
- }
1288
-
1289
- MessageFormatter.success(`Created collection '${collection.name}'`);
1290
- await delay(500); // Wait for collection to be ready
1291
- } catch (createError) {
1292
- MessageFormatter.error(
1293
- `Failed to create collection '${collection.name}'`,
1294
- createError instanceof Error ? createError : new Error(String(createError))
1295
- );
1296
- return "error";
1297
- }
1298
- } else {
1299
- // Other error - re-throw
1300
- throw error;
1301
- }
1302
- }
1303
-
1304
- // Use adapter-based attribute creation/update
1305
- if (action === "create") {
1306
- await tryAwaitWithRetry(
1307
- async () => await createAttributeViaAdapter(db, dbId, collection.$id, finalAttribute)
1308
- );
1309
- } else {
1310
- await tryAwaitWithRetry(
1311
- async () => await updateAttributeViaAdapter(db, dbId, collection.$id, finalAttribute)
1312
- );
1313
- }
1314
- return "processed";
1315
- };
1316
-
1317
- /**
1318
- * Enhanced collection attribute creation with proper status monitoring
1319
- */
1320
- export const createUpdateCollectionAttributesWithStatusCheck = async (
1321
- db: Databases | DatabaseAdapter,
1322
- dbId: string,
1323
- collection: Models.Collection,
1324
- attributes: Attribute[]
1325
- ): Promise<boolean> => {
1326
- const existingAttributes: Attribute[] =
1327
- // @ts-expect-error
1328
- collection.attributes.map((attr) => parseAttribute(attr)) || [];
1329
-
1330
- const attributesToRemove = existingAttributes.filter(
1331
- (attr) => !attributes.some((a) => a.key === attr.key)
1332
- );
1333
- const indexesToRemove = collection.indexes.filter((index) =>
1334
- attributesToRemove.some((attr) => index.attributes.includes(attr.key))
1335
- );
1336
-
1337
- // Handle attribute removal first
1338
- if (attributesToRemove.length > 0) {
1339
- if (indexesToRemove.length > 0) {
1340
- MessageFormatter.info(
1341
- chalk.red(
1342
- `Removing indexes as they rely on an attribute that is being removed: ${indexesToRemove
1343
- .map((index) => index.key)
1344
- .join(", ")}`
1345
- )
1346
- );
1347
- for (const index of indexesToRemove) {
1348
- await tryAwaitWithRetry(
1349
- async () => {
1350
- if (isDatabaseAdapter(db)) {
1351
- await db.deleteIndex({ databaseId: dbId, tableId: collection.$id, key: index.key });
1352
- } else {
1353
- await db.deleteIndex(dbId, collection.$id, index.key);
1354
- }
1355
- }
1356
- );
1357
- await delay(500); // Longer delay for deletions
1358
- }
1359
- }
1360
- for (const attr of attributesToRemove) {
1361
- MessageFormatter.info(
1362
- chalk.red(
1363
- `Removing attribute: ${attr.key} as it is no longer in the collection`
1364
- )
1365
- );
1366
- await tryAwaitWithRetry(
1367
- async () => {
1368
- if (isDatabaseAdapter(db)) {
1369
- await db.deleteAttribute({ databaseId: dbId, tableId: collection.$id, key: attr.key });
1370
- } else {
1371
- await db.deleteAttribute(dbId, collection.$id, attr.key);
1372
- }
1373
- }
1374
- );
1375
- await delay(500); // Longer delay for deletions
1376
- }
1377
- }
1378
-
1379
- // First, get fresh collection data and determine which attributes actually need processing
1380
- let currentCollection = collection;
1381
- try {
1382
- currentCollection = isDatabaseAdapter(db)
1383
- ? (await db.getTable({ databaseId: dbId, tableId: collection.$id })).data
1384
- : await db.getCollection(dbId, collection.$id);
1385
- } catch (error) {
1386
- MessageFormatter.info(
1387
- chalk.yellow(`Warning: Could not refresh collection data: ${error}`)
1388
- );
1389
- }
1390
-
1391
- const existingAttributesMap = new Map<string, Attribute>();
1392
- try {
1393
- const parsedAttributes = currentCollection.attributes.map((attr) =>
1394
- // @ts-expect-error
1395
- parseAttribute(attr)
1396
- );
1397
- parsedAttributes.forEach((attr) =>
1398
- existingAttributesMap.set(attr.key, attr)
1399
- );
1400
- } catch (error) {
1401
- MessageFormatter.info(
1402
- chalk.yellow(`Warning: Could not parse existing attributes: ${error}`)
1403
- );
1404
- }
1405
-
1406
- // Filter to only attributes that need processing (new, changed, or not yet processed)
1407
- const attributesToProcess = attributes.filter((attribute) => {
1408
- // Skip if already processed in this session
1409
- if (isAttributeProcessed(currentCollection.$id, attribute.key)) {
1410
- return false;
1411
- }
1412
-
1413
- const existing = existingAttributesMap.get(attribute.key);
1414
- if (!existing) {
1415
- MessageFormatter.info(`➕ ${attribute.key}`);
1416
- return true;
1417
- }
1418
-
1419
- const needsUpdate = !attributesSame(existing, parseAttribute(attribute));
1420
- if (needsUpdate) {
1421
- MessageFormatter.info(`🔄 ${attribute.key}`);
1422
- } else {
1423
- MessageFormatter.info(chalk.gray(`✅ ${attribute.key}`));
1424
- }
1425
- return needsUpdate;
1426
- });
1427
-
1428
- if (attributesToProcess.length === 0) {
1429
- return true;
1430
- }
1431
-
1432
- let remainingAttributes = [...attributesToProcess];
1433
- let overallRetryCount = 0;
1434
- const maxOverallRetries = 3;
1435
-
1436
- while (
1437
- remainingAttributes.length > 0 &&
1438
- overallRetryCount < maxOverallRetries
1439
- ) {
1440
- const attributesToProcessThisRound = [...remainingAttributes];
1441
- remainingAttributes = []; // Reset for next iteration
1442
-
1443
- for (const attribute of attributesToProcessThisRound) {
1444
- const success = await createOrUpdateAttributeWithStatusCheck(
1445
- db,
1446
- dbId,
1447
- currentCollection,
1448
- attribute
1449
- );
1450
-
1451
- if (success) {
1452
- // Mark this specific attribute as processed
1453
- markAttributeProcessed(currentCollection.$id, attribute.key);
1454
-
1455
- // Get updated collection data for next iteration
1456
- try {
1457
- currentCollection = isDatabaseAdapter(db)
1458
- ? (await db.getTable({ databaseId: dbId, tableId: collection.$id })).data as Models.Collection
1459
- : await db.getCollection(dbId, collection.$id);
1460
- } catch (error) {
1461
- MessageFormatter.info(
1462
- chalk.yellow(`Warning: Could not refresh collection data: ${error}`)
1463
- );
1464
- }
1465
-
1466
- // Add delay between successful attributes
1467
- await delay(1000);
1468
- } else {
1469
- MessageFormatter.info(chalk.red(`❌ ${attribute.key}`));
1470
- remainingAttributes.push(attribute); // Add back to retry list
1471
- }
1472
- }
1473
-
1474
- if (remainingAttributes.length === 0) {
1475
- return true;
1476
- }
1477
-
1478
- overallRetryCount++;
1479
-
1480
- if (overallRetryCount < maxOverallRetries) {
1481
- MessageFormatter.info(
1482
- chalk.yellow(`⏳ Retrying ${remainingAttributes.length} failed attributes...`)
1483
- );
1484
- await delay(5000);
1485
-
1486
- // Refresh collection data before retry
1487
- try {
1488
- currentCollection = isDatabaseAdapter(db)
1489
- ? (await db.getTable({ databaseId: dbId, tableId: collection.$id })).data as Models.Collection
1490
- : await db.getCollection(dbId, collection.$id);
1491
- } catch (error) {
1492
- // Silently continue if refresh fails
1493
- }
1494
- }
1495
- }
1496
-
1497
- // If we get here, some attributes still failed after all retries
1498
- if (attributesToProcess.length > 0) {
1499
- MessageFormatter.info(
1500
- chalk.red(
1501
- `\n❌ Failed to create ${
1502
- attributesToProcess.length
1503
- } attributes after ${maxOverallRetries} attempts: ${attributesToProcess
1504
- .map((a) => a.key)
1505
- .join(", ")}`
1506
- )
1507
- );
1508
- MessageFormatter.info(
1509
- chalk.red(
1510
- `This may indicate a fundamental issue with the attribute definitions or Appwrite instance`
1511
- )
1512
- );
1513
- return false;
1514
- }
1515
-
1516
- MessageFormatter.info(
1517
- chalk.green(
1518
- `\n✅ Successfully created all ${attributes.length} attributes for collection: ${collection.name}`
1519
- )
1520
- );
1521
- return true;
1522
- };
1523
-
1524
- export const createUpdateCollectionAttributes = async (
1525
- db: Databases | DatabaseAdapter,
1526
- dbId: string,
1527
- collection: Models.Collection,
1528
- attributes: Attribute[]
1529
- ): Promise<void> => {
1530
- MessageFormatter.info(
1531
- chalk.green(
1532
- `Creating/Updating attributes for collection: ${collection.name}`
1533
- )
1534
- );
1535
-
1536
- const existingAttributes: Attribute[] =
1537
- // @ts-expect-error
1538
- collection.attributes.map((attr) => parseAttribute(attr)) || [];
1539
-
1540
- const attributesToRemove = existingAttributes.filter(
1541
- (attr) => !attributes.some((a) => a.key === attr.key)
1542
- );
1543
- const indexesToRemove = collection.indexes.filter((index) =>
1544
- attributesToRemove.some((attr) => index.attributes.includes(attr.key))
1545
- );
1546
-
1547
- if (attributesToRemove.length > 0) {
1548
- if (indexesToRemove.length > 0) {
1549
- MessageFormatter.info(
1550
- chalk.red(
1551
- `Removing indexes as they rely on an attribute that is being removed: ${indexesToRemove
1552
- .map((index) => index.key)
1553
- .join(", ")}`
1554
- )
1555
- );
1556
- for (const index of indexesToRemove) {
1557
- await tryAwaitWithRetry(
1558
- async () => {
1559
- if (isDatabaseAdapter(db)) {
1560
- await db.deleteIndex({ databaseId: dbId, tableId: collection.$id, key: index.key });
1561
- } else {
1562
- await db.deleteIndex(dbId, collection.$id, index.key);
1563
- }
1564
- }
1565
- );
1566
- await delay(100);
1567
- }
1568
- }
1569
- for (const attr of attributesToRemove) {
1570
- MessageFormatter.info(
1571
- chalk.red(
1572
- `Removing attribute: ${attr.key} as it is no longer in the collection`
1573
- )
1574
- );
1575
- await tryAwaitWithRetry(
1576
- async () => {
1577
- if (isDatabaseAdapter(db)) {
1578
- await db.deleteAttribute({ databaseId: dbId, tableId: collection.$id, key: attr.key });
1579
- } else {
1580
- await db.deleteAttribute(dbId, collection.$id, attr.key);
1581
- }
1582
- }
1583
- );
1584
- await delay(50);
1585
- }
1586
- }
1587
-
1588
- const batchSize = 3;
1589
- for (let i = 0; i < attributes.length; i += batchSize) {
1590
- const batch = attributes.slice(i, i + batchSize);
1591
- const attributePromises = batch.map((attribute) =>
1592
- tryAwaitWithRetry(
1593
- async () =>
1594
- await createOrUpdateAttribute(db, dbId, collection, attribute)
1595
- )
1596
- );
1597
-
1598
- const results = await Promise.allSettled(attributePromises);
1599
- results.forEach((result) => {
1600
- if (result.status === "rejected") {
1601
- MessageFormatter.error("An attribute promise was rejected:", result.reason);
1602
- }
1603
- });
1604
-
1605
- // Add delay after each batch
1606
- await delay(200);
1607
- }
1608
- MessageFormatter.info(
1609
- `Finished creating/updating attributes for collection: ${collection.name}`
1610
- );
1611
- };
1
+ import { Query, type Databases, type Models } from "node-appwrite";
2
+ import {
3
+ attributeSchema,
4
+ parseAttribute,
5
+ type Attribute,
6
+ } from "appwrite-utils";
7
+ import {
8
+ nameToIdMapping,
9
+ enqueueOperation,
10
+ markAttributeProcessed,
11
+ isAttributeProcessed,
12
+ } from "../shared/operationQueue.js";
13
+ import {
14
+ delay,
15
+ tryAwaitWithRetry,
16
+ calculateExponentialBackoff,
17
+ } from "../utils/helperFunctions.js";
18
+ import chalk from "chalk";
19
+ import { Decimal } from "decimal.js";
20
+ import type { DatabaseAdapter, CreateAttributeParams, UpdateAttributeParams, DeleteAttributeParams } from "../adapters/DatabaseAdapter.js";
21
+ import { logger } from "../shared/logging.js";
22
+ import { MessageFormatter } from "../shared/messageFormatter.js";
23
+ import { isDatabaseAdapter } from "../utils/typeGuards.js";
24
+
25
+ // Extreme values that Appwrite may return, which should be treated as undefined
26
+ const EXTREME_MIN_INTEGER = -9223372036854776000;
27
+ const EXTREME_MAX_INTEGER = 9223372036854776000;
28
+ const EXTREME_MIN_FLOAT = -1.7976931348623157e308;
29
+ const EXTREME_MAX_FLOAT = 1.7976931348623157e308;
30
+
31
+ /**
32
+ * Type guard to check if an attribute has min/max properties
33
+ */
34
+ const hasMinMaxProperties = (
35
+ attribute: Attribute
36
+ ): attribute is Attribute & { min?: number; max?: number } => {
37
+ return (
38
+ attribute.type === "integer" ||
39
+ attribute.type === "double" ||
40
+ attribute.type === "float"
41
+ );
42
+ };
43
+
44
+ /**
45
+ * Normalizes min/max values for integer and float attributes using Decimal.js for precision
46
+ * Validates that min < max and handles extreme database values
47
+ */
48
+ const normalizeMinMaxValues = (
49
+ attribute: Attribute
50
+ ): { min?: number; max?: number } => {
51
+ if (!hasMinMaxProperties(attribute)) {
52
+ logger.debug(
53
+ `Attribute '${attribute.key}' does not have min/max properties`,
54
+ {
55
+ type: attribute.type,
56
+ operation: "normalizeMinMaxValues",
57
+ }
58
+ );
59
+ return {};
60
+ }
61
+
62
+ const { type, min, max } = attribute;
63
+ let normalizedMin = min;
64
+ let normalizedMax = max;
65
+
66
+ logger.debug(`Normalizing min/max values for attribute '${attribute.key}'`, {
67
+ type,
68
+ originalMin: min,
69
+ originalMax: max,
70
+ operation: "normalizeMinMaxValues",
71
+ });
72
+
73
+ // Handle min value - only filter out extreme database values
74
+ if (normalizedMin !== undefined && normalizedMin !== null) {
75
+ const minValue = Number(normalizedMin);
76
+ const originalMin = normalizedMin;
77
+
78
+ // Check if it's an extreme database value (but don't filter out large numbers)
79
+ if (type === 'integer') {
80
+ if (minValue === EXTREME_MIN_INTEGER) {
81
+ logger.debug(`Min value normalized to undefined for attribute '${attribute.key}'`, {
82
+ type,
83
+ originalValue: originalMin,
84
+ numericValue: minValue,
85
+ reason: 'extreme_database_value',
86
+ extremeValue: EXTREME_MIN_INTEGER,
87
+ operation: 'normalizeMinMaxValues'
88
+ });
89
+ normalizedMin = undefined;
90
+ }
91
+ } else { // float/double
92
+ if (minValue === EXTREME_MIN_FLOAT) {
93
+ logger.debug(`Min value normalized to undefined for attribute '${attribute.key}'`, {
94
+ type,
95
+ originalValue: originalMin,
96
+ numericValue: minValue,
97
+ reason: 'extreme_database_value',
98
+ extremeValue: EXTREME_MIN_FLOAT,
99
+ operation: 'normalizeMinMaxValues'
100
+ });
101
+ normalizedMin = undefined;
102
+ }
103
+ }
104
+ }
105
+
106
+ // Handle max value - only filter out extreme database values
107
+ if (normalizedMax !== undefined && normalizedMax !== null) {
108
+ const maxValue = Number(normalizedMax);
109
+ const originalMax = normalizedMax;
110
+
111
+ // Check if it's an extreme database value (but don't filter out large numbers)
112
+ if (type === 'integer') {
113
+ if (maxValue === EXTREME_MAX_INTEGER) {
114
+ logger.debug(`Max value normalized to undefined for attribute '${attribute.key}'`, {
115
+ type,
116
+ originalValue: originalMax,
117
+ numericValue: maxValue,
118
+ reason: 'extreme_database_value',
119
+ extremeValue: EXTREME_MAX_INTEGER,
120
+ operation: 'normalizeMinMaxValues'
121
+ });
122
+ normalizedMax = undefined;
123
+ }
124
+ } else { // float/double
125
+ if (maxValue === EXTREME_MAX_FLOAT) {
126
+ logger.debug(`Max value normalized to undefined for attribute '${attribute.key}'`, {
127
+ type,
128
+ originalValue: originalMax,
129
+ numericValue: maxValue,
130
+ reason: 'extreme_database_value',
131
+ extremeValue: EXTREME_MAX_FLOAT,
132
+ operation: 'normalizeMinMaxValues'
133
+ });
134
+ normalizedMax = undefined;
135
+ }
136
+ }
137
+ }
138
+
139
+ // Validate that min < max using multiple comparison methods for reliability
140
+ if (normalizedMin !== undefined && normalizedMax !== undefined &&
141
+ normalizedMin !== null && normalizedMax !== null) {
142
+
143
+ logger.debug(`Validating min/max values for attribute '${attribute.key}'`, {
144
+ type,
145
+ normalizedMin,
146
+ normalizedMax,
147
+ normalizedMinType: typeof normalizedMin,
148
+ normalizedMaxType: typeof normalizedMax,
149
+ operation: 'normalizeMinMaxValues'
150
+ });
151
+
152
+ // Use multiple validation approaches to ensure reliability
153
+ let needsSwap = false;
154
+ let comparisonMethod = '';
155
+
156
+ try {
157
+ // Method 1: Direct number comparison (most reliable for normal numbers)
158
+ const minNum = Number(normalizedMin);
159
+ const maxNum = Number(normalizedMax);
160
+
161
+ if (!isNaN(minNum) && !isNaN(maxNum)) {
162
+ needsSwap = minNum >= maxNum;
163
+ comparisonMethod = 'direct_number_comparison';
164
+ logger.debug(`Direct number comparison: ${minNum} >= ${maxNum} = ${needsSwap}`, {
165
+ operation: 'normalizeMinMaxValues'
166
+ });
167
+ }
168
+
169
+ // Method 2: Fallback to string comparison for very large numbers
170
+ if (!needsSwap && (isNaN(minNum) || isNaN(maxNum) || Math.abs(minNum) > Number.MAX_SAFE_INTEGER || Math.abs(maxNum) > Number.MAX_SAFE_INTEGER)) {
171
+ const minStr = normalizedMin.toString();
172
+ const maxStr = normalizedMax.toString();
173
+
174
+ // Simple string length and lexicographical comparison for very large numbers
175
+ if (minStr.length !== maxStr.length) {
176
+ needsSwap = minStr.length > maxStr.length;
177
+ } else {
178
+ needsSwap = minStr >= maxStr;
179
+ }
180
+ comparisonMethod = 'string_comparison_fallback';
181
+ logger.debug(`String comparison fallback: '${minStr}' >= '${maxStr}' = ${needsSwap}`, {
182
+ operation: 'normalizeMinMaxValues'
183
+ });
184
+ }
185
+
186
+ // Method 3: Final validation using Decimal.js as last resort
187
+ if (!needsSwap && (typeof normalizedMin === 'string' || typeof normalizedMax === 'string')) {
188
+ try {
189
+ const minDecimal = new Decimal(normalizedMin.toString());
190
+ const maxDecimal = new Decimal(normalizedMax.toString());
191
+ needsSwap = minDecimal.greaterThanOrEqualTo(maxDecimal);
192
+ comparisonMethod = 'decimal_js_fallback';
193
+ logger.debug(`Decimal.js fallback: ${normalizedMin} >= ${normalizedMax} = ${needsSwap}`, {
194
+ operation: 'normalizeMinMaxValues'
195
+ });
196
+ } catch (decimalError) {
197
+ logger.warn(`Decimal.js comparison failed for attribute '${attribute.key}': ${decimalError instanceof Error ? decimalError.message : String(decimalError)}`, {
198
+ operation: 'normalizeMinMaxValues'
199
+ });
200
+ }
201
+ }
202
+
203
+ // Log final validation result
204
+ if (needsSwap) {
205
+ logger.error(`Invalid min/max values detected for attribute '${attribute.key}': min (${normalizedMin}) must be less than max (${normalizedMax})`, {
206
+ type,
207
+ min: normalizedMin,
208
+ max: normalizedMax,
209
+ comparisonMethod,
210
+ operation: 'normalizeMinMaxValues'
211
+ });
212
+
213
+ // Swap values to ensure min < max (graceful handling)
214
+ logger.warn(`Swapping min/max values for attribute '${attribute.key}' to fix validation`, {
215
+ type,
216
+ originalMin: normalizedMin,
217
+ originalMax: normalizedMax,
218
+ newMin: normalizedMax,
219
+ newMax: normalizedMin,
220
+ comparisonMethod,
221
+ operation: 'normalizeMinMaxValues'
222
+ });
223
+
224
+ const temp = normalizedMin;
225
+ normalizedMin = normalizedMax;
226
+ normalizedMax = temp;
227
+ } else {
228
+ logger.debug(`Min/max validation passed for attribute '${attribute.key}'`, {
229
+ type,
230
+ min: normalizedMin,
231
+ max: normalizedMax,
232
+ comparisonMethod,
233
+ operation: 'normalizeMinMaxValues'
234
+ });
235
+ }
236
+
237
+ } catch (error) {
238
+ logger.error(`Critical error during min/max validation for attribute '${attribute.key}'`, {
239
+ type,
240
+ min: normalizedMin,
241
+ max: normalizedMax,
242
+ error: error instanceof Error ? error.message : String(error),
243
+ operation: 'normalizeMinMaxValues'
244
+ });
245
+
246
+ // If all comparison methods fail, set both to undefined to avoid API errors
247
+ normalizedMin = undefined;
248
+ normalizedMax = undefined;
249
+ }
250
+ }
251
+
252
+ const result = { min: normalizedMin, max: normalizedMax };
253
+ logger.debug(
254
+ `Min/max normalization complete for attribute '${attribute.key}'`,
255
+ {
256
+ type,
257
+ result,
258
+ operation: "normalizeMinMaxValues",
259
+ }
260
+ );
261
+
262
+ return result;
263
+ };
264
+
265
+ /**
266
+ * Normalizes an attribute for comparison by handling extreme database values
267
+ * This is used when comparing database attributes with config attributes
268
+ */
269
+ const normalizeAttributeForComparison = (attribute: Attribute): Attribute => {
270
+ const normalized: any = { ...attribute };
271
+
272
+ // Ignore defaults on required attributes to prevent false positives
273
+ if (normalized.required === true && "xdefault" in normalized) {
274
+ delete normalized.xdefault;
275
+ }
276
+
277
+ // Normalize min/max for numeric types
278
+ if (hasMinMaxProperties(attribute)) {
279
+ const { min, max } = normalizeMinMaxValues(attribute);
280
+ normalized.min = min;
281
+ normalized.max = max;
282
+ }
283
+
284
+ // Remove xdefault if null/undefined to ensure consistent comparison
285
+ // Appwrite sets xdefault: null for required attributes, but config files omit it
286
+ if (
287
+ "xdefault" in normalized &&
288
+ (normalized.xdefault === null || normalized.xdefault === undefined)
289
+ ) {
290
+ delete normalized.xdefault;
291
+ }
292
+
293
+ return normalized;
294
+ };
295
+
296
+ /**
297
+ * Helper function to create an attribute using either the adapter or legacy API
298
+ */
299
+ const createAttributeViaAdapter = async (
300
+ db: Databases | DatabaseAdapter,
301
+ dbId: string,
302
+ collectionId: string,
303
+ attribute: Attribute
304
+ ): Promise<void> => {
305
+ const startTime = Date.now();
306
+ const adapterType = isDatabaseAdapter(db) ? "adapter" : "legacy";
307
+
308
+ logger.info(`Creating attribute '${attribute.key}' via ${adapterType}`, {
309
+ type: attribute.type,
310
+ dbId,
311
+ collectionId,
312
+ adapterType,
313
+ operation: "createAttributeViaAdapter",
314
+ });
315
+
316
+ if (isDatabaseAdapter(db)) {
317
+ // Use the adapter's unified createAttribute method
318
+ const params: CreateAttributeParams = {
319
+ databaseId: dbId,
320
+ tableId: collectionId,
321
+ key: attribute.key,
322
+ type: attribute.type,
323
+ required: attribute.required || false,
324
+ array: attribute.array || false,
325
+ ...((attribute as any).size && { size: (attribute as any).size }),
326
+ ...((attribute as any).xdefault !== undefined &&
327
+ !attribute.required && { default: (attribute as any).xdefault }),
328
+ ...((attribute as any).encrypted && {
329
+ encrypt: (attribute as any).encrypted,
330
+ }),
331
+ ...((attribute as any).min !== undefined && {
332
+ min: (attribute as any).min,
333
+ }),
334
+ ...((attribute as any).max !== undefined && {
335
+ max: (attribute as any).max,
336
+ }),
337
+ ...((attribute as any).elements && {
338
+ elements: (attribute as any).elements,
339
+ }),
340
+ ...((attribute as any).relatedCollection && {
341
+ relatedCollection: (attribute as any).relatedCollection,
342
+ }),
343
+ ...((attribute as any).relationType && {
344
+ relationType: (attribute as any).relationType,
345
+ }),
346
+ ...((attribute as any).twoWay !== undefined && {
347
+ twoWay: (attribute as any).twoWay,
348
+ }),
349
+ ...((attribute as any).onDelete && {
350
+ onDelete: (attribute as any).onDelete,
351
+ }),
352
+ ...((attribute as any).twoWayKey && {
353
+ twoWayKey: (attribute as any).twoWayKey,
354
+ }),
355
+ };
356
+
357
+ logger.debug(`Adapter create parameters for '${attribute.key}'`, {
358
+ params,
359
+ operation: "createAttributeViaAdapter",
360
+ });
361
+
362
+ await db.createAttribute(params);
363
+
364
+ const duration = Date.now() - startTime;
365
+ logger.info(
366
+ `Successfully created attribute '${attribute.key}' via adapter`,
367
+ {
368
+ duration,
369
+ operation: "createAttributeViaAdapter",
370
+ }
371
+ );
372
+ } else {
373
+ // Use legacy type-specific methods
374
+ logger.debug(`Using legacy creation for attribute '${attribute.key}'`, {
375
+ operation: "createAttributeViaAdapter",
376
+ });
377
+ await createLegacyAttribute(db, dbId, collectionId, attribute);
378
+
379
+ const duration = Date.now() - startTime;
380
+ logger.info(
381
+ `Successfully created attribute '${attribute.key}' via legacy`,
382
+ {
383
+ duration,
384
+ operation: "createAttributeViaAdapter",
385
+ }
386
+ );
387
+ }
388
+ };
389
+
390
+ /**
391
+ * Helper function to update an attribute using either the adapter or legacy API
392
+ */
393
+ const updateAttributeViaAdapter = async (
394
+ db: Databases | DatabaseAdapter,
395
+ dbId: string,
396
+ collectionId: string,
397
+ attribute: Attribute
398
+ ): Promise<void> => {
399
+ if (isDatabaseAdapter(db)) {
400
+ // Use the adapter's unified updateAttribute method
401
+ const params: UpdateAttributeParams = {
402
+ databaseId: dbId,
403
+ tableId: collectionId,
404
+ key: attribute.key,
405
+ type: attribute.type,
406
+ required: attribute.required || false,
407
+ array: attribute.array || false,
408
+ size: (attribute as any).size,
409
+ min: (attribute as any).min,
410
+ max: (attribute as any).max,
411
+ encrypt: (attribute as any).encrypted ?? (attribute as any).encrypt,
412
+ elements: (attribute as any).elements,
413
+ relatedCollection: (attribute as any).relatedCollection,
414
+ relationType: (attribute as any).relationType,
415
+ twoWay: (attribute as any).twoWay,
416
+ twoWayKey: (attribute as any).twoWayKey,
417
+ onDelete: (attribute as any).onDelete
418
+ };
419
+ if (!attribute.required && (attribute as any).xdefault !== undefined) {
420
+ params.default = (attribute as any).xdefault;
421
+ }
422
+ await db.updateAttribute(params);
423
+ } else {
424
+ // Use legacy type-specific methods
425
+ await updateLegacyAttribute(db, dbId, collectionId, attribute);
426
+ }
427
+ };
428
+
429
+ /**
430
+ * Legacy attribute creation using type-specific methods
431
+ */
432
+ const createLegacyAttribute = async (
433
+ db: Databases,
434
+ dbId: string,
435
+ collectionId: string,
436
+ attribute: Attribute
437
+ ): Promise<void> => {
438
+ const startTime = Date.now();
439
+ const { min: normalizedMin, max: normalizedMax } =
440
+ normalizeMinMaxValues(attribute);
441
+
442
+ logger.info(`Creating legacy attribute '${attribute.key}'`, {
443
+ type: attribute.type,
444
+ dbId,
445
+ collectionId,
446
+ normalizedMin,
447
+ normalizedMax,
448
+ operation: "createLegacyAttribute",
449
+ });
450
+
451
+ switch (attribute.type) {
452
+ case "string":
453
+ const stringParams = {
454
+ size: (attribute as any).size || 255,
455
+ required: attribute.required || false,
456
+ defaultValue:
457
+ (attribute as any).xdefault !== undefined && !attribute.required
458
+ ? (attribute as any).xdefault
459
+ : undefined,
460
+ array: attribute.array || false,
461
+ encrypted: (attribute as any).encrypted,
462
+ };
463
+ logger.debug(`Creating string attribute '${attribute.key}'`, {
464
+ ...stringParams,
465
+ operation: "createLegacyAttribute",
466
+ });
467
+ await db.createStringAttribute(
468
+ dbId,
469
+ collectionId,
470
+ attribute.key,
471
+ stringParams.size,
472
+ stringParams.required,
473
+ stringParams.defaultValue,
474
+ stringParams.array,
475
+ stringParams.encrypted
476
+ );
477
+ break;
478
+ case "integer":
479
+ const integerParams = {
480
+ required: attribute.required || false,
481
+ min:
482
+ normalizedMin !== undefined
483
+ ? parseInt(String(normalizedMin))
484
+ : undefined,
485
+ max:
486
+ normalizedMax !== undefined
487
+ ? parseInt(String(normalizedMax))
488
+ : undefined,
489
+ defaultValue:
490
+ (attribute as any).xdefault !== undefined && !attribute.required
491
+ ? (attribute as any).xdefault
492
+ : undefined,
493
+ array: attribute.array || false,
494
+ };
495
+ logger.debug(`Creating integer attribute '${attribute.key}'`, {
496
+ ...integerParams,
497
+ operation: "createLegacyAttribute",
498
+ });
499
+ await db.createIntegerAttribute(
500
+ dbId,
501
+ collectionId,
502
+ attribute.key,
503
+ integerParams.required,
504
+ integerParams.min,
505
+ integerParams.max,
506
+ integerParams.defaultValue,
507
+ integerParams.array
508
+ );
509
+ break;
510
+ case "double":
511
+ case "float":
512
+ await db.createFloatAttribute(
513
+ dbId,
514
+ collectionId,
515
+ attribute.key,
516
+ attribute.required || false,
517
+ normalizedMin !== undefined ? Number(normalizedMin) : undefined,
518
+ normalizedMax !== undefined ? Number(normalizedMax) : undefined,
519
+ (attribute as any).xdefault !== undefined && !attribute.required
520
+ ? (attribute as any).xdefault
521
+ : undefined,
522
+ attribute.array || false
523
+ );
524
+ break;
525
+ case "boolean":
526
+ await db.createBooleanAttribute(
527
+ dbId,
528
+ collectionId,
529
+ attribute.key,
530
+ attribute.required || false,
531
+ (attribute as any).xdefault !== undefined && !attribute.required
532
+ ? (attribute as any).xdefault
533
+ : undefined,
534
+ attribute.array || false
535
+ );
536
+ break;
537
+ case "datetime":
538
+ await db.createDatetimeAttribute(
539
+ dbId,
540
+ collectionId,
541
+ attribute.key,
542
+ attribute.required || false,
543
+ (attribute as any).xdefault !== undefined && !attribute.required
544
+ ? (attribute as any).xdefault
545
+ : undefined,
546
+ attribute.array || false
547
+ );
548
+ break;
549
+ case "email":
550
+ await db.createEmailAttribute(
551
+ dbId,
552
+ collectionId,
553
+ attribute.key,
554
+ attribute.required || false,
555
+ (attribute as any).xdefault !== undefined && !attribute.required
556
+ ? (attribute as any).xdefault
557
+ : undefined,
558
+ attribute.array || false
559
+ );
560
+ break;
561
+ case "ip":
562
+ await db.createIpAttribute(
563
+ dbId,
564
+ collectionId,
565
+ attribute.key,
566
+ attribute.required || false,
567
+ (attribute as any).xdefault !== undefined && !attribute.required
568
+ ? (attribute as any).xdefault
569
+ : undefined,
570
+ attribute.array || false
571
+ );
572
+ break;
573
+ case "url":
574
+ await db.createUrlAttribute(
575
+ dbId,
576
+ collectionId,
577
+ attribute.key,
578
+ attribute.required || false,
579
+ (attribute as any).xdefault !== undefined && !attribute.required
580
+ ? (attribute as any).xdefault
581
+ : undefined,
582
+ attribute.array || false
583
+ );
584
+ break;
585
+ case "enum":
586
+ await db.createEnumAttribute(
587
+ dbId,
588
+ collectionId,
589
+ attribute.key,
590
+ (attribute as any).elements || [],
591
+ attribute.required || false,
592
+ (attribute as any).xdefault !== undefined && !attribute.required
593
+ ? (attribute as any).xdefault
594
+ : undefined,
595
+ attribute.array || false
596
+ );
597
+ break;
598
+ case "relationship":
599
+ await db.createRelationshipAttribute(
600
+ dbId,
601
+ collectionId,
602
+ (attribute as any).relatedCollection!,
603
+ (attribute as any).relationType!,
604
+ (attribute as any).twoWay,
605
+ attribute.key,
606
+ (attribute as any).twoWayKey,
607
+ (attribute as any).onDelete
608
+ );
609
+ break;
610
+ default:
611
+ const error = new Error(
612
+ `Unsupported attribute type: ${(attribute as any).type}`
613
+ );
614
+ logger.error(
615
+ `Unsupported attribute type for '${(attribute as any).key}'`,
616
+ {
617
+ type: (attribute as any).type,
618
+ supportedTypes: [
619
+ "string",
620
+ "integer",
621
+ "double",
622
+ "float",
623
+ "boolean",
624
+ "datetime",
625
+ "email",
626
+ "ip",
627
+ "url",
628
+ "enum",
629
+ "relationship",
630
+ ],
631
+ operation: "createLegacyAttribute",
632
+ }
633
+ );
634
+ throw error;
635
+ }
636
+
637
+ const duration = Date.now() - startTime;
638
+ logger.info(`Successfully created legacy attribute '${attribute.key}'`, {
639
+ type: attribute.type,
640
+ duration,
641
+ operation: "createLegacyAttribute",
642
+ });
643
+ };
644
+
645
+ /**
646
+ * Legacy attribute update using type-specific methods
647
+ */
648
+ const updateLegacyAttribute = async (
649
+ db: Databases,
650
+ dbId: string,
651
+ collectionId: string,
652
+ attribute: Attribute
653
+ ): Promise<void> => {
654
+ console.log(`DEBUG updateLegacyAttribute before normalizeMinMaxValues:`, {
655
+ key: attribute.key,
656
+ type: attribute.type,
657
+ min: (attribute as any).min,
658
+ max: (attribute as any).max
659
+ });
660
+
661
+ const { min: normalizedMin, max: normalizedMax } =
662
+ normalizeMinMaxValues(attribute);
663
+
664
+
665
+
666
+ switch (attribute.type) {
667
+ case "string":
668
+ await db.updateStringAttribute(
669
+ dbId,
670
+ collectionId,
671
+ attribute.key,
672
+ attribute.required || false,
673
+ !attribute.required && (attribute as any).xdefault !== undefined
674
+ ? (attribute as any).xdefault
675
+ : null,
676
+ attribute.size
677
+ );
678
+ break;
679
+ case "integer":
680
+ await db.updateIntegerAttribute(
681
+ dbId,
682
+ collectionId,
683
+ attribute.key,
684
+ attribute.required || false,
685
+ !attribute.required && (attribute as any).xdefault !== undefined
686
+ ? (attribute as any).xdefault
687
+ : null,
688
+ normalizedMin !== undefined
689
+ ? parseInt(String(normalizedMin))
690
+ : undefined,
691
+ normalizedMax !== undefined
692
+ ? parseInt(String(normalizedMax))
693
+ : undefined
694
+ );
695
+ break;
696
+ case "double":
697
+ case "float":
698
+ const minParam = normalizedMin !== undefined ? Number(normalizedMin) : undefined;
699
+ const maxParam = normalizedMax !== undefined ? Number(normalizedMax) : undefined;
700
+
701
+
702
+
703
+ await db.updateFloatAttribute(
704
+ dbId,
705
+ collectionId,
706
+ attribute.key,
707
+ attribute.required || false,
708
+ minParam,
709
+ maxParam,
710
+ !attribute.required && (attribute as any).xdefault !== undefined
711
+ ? (attribute as any).xdefault
712
+ : null
713
+ );
714
+ break;
715
+ case "boolean":
716
+ await db.updateBooleanAttribute(
717
+ dbId,
718
+ collectionId,
719
+ attribute.key,
720
+ attribute.required || false,
721
+ !attribute.required && (attribute as any).xdefault !== undefined
722
+ ? (attribute as any).xdefault
723
+ : null
724
+ );
725
+ break;
726
+ case "datetime":
727
+ await db.updateDatetimeAttribute(
728
+ dbId,
729
+ collectionId,
730
+ attribute.key,
731
+ attribute.required || false,
732
+ !attribute.required && (attribute as any).xdefault !== undefined
733
+ ? (attribute as any).xdefault
734
+ : null
735
+ );
736
+ break;
737
+ case "email":
738
+ await db.updateEmailAttribute(
739
+ dbId,
740
+ collectionId,
741
+ attribute.key,
742
+ attribute.required || false,
743
+ !attribute.required && (attribute as any).xdefault !== undefined
744
+ ? (attribute as any).xdefault
745
+ : null
746
+ );
747
+ break;
748
+ case "ip":
749
+ await db.updateIpAttribute(
750
+ dbId,
751
+ collectionId,
752
+ attribute.key,
753
+ attribute.required || false,
754
+ !attribute.required && (attribute as any).xdefault !== undefined
755
+ ? (attribute as any).xdefault
756
+ : null
757
+ );
758
+ break;
759
+ case "url":
760
+ await db.updateUrlAttribute(
761
+ dbId,
762
+ collectionId,
763
+ attribute.key,
764
+ attribute.required || false,
765
+ !attribute.required && (attribute as any).xdefault !== undefined
766
+ ? (attribute as any).xdefault
767
+ : null
768
+ );
769
+ break;
770
+ case "enum":
771
+ await db.updateEnumAttribute(
772
+ dbId,
773
+ collectionId,
774
+ attribute.key,
775
+ (attribute as any).elements || [],
776
+ attribute.required || false,
777
+ !attribute.required && (attribute as any).xdefault !== undefined
778
+ ? (attribute as any).xdefault
779
+ : null
780
+ );
781
+ break;
782
+ case "relationship":
783
+ await db.updateRelationshipAttribute(
784
+ dbId,
785
+ collectionId,
786
+ attribute.key,
787
+ (attribute as any).onDelete
788
+ );
789
+ break;
790
+ default:
791
+ throw new Error(
792
+ `Unsupported attribute type for update: ${(attribute as any).type}`
793
+ );
794
+ }
795
+ };
796
+
797
+ // Interface for attribute with status (fixing the type issue)
798
+ interface AttributeWithStatus {
799
+ key: string;
800
+ type: string;
801
+ status: "available" | "processing" | "deleting" | "stuck" | "failed";
802
+ error: string;
803
+ required: boolean;
804
+ array?: boolean;
805
+ $createdAt: string;
806
+ $updatedAt: string;
807
+ [key: string]: any; // For type-specific fields
808
+ }
809
+
810
+ /**
811
+ * Wait for attribute to become available, with retry logic for stuck attributes and exponential backoff
812
+ */
813
+ const waitForAttributeAvailable = async (
814
+ db: Databases | DatabaseAdapter,
815
+ dbId: string,
816
+ collectionId: string,
817
+ attributeKey: string,
818
+ maxWaitTime: number = 60000, // 1 minute
819
+ retryCount: number = 0,
820
+ maxRetries: number = 5
821
+ ): Promise<boolean> => {
822
+ const startTime = Date.now();
823
+ let checkInterval = 2000; // Start with 2 seconds
824
+
825
+ logger.info(`Waiting for attribute '${attributeKey}' to become available`, {
826
+ dbId,
827
+ collectionId,
828
+ maxWaitTime,
829
+ retryCount,
830
+ maxRetries,
831
+ operation: "waitForAttributeAvailable",
832
+ });
833
+
834
+ // Calculate exponential backoff: 2s, 4s, 8s, 16s, 30s (capped at 30s)
835
+ if (retryCount > 0) {
836
+ const exponentialDelay = calculateExponentialBackoff(retryCount);
837
+ await delay(exponentialDelay);
838
+ }
839
+
840
+ while (Date.now() - startTime < maxWaitTime) {
841
+ try {
842
+ const collection = isDatabaseAdapter(db)
843
+ ? (await db.getTable({ databaseId: dbId, tableId: collectionId })).data
844
+ : await db.getCollection(dbId, collectionId);
845
+ const attribute = (collection.attributes as any[]).find(
846
+ (attr: AttributeWithStatus) => attr.key === attributeKey
847
+ ) as AttributeWithStatus | undefined;
848
+
849
+ if (!attribute) {
850
+ MessageFormatter.error(`Attribute '${attributeKey}' not found`);
851
+ return false;
852
+ }
853
+
854
+ const statusInfo = {
855
+ attributeKey,
856
+ status: attribute.status,
857
+ error: attribute.error,
858
+ dbId,
859
+ collectionId,
860
+ waitTime: Date.now() - startTime,
861
+ operation: "waitForAttributeAvailable",
862
+ };
863
+
864
+ switch (attribute.status) {
865
+ case "available":
866
+ logger.info(
867
+ `Attribute '${attributeKey}' became available`,
868
+ statusInfo
869
+ );
870
+ return true;
871
+
872
+ case "failed":
873
+ logger.error(`Attribute '${attributeKey}' failed`, statusInfo);
874
+ return false;
875
+
876
+ case "stuck":
877
+ logger.warn(`Attribute '${attributeKey}' is stuck`, statusInfo);
878
+ return false;
879
+
880
+ case "processing":
881
+ // Continue waiting
882
+ logger.debug(
883
+ `Attribute '${attributeKey}' still processing`,
884
+ statusInfo
885
+ );
886
+ break;
887
+
888
+ case "deleting":
889
+ MessageFormatter.info(
890
+ chalk.yellow(`Attribute '${attributeKey}' is being deleted`)
891
+ );
892
+ logger.warn(
893
+ `Attribute '${attributeKey}' is being deleted`,
894
+ statusInfo
895
+ );
896
+ break;
897
+
898
+ default:
899
+ MessageFormatter.info(
900
+ chalk.yellow(
901
+ `Unknown status '${attribute.status}' for attribute '${attributeKey}'`
902
+ )
903
+ );
904
+ logger.warn(
905
+ `Unknown status for attribute '${attributeKey}'`,
906
+ statusInfo
907
+ );
908
+ break;
909
+ }
910
+
911
+ await delay(checkInterval);
912
+ } catch (error) {
913
+ const errorMessage =
914
+ error instanceof Error ? error.message : String(error);
915
+ MessageFormatter.error(
916
+ `Error checking attribute status: ${errorMessage}`
917
+ );
918
+
919
+ logger.error("Error checking attribute status", {
920
+ attributeKey,
921
+ dbId,
922
+ collectionId,
923
+ error: errorMessage,
924
+ waitTime: Date.now() - startTime,
925
+ operation: "waitForAttributeAvailable",
926
+ });
927
+
928
+ return false;
929
+ }
930
+ }
931
+
932
+ // Timeout reached
933
+ MessageFormatter.info(
934
+ chalk.yellow(
935
+ `⏰ Timeout waiting for attribute '${attributeKey}' (${maxWaitTime}ms)`
936
+ )
937
+ );
938
+
939
+ // If we have retries left and this isn't the last retry, try recreating
940
+ if (retryCount < maxRetries) {
941
+ MessageFormatter.info(
942
+ chalk.yellow(
943
+ `🔄 Retrying attribute creation (attempt ${
944
+ retryCount + 1
945
+ }/${maxRetries})`
946
+ )
947
+ );
948
+ return false; // Signal that we need to retry
949
+ }
950
+
951
+ return false;
952
+ };
953
+
954
+ /**
955
+ * Wait for all attributes in a collection to become available
956
+ */
957
+ const waitForAllAttributesAvailable = async (
958
+ db: Databases | DatabaseAdapter,
959
+ dbId: string,
960
+ collectionId: string,
961
+ attributeKeys: string[],
962
+ maxWaitTime: number = 60000
963
+ ): Promise<string[]> => {
964
+ MessageFormatter.info(
965
+ chalk.blue(
966
+ `Waiting for ${attributeKeys.length} attributes to become available...`
967
+ )
968
+ );
969
+
970
+ const failedAttributes: string[] = [];
971
+
972
+ for (const attributeKey of attributeKeys) {
973
+ const success = await waitForAttributeAvailable(
974
+ db,
975
+ dbId,
976
+ collectionId,
977
+ attributeKey,
978
+ maxWaitTime
979
+ );
980
+ if (!success) {
981
+ failedAttributes.push(attributeKey);
982
+ }
983
+ }
984
+
985
+ return failedAttributes;
986
+ };
987
+
988
+ /**
989
+ * Delete collection and recreate with retry logic
990
+ */
991
+ const deleteAndRecreateCollection = async (
992
+ db: Databases | DatabaseAdapter,
993
+ dbId: string,
994
+ collection: Models.Collection,
995
+ retryCount: number
996
+ ): Promise<Models.Collection | null> => {
997
+ try {
998
+ MessageFormatter.info(
999
+ chalk.yellow(
1000
+ `🗑️ Deleting collection '${collection.name}' for retry ${retryCount}`
1001
+ )
1002
+ );
1003
+
1004
+ // Delete the collection
1005
+ if (isDatabaseAdapter(db)) {
1006
+ await db.deleteTable({ databaseId: dbId, tableId: collection.$id });
1007
+ } else {
1008
+ await db.deleteCollection(dbId, collection.$id);
1009
+ }
1010
+ MessageFormatter.warning(`Deleted collection '${collection.name}'`);
1011
+
1012
+ // Wait a bit before recreating
1013
+ await delay(2000);
1014
+
1015
+ // Recreate the collection
1016
+ MessageFormatter.info(`🔄 Recreating collection '${collection.name}'`);
1017
+ const newCollection = isDatabaseAdapter(db)
1018
+ ? (
1019
+ await db.createTable({
1020
+ databaseId: dbId,
1021
+ id: collection.$id,
1022
+ name: collection.name,
1023
+ permissions: collection.$permissions,
1024
+ documentSecurity: collection.documentSecurity,
1025
+ enabled: collection.enabled,
1026
+ })
1027
+ ).data
1028
+ : await db.createCollection(
1029
+ dbId,
1030
+ collection.$id,
1031
+ collection.name,
1032
+ collection.$permissions,
1033
+ collection.documentSecurity,
1034
+ collection.enabled
1035
+ );
1036
+
1037
+ MessageFormatter.success(`✅ Recreated collection '${collection.name}'`);
1038
+ return newCollection;
1039
+ } catch (error) {
1040
+ MessageFormatter.info(
1041
+ chalk.red(
1042
+ `Failed to delete/recreate collection '${collection.name}': ${error}`
1043
+ )
1044
+ );
1045
+ return null;
1046
+ }
1047
+ };
1048
+
1049
+ /**
1050
+ * Get the fields that should be compared for a specific attribute type
1051
+ * Only returns fields that are valid for the given type to avoid false positives
1052
+ */
1053
+ const getComparableFields = (type: string): string[] => {
1054
+ const baseFields = ["key", "type", "array", "required", "xdefault"];
1055
+
1056
+ switch (type) {
1057
+ case "string":
1058
+ return [...baseFields, "size", "encrypted"];
1059
+
1060
+ case "integer":
1061
+ case "double":
1062
+ case "float":
1063
+ return [...baseFields, "min", "max"];
1064
+
1065
+ case "enum":
1066
+ return [...baseFields, "elements"];
1067
+
1068
+ case "relationship":
1069
+ return [
1070
+ ...baseFields,
1071
+ "relationType",
1072
+ "twoWay",
1073
+ "twoWayKey",
1074
+ "onDelete",
1075
+ "relatedCollection",
1076
+ ];
1077
+
1078
+ case "boolean":
1079
+ case "datetime":
1080
+ case "email":
1081
+ case "ip":
1082
+ case "url":
1083
+ return baseFields;
1084
+
1085
+ default:
1086
+ // Fallback to all fields for unknown types
1087
+ return [
1088
+ "key",
1089
+ "type",
1090
+ "array",
1091
+ "encrypted",
1092
+ "required",
1093
+ "size",
1094
+ "min",
1095
+ "max",
1096
+ "xdefault",
1097
+ "elements",
1098
+ "relationType",
1099
+ "twoWay",
1100
+ "twoWayKey",
1101
+ "onDelete",
1102
+ "relatedCollection",
1103
+ ];
1104
+ }
1105
+ };
1106
+
1107
+ const attributesSame = (
1108
+ databaseAttribute: Attribute,
1109
+ configAttribute: Attribute
1110
+ ): boolean => {
1111
+ // Normalize both attributes for comparison (handle extreme database values)
1112
+ const normalizedDbAttr = normalizeAttributeForComparison(databaseAttribute);
1113
+ const normalizedConfigAttr = normalizeAttributeForComparison(configAttribute);
1114
+
1115
+ // Use type-specific field list to avoid false positives from irrelevant fields
1116
+ const attributesToCheck = getComparableFields(normalizedConfigAttr.type);
1117
+ const fieldsToCheck = attributesToCheck.filter((attr) => {
1118
+ if (attr !== "xdefault") {
1119
+ return true;
1120
+ }
1121
+ const dbRequired = Boolean((normalizedDbAttr as any).required);
1122
+ const configRequired = Boolean((normalizedConfigAttr as any).required);
1123
+ return !(dbRequired || configRequired);
1124
+ });
1125
+
1126
+ const differences: string[] = [];
1127
+
1128
+ const result = fieldsToCheck.every((attr) => {
1129
+ // Check if both objects have the attribute
1130
+ const dbHasAttr = attr in normalizedDbAttr;
1131
+ const configHasAttr = attr in normalizedConfigAttr;
1132
+
1133
+ // If both have the attribute, compare values
1134
+ if (dbHasAttr && configHasAttr) {
1135
+ const dbValue = normalizedDbAttr[attr as keyof typeof normalizedDbAttr];
1136
+ const configValue =
1137
+ normalizedConfigAttr[attr as keyof typeof normalizedConfigAttr];
1138
+
1139
+ // Consider undefined and null as equivalent
1140
+ if (
1141
+ (dbValue === undefined || dbValue === null) &&
1142
+ (configValue === undefined || configValue === null)
1143
+ ) {
1144
+ return true;
1145
+ }
1146
+
1147
+ // Normalize booleans: treat undefined and false as equivalent
1148
+ if (typeof dbValue === "boolean" || typeof configValue === "boolean") {
1149
+ const boolMatch = Boolean(dbValue) === Boolean(configValue);
1150
+ if (!boolMatch) {
1151
+ differences.push(`${attr}: db=${dbValue} config=${configValue}`);
1152
+ }
1153
+ return boolMatch;
1154
+ }
1155
+ // For numeric comparisons, compare numbers if both are numeric-like
1156
+ if (
1157
+ (typeof dbValue === "number" ||
1158
+ (typeof dbValue === "string" &&
1159
+ dbValue !== "" &&
1160
+ !isNaN(Number(dbValue)))) &&
1161
+ (typeof configValue === "number" ||
1162
+ (typeof configValue === "string" &&
1163
+ configValue !== "" &&
1164
+ !isNaN(Number(configValue))))
1165
+ ) {
1166
+ const numMatch = Number(dbValue) === Number(configValue);
1167
+ if (!numMatch) {
1168
+ differences.push(`${attr}: db=${dbValue} config=${configValue}`);
1169
+ }
1170
+ return numMatch;
1171
+ }
1172
+
1173
+ // For array comparisons (e.g., enum elements), use order-independent equality
1174
+ if (Array.isArray(dbValue) && Array.isArray(configValue)) {
1175
+ const arrayMatch =
1176
+ dbValue.length === configValue.length &&
1177
+ dbValue.every((val) => configValue.includes(val));
1178
+ if (!arrayMatch) {
1179
+ differences.push(
1180
+ `${attr}: db=${JSON.stringify(dbValue)} config=${JSON.stringify(
1181
+ configValue
1182
+ )}`
1183
+ );
1184
+ }
1185
+ return arrayMatch;
1186
+ }
1187
+
1188
+ const match = dbValue === configValue;
1189
+ if (!match) {
1190
+ differences.push(
1191
+ `${attr}: db=${JSON.stringify(dbValue)} config=${JSON.stringify(
1192
+ configValue
1193
+ )}`
1194
+ );
1195
+ }
1196
+ return match;
1197
+ }
1198
+
1199
+ // If neither has the attribute, consider it the same
1200
+ if (!dbHasAttr && !configHasAttr) {
1201
+ return true;
1202
+ }
1203
+
1204
+ // If one has the attribute and the other doesn't, check if it's undefined or null
1205
+ if (dbHasAttr && !configHasAttr) {
1206
+ const dbValue = normalizedDbAttr[attr as keyof typeof normalizedDbAttr];
1207
+ // Consider default-false booleans as equal to missing in config
1208
+ if (typeof dbValue === "boolean") {
1209
+ const match = dbValue === false; // missing in config equals false in db
1210
+ if (!match) {
1211
+ differences.push(`${attr}: db=${dbValue} config=<missing>`);
1212
+ }
1213
+ return match;
1214
+ }
1215
+ const match = dbValue === undefined || dbValue === null;
1216
+ if (!match) {
1217
+ differences.push(
1218
+ `${attr}: db=${JSON.stringify(dbValue)} config=<missing>`
1219
+ );
1220
+ }
1221
+ return match;
1222
+ }
1223
+
1224
+ if (!dbHasAttr && configHasAttr) {
1225
+ const configValue =
1226
+ normalizedConfigAttr[attr as keyof typeof normalizedConfigAttr];
1227
+ // Consider default-false booleans as equal to missing in db
1228
+ if (typeof configValue === "boolean") {
1229
+ const match = configValue === false; // missing in db equals false in config
1230
+ if (!match) {
1231
+ differences.push(`${attr}: db=<missing> config=${configValue}`);
1232
+ }
1233
+ return match;
1234
+ }
1235
+ const match = configValue === undefined || configValue === null;
1236
+ if (!match) {
1237
+ differences.push(
1238
+ `${attr}: db=<missing> config=${JSON.stringify(configValue)}`
1239
+ );
1240
+ }
1241
+ return match;
1242
+ }
1243
+
1244
+ // If we reach here, the attributes are different
1245
+ differences.push(`${attr}: unexpected comparison state`);
1246
+ return false;
1247
+ });
1248
+
1249
+ if (!result && differences.length > 0) {
1250
+ logger.debug(
1251
+ `Attribute mismatch detected for '${normalizedConfigAttr.key}'`,
1252
+ {
1253
+ differences,
1254
+ dbAttribute: normalizedDbAttr,
1255
+ configAttribute: normalizedConfigAttr,
1256
+ operation: "attributesSame",
1257
+ }
1258
+ );
1259
+ }
1260
+
1261
+ return result;
1262
+ };
1263
+
1264
+ /**
1265
+ * Enhanced attribute creation with proper status monitoring and retry logic
1266
+ */
1267
+ export const createOrUpdateAttributeWithStatusCheck = async (
1268
+ db: Databases | DatabaseAdapter,
1269
+ dbId: string,
1270
+ collection: Models.Collection,
1271
+ attribute: Attribute,
1272
+ retryCount: number = 0,
1273
+ maxRetries: number = 5
1274
+ ): Promise<boolean> => {
1275
+ try {
1276
+ // First, try to create/update the attribute using existing logic
1277
+ const result = await createOrUpdateAttribute(
1278
+ db,
1279
+ dbId,
1280
+ collection,
1281
+ attribute
1282
+ );
1283
+
1284
+ // If the attribute was queued (relationship dependency unresolved),
1285
+ // skip status polling and retry logic — the queue will handle it later.
1286
+ if (result === "queued") {
1287
+ MessageFormatter.info(
1288
+ chalk.yellow(
1289
+ `⏭️ Deferred relationship attribute '${attribute.key}' — queued for later once dependencies are available`
1290
+ )
1291
+ );
1292
+ return true;
1293
+ }
1294
+
1295
+ // If collection creation failed, return false to indicate failure
1296
+ if (result === "error") {
1297
+ MessageFormatter.error(
1298
+ `Failed to create collection for attribute '${attribute.key}'`
1299
+ );
1300
+ return false;
1301
+ }
1302
+
1303
+ // Now wait for the attribute to become available
1304
+ const success = await waitForAttributeAvailable(
1305
+ db,
1306
+ dbId,
1307
+ collection.$id,
1308
+ attribute.key,
1309
+ 60000, // 1 minute timeout
1310
+ retryCount,
1311
+ maxRetries
1312
+ );
1313
+
1314
+ if (success) {
1315
+ return true;
1316
+ }
1317
+
1318
+ // If not successful and we have retries left, delete specific attribute and try again
1319
+ if (retryCount < maxRetries) {
1320
+ MessageFormatter.info(
1321
+ chalk.yellow(
1322
+ `Attribute '${attribute.key}' failed/stuck, deleting and retrying...`
1323
+ )
1324
+ );
1325
+
1326
+ // Try to delete the specific stuck attribute instead of the entire collection
1327
+ try {
1328
+ if (isDatabaseAdapter(db)) {
1329
+ await db.deleteAttribute({
1330
+ databaseId: dbId,
1331
+ tableId: collection.$id,
1332
+ key: attribute.key,
1333
+ });
1334
+ } else {
1335
+ await db.deleteAttribute(dbId, collection.$id, attribute.key);
1336
+ }
1337
+ MessageFormatter.info(
1338
+ chalk.yellow(
1339
+ `Deleted stuck attribute '${attribute.key}', will retry creation`
1340
+ )
1341
+ );
1342
+
1343
+ // Wait a bit before retry
1344
+ await delay(3000);
1345
+
1346
+ // Get fresh collection data
1347
+ const freshCollection = isDatabaseAdapter(db)
1348
+ ? (await db.getTable({ databaseId: dbId, tableId: collection.$id }))
1349
+ .data
1350
+ : await db.getCollection(dbId, collection.$id);
1351
+
1352
+ // Retry with the same collection (attribute should be gone now)
1353
+ return await createOrUpdateAttributeWithStatusCheck(
1354
+ db,
1355
+ dbId,
1356
+ freshCollection,
1357
+ attribute,
1358
+ retryCount + 1,
1359
+ maxRetries
1360
+ );
1361
+ } catch (deleteError) {
1362
+ MessageFormatter.info(
1363
+ chalk.red(
1364
+ `Failed to delete stuck attribute '${attribute.key}': ${deleteError}`
1365
+ )
1366
+ );
1367
+
1368
+ // If attribute deletion fails, only then try collection recreation as last resort
1369
+ if (retryCount >= maxRetries - 1) {
1370
+ MessageFormatter.info(
1371
+ chalk.yellow(
1372
+ `Last resort: Recreating collection for attribute '${attribute.key}'`
1373
+ )
1374
+ );
1375
+
1376
+ // Get fresh collection data
1377
+ const freshCollection = isDatabaseAdapter(db)
1378
+ ? (await db.getTable({ databaseId: dbId, tableId: collection.$id }))
1379
+ .data
1380
+ : await db.getCollection(dbId, collection.$id);
1381
+
1382
+ // Delete and recreate collection
1383
+ const newCollection = await deleteAndRecreateCollection(
1384
+ db,
1385
+ dbId,
1386
+ freshCollection,
1387
+ retryCount + 1
1388
+ );
1389
+
1390
+ if (newCollection) {
1391
+ // Retry with the new collection
1392
+ return await createOrUpdateAttributeWithStatusCheck(
1393
+ db,
1394
+ dbId,
1395
+ newCollection,
1396
+ attribute,
1397
+ retryCount + 1,
1398
+ maxRetries
1399
+ );
1400
+ }
1401
+ } else {
1402
+ // Continue to next retry without collection recreation
1403
+ return await createOrUpdateAttributeWithStatusCheck(
1404
+ db,
1405
+ dbId,
1406
+ collection,
1407
+ attribute,
1408
+ retryCount + 1,
1409
+ maxRetries
1410
+ );
1411
+ }
1412
+ }
1413
+ }
1414
+
1415
+ MessageFormatter.info(
1416
+ chalk.red(
1417
+ `❌ Failed to create attribute '${attribute.key}' after ${
1418
+ maxRetries + 1
1419
+ } attempts`
1420
+ )
1421
+ );
1422
+ return false;
1423
+ } catch (error) {
1424
+ MessageFormatter.info(
1425
+ chalk.red(`Error creating attribute '${attribute.key}': ${error}`)
1426
+ );
1427
+
1428
+ if (retryCount < maxRetries) {
1429
+ MessageFormatter.info(
1430
+ chalk.yellow(`Retrying attribute '${attribute.key}' due to error...`)
1431
+ );
1432
+
1433
+ // Wait a bit before retry
1434
+ await delay(2000);
1435
+
1436
+ return await createOrUpdateAttributeWithStatusCheck(
1437
+ db,
1438
+ dbId,
1439
+ collection,
1440
+ attribute,
1441
+ retryCount + 1,
1442
+ maxRetries
1443
+ );
1444
+ }
1445
+
1446
+ return false;
1447
+ }
1448
+ };
1449
+
1450
+ export const createOrUpdateAttribute = async (
1451
+ db: Databases | DatabaseAdapter,
1452
+ dbId: string,
1453
+ collection: Models.Collection,
1454
+ attribute: Attribute
1455
+ ): Promise<"queued" | "processed" | "error"> => {
1456
+ let action = "create";
1457
+ let foundAttribute: Attribute | undefined;
1458
+ const updateEnabled = true;
1459
+ let finalAttribute: any = attribute;
1460
+ try {
1461
+ const collectionAttr = collection.attributes.find(
1462
+ (attr: any) => attr.key === attribute.key
1463
+ ) as unknown as any;
1464
+ foundAttribute = parseAttribute(collectionAttr);
1465
+ } catch (error) {
1466
+ foundAttribute = undefined;
1467
+ }
1468
+
1469
+ // If attribute exists but type changed, delete it so we can recreate with new type
1470
+ if (
1471
+ foundAttribute &&
1472
+ foundAttribute.type !== attribute.type
1473
+ ) {
1474
+ MessageFormatter.info(
1475
+ chalk.yellow(
1476
+ `Attribute '${attribute.key}' type changed from '${foundAttribute.type}' to '${attribute.type}'. Recreating attribute.`
1477
+ )
1478
+ );
1479
+ try {
1480
+ if (isDatabaseAdapter(db)) {
1481
+ await db.deleteAttribute({
1482
+ databaseId: dbId,
1483
+ tableId: collection.$id,
1484
+ key: attribute.key
1485
+ });
1486
+ } else {
1487
+ await db.deleteAttribute(dbId, collection.$id, attribute.key);
1488
+ }
1489
+ // Remove from local collection metadata so downstream logic treats it as new
1490
+ collection.attributes = collection.attributes.filter(
1491
+ (attr: any) => attr.key !== attribute.key
1492
+ );
1493
+ foundAttribute = undefined;
1494
+ } catch (deleteError) {
1495
+ MessageFormatter.error(
1496
+ `Failed to delete attribute '${attribute.key}' before recreation: ${deleteError}`
1497
+ );
1498
+ return "error";
1499
+ }
1500
+ }
1501
+
1502
+ if (
1503
+ foundAttribute &&
1504
+ attributesSame(foundAttribute, attribute) &&
1505
+ updateEnabled
1506
+ ) {
1507
+ // No need to do anything, they are the same
1508
+ return "processed";
1509
+ } else if (
1510
+ foundAttribute &&
1511
+ !attributesSame(foundAttribute, attribute) &&
1512
+ updateEnabled
1513
+ ) {
1514
+ // MessageFormatter.info(
1515
+ // `Updating attribute with same key ${attribute.key} but different values`
1516
+ // );
1517
+
1518
+ // DEBUG: Log before object merge to detect corruption
1519
+ if ((attribute.key === 'conversationType' || attribute.key === 'messageStreakCount')) {
1520
+ console.log(`[DEBUG] MERGE - key="${attribute.key}"`, {
1521
+ found: {
1522
+ elements: (foundAttribute as any)?.elements,
1523
+ min: (foundAttribute as any)?.min,
1524
+ max: (foundAttribute as any)?.max
1525
+ },
1526
+ desired: {
1527
+ elements: (attribute as any)?.elements,
1528
+ min: (attribute as any)?.min,
1529
+ max: (attribute as any)?.max
1530
+ }
1531
+ });
1532
+ }
1533
+
1534
+ finalAttribute = {
1535
+ ...foundAttribute,
1536
+ ...attribute,
1537
+ };
1538
+
1539
+ // DEBUG: Log after object merge to detect corruption
1540
+ if ((finalAttribute.key === 'conversationType' || finalAttribute.key === 'messageStreakCount')) {
1541
+ console.log(`[DEBUG] AFTER_MERGE - key="${finalAttribute.key}"`, {
1542
+ merged: {
1543
+ elements: finalAttribute?.elements,
1544
+ min: (finalAttribute as any)?.min,
1545
+ max: (finalAttribute as any)?.max
1546
+ }
1547
+ });
1548
+ }
1549
+ action = "update";
1550
+ } else if (
1551
+ !updateEnabled &&
1552
+ foundAttribute &&
1553
+ !attributesSame(foundAttribute, attribute)
1554
+ ) {
1555
+ if (isDatabaseAdapter(db)) {
1556
+ await db.deleteAttribute({
1557
+ databaseId: dbId,
1558
+ tableId: collection.$id,
1559
+ key: attribute.key,
1560
+ });
1561
+ } else {
1562
+ await db.deleteAttribute(dbId, collection.$id, attribute.key);
1563
+ }
1564
+ MessageFormatter.info(
1565
+ `Deleted attribute: ${attribute.key} to recreate it because they diff (update disabled temporarily)`
1566
+ );
1567
+ return "processed";
1568
+ }
1569
+
1570
+ // Relationship attribute logic with adjustments
1571
+ let collectionFoundViaRelatedCollection: Models.Collection | undefined;
1572
+ let relatedCollectionId: string | undefined;
1573
+ if (
1574
+ finalAttribute.type === "relationship" &&
1575
+ finalAttribute.relatedCollection
1576
+ ) {
1577
+ // First try treating relatedCollection as an ID directly
1578
+ try {
1579
+ const byIdCollection = isDatabaseAdapter(db)
1580
+ ? (
1581
+ await db.getTable({
1582
+ databaseId: dbId,
1583
+ tableId: finalAttribute.relatedCollection,
1584
+ })
1585
+ ).data
1586
+ : await db.getCollection(dbId, finalAttribute.relatedCollection);
1587
+ collectionFoundViaRelatedCollection = byIdCollection;
1588
+ relatedCollectionId = byIdCollection.$id;
1589
+ // Cache by name for subsequent lookups
1590
+ nameToIdMapping.set(byIdCollection.name, byIdCollection.$id);
1591
+ } catch (_) {
1592
+ // Not an ID or not found — fall back to name-based resolution below
1593
+ }
1594
+
1595
+ if (
1596
+ !collectionFoundViaRelatedCollection &&
1597
+ nameToIdMapping.has(finalAttribute.relatedCollection)
1598
+ ) {
1599
+ relatedCollectionId = nameToIdMapping.get(
1600
+ finalAttribute.relatedCollection
1601
+ );
1602
+ try {
1603
+ collectionFoundViaRelatedCollection = isDatabaseAdapter(db)
1604
+ ? (
1605
+ await db.getTable({
1606
+ databaseId: dbId,
1607
+ tableId: relatedCollectionId!,
1608
+ })
1609
+ ).data
1610
+ : await db.getCollection(dbId, relatedCollectionId!);
1611
+ } catch (e) {
1612
+ // MessageFormatter.info(
1613
+ // `Collection not found: ${finalAttribute.relatedCollection} when nameToIdMapping was set`
1614
+ // );
1615
+ collectionFoundViaRelatedCollection = undefined;
1616
+ }
1617
+ } else if (!collectionFoundViaRelatedCollection) {
1618
+ const collectionsPulled = isDatabaseAdapter(db)
1619
+ ? await db.listTables({
1620
+ databaseId: dbId,
1621
+ queries: [Query.equal("name", finalAttribute.relatedCollection)],
1622
+ })
1623
+ : await db.listCollections(dbId, [
1624
+ Query.equal("name", finalAttribute.relatedCollection),
1625
+ ]);
1626
+ if (collectionsPulled.total && collectionsPulled.total > 0) {
1627
+ collectionFoundViaRelatedCollection = isDatabaseAdapter(db)
1628
+ ? (collectionsPulled as any).tables?.[0]
1629
+ : (collectionsPulled as any).collections?.[0];
1630
+ relatedCollectionId = collectionFoundViaRelatedCollection?.$id;
1631
+ if (relatedCollectionId) {
1632
+ nameToIdMapping.set(
1633
+ finalAttribute.relatedCollection,
1634
+ relatedCollectionId
1635
+ );
1636
+ }
1637
+ }
1638
+ }
1639
+ // ONLY queue relationship attributes that have actual unresolved dependencies
1640
+ if (!(relatedCollectionId && collectionFoundViaRelatedCollection)) {
1641
+ MessageFormatter.info(
1642
+ chalk.yellow(
1643
+ `⏳ Queueing relationship attribute '${finalAttribute.key}' - related collection '${finalAttribute.relatedCollection}' not found yet`
1644
+ )
1645
+ );
1646
+ enqueueOperation({
1647
+ type: "attribute",
1648
+ collectionId: collection.$id,
1649
+ collection: collection,
1650
+ attribute,
1651
+ dependencies: [finalAttribute.relatedCollection],
1652
+ });
1653
+ return "queued";
1654
+ }
1655
+ }
1656
+ finalAttribute = parseAttribute(finalAttribute);
1657
+
1658
+ // Ensure collection/table exists - create it if it doesn't
1659
+ try {
1660
+ await (isDatabaseAdapter(db)
1661
+ ? db.getTable({ databaseId: dbId, tableId: collection.$id })
1662
+ : db.getCollection(dbId, collection.$id));
1663
+ } catch (error) {
1664
+ // Collection doesn't exist - create it
1665
+ if (
1666
+ (error as any).code === 404 ||
1667
+ (error instanceof Error &&
1668
+ (error.message.includes("collection_not_found") ||
1669
+ error.message.includes(
1670
+ "Collection with the requested ID could not be found"
1671
+ )))
1672
+ ) {
1673
+ MessageFormatter.info(
1674
+ `Collection '${collection.name}' doesn't exist, creating it first...`
1675
+ );
1676
+
1677
+ try {
1678
+ if (isDatabaseAdapter(db)) {
1679
+ await db.createTable({
1680
+ databaseId: dbId,
1681
+ id: collection.$id,
1682
+ name: collection.name,
1683
+ permissions: collection.$permissions || [],
1684
+ documentSecurity: collection.documentSecurity ?? false,
1685
+ enabled: collection.enabled ?? true,
1686
+ });
1687
+ } else {
1688
+ await db.createCollection(
1689
+ dbId,
1690
+ collection.$id,
1691
+ collection.name,
1692
+ collection.$permissions || [],
1693
+ collection.documentSecurity ?? false,
1694
+ collection.enabled ?? true
1695
+ );
1696
+ }
1697
+
1698
+ MessageFormatter.success(`Created collection '${collection.name}'`);
1699
+ await delay(500); // Wait for collection to be ready
1700
+ } catch (createError) {
1701
+ MessageFormatter.error(
1702
+ `Failed to create collection '${collection.name}'`,
1703
+ createError instanceof Error
1704
+ ? createError
1705
+ : new Error(String(createError))
1706
+ );
1707
+ return "error";
1708
+ }
1709
+ } else {
1710
+ // Other error - re-throw
1711
+ throw error;
1712
+ }
1713
+ }
1714
+
1715
+ // Use adapter-based attribute creation/update
1716
+ if (action === "create") {
1717
+ await tryAwaitWithRetry(
1718
+ async () =>
1719
+ await createAttributeViaAdapter(
1720
+ db,
1721
+ dbId,
1722
+ collection.$id,
1723
+ finalAttribute
1724
+ )
1725
+ );
1726
+ } else {
1727
+ console.log(`Updating attribute '${finalAttribute.key}'...`);
1728
+ if (finalAttribute.type === "double" || finalAttribute.type === "integer") {
1729
+ console.log("finalAttribute:", finalAttribute);
1730
+ }
1731
+ await tryAwaitWithRetry(
1732
+ async () =>
1733
+ await updateAttributeViaAdapter(
1734
+ db,
1735
+ dbId,
1736
+ collection.$id,
1737
+ finalAttribute
1738
+ )
1739
+ );
1740
+ }
1741
+ return "processed";
1742
+ };
1743
+
1744
+ /**
1745
+ * Enhanced collection attribute creation with proper status monitoring
1746
+ */
1747
+ export const createUpdateCollectionAttributesWithStatusCheck = async (
1748
+ db: Databases | DatabaseAdapter,
1749
+ dbId: string,
1750
+ collection: Models.Collection,
1751
+ attributes: Attribute[]
1752
+ ): Promise<boolean> => {
1753
+ const existingAttributes: Attribute[] =
1754
+ collection.attributes.map((attr) => parseAttribute(attr as any)) || [];
1755
+
1756
+ const attributesToRemove = existingAttributes.filter(
1757
+ (attr) => !attributes.some((a) => a.key === attr.key)
1758
+ );
1759
+ const indexesToRemove = collection.indexes.filter((index) =>
1760
+ attributesToRemove.some((attr) => index.attributes.includes(attr.key))
1761
+ );
1762
+
1763
+ // Handle attribute removal first
1764
+ if (attributesToRemove.length > 0) {
1765
+ if (indexesToRemove.length > 0) {
1766
+ MessageFormatter.info(
1767
+ chalk.red(
1768
+ `Removing indexes as they rely on an attribute that is being removed: ${indexesToRemove
1769
+ .map((index) => index.key)
1770
+ .join(", ")}`
1771
+ )
1772
+ );
1773
+ for (const index of indexesToRemove) {
1774
+ await tryAwaitWithRetry(async () => {
1775
+ if (isDatabaseAdapter(db)) {
1776
+ await db.deleteIndex({
1777
+ databaseId: dbId,
1778
+ tableId: collection.$id,
1779
+ key: index.key,
1780
+ });
1781
+ } else {
1782
+ await db.deleteIndex(dbId, collection.$id, index.key);
1783
+ }
1784
+ });
1785
+ await delay(500); // Longer delay for deletions
1786
+ }
1787
+ }
1788
+ for (const attr of attributesToRemove) {
1789
+ MessageFormatter.info(
1790
+ chalk.red(
1791
+ `Removing attribute: ${attr.key} as it is no longer in the collection`
1792
+ )
1793
+ );
1794
+ await tryAwaitWithRetry(async () => {
1795
+ if (isDatabaseAdapter(db)) {
1796
+ await db.deleteAttribute({
1797
+ databaseId: dbId,
1798
+ tableId: collection.$id,
1799
+ key: attr.key,
1800
+ });
1801
+ } else {
1802
+ await db.deleteAttribute(dbId, collection.$id, attr.key);
1803
+ }
1804
+ });
1805
+ await delay(500); // Longer delay for deletions
1806
+ }
1807
+ }
1808
+
1809
+ // First, get fresh collection data and determine which attributes actually need processing
1810
+ let currentCollection = collection;
1811
+ try {
1812
+ currentCollection = isDatabaseAdapter(db)
1813
+ ? (await db.getTable({ databaseId: dbId, tableId: collection.$id })).data
1814
+ : await db.getCollection(dbId, collection.$id);
1815
+ } catch (error) {
1816
+ MessageFormatter.info(
1817
+ chalk.yellow(`Warning: Could not refresh collection data: ${error}`)
1818
+ );
1819
+ }
1820
+
1821
+ const existingAttributesMap = new Map<string, Attribute>();
1822
+ try {
1823
+ const parsedAttributes = currentCollection.attributes.map((attr) =>
1824
+ parseAttribute(attr as any)
1825
+ );
1826
+ parsedAttributes.forEach((attr) =>
1827
+ existingAttributesMap.set(attr.key, attr)
1828
+ );
1829
+ } catch (error) {
1830
+ MessageFormatter.info(
1831
+ chalk.yellow(`Warning: Could not parse existing attributes: ${error}`)
1832
+ );
1833
+ }
1834
+
1835
+ // Filter to only attributes that need processing (new, changed, or not yet processed)
1836
+ const attributesToProcess = attributes.filter((attribute) => {
1837
+ // Skip if already processed in this session
1838
+ if (isAttributeProcessed(currentCollection.$id, attribute.key)) {
1839
+ return false;
1840
+ }
1841
+
1842
+ const existing = existingAttributesMap.get(attribute.key);
1843
+ if (!existing) {
1844
+ MessageFormatter.info(`➕ ${attribute.key}`);
1845
+ return true;
1846
+ }
1847
+
1848
+ const needsUpdate = !attributesSame(existing, parseAttribute(attribute));
1849
+ if (needsUpdate) {
1850
+ MessageFormatter.info(`🔄 ${attribute.key}`);
1851
+ } else {
1852
+ MessageFormatter.info(chalk.gray(`✅ ${attribute.key}`));
1853
+ }
1854
+ return needsUpdate;
1855
+ });
1856
+
1857
+ if (attributesToProcess.length === 0) {
1858
+ return true;
1859
+ }
1860
+
1861
+ let remainingAttributes = [...attributesToProcess];
1862
+ let overallRetryCount = 0;
1863
+ const maxOverallRetries = 3;
1864
+
1865
+ while (
1866
+ remainingAttributes.length > 0 &&
1867
+ overallRetryCount < maxOverallRetries
1868
+ ) {
1869
+ const attributesToProcessThisRound = [...remainingAttributes];
1870
+ remainingAttributes = []; // Reset for next iteration
1871
+
1872
+ for (const attribute of attributesToProcessThisRound) {
1873
+ const success = await createOrUpdateAttributeWithStatusCheck(
1874
+ db,
1875
+ dbId,
1876
+ currentCollection,
1877
+ attribute
1878
+ );
1879
+
1880
+ if (success) {
1881
+ // Mark this specific attribute as processed
1882
+ markAttributeProcessed(currentCollection.$id, attribute.key);
1883
+
1884
+ // Get updated collection data for next iteration
1885
+ try {
1886
+ currentCollection = isDatabaseAdapter(db)
1887
+ ? ((
1888
+ await db.getTable({ databaseId: dbId, tableId: collection.$id })
1889
+ ).data as Models.Collection)
1890
+ : await db.getCollection({
1891
+ databaseId: dbId,
1892
+ collectionId: collection.$id,
1893
+ });
1894
+ } catch (error) {
1895
+ MessageFormatter.info(
1896
+ chalk.yellow(`Warning: Could not refresh collection data: ${error}`)
1897
+ );
1898
+ }
1899
+
1900
+ // Add delay between successful attributes
1901
+ await delay(1000);
1902
+ } else {
1903
+ MessageFormatter.info(chalk.red(`❌ ${attribute.key}`));
1904
+ remainingAttributes.push(attribute); // Add back to retry list
1905
+ }
1906
+ }
1907
+
1908
+ if (remainingAttributes.length === 0) {
1909
+ return true;
1910
+ }
1911
+
1912
+ overallRetryCount++;
1913
+
1914
+ if (overallRetryCount < maxOverallRetries) {
1915
+ MessageFormatter.info(
1916
+ chalk.yellow(
1917
+ `⏳ Retrying ${remainingAttributes.length} failed attributes...`
1918
+ )
1919
+ );
1920
+ await delay(5000);
1921
+
1922
+ // Refresh collection data before retry
1923
+ try {
1924
+ currentCollection = isDatabaseAdapter(db)
1925
+ ? ((await db.getTable({ databaseId: dbId, tableId: collection.$id }))
1926
+ .data as Models.Collection)
1927
+ : await db.getCollection(dbId, collection.$id);
1928
+ } catch (error) {
1929
+ // Silently continue if refresh fails
1930
+ }
1931
+ }
1932
+ }
1933
+
1934
+ // If we get here, some attributes still failed after all retries
1935
+ if (attributesToProcess.length > 0) {
1936
+ MessageFormatter.info(
1937
+ chalk.red(
1938
+ `\n❌ Failed to create ${
1939
+ attributesToProcess.length
1940
+ } attributes after ${maxOverallRetries} attempts: ${attributesToProcess
1941
+ .map((a) => a.key)
1942
+ .join(", ")}`
1943
+ )
1944
+ );
1945
+ MessageFormatter.info(
1946
+ chalk.red(
1947
+ `This may indicate a fundamental issue with the attribute definitions or Appwrite instance`
1948
+ )
1949
+ );
1950
+ return false;
1951
+ }
1952
+
1953
+ MessageFormatter.info(
1954
+ chalk.green(
1955
+ `\n✅ Successfully created all ${attributes.length} attributes for collection: ${collection.name}`
1956
+ )
1957
+ );
1958
+ return true;
1959
+ };
1960
+
1961
+ export const createUpdateCollectionAttributes = async (
1962
+ db: Databases | DatabaseAdapter,
1963
+ dbId: string,
1964
+ collection: Models.Collection,
1965
+ attributes: Attribute[]
1966
+ ): Promise<void> => {
1967
+ MessageFormatter.info(
1968
+ chalk.green(
1969
+ `Creating/Updating attributes for collection: ${collection.name}`
1970
+ )
1971
+ );
1972
+
1973
+ const existingAttributes: Attribute[] =
1974
+ collection.attributes.map((attr) => parseAttribute(attr as any)) || [];
1975
+
1976
+ const attributesToRemove = existingAttributes.filter(
1977
+ (attr) => !attributes.some((a) => a.key === attr.key)
1978
+ );
1979
+ const indexesToRemove = collection.indexes.filter((index) =>
1980
+ attributesToRemove.some((attr) => index.attributes.includes(attr.key))
1981
+ );
1982
+
1983
+ if (attributesToRemove.length > 0) {
1984
+ if (indexesToRemove.length > 0) {
1985
+ MessageFormatter.info(
1986
+ chalk.red(
1987
+ `Removing indexes as they rely on an attribute that is being removed: ${indexesToRemove
1988
+ .map((index) => index.key)
1989
+ .join(", ")}`
1990
+ )
1991
+ );
1992
+ for (const index of indexesToRemove) {
1993
+ await tryAwaitWithRetry(async () => {
1994
+ if (isDatabaseAdapter(db)) {
1995
+ await db.deleteIndex({
1996
+ databaseId: dbId,
1997
+ tableId: collection.$id,
1998
+ key: index.key,
1999
+ });
2000
+ } else {
2001
+ await db.deleteIndex(dbId, collection.$id, index.key);
2002
+ }
2003
+ });
2004
+ await delay(100);
2005
+ }
2006
+ }
2007
+ for (const attr of attributesToRemove) {
2008
+ MessageFormatter.info(
2009
+ chalk.red(
2010
+ `Removing attribute: ${attr.key} as it is no longer in the collection`
2011
+ )
2012
+ );
2013
+ await tryAwaitWithRetry(async () => {
2014
+ if (isDatabaseAdapter(db)) {
2015
+ await db.deleteAttribute({
2016
+ databaseId: dbId,
2017
+ tableId: collection.$id,
2018
+ key: attr.key,
2019
+ });
2020
+ } else {
2021
+ await db.deleteAttribute(dbId, collection.$id, attr.key);
2022
+ }
2023
+ });
2024
+ await delay(50);
2025
+ }
2026
+ }
2027
+
2028
+ const batchSize = 3;
2029
+ for (let i = 0; i < attributes.length; i += batchSize) {
2030
+ const batch = attributes.slice(i, i + batchSize);
2031
+ const attributePromises = batch.map((attribute) =>
2032
+ tryAwaitWithRetry(
2033
+ async () =>
2034
+ await createOrUpdateAttribute(db, dbId, collection, attribute)
2035
+ )
2036
+ );
2037
+
2038
+ const results = await Promise.allSettled(attributePromises);
2039
+ results.forEach((result) => {
2040
+ if (result.status === "rejected") {
2041
+ MessageFormatter.error(
2042
+ "An attribute promise was rejected:",
2043
+ result.reason
2044
+ );
2045
+ }
2046
+ });
2047
+
2048
+ // Add delay after each batch
2049
+ await delay(200);
2050
+ }
2051
+ MessageFormatter.info(
2052
+ `Finished creating/updating attributes for collection: ${collection.name}`
2053
+ );
2054
+ };