appwrite-utils-cli 1.11.0 → 1.12.0

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