appwrite-utils-cli 1.9.5 → 1.9.7
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.
- package/dist/adapters/AdapterFactory.d.ts +1 -1
- package/dist/adapters/AdapterFactory.js +52 -37
- package/dist/adapters/DatabaseAdapter.d.ts +1 -0
- package/dist/adapters/LegacyAdapter.js +5 -2
- package/dist/adapters/TablesDBAdapter.js +18 -3
- package/dist/collections/attributes.js +0 -31
- package/dist/collections/methods.js +89 -184
- package/dist/collections/tableOperations.js +25 -12
- package/dist/config/ConfigManager.js +25 -0
- package/dist/shared/attributeMapper.js +1 -1
- package/dist/tables/indexManager.d.ts +65 -0
- package/dist/tables/indexManager.js +294 -0
- package/dist/utilsController.js +10 -1
- package/package.json +1 -1
- package/src/adapters/AdapterFactory.ts +146 -127
- package/src/adapters/DatabaseAdapter.ts +1 -0
- package/src/adapters/LegacyAdapter.ts +5 -2
- package/src/adapters/TablesDBAdapter.ts +18 -10
- package/src/collections/attributes.ts +0 -34
- package/src/collections/methods.ts +361 -406
- package/src/collections/tableOperations.ts +28 -13
- package/src/config/ConfigManager.ts +32 -0
- package/src/shared/attributeMapper.ts +1 -1
- package/src/tables/indexManager.ts +409 -0
- package/src/utilsController.ts +10 -1
- package/dist/shared/indexManager.d.ts +0 -24
- package/dist/shared/indexManager.js +0 -151
- package/src/shared/indexManager.ts +0 -254
|
@@ -90,11 +90,13 @@ export function normalizeAttributeToComparable(attr) {
|
|
|
90
90
|
return base;
|
|
91
91
|
}
|
|
92
92
|
export function normalizeColumnToComparable(col) {
|
|
93
|
-
// Detect enum surfaced as string+elements from server and normalize to enum for comparison
|
|
93
|
+
// Detect enum surfaced as string+elements or string+format:enum from server and normalize to enum for comparison
|
|
94
94
|
let t = String((col?.type ?? col?.columnType ?? '')).toLowerCase();
|
|
95
95
|
const hasElements = Array.isArray(col?.elements) && col.elements.length > 0;
|
|
96
|
-
|
|
96
|
+
const hasEnumFormat = (col?.format === 'enum');
|
|
97
|
+
if (t === 'string' && (hasElements || hasEnumFormat)) {
|
|
97
98
|
t = 'enum';
|
|
99
|
+
}
|
|
98
100
|
const base = {
|
|
99
101
|
key: col?.key,
|
|
100
102
|
type: t,
|
|
@@ -185,18 +187,27 @@ export function isIndexEqualToIndex(a, b) {
|
|
|
185
187
|
if (String(a.type).toLowerCase() !== String(b.type).toLowerCase())
|
|
186
188
|
return false;
|
|
187
189
|
// Compare attributes as sets (order-insensitive)
|
|
188
|
-
|
|
189
|
-
const
|
|
190
|
+
// Support TablesDB which returns 'columns' instead of 'attributes'
|
|
191
|
+
const attrsAraw = Array.isArray(a.attributes)
|
|
192
|
+
? a.attributes
|
|
193
|
+
: (Array.isArray(a.columns) ? a.columns : []);
|
|
194
|
+
const attrsA = [...attrsAraw].sort();
|
|
195
|
+
const attrsB = Array.isArray(b.attributes)
|
|
196
|
+
? [...b.attributes].sort()
|
|
197
|
+
: (Array.isArray(b.columns) ? [...b.columns].sort() : []);
|
|
190
198
|
if (attrsA.length !== attrsB.length)
|
|
191
199
|
return false;
|
|
192
200
|
for (let i = 0; i < attrsA.length; i++)
|
|
193
201
|
if (attrsA[i] !== attrsB[i])
|
|
194
202
|
return false;
|
|
195
|
-
// Orders are only considered if
|
|
196
|
-
|
|
197
|
-
const
|
|
198
|
-
if (
|
|
199
|
-
|
|
203
|
+
// Orders are only considered if CONFIG (b) has orders defined
|
|
204
|
+
// This prevents false positives when Appwrite returns orders but user didn't specify them
|
|
205
|
+
const hasConfigOrders = Array.isArray(b.orders) && b.orders.length > 0;
|
|
206
|
+
if (hasConfigOrders) {
|
|
207
|
+
// Some APIs may expose 'directions' instead of 'orders'
|
|
208
|
+
const ordersA = Array.isArray(a.orders)
|
|
209
|
+
? [...a.orders].sort()
|
|
210
|
+
: (Array.isArray(a.directions) ? [...a.directions].sort() : []);
|
|
200
211
|
const ordersB = [...b.orders].sort();
|
|
201
212
|
if (ordersA.length !== ordersB.length)
|
|
202
213
|
return false;
|
|
@@ -204,7 +215,6 @@ export function isIndexEqualToIndex(a, b) {
|
|
|
204
215
|
if (ordersA[i] !== ordersB[i])
|
|
205
216
|
return false;
|
|
206
217
|
}
|
|
207
|
-
// If only one side has orders, treat as equal (orders unspecified by user)
|
|
208
218
|
return true;
|
|
209
219
|
}
|
|
210
220
|
/**
|
|
@@ -213,6 +223,7 @@ export function isIndexEqualToIndex(a, b) {
|
|
|
213
223
|
function compareColumnProperties(oldColumn, newAttribute, columnType) {
|
|
214
224
|
const changes = [];
|
|
215
225
|
const t = String(columnType || newAttribute.type || '').toLowerCase();
|
|
226
|
+
const key = newAttribute?.key || 'unknown';
|
|
216
227
|
const mutableProps = MUTABLE_PROPERTIES[t] || [];
|
|
217
228
|
const immutableProps = IMMUTABLE_PROPERTIES[t] || [];
|
|
218
229
|
const getNewVal = (prop) => {
|
|
@@ -233,8 +244,9 @@ function compareColumnProperties(oldColumn, newAttribute, columnType) {
|
|
|
233
244
|
let newValue = getNewVal(prop);
|
|
234
245
|
// Special-case: enum elements empty/missing should not trigger updates
|
|
235
246
|
if (t === 'enum' && prop === 'elements') {
|
|
236
|
-
if (!Array.isArray(newValue) || newValue.length === 0)
|
|
247
|
+
if (!Array.isArray(newValue) || newValue.length === 0) {
|
|
237
248
|
newValue = oldValue;
|
|
249
|
+
}
|
|
238
250
|
}
|
|
239
251
|
if (Array.isArray(oldValue) && Array.isArray(newValue)) {
|
|
240
252
|
if (oldValue.length !== newValue.length || oldValue.some((v, i) => v !== newValue[i])) {
|
|
@@ -260,7 +272,8 @@ function compareColumnProperties(oldColumn, newAttribute, columnType) {
|
|
|
260
272
|
// Type change requires recreate (normalize string+elements to enum on old side)
|
|
261
273
|
const oldTypeRaw = String(oldColumn?.type || oldColumn?.columnType || '').toLowerCase();
|
|
262
274
|
const oldHasElements = Array.isArray(oldColumn?.elements) && oldColumn.elements.length > 0;
|
|
263
|
-
const
|
|
275
|
+
const oldHasEnumFormat = (oldColumn?.format === 'enum');
|
|
276
|
+
const oldType = oldTypeRaw === 'string' && (oldHasElements || oldHasEnumFormat) ? 'enum' : oldTypeRaw;
|
|
264
277
|
if (oldType && t && oldType !== t && TYPE_CHANGE_REQUIRES_RECREATE.includes(oldType)) {
|
|
265
278
|
changes.push({ property: 'type', oldValue: oldType, newValue: t, requiresRecreate: true });
|
|
266
279
|
}
|
|
@@ -3,6 +3,7 @@ import path from "path";
|
|
|
3
3
|
import { ConfigDiscoveryService, ConfigLoaderService, ConfigMergeService, ConfigValidationService, SessionAuthService, } from "./services/index.js";
|
|
4
4
|
import { MessageFormatter } from "../shared/messageFormatter.js";
|
|
5
5
|
import { logger } from "../shared/logging.js";
|
|
6
|
+
import { detectAppwriteVersionCached } from "../utils/versionDetection.js";
|
|
6
7
|
/**
|
|
7
8
|
* Centralized configuration manager with intelligent caching and session management.
|
|
8
9
|
*
|
|
@@ -178,6 +179,30 @@ export class ConfigManager {
|
|
|
178
179
|
`Run with reportValidation: true to see details.`);
|
|
179
180
|
}
|
|
180
181
|
}
|
|
182
|
+
// 8. Run version detection and set apiMode if not explicitly configured
|
|
183
|
+
if (!config.apiMode || config.apiMode === 'auto') {
|
|
184
|
+
try {
|
|
185
|
+
logger.debug('Running version detection for API mode detection', {
|
|
186
|
+
prefix: "ConfigManager",
|
|
187
|
+
endpoint: config.appwriteEndpoint
|
|
188
|
+
});
|
|
189
|
+
const versionResult = await detectAppwriteVersionCached(config.appwriteEndpoint, config.appwriteProject, config.appwriteKey);
|
|
190
|
+
config.apiMode = versionResult.apiMode;
|
|
191
|
+
logger.info(`API mode detected: ${config.apiMode}`, {
|
|
192
|
+
prefix: "ConfigManager",
|
|
193
|
+
method: versionResult.detectionMethod,
|
|
194
|
+
confidence: versionResult.confidence,
|
|
195
|
+
serverVersion: versionResult.serverVersion,
|
|
196
|
+
});
|
|
197
|
+
}
|
|
198
|
+
catch (error) {
|
|
199
|
+
logger.warn('Version detection failed, defaulting to legacy mode', {
|
|
200
|
+
prefix: "ConfigManager",
|
|
201
|
+
error: error instanceof Error ? error.message : 'Unknown error',
|
|
202
|
+
});
|
|
203
|
+
config.apiMode = 'legacy';
|
|
204
|
+
}
|
|
205
|
+
}
|
|
181
206
|
// 8. Cache the config
|
|
182
207
|
this.cachedConfig = config;
|
|
183
208
|
this.cachedConfigPath = configPath;
|
|
@@ -140,7 +140,7 @@ export function mapToCreateAttributeParams(attr, base) {
|
|
|
140
140
|
* so we never send an empty elements array (preserve existing on server).
|
|
141
141
|
*/
|
|
142
142
|
export function mapToUpdateAttributeParams(attr, base) {
|
|
143
|
-
const type = String(attr.type ||
|
|
143
|
+
const type = String((attr.type == 'string' && attr.format !== 'enum') || attr.type !== 'string' ? attr.type : 'enum').toLowerCase();
|
|
144
144
|
const params = {
|
|
145
145
|
databaseId: base.databaseId,
|
|
146
146
|
tableId: base.tableId,
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import type { Index } from "appwrite-utils";
|
|
2
|
+
import type { DatabaseAdapter } from "../adapters/DatabaseAdapter.js";
|
|
3
|
+
import type { Models } from "node-appwrite";
|
|
4
|
+
export interface IndexOperation {
|
|
5
|
+
type: 'create' | 'update' | 'skip' | 'delete';
|
|
6
|
+
index: Index;
|
|
7
|
+
existingIndex?: Models.Index;
|
|
8
|
+
reason?: string;
|
|
9
|
+
}
|
|
10
|
+
export interface IndexOperationPlan {
|
|
11
|
+
toCreate: IndexOperation[];
|
|
12
|
+
toUpdate: IndexOperation[];
|
|
13
|
+
toSkip: IndexOperation[];
|
|
14
|
+
toDelete: IndexOperation[];
|
|
15
|
+
}
|
|
16
|
+
export interface IndexExecutionResult {
|
|
17
|
+
created: string[];
|
|
18
|
+
updated: string[];
|
|
19
|
+
skipped: string[];
|
|
20
|
+
deleted: string[];
|
|
21
|
+
errors: Array<{
|
|
22
|
+
key: string;
|
|
23
|
+
error: string;
|
|
24
|
+
}>;
|
|
25
|
+
summary: {
|
|
26
|
+
total: number;
|
|
27
|
+
created: number;
|
|
28
|
+
updated: number;
|
|
29
|
+
skipped: number;
|
|
30
|
+
deleted: number;
|
|
31
|
+
errors: number;
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Plan index operations by comparing desired indexes with existing ones
|
|
36
|
+
* Uses the existing isIndexEqualToIndex function for consistent comparison
|
|
37
|
+
*/
|
|
38
|
+
export declare function planIndexOperations(desiredIndexes: Index[], existingIndexes: Models.Index[]): IndexOperationPlan;
|
|
39
|
+
/**
|
|
40
|
+
* Plan index deletions for indexes that exist but aren't in the desired configuration
|
|
41
|
+
*/
|
|
42
|
+
export declare function planIndexDeletions(desiredIndexKeys: Set<string>, existingIndexes: Models.Index[]): IndexOperation[];
|
|
43
|
+
/**
|
|
44
|
+
* Execute index operations with proper error handling and status monitoring
|
|
45
|
+
*/
|
|
46
|
+
export declare function executeIndexOperations(adapter: DatabaseAdapter, databaseId: string, tableId: string, plan: IndexOperationPlan): Promise<IndexExecutionResult>;
|
|
47
|
+
/**
|
|
48
|
+
* Execute index deletions with proper error handling
|
|
49
|
+
*/
|
|
50
|
+
export declare function executeIndexDeletions(adapter: DatabaseAdapter, databaseId: string, tableId: string, deletions: IndexOperation[]): Promise<{
|
|
51
|
+
deleted: string[];
|
|
52
|
+
errors: Array<{
|
|
53
|
+
key: string;
|
|
54
|
+
error: string;
|
|
55
|
+
}>;
|
|
56
|
+
}>;
|
|
57
|
+
/**
|
|
58
|
+
* Main function to create/update indexes via adapter
|
|
59
|
+
* This replaces the messy inline code in methods.ts
|
|
60
|
+
*/
|
|
61
|
+
export declare function createOrUpdateIndexesViaAdapter(adapter: DatabaseAdapter, databaseId: string, tableId: string, desiredIndexes: Index[], configIndexes?: Index[]): Promise<void>;
|
|
62
|
+
/**
|
|
63
|
+
* Handle index deletions for obsolete indexes
|
|
64
|
+
*/
|
|
65
|
+
export declare function deleteObsoleteIndexesViaAdapter(adapter: DatabaseAdapter, databaseId: string, tableId: string, desiredIndexKeys: Set<string>): Promise<void>;
|
|
@@ -0,0 +1,294 @@
|
|
|
1
|
+
import { isIndexEqualToIndex } from "../collections/tableOperations.js";
|
|
2
|
+
import { MessageFormatter } from "../shared/messageFormatter.js";
|
|
3
|
+
import { delay, tryAwaitWithRetry } from "../utils/helperFunctions.js";
|
|
4
|
+
/**
|
|
5
|
+
* Plan index operations by comparing desired indexes with existing ones
|
|
6
|
+
* Uses the existing isIndexEqualToIndex function for consistent comparison
|
|
7
|
+
*/
|
|
8
|
+
export function planIndexOperations(desiredIndexes, existingIndexes) {
|
|
9
|
+
const plan = {
|
|
10
|
+
toCreate: [],
|
|
11
|
+
toUpdate: [],
|
|
12
|
+
toSkip: [],
|
|
13
|
+
toDelete: []
|
|
14
|
+
};
|
|
15
|
+
for (const desiredIndex of desiredIndexes) {
|
|
16
|
+
const existingIndex = existingIndexes.find(idx => idx.key === desiredIndex.key);
|
|
17
|
+
if (!existingIndex) {
|
|
18
|
+
// Index doesn't exist - create it
|
|
19
|
+
plan.toCreate.push({
|
|
20
|
+
type: 'create',
|
|
21
|
+
index: desiredIndex,
|
|
22
|
+
reason: 'New index'
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
else if (isIndexEqualToIndex(existingIndex, desiredIndex)) {
|
|
26
|
+
// Index exists and is identical - skip it
|
|
27
|
+
plan.toSkip.push({
|
|
28
|
+
type: 'skip',
|
|
29
|
+
index: desiredIndex,
|
|
30
|
+
existingIndex,
|
|
31
|
+
reason: 'Index unchanged'
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
else {
|
|
35
|
+
// Index exists but is different - update it
|
|
36
|
+
plan.toUpdate.push({
|
|
37
|
+
type: 'update',
|
|
38
|
+
index: desiredIndex,
|
|
39
|
+
existingIndex,
|
|
40
|
+
reason: 'Index configuration changed'
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
return plan;
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Plan index deletions for indexes that exist but aren't in the desired configuration
|
|
48
|
+
*/
|
|
49
|
+
export function planIndexDeletions(desiredIndexKeys, existingIndexes) {
|
|
50
|
+
const deletions = [];
|
|
51
|
+
for (const existingIndex of existingIndexes) {
|
|
52
|
+
if (!desiredIndexKeys.has(existingIndex.key)) {
|
|
53
|
+
deletions.push({
|
|
54
|
+
type: 'delete',
|
|
55
|
+
index: existingIndex, // Convert Models.Index to Index for compatibility
|
|
56
|
+
reason: 'Obsolete index'
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
return deletions;
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Execute index operations with proper error handling and status monitoring
|
|
64
|
+
*/
|
|
65
|
+
export async function executeIndexOperations(adapter, databaseId, tableId, plan) {
|
|
66
|
+
const result = {
|
|
67
|
+
created: [],
|
|
68
|
+
updated: [],
|
|
69
|
+
skipped: [],
|
|
70
|
+
deleted: [],
|
|
71
|
+
errors: [],
|
|
72
|
+
summary: {
|
|
73
|
+
total: 0,
|
|
74
|
+
created: 0,
|
|
75
|
+
updated: 0,
|
|
76
|
+
skipped: 0,
|
|
77
|
+
deleted: 0,
|
|
78
|
+
errors: 0
|
|
79
|
+
}
|
|
80
|
+
};
|
|
81
|
+
// Execute creates
|
|
82
|
+
for (const operation of plan.toCreate) {
|
|
83
|
+
try {
|
|
84
|
+
await adapter.createIndex({
|
|
85
|
+
databaseId,
|
|
86
|
+
tableId,
|
|
87
|
+
key: operation.index.key,
|
|
88
|
+
type: operation.index.type,
|
|
89
|
+
attributes: operation.index.attributes,
|
|
90
|
+
orders: operation.index.orders || []
|
|
91
|
+
});
|
|
92
|
+
result.created.push(operation.index.key);
|
|
93
|
+
MessageFormatter.success(`Created index ${operation.index.key}`, { prefix: 'Indexes' });
|
|
94
|
+
// Wait for index to become available
|
|
95
|
+
await waitForIndexAvailable(adapter, databaseId, tableId, operation.index.key);
|
|
96
|
+
await delay(150); // Brief delay between operations
|
|
97
|
+
}
|
|
98
|
+
catch (error) {
|
|
99
|
+
const errorMessage = error instanceof Error ? error.message : String(error);
|
|
100
|
+
result.errors.push({ key: operation.index.key, error: errorMessage });
|
|
101
|
+
MessageFormatter.error(`Failed to create index ${operation.index.key}`, error instanceof Error ? error : new Error(String(error)), { prefix: 'Indexes' });
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
// Execute updates (delete + recreate)
|
|
105
|
+
for (const operation of plan.toUpdate) {
|
|
106
|
+
try {
|
|
107
|
+
// Delete existing index first
|
|
108
|
+
await adapter.deleteIndex({
|
|
109
|
+
databaseId,
|
|
110
|
+
tableId,
|
|
111
|
+
key: operation.index.key
|
|
112
|
+
});
|
|
113
|
+
await delay(100); // Brief delay for deletion to settle
|
|
114
|
+
// Create new index
|
|
115
|
+
await adapter.createIndex({
|
|
116
|
+
databaseId,
|
|
117
|
+
tableId,
|
|
118
|
+
key: operation.index.key,
|
|
119
|
+
type: operation.index.type,
|
|
120
|
+
attributes: operation.index.attributes,
|
|
121
|
+
orders: operation.index.orders || operation.existingIndex?.orders || []
|
|
122
|
+
});
|
|
123
|
+
result.updated.push(operation.index.key);
|
|
124
|
+
MessageFormatter.success(`Updated index ${operation.index.key}`, { prefix: 'Indexes' });
|
|
125
|
+
// Wait for index to become available
|
|
126
|
+
await waitForIndexAvailable(adapter, databaseId, tableId, operation.index.key);
|
|
127
|
+
await delay(150); // Brief delay between operations
|
|
128
|
+
}
|
|
129
|
+
catch (error) {
|
|
130
|
+
const errorMessage = error instanceof Error ? error.message : String(error);
|
|
131
|
+
result.errors.push({ key: operation.index.key, error: errorMessage });
|
|
132
|
+
MessageFormatter.error(`Failed to update index ${operation.index.key}`, error instanceof Error ? error : new Error(String(error)), { prefix: 'Indexes' });
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
// Execute skips
|
|
136
|
+
for (const operation of plan.toSkip) {
|
|
137
|
+
result.skipped.push(operation.index.key);
|
|
138
|
+
MessageFormatter.info(`Index ${operation.index.key} unchanged`, { prefix: 'Indexes' });
|
|
139
|
+
}
|
|
140
|
+
// Calculate summary
|
|
141
|
+
result.summary.total = result.created.length + result.updated.length + result.skipped.length + result.deleted.length;
|
|
142
|
+
result.summary.created = result.created.length;
|
|
143
|
+
result.summary.updated = result.updated.length;
|
|
144
|
+
result.summary.skipped = result.skipped.length;
|
|
145
|
+
result.summary.deleted = result.deleted.length;
|
|
146
|
+
result.summary.errors = result.errors.length;
|
|
147
|
+
return result;
|
|
148
|
+
}
|
|
149
|
+
/**
|
|
150
|
+
* Execute index deletions with proper error handling
|
|
151
|
+
*/
|
|
152
|
+
export async function executeIndexDeletions(adapter, databaseId, tableId, deletions) {
|
|
153
|
+
const result = {
|
|
154
|
+
deleted: [],
|
|
155
|
+
errors: []
|
|
156
|
+
};
|
|
157
|
+
for (const operation of deletions) {
|
|
158
|
+
try {
|
|
159
|
+
await adapter.deleteIndex({
|
|
160
|
+
databaseId,
|
|
161
|
+
tableId,
|
|
162
|
+
key: operation.index.key
|
|
163
|
+
});
|
|
164
|
+
result.deleted.push(operation.index.key);
|
|
165
|
+
MessageFormatter.info(`Deleted obsolete index ${operation.index.key}`, { prefix: 'Indexes' });
|
|
166
|
+
// Wait briefly for deletion to settle
|
|
167
|
+
await delay(500);
|
|
168
|
+
}
|
|
169
|
+
catch (error) {
|
|
170
|
+
const errorMessage = error instanceof Error ? error.message : String(error);
|
|
171
|
+
result.errors.push({ key: operation.index.key, error: errorMessage });
|
|
172
|
+
MessageFormatter.error(`Failed to delete index ${operation.index.key}`, error instanceof Error ? error : new Error(String(error)), { prefix: 'Indexes' });
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
return result;
|
|
176
|
+
}
|
|
177
|
+
/**
|
|
178
|
+
* Wait for an index to become available with timeout and retry logic
|
|
179
|
+
* This is an adapter-aware version of the logic from collections/indexes.ts
|
|
180
|
+
*/
|
|
181
|
+
async function waitForIndexAvailable(adapter, databaseId, tableId, indexKey, maxWaitTime = 60000, // 1 minute
|
|
182
|
+
checkInterval = 2000 // 2 seconds
|
|
183
|
+
) {
|
|
184
|
+
const startTime = Date.now();
|
|
185
|
+
while (Date.now() - startTime < maxWaitTime) {
|
|
186
|
+
try {
|
|
187
|
+
const indexList = await adapter.listIndexes({ databaseId, tableId });
|
|
188
|
+
const indexes = indexList.data || indexList.indexes || [];
|
|
189
|
+
const index = indexes.find((idx) => idx.key === indexKey);
|
|
190
|
+
if (!index) {
|
|
191
|
+
MessageFormatter.error(`Index '${indexKey}' not found after creation`, undefined, { prefix: 'Indexes' });
|
|
192
|
+
return false;
|
|
193
|
+
}
|
|
194
|
+
switch (index.status) {
|
|
195
|
+
case 'available':
|
|
196
|
+
return true;
|
|
197
|
+
case 'failed':
|
|
198
|
+
MessageFormatter.error(`Index '${indexKey}' failed: ${index.error || 'unknown error'}`, undefined, { prefix: 'Indexes' });
|
|
199
|
+
return false;
|
|
200
|
+
case 'stuck':
|
|
201
|
+
MessageFormatter.warning(`Index '${indexKey}' is stuck`, { prefix: 'Indexes' });
|
|
202
|
+
return false;
|
|
203
|
+
case 'processing':
|
|
204
|
+
case 'deleting':
|
|
205
|
+
// Continue waiting
|
|
206
|
+
break;
|
|
207
|
+
default:
|
|
208
|
+
MessageFormatter.warning(`Unknown status '${index.status}' for index '${indexKey}'`, { prefix: 'Indexes' });
|
|
209
|
+
break;
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
catch (error) {
|
|
213
|
+
MessageFormatter.error(`Error checking index '${indexKey}' status: ${error}`, undefined, { prefix: 'Indexes' });
|
|
214
|
+
}
|
|
215
|
+
await delay(checkInterval);
|
|
216
|
+
}
|
|
217
|
+
MessageFormatter.warning(`Timeout waiting for index '${indexKey}' to become available (${maxWaitTime}ms)`, { prefix: 'Indexes' });
|
|
218
|
+
return false;
|
|
219
|
+
}
|
|
220
|
+
/**
|
|
221
|
+
* Main function to create/update indexes via adapter
|
|
222
|
+
* This replaces the messy inline code in methods.ts
|
|
223
|
+
*/
|
|
224
|
+
export async function createOrUpdateIndexesViaAdapter(adapter, databaseId, tableId, desiredIndexes, configIndexes) {
|
|
225
|
+
if (!desiredIndexes || desiredIndexes.length === 0) {
|
|
226
|
+
MessageFormatter.info('No indexes to process', { prefix: 'Indexes' });
|
|
227
|
+
return;
|
|
228
|
+
}
|
|
229
|
+
MessageFormatter.info(`Processing ${desiredIndexes.length} indexes for table ${tableId}`, { prefix: 'Indexes' });
|
|
230
|
+
try {
|
|
231
|
+
// Get existing indexes
|
|
232
|
+
const existingIdxRes = await adapter.listIndexes({ databaseId, tableId });
|
|
233
|
+
const existingIndexes = existingIdxRes.data || existingIdxRes.indexes || [];
|
|
234
|
+
// Plan operations
|
|
235
|
+
const plan = planIndexOperations(desiredIndexes, existingIndexes);
|
|
236
|
+
// Show plan with icons (consistent with attribute handling)
|
|
237
|
+
const planParts = [];
|
|
238
|
+
if (plan.toCreate.length)
|
|
239
|
+
planParts.push(`➕ ${plan.toCreate.length} (${plan.toCreate.map(op => op.index.key).join(', ')})`);
|
|
240
|
+
if (plan.toUpdate.length)
|
|
241
|
+
planParts.push(`🔧 ${plan.toUpdate.length} (${plan.toUpdate.map(op => op.index.key).join(', ')})`);
|
|
242
|
+
if (plan.toSkip.length)
|
|
243
|
+
planParts.push(`⏭️ ${plan.toSkip.length}`);
|
|
244
|
+
MessageFormatter.info(`Plan → ${planParts.join(' | ') || 'no changes'}`, { prefix: 'Indexes' });
|
|
245
|
+
// Execute operations
|
|
246
|
+
const result = await executeIndexOperations(adapter, databaseId, tableId, plan);
|
|
247
|
+
// Show summary
|
|
248
|
+
MessageFormatter.info(`Summary → ➕ ${result.summary.created} | 🔧 ${result.summary.updated} | ⏭️ ${result.summary.skipped}`, { prefix: 'Indexes' });
|
|
249
|
+
// Handle errors if any
|
|
250
|
+
if (result.errors.length > 0) {
|
|
251
|
+
MessageFormatter.error(`${result.errors.length} index operations failed:`, undefined, { prefix: 'Indexes' });
|
|
252
|
+
for (const error of result.errors) {
|
|
253
|
+
MessageFormatter.error(` ${error.key}: ${error.error}`, undefined, { prefix: 'Indexes' });
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
catch (error) {
|
|
258
|
+
MessageFormatter.error('Failed to process indexes', error instanceof Error ? error : new Error(String(error)), { prefix: 'Indexes' });
|
|
259
|
+
throw error;
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
/**
|
|
263
|
+
* Handle index deletions for obsolete indexes
|
|
264
|
+
*/
|
|
265
|
+
export async function deleteObsoleteIndexesViaAdapter(adapter, databaseId, tableId, desiredIndexKeys) {
|
|
266
|
+
try {
|
|
267
|
+
// Get existing indexes
|
|
268
|
+
const existingIdxRes = await adapter.listIndexes({ databaseId, tableId });
|
|
269
|
+
const existingIndexes = existingIdxRes.data || existingIdxRes.indexes || [];
|
|
270
|
+
// Plan deletions
|
|
271
|
+
const deletions = planIndexDeletions(desiredIndexKeys, existingIndexes);
|
|
272
|
+
if (deletions.length === 0) {
|
|
273
|
+
MessageFormatter.info('Plan → 🗑️ 0 indexes', { prefix: 'Indexes' });
|
|
274
|
+
return;
|
|
275
|
+
}
|
|
276
|
+
// Show deletion plan
|
|
277
|
+
MessageFormatter.info(`Plan → 🗑️ ${deletions.length} (${deletions.map(op => op.index.key).join(', ')})`, { prefix: 'Indexes' });
|
|
278
|
+
// Execute deletions
|
|
279
|
+
const result = await executeIndexDeletions(adapter, databaseId, tableId, deletions);
|
|
280
|
+
// Show results
|
|
281
|
+
if (result.deleted.length > 0) {
|
|
282
|
+
MessageFormatter.success(`Deleted ${result.deleted.length} indexes: ${result.deleted.join(', ')}`, { prefix: 'Indexes' });
|
|
283
|
+
}
|
|
284
|
+
if (result.errors.length > 0) {
|
|
285
|
+
MessageFormatter.error(`${result.errors.length} index deletions failed:`, undefined, { prefix: 'Indexes' });
|
|
286
|
+
for (const error of result.errors) {
|
|
287
|
+
MessageFormatter.error(` ${error.key}: ${error.error}`, undefined, { prefix: 'Indexes' });
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
catch (error) {
|
|
292
|
+
MessageFormatter.warning(`Could not evaluate index deletions: ${error?.message || error}`, { prefix: 'Indexes' });
|
|
293
|
+
}
|
|
294
|
+
}
|
package/dist/utilsController.js
CHANGED
|
@@ -30,6 +30,7 @@ import { createImportSchemas } from "./migrations/yaml/generateImportSchemas.js"
|
|
|
30
30
|
import { validateCollectionsTablesConfig, reportValidationResults, validateWithStrictMode } from "./config/configValidation.js";
|
|
31
31
|
import { ConfigManager } from "./config/ConfigManager.js";
|
|
32
32
|
import { ClientFactory } from "./utils/ClientFactory.js";
|
|
33
|
+
import { clearProcessingState, processQueue } from "./shared/operationQueue.js";
|
|
33
34
|
export class UtilsController {
|
|
34
35
|
// ──────────────────────────────────────────────────
|
|
35
36
|
// SINGLETON PATTERN
|
|
@@ -458,7 +459,6 @@ export class UtilsController {
|
|
|
458
459
|
// Ensure we don't carry state between databases in a multi-db push
|
|
459
460
|
// This resets processed sets and name->id mapping per database
|
|
460
461
|
try {
|
|
461
|
-
const { clearProcessingState } = await import('./shared/operationQueue.js');
|
|
462
462
|
clearProcessingState();
|
|
463
463
|
}
|
|
464
464
|
catch { }
|
|
@@ -475,6 +475,15 @@ export class UtilsController {
|
|
|
475
475
|
logger.debug("Adapter unavailable, falling back to legacy Databases path", { prefix: "UtilsController" });
|
|
476
476
|
await createOrUpdateCollections(this.database, database.$id, this.config, deletedCollections, collections);
|
|
477
477
|
}
|
|
478
|
+
// Safety net: Process any remaining queued operations to complete relationship sync
|
|
479
|
+
try {
|
|
480
|
+
MessageFormatter.info(`🔄 Processing final operation queue for database ${database.$id}`, { prefix: "UtilsController" });
|
|
481
|
+
await processQueue(this.adapter || this.database, database.$id);
|
|
482
|
+
MessageFormatter.info(`✅ Operation queue processing completed`, { prefix: "UtilsController" });
|
|
483
|
+
}
|
|
484
|
+
catch (error) {
|
|
485
|
+
MessageFormatter.error(`Failed to process operation queue`, error instanceof Error ? error : new Error(String(error)), { prefix: 'UtilsController' });
|
|
486
|
+
}
|
|
478
487
|
}
|
|
479
488
|
async generateSchemas() {
|
|
480
489
|
// Schema generation doesn't need Appwrite connection, just config
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "appwrite-utils-cli",
|
|
3
3
|
"description": "Appwrite Utility Functions to help with database management, data conversion, data import, migrations, and much more. Meant to be used as a CLI tool, I do not recommend installing this in frontend environments.",
|
|
4
|
-
"version": "1.9.
|
|
4
|
+
"version": "1.9.7",
|
|
5
5
|
"main": "src/main.ts",
|
|
6
6
|
"type": "module",
|
|
7
7
|
"repository": {
|