appwrite-utils-cli 1.1.0 → 1.1.1
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/interactiveCLI.js
CHANGED
@@ -1516,7 +1516,7 @@ export class InteractiveCLI {
|
|
1516
1516
|
async comprehensiveTransfer() {
|
1517
1517
|
MessageFormatter.info("Starting comprehensive transfer configuration...", { prefix: "Transfer" });
|
1518
1518
|
try {
|
1519
|
-
// Check if user has an appwrite config for easier setup
|
1519
|
+
// Initialize controller to optionally load config if available (supports both YAML and TypeScript configs)\n await this.initControllerIfNeeded();\n \n // Check if user has an appwrite config for easier setup
|
1520
1520
|
const hasAppwriteConfig = this.controller?.config?.appwriteEndpoint &&
|
1521
1521
|
this.controller?.config?.appwriteProject &&
|
1522
1522
|
this.controller?.config?.appwriteKey;
|
@@ -57,10 +57,38 @@ export declare class ComprehensiveTransfer {
|
|
57
57
|
execute(): Promise<TransferResults>;
|
58
58
|
private transferAllUsers;
|
59
59
|
private transferAllDatabases;
|
60
|
+
/**
|
61
|
+
* Phase 1: Create database structure (collections, attributes, indexes) without transferring documents
|
62
|
+
*/
|
63
|
+
private createDatabaseStructure;
|
64
|
+
/**
|
65
|
+
* Phase 2: Transfer documents to all collections in the database
|
66
|
+
*/
|
67
|
+
private transferDatabaseDocuments;
|
60
68
|
private transferAllBuckets;
|
61
69
|
private transferBucketFiles;
|
62
70
|
private validateAndDownloadFile;
|
63
71
|
private transferAllFunctions;
|
64
72
|
private downloadFunction;
|
73
|
+
/**
|
74
|
+
* Helper method to fetch all collections from a database
|
75
|
+
*/
|
76
|
+
private fetchAllCollections;
|
77
|
+
/**
|
78
|
+
* Helper method to parse attribute objects (simplified version of parseAttribute)
|
79
|
+
*/
|
80
|
+
private parseAttribute;
|
81
|
+
/**
|
82
|
+
* Helper method to create collection attributes with status checking
|
83
|
+
*/
|
84
|
+
private createCollectionAttributesWithStatusCheck;
|
85
|
+
/**
|
86
|
+
* Helper method to create collection indexes with status checking
|
87
|
+
*/
|
88
|
+
private createCollectionIndexesWithStatusCheck;
|
89
|
+
/**
|
90
|
+
* Helper method to transfer documents between databases
|
91
|
+
*/
|
92
|
+
private transferDocumentsBetweenDatabases;
|
65
93
|
private printSummary;
|
66
94
|
}
|
@@ -1,4 +1,4 @@
|
|
1
|
-
import { converterFunctions, tryAwaitWithRetry } from "appwrite-utils";
|
1
|
+
import { converterFunctions, tryAwaitWithRetry, parseAttribute } from "appwrite-utils";
|
2
2
|
import { Client, Databases, Storage, Users, Functions, Query, } from "node-appwrite";
|
3
3
|
import { InputFile } from "node-appwrite/file";
|
4
4
|
import { MessageFormatter } from "../shared/messageFormatter.js";
|
@@ -132,7 +132,9 @@ export class ComprehensiveTransfer {
|
|
132
132
|
MessageFormatter.info(`DRY RUN: Would transfer ${sourceDatabases.databases.length} databases`, { prefix: "Transfer" });
|
133
133
|
return;
|
134
134
|
}
|
135
|
-
|
135
|
+
// Phase 1: Create all databases and collections (structure only)
|
136
|
+
MessageFormatter.info("Phase 1: Creating database structures (databases, collections, attributes, indexes)", { prefix: "Transfer" });
|
137
|
+
const structureCreationTasks = sourceDatabases.databases.map(db => this.limit(async () => {
|
136
138
|
try {
|
137
139
|
// Check if database exists in target
|
138
140
|
const existingDb = targetDatabases.databases.find(tdb => tdb.$id === db.$id);
|
@@ -141,23 +143,130 @@ export class ComprehensiveTransfer {
|
|
141
143
|
await this.targetDatabases.create(db.$id, db.name, db.enabled);
|
142
144
|
MessageFormatter.success(`Created database: ${db.name}`, { prefix: "Transfer" });
|
143
145
|
}
|
144
|
-
//
|
145
|
-
await
|
146
|
+
// Create collections, attributes, and indexes WITHOUT transferring documents
|
147
|
+
await this.createDatabaseStructure(db.$id);
|
148
|
+
MessageFormatter.success(`Database structure created: ${db.name}`, { prefix: "Transfer" });
|
149
|
+
}
|
150
|
+
catch (error) {
|
151
|
+
MessageFormatter.error(`Database structure creation failed for ${db.name}`, error instanceof Error ? error : new Error(String(error)), { prefix: "Transfer" });
|
152
|
+
this.results.databases.failed++;
|
153
|
+
}
|
154
|
+
}));
|
155
|
+
await Promise.all(structureCreationTasks);
|
156
|
+
// Phase 2: Transfer all documents after all structures are created
|
157
|
+
MessageFormatter.info("Phase 2: Transferring documents to all collections", { prefix: "Transfer" });
|
158
|
+
const documentTransferTasks = sourceDatabases.databases.map(db => this.limit(async () => {
|
159
|
+
try {
|
160
|
+
// Transfer documents for this database
|
161
|
+
await this.transferDatabaseDocuments(db.$id);
|
146
162
|
this.results.databases.transferred++;
|
147
|
-
MessageFormatter.success(`Database ${db.name}
|
163
|
+
MessageFormatter.success(`Database documents transferred: ${db.name}`, { prefix: "Transfer" });
|
148
164
|
}
|
149
165
|
catch (error) {
|
150
|
-
MessageFormatter.error(`
|
166
|
+
MessageFormatter.error(`Document transfer failed for ${db.name}`, error instanceof Error ? error : new Error(String(error)), { prefix: "Transfer" });
|
151
167
|
this.results.databases.failed++;
|
152
168
|
}
|
153
169
|
}));
|
154
|
-
await Promise.all(
|
170
|
+
await Promise.all(documentTransferTasks);
|
155
171
|
MessageFormatter.success("Database transfer phase completed", { prefix: "Transfer" });
|
156
172
|
}
|
157
173
|
catch (error) {
|
158
174
|
MessageFormatter.error("Database transfer phase failed", error instanceof Error ? error : new Error(String(error)), { prefix: "Transfer" });
|
159
175
|
}
|
160
176
|
}
|
177
|
+
/**
|
178
|
+
* Phase 1: Create database structure (collections, attributes, indexes) without transferring documents
|
179
|
+
*/
|
180
|
+
async createDatabaseStructure(dbId) {
|
181
|
+
MessageFormatter.info(`Creating database structure for ${dbId}`, { prefix: "Transfer" });
|
182
|
+
try {
|
183
|
+
// Get all collections from source database
|
184
|
+
const sourceCollections = await this.fetchAllCollections(dbId, this.sourceDatabases);
|
185
|
+
MessageFormatter.info(`Found ${sourceCollections.length} collections in source database ${dbId}`, { prefix: "Transfer" });
|
186
|
+
// Process each collection
|
187
|
+
for (const collection of sourceCollections) {
|
188
|
+
MessageFormatter.info(`Processing collection: ${collection.name} (${collection.$id})`, { prefix: "Transfer" });
|
189
|
+
try {
|
190
|
+
// Create or update collection in target
|
191
|
+
let targetCollection;
|
192
|
+
const existingCollection = await tryAwaitWithRetry(async () => this.targetDatabases.listCollections(dbId, [Query.equal("$id", collection.$id)]));
|
193
|
+
if (existingCollection.collections.length > 0) {
|
194
|
+
targetCollection = existingCollection.collections[0];
|
195
|
+
MessageFormatter.info(`Collection ${collection.name} exists in target database`, { prefix: "Transfer" });
|
196
|
+
// Update collection if needed
|
197
|
+
if (targetCollection.name !== collection.name ||
|
198
|
+
JSON.stringify(targetCollection.$permissions) !== JSON.stringify(collection.$permissions) ||
|
199
|
+
targetCollection.documentSecurity !== collection.documentSecurity ||
|
200
|
+
targetCollection.enabled !== collection.enabled) {
|
201
|
+
targetCollection = await tryAwaitWithRetry(async () => this.targetDatabases.updateCollection(dbId, collection.$id, collection.name, collection.$permissions, collection.documentSecurity, collection.enabled));
|
202
|
+
MessageFormatter.success(`Collection ${collection.name} updated`, { prefix: "Transfer" });
|
203
|
+
}
|
204
|
+
}
|
205
|
+
else {
|
206
|
+
MessageFormatter.info(`Creating collection ${collection.name} in target database...`, { prefix: "Transfer" });
|
207
|
+
targetCollection = await tryAwaitWithRetry(async () => this.targetDatabases.createCollection(dbId, collection.$id, collection.name, collection.$permissions, collection.documentSecurity, collection.enabled));
|
208
|
+
MessageFormatter.success(`Collection ${collection.name} created`, { prefix: "Transfer" });
|
209
|
+
}
|
210
|
+
// Handle attributes with enhanced status checking
|
211
|
+
MessageFormatter.info(`Creating attributes for collection ${collection.name} with enhanced monitoring...`, { prefix: "Transfer" });
|
212
|
+
const attributesToCreate = collection.attributes.map(attr => parseAttribute(attr));
|
213
|
+
const attributesSuccess = await this.createCollectionAttributesWithStatusCheck(this.targetDatabases, dbId, targetCollection, attributesToCreate);
|
214
|
+
if (!attributesSuccess) {
|
215
|
+
MessageFormatter.error(`Failed to create some attributes for collection ${collection.name}`, undefined, { prefix: "Transfer" });
|
216
|
+
// Continue with the transfer even if some attributes failed
|
217
|
+
}
|
218
|
+
else {
|
219
|
+
MessageFormatter.success(`All attributes created successfully for collection ${collection.name}`, { prefix: "Transfer" });
|
220
|
+
}
|
221
|
+
// Handle indexes with enhanced status checking
|
222
|
+
MessageFormatter.info(`Creating indexes for collection ${collection.name} with enhanced monitoring...`, { prefix: "Transfer" });
|
223
|
+
const indexesSuccess = await this.createCollectionIndexesWithStatusCheck(dbId, this.targetDatabases, targetCollection.$id, targetCollection, collection.indexes);
|
224
|
+
if (!indexesSuccess) {
|
225
|
+
MessageFormatter.error(`Failed to create some indexes for collection ${collection.name}`, undefined, { prefix: "Transfer" });
|
226
|
+
// Continue with the transfer even if some indexes failed
|
227
|
+
}
|
228
|
+
else {
|
229
|
+
MessageFormatter.success(`All indexes created successfully for collection ${collection.name}`, { prefix: "Transfer" });
|
230
|
+
}
|
231
|
+
MessageFormatter.success(`Structure complete for collection ${collection.name}`, { prefix: "Transfer" });
|
232
|
+
}
|
233
|
+
catch (error) {
|
234
|
+
MessageFormatter.error(`Error processing collection ${collection.name}`, error instanceof Error ? error : new Error(String(error)), { prefix: "Transfer" });
|
235
|
+
}
|
236
|
+
}
|
237
|
+
}
|
238
|
+
catch (error) {
|
239
|
+
MessageFormatter.error(`Failed to create database structure for ${dbId}`, error instanceof Error ? error : new Error(String(error)), { prefix: "Transfer" });
|
240
|
+
throw error;
|
241
|
+
}
|
242
|
+
}
|
243
|
+
/**
|
244
|
+
* Phase 2: Transfer documents to all collections in the database
|
245
|
+
*/
|
246
|
+
async transferDatabaseDocuments(dbId) {
|
247
|
+
MessageFormatter.info(`Transferring documents for database ${dbId}`, { prefix: "Transfer" });
|
248
|
+
try {
|
249
|
+
// Get all collections from source database
|
250
|
+
const sourceCollections = await this.fetchAllCollections(dbId, this.sourceDatabases);
|
251
|
+
MessageFormatter.info(`Transferring documents for ${sourceCollections.length} collections in database ${dbId}`, { prefix: "Transfer" });
|
252
|
+
// Process each collection
|
253
|
+
for (const collection of sourceCollections) {
|
254
|
+
MessageFormatter.info(`Transferring documents for collection: ${collection.name} (${collection.$id})`, { prefix: "Transfer" });
|
255
|
+
try {
|
256
|
+
// Transfer documents
|
257
|
+
await this.transferDocumentsBetweenDatabases(this.sourceDatabases, this.targetDatabases, dbId, dbId, collection.$id, collection.$id);
|
258
|
+
MessageFormatter.success(`Documents transferred for collection ${collection.name}`, { prefix: "Transfer" });
|
259
|
+
}
|
260
|
+
catch (error) {
|
261
|
+
MessageFormatter.error(`Error transferring documents for collection ${collection.name}`, error instanceof Error ? error : new Error(String(error)), { prefix: "Transfer" });
|
262
|
+
}
|
263
|
+
}
|
264
|
+
}
|
265
|
+
catch (error) {
|
266
|
+
MessageFormatter.error(`Failed to transfer documents for database ${dbId}`, error instanceof Error ? error : new Error(String(error)), { prefix: "Transfer" });
|
267
|
+
throw error;
|
268
|
+
}
|
269
|
+
}
|
161
270
|
async transferAllBuckets() {
|
162
271
|
MessageFormatter.info("Starting bucket transfer phase", { prefix: "Transfer" });
|
163
272
|
try {
|
@@ -346,6 +455,115 @@ export class ComprehensiveTransfer {
|
|
346
455
|
return null;
|
347
456
|
}
|
348
457
|
}
|
458
|
+
/**
|
459
|
+
* Helper method to fetch all collections from a database
|
460
|
+
*/
|
461
|
+
async fetchAllCollections(dbId, databases) {
|
462
|
+
const collections = [];
|
463
|
+
let lastId;
|
464
|
+
while (true) {
|
465
|
+
const queries = [Query.limit(100)];
|
466
|
+
if (lastId) {
|
467
|
+
queries.push(Query.cursorAfter(lastId));
|
468
|
+
}
|
469
|
+
const result = await tryAwaitWithRetry(async () => databases.listCollections(dbId, queries));
|
470
|
+
if (result.collections.length === 0) {
|
471
|
+
break;
|
472
|
+
}
|
473
|
+
collections.push(...result.collections);
|
474
|
+
if (result.collections.length < 100) {
|
475
|
+
break;
|
476
|
+
}
|
477
|
+
lastId = result.collections[result.collections.length - 1].$id;
|
478
|
+
}
|
479
|
+
return collections;
|
480
|
+
}
|
481
|
+
/**
|
482
|
+
* Helper method to parse attribute objects (simplified version of parseAttribute)
|
483
|
+
*/
|
484
|
+
parseAttribute(attr) {
|
485
|
+
// This is a simplified version - in production you'd use the actual parseAttribute from appwrite-utils
|
486
|
+
return {
|
487
|
+
key: attr.key,
|
488
|
+
type: attr.type,
|
489
|
+
size: attr.size,
|
490
|
+
required: attr.required,
|
491
|
+
array: attr.array,
|
492
|
+
default: attr.default,
|
493
|
+
format: attr.format,
|
494
|
+
elements: attr.elements,
|
495
|
+
min: attr.min,
|
496
|
+
max: attr.max,
|
497
|
+
relatedCollection: attr.relatedCollection,
|
498
|
+
relationType: attr.relationType,
|
499
|
+
twoWay: attr.twoWay,
|
500
|
+
twoWayKey: attr.twoWayKey,
|
501
|
+
onDelete: attr.onDelete,
|
502
|
+
side: attr.side
|
503
|
+
};
|
504
|
+
}
|
505
|
+
/**
|
506
|
+
* Helper method to create collection attributes with status checking
|
507
|
+
*/
|
508
|
+
async createCollectionAttributesWithStatusCheck(databases, dbId, collection, attributes) {
|
509
|
+
// Import the enhanced attribute creation function
|
510
|
+
const { createUpdateCollectionAttributesWithStatusCheck } = await import("../collections/attributes.js");
|
511
|
+
return await createUpdateCollectionAttributesWithStatusCheck(databases, dbId, collection, attributes);
|
512
|
+
}
|
513
|
+
/**
|
514
|
+
* Helper method to create collection indexes with status checking
|
515
|
+
*/
|
516
|
+
async createCollectionIndexesWithStatusCheck(dbId, databases, collectionId, collection, indexes) {
|
517
|
+
// Import the enhanced index creation function
|
518
|
+
const { createOrUpdateIndexesWithStatusCheck } = await import("../collections/indexes.js");
|
519
|
+
return await createOrUpdateIndexesWithStatusCheck(dbId, databases, collectionId, collection, indexes);
|
520
|
+
}
|
521
|
+
/**
|
522
|
+
* Helper method to transfer documents between databases
|
523
|
+
*/
|
524
|
+
async transferDocumentsBetweenDatabases(sourceDb, targetDb, sourceDbId, targetDbId, sourceCollectionId, targetCollectionId) {
|
525
|
+
MessageFormatter.info(`Transferring documents from ${sourceCollectionId} to ${targetCollectionId}`, { prefix: "Transfer" });
|
526
|
+
let lastId;
|
527
|
+
let totalTransferred = 0;
|
528
|
+
while (true) {
|
529
|
+
const queries = [Query.limit(50)]; // Smaller batch size for better performance
|
530
|
+
if (lastId) {
|
531
|
+
queries.push(Query.cursorAfter(lastId));
|
532
|
+
}
|
533
|
+
const documents = await tryAwaitWithRetry(async () => sourceDb.listDocuments(sourceDbId, sourceCollectionId, queries));
|
534
|
+
if (documents.documents.length === 0) {
|
535
|
+
break;
|
536
|
+
}
|
537
|
+
// Transfer documents with rate limiting
|
538
|
+
const transferTasks = documents.documents.map(doc => this.limit(async () => {
|
539
|
+
try {
|
540
|
+
// Check if document already exists
|
541
|
+
try {
|
542
|
+
await targetDb.getDocument(targetDbId, targetCollectionId, doc.$id);
|
543
|
+
MessageFormatter.info(`Document ${doc.$id} already exists, skipping`, { prefix: "Transfer" });
|
544
|
+
return;
|
545
|
+
}
|
546
|
+
catch (error) {
|
547
|
+
// Document doesn't exist, proceed with creation
|
548
|
+
}
|
549
|
+
// Create document in target
|
550
|
+
const { $id, $createdAt, $updatedAt, $permissions, $databaseId, $collectionId, ...docData } = doc;
|
551
|
+
await tryAwaitWithRetry(async () => targetDb.createDocument(targetDbId, targetCollectionId, doc.$id, docData, doc.$permissions));
|
552
|
+
totalTransferred++;
|
553
|
+
MessageFormatter.success(`Transferred document ${doc.$id}`, { prefix: "Transfer" });
|
554
|
+
}
|
555
|
+
catch (error) {
|
556
|
+
MessageFormatter.error(`Failed to transfer document ${doc.$id}`, error instanceof Error ? error : new Error(String(error)), { prefix: "Transfer" });
|
557
|
+
}
|
558
|
+
}));
|
559
|
+
await Promise.all(transferTasks);
|
560
|
+
if (documents.documents.length < 50) {
|
561
|
+
break;
|
562
|
+
}
|
563
|
+
lastId = documents.documents[documents.documents.length - 1].$id;
|
564
|
+
}
|
565
|
+
MessageFormatter.info(`Transferred ${totalTransferred} documents from ${sourceCollectionId} to ${targetCollectionId}`, { prefix: "Transfer" });
|
566
|
+
}
|
349
567
|
printSummary() {
|
350
568
|
const duration = Math.round((Date.now() - this.startTime) / 1000);
|
351
569
|
MessageFormatter.info("=== COMPREHENSIVE TRANSFER SUMMARY ===", { prefix: "Transfer" });
|
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.1.
|
4
|
+
"version": "1.1.1",
|
5
5
|
"main": "src/main.ts",
|
6
6
|
"type": "module",
|
7
7
|
"repository": {
|
package/src/interactiveCLI.ts
CHANGED
@@ -2052,7 +2052,7 @@ export class InteractiveCLI {
|
|
2052
2052
|
MessageFormatter.info("Starting comprehensive transfer configuration...", { prefix: "Transfer" });
|
2053
2053
|
|
2054
2054
|
try {
|
2055
|
-
// Check if user has an appwrite config for easier setup
|
2055
|
+
// Initialize controller to optionally load config if available (supports both YAML and TypeScript configs)\n await this.initControllerIfNeeded();\n \n // Check if user has an appwrite config for easier setup
|
2056
2056
|
const hasAppwriteConfig = this.controller?.config?.appwriteEndpoint &&
|
2057
2057
|
this.controller?.config?.appwriteProject &&
|
2058
2058
|
this.controller?.config?.appwriteKey;
|
@@ -1,4 +1,4 @@
|
|
1
|
-
import { converterFunctions, tryAwaitWithRetry } from "appwrite-utils";
|
1
|
+
import { converterFunctions, tryAwaitWithRetry, parseAttribute } from "appwrite-utils";
|
2
2
|
import {
|
3
3
|
Client,
|
4
4
|
Databases,
|
@@ -204,7 +204,10 @@ export class ComprehensiveTransfer {
|
|
204
204
|
return;
|
205
205
|
}
|
206
206
|
|
207
|
-
|
207
|
+
// Phase 1: Create all databases and collections (structure only)
|
208
|
+
MessageFormatter.info("Phase 1: Creating database structures (databases, collections, attributes, indexes)", { prefix: "Transfer" });
|
209
|
+
|
210
|
+
const structureCreationTasks = sourceDatabases.databases.map(db =>
|
208
211
|
this.limit(async () => {
|
209
212
|
try {
|
210
213
|
// Check if database exists in target
|
@@ -216,32 +219,189 @@ export class ComprehensiveTransfer {
|
|
216
219
|
MessageFormatter.success(`Created database: ${db.name}`, { prefix: "Transfer" });
|
217
220
|
}
|
218
221
|
|
219
|
-
//
|
220
|
-
await
|
221
|
-
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
|
226
|
-
|
227
|
-
|
222
|
+
// Create collections, attributes, and indexes WITHOUT transferring documents
|
223
|
+
await this.createDatabaseStructure(db.$id);
|
224
|
+
|
225
|
+
MessageFormatter.success(`Database structure created: ${db.name}`, { prefix: "Transfer" });
|
226
|
+
} catch (error) {
|
227
|
+
MessageFormatter.error(`Database structure creation failed for ${db.name}`, error instanceof Error ? error : new Error(String(error)), { prefix: "Transfer" });
|
228
|
+
this.results.databases.failed++;
|
229
|
+
}
|
230
|
+
})
|
231
|
+
);
|
232
|
+
|
233
|
+
await Promise.all(structureCreationTasks);
|
234
|
+
|
235
|
+
// Phase 2: Transfer all documents after all structures are created
|
236
|
+
MessageFormatter.info("Phase 2: Transferring documents to all collections", { prefix: "Transfer" });
|
237
|
+
|
238
|
+
const documentTransferTasks = sourceDatabases.databases.map(db =>
|
239
|
+
this.limit(async () => {
|
240
|
+
try {
|
241
|
+
// Transfer documents for this database
|
242
|
+
await this.transferDatabaseDocuments(db.$id);
|
228
243
|
|
229
244
|
this.results.databases.transferred++;
|
230
|
-
MessageFormatter.success(`Database ${db.name}
|
245
|
+
MessageFormatter.success(`Database documents transferred: ${db.name}`, { prefix: "Transfer" });
|
231
246
|
} catch (error) {
|
232
|
-
MessageFormatter.error(`
|
247
|
+
MessageFormatter.error(`Document transfer failed for ${db.name}`, error instanceof Error ? error : new Error(String(error)), { prefix: "Transfer" });
|
233
248
|
this.results.databases.failed++;
|
234
249
|
}
|
235
250
|
})
|
236
251
|
);
|
237
252
|
|
238
|
-
await Promise.all(
|
253
|
+
await Promise.all(documentTransferTasks);
|
239
254
|
MessageFormatter.success("Database transfer phase completed", { prefix: "Transfer" });
|
240
255
|
} catch (error) {
|
241
256
|
MessageFormatter.error("Database transfer phase failed", error instanceof Error ? error : new Error(String(error)), { prefix: "Transfer" });
|
242
257
|
}
|
243
258
|
}
|
244
259
|
|
260
|
+
/**
|
261
|
+
* Phase 1: Create database structure (collections, attributes, indexes) without transferring documents
|
262
|
+
*/
|
263
|
+
private async createDatabaseStructure(dbId: string): Promise<void> {
|
264
|
+
MessageFormatter.info(`Creating database structure for ${dbId}`, { prefix: "Transfer" });
|
265
|
+
|
266
|
+
try {
|
267
|
+
// Get all collections from source database
|
268
|
+
const sourceCollections = await this.fetchAllCollections(dbId, this.sourceDatabases);
|
269
|
+
MessageFormatter.info(`Found ${sourceCollections.length} collections in source database ${dbId}`, { prefix: "Transfer" });
|
270
|
+
|
271
|
+
// Process each collection
|
272
|
+
for (const collection of sourceCollections) {
|
273
|
+
MessageFormatter.info(`Processing collection: ${collection.name} (${collection.$id})`, { prefix: "Transfer" });
|
274
|
+
|
275
|
+
try {
|
276
|
+
// Create or update collection in target
|
277
|
+
let targetCollection: Models.Collection;
|
278
|
+
const existingCollection = await tryAwaitWithRetry(async () =>
|
279
|
+
this.targetDatabases.listCollections(dbId, [Query.equal("$id", collection.$id)])
|
280
|
+
);
|
281
|
+
|
282
|
+
if (existingCollection.collections.length > 0) {
|
283
|
+
targetCollection = existingCollection.collections[0];
|
284
|
+
MessageFormatter.info(`Collection ${collection.name} exists in target database`, { prefix: "Transfer" });
|
285
|
+
|
286
|
+
// Update collection if needed
|
287
|
+
if (
|
288
|
+
targetCollection.name !== collection.name ||
|
289
|
+
JSON.stringify(targetCollection.$permissions) !== JSON.stringify(collection.$permissions) ||
|
290
|
+
targetCollection.documentSecurity !== collection.documentSecurity ||
|
291
|
+
targetCollection.enabled !== collection.enabled
|
292
|
+
) {
|
293
|
+
targetCollection = await tryAwaitWithRetry(async () =>
|
294
|
+
this.targetDatabases.updateCollection(
|
295
|
+
dbId,
|
296
|
+
collection.$id,
|
297
|
+
collection.name,
|
298
|
+
collection.$permissions,
|
299
|
+
collection.documentSecurity,
|
300
|
+
collection.enabled
|
301
|
+
)
|
302
|
+
);
|
303
|
+
MessageFormatter.success(`Collection ${collection.name} updated`, { prefix: "Transfer" });
|
304
|
+
}
|
305
|
+
} else {
|
306
|
+
MessageFormatter.info(`Creating collection ${collection.name} in target database...`, { prefix: "Transfer" });
|
307
|
+
targetCollection = await tryAwaitWithRetry(async () =>
|
308
|
+
this.targetDatabases.createCollection(
|
309
|
+
dbId,
|
310
|
+
collection.$id,
|
311
|
+
collection.name,
|
312
|
+
collection.$permissions,
|
313
|
+
collection.documentSecurity,
|
314
|
+
collection.enabled
|
315
|
+
)
|
316
|
+
);
|
317
|
+
MessageFormatter.success(`Collection ${collection.name} created`, { prefix: "Transfer" });
|
318
|
+
}
|
319
|
+
|
320
|
+
// Handle attributes with enhanced status checking
|
321
|
+
MessageFormatter.info(`Creating attributes for collection ${collection.name} with enhanced monitoring...`, { prefix: "Transfer" });
|
322
|
+
|
323
|
+
const attributesToCreate = collection.attributes.map(attr => parseAttribute(attr as any));
|
324
|
+
|
325
|
+
const attributesSuccess = await this.createCollectionAttributesWithStatusCheck(
|
326
|
+
this.targetDatabases,
|
327
|
+
dbId,
|
328
|
+
targetCollection,
|
329
|
+
attributesToCreate
|
330
|
+
);
|
331
|
+
|
332
|
+
if (!attributesSuccess) {
|
333
|
+
MessageFormatter.error(`Failed to create some attributes for collection ${collection.name}`, undefined, { prefix: "Transfer" });
|
334
|
+
// Continue with the transfer even if some attributes failed
|
335
|
+
} else {
|
336
|
+
MessageFormatter.success(`All attributes created successfully for collection ${collection.name}`, { prefix: "Transfer" });
|
337
|
+
}
|
338
|
+
|
339
|
+
// Handle indexes with enhanced status checking
|
340
|
+
MessageFormatter.info(`Creating indexes for collection ${collection.name} with enhanced monitoring...`, { prefix: "Transfer" });
|
341
|
+
|
342
|
+
const indexesSuccess = await this.createCollectionIndexesWithStatusCheck(
|
343
|
+
dbId,
|
344
|
+
this.targetDatabases,
|
345
|
+
targetCollection.$id,
|
346
|
+
targetCollection,
|
347
|
+
collection.indexes as any
|
348
|
+
);
|
349
|
+
|
350
|
+
if (!indexesSuccess) {
|
351
|
+
MessageFormatter.error(`Failed to create some indexes for collection ${collection.name}`, undefined, { prefix: "Transfer" });
|
352
|
+
// Continue with the transfer even if some indexes failed
|
353
|
+
} else {
|
354
|
+
MessageFormatter.success(`All indexes created successfully for collection ${collection.name}`, { prefix: "Transfer" });
|
355
|
+
}
|
356
|
+
|
357
|
+
MessageFormatter.success(`Structure complete for collection ${collection.name}`, { prefix: "Transfer" });
|
358
|
+
} catch (error) {
|
359
|
+
MessageFormatter.error(`Error processing collection ${collection.name}`, error instanceof Error ? error : new Error(String(error)), { prefix: "Transfer" });
|
360
|
+
}
|
361
|
+
}
|
362
|
+
} catch (error) {
|
363
|
+
MessageFormatter.error(`Failed to create database structure for ${dbId}`, error instanceof Error ? error : new Error(String(error)), { prefix: "Transfer" });
|
364
|
+
throw error;
|
365
|
+
}
|
366
|
+
}
|
367
|
+
|
368
|
+
/**
|
369
|
+
* Phase 2: Transfer documents to all collections in the database
|
370
|
+
*/
|
371
|
+
private async transferDatabaseDocuments(dbId: string): Promise<void> {
|
372
|
+
MessageFormatter.info(`Transferring documents for database ${dbId}`, { prefix: "Transfer" });
|
373
|
+
|
374
|
+
try {
|
375
|
+
// Get all collections from source database
|
376
|
+
const sourceCollections = await this.fetchAllCollections(dbId, this.sourceDatabases);
|
377
|
+
MessageFormatter.info(`Transferring documents for ${sourceCollections.length} collections in database ${dbId}`, { prefix: "Transfer" });
|
378
|
+
|
379
|
+
// Process each collection
|
380
|
+
for (const collection of sourceCollections) {
|
381
|
+
MessageFormatter.info(`Transferring documents for collection: ${collection.name} (${collection.$id})`, { prefix: "Transfer" });
|
382
|
+
|
383
|
+
try {
|
384
|
+
// Transfer documents
|
385
|
+
await this.transferDocumentsBetweenDatabases(
|
386
|
+
this.sourceDatabases,
|
387
|
+
this.targetDatabases,
|
388
|
+
dbId,
|
389
|
+
dbId,
|
390
|
+
collection.$id,
|
391
|
+
collection.$id
|
392
|
+
);
|
393
|
+
|
394
|
+
MessageFormatter.success(`Documents transferred for collection ${collection.name}`, { prefix: "Transfer" });
|
395
|
+
} catch (error) {
|
396
|
+
MessageFormatter.error(`Error transferring documents for collection ${collection.name}`, error instanceof Error ? error : new Error(String(error)), { prefix: "Transfer" });
|
397
|
+
}
|
398
|
+
}
|
399
|
+
} catch (error) {
|
400
|
+
MessageFormatter.error(`Failed to transfer documents for database ${dbId}`, error instanceof Error ? error : new Error(String(error)), { prefix: "Transfer" });
|
401
|
+
throw error;
|
402
|
+
}
|
403
|
+
}
|
404
|
+
|
245
405
|
private async transferAllBuckets(): Promise<void> {
|
246
406
|
MessageFormatter.info("Starting bucket transfer phase", { prefix: "Transfer" });
|
247
407
|
|
@@ -483,6 +643,180 @@ export class ComprehensiveTransfer {
|
|
483
643
|
}
|
484
644
|
}
|
485
645
|
|
646
|
+
/**
|
647
|
+
* Helper method to fetch all collections from a database
|
648
|
+
*/
|
649
|
+
private async fetchAllCollections(dbId: string, databases: Databases): Promise<Models.Collection[]> {
|
650
|
+
const collections: Models.Collection[] = [];
|
651
|
+
let lastId: string | undefined;
|
652
|
+
|
653
|
+
while (true) {
|
654
|
+
const queries = [Query.limit(100)];
|
655
|
+
if (lastId) {
|
656
|
+
queries.push(Query.cursorAfter(lastId));
|
657
|
+
}
|
658
|
+
|
659
|
+
const result = await tryAwaitWithRetry(async () => databases.listCollections(dbId, queries));
|
660
|
+
|
661
|
+
if (result.collections.length === 0) {
|
662
|
+
break;
|
663
|
+
}
|
664
|
+
|
665
|
+
collections.push(...result.collections);
|
666
|
+
|
667
|
+
if (result.collections.length < 100) {
|
668
|
+
break;
|
669
|
+
}
|
670
|
+
|
671
|
+
lastId = result.collections[result.collections.length - 1].$id;
|
672
|
+
}
|
673
|
+
|
674
|
+
return collections;
|
675
|
+
}
|
676
|
+
|
677
|
+
/**
|
678
|
+
* Helper method to parse attribute objects (simplified version of parseAttribute)
|
679
|
+
*/
|
680
|
+
private parseAttribute(attr: any): any {
|
681
|
+
// This is a simplified version - in production you'd use the actual parseAttribute from appwrite-utils
|
682
|
+
return {
|
683
|
+
key: attr.key,
|
684
|
+
type: attr.type,
|
685
|
+
size: attr.size,
|
686
|
+
required: attr.required,
|
687
|
+
array: attr.array,
|
688
|
+
default: attr.default,
|
689
|
+
format: attr.format,
|
690
|
+
elements: attr.elements,
|
691
|
+
min: attr.min,
|
692
|
+
max: attr.max,
|
693
|
+
relatedCollection: attr.relatedCollection,
|
694
|
+
relationType: attr.relationType,
|
695
|
+
twoWay: attr.twoWay,
|
696
|
+
twoWayKey: attr.twoWayKey,
|
697
|
+
onDelete: attr.onDelete,
|
698
|
+
side: attr.side
|
699
|
+
};
|
700
|
+
}
|
701
|
+
|
702
|
+
/**
|
703
|
+
* Helper method to create collection attributes with status checking
|
704
|
+
*/
|
705
|
+
private async createCollectionAttributesWithStatusCheck(
|
706
|
+
databases: Databases,
|
707
|
+
dbId: string,
|
708
|
+
collection: Models.Collection,
|
709
|
+
attributes: any[]
|
710
|
+
): Promise<boolean> {
|
711
|
+
// Import the enhanced attribute creation function
|
712
|
+
const { createUpdateCollectionAttributesWithStatusCheck } = await import("../collections/attributes.js");
|
713
|
+
|
714
|
+
return await createUpdateCollectionAttributesWithStatusCheck(
|
715
|
+
databases,
|
716
|
+
dbId,
|
717
|
+
collection,
|
718
|
+
attributes
|
719
|
+
);
|
720
|
+
}
|
721
|
+
|
722
|
+
/**
|
723
|
+
* Helper method to create collection indexes with status checking
|
724
|
+
*/
|
725
|
+
private async createCollectionIndexesWithStatusCheck(
|
726
|
+
dbId: string,
|
727
|
+
databases: Databases,
|
728
|
+
collectionId: string,
|
729
|
+
collection: Models.Collection,
|
730
|
+
indexes: any[]
|
731
|
+
): Promise<boolean> {
|
732
|
+
// Import the enhanced index creation function
|
733
|
+
const { createOrUpdateIndexesWithStatusCheck } = await import("../collections/indexes.js");
|
734
|
+
|
735
|
+
return await createOrUpdateIndexesWithStatusCheck(
|
736
|
+
dbId,
|
737
|
+
databases,
|
738
|
+
collectionId,
|
739
|
+
collection,
|
740
|
+
indexes
|
741
|
+
);
|
742
|
+
}
|
743
|
+
|
744
|
+
/**
|
745
|
+
* Helper method to transfer documents between databases
|
746
|
+
*/
|
747
|
+
private async transferDocumentsBetweenDatabases(
|
748
|
+
sourceDb: Databases,
|
749
|
+
targetDb: Databases,
|
750
|
+
sourceDbId: string,
|
751
|
+
targetDbId: string,
|
752
|
+
sourceCollectionId: string,
|
753
|
+
targetCollectionId: string
|
754
|
+
): Promise<void> {
|
755
|
+
MessageFormatter.info(`Transferring documents from ${sourceCollectionId} to ${targetCollectionId}`, { prefix: "Transfer" });
|
756
|
+
|
757
|
+
let lastId: string | undefined;
|
758
|
+
let totalTransferred = 0;
|
759
|
+
|
760
|
+
while (true) {
|
761
|
+
const queries = [Query.limit(50)]; // Smaller batch size for better performance
|
762
|
+
if (lastId) {
|
763
|
+
queries.push(Query.cursorAfter(lastId));
|
764
|
+
}
|
765
|
+
|
766
|
+
const documents = await tryAwaitWithRetry(async () =>
|
767
|
+
sourceDb.listDocuments(sourceDbId, sourceCollectionId, queries)
|
768
|
+
);
|
769
|
+
|
770
|
+
if (documents.documents.length === 0) {
|
771
|
+
break;
|
772
|
+
}
|
773
|
+
|
774
|
+
// Transfer documents with rate limiting
|
775
|
+
const transferTasks = documents.documents.map(doc =>
|
776
|
+
this.limit(async () => {
|
777
|
+
try {
|
778
|
+
// Check if document already exists
|
779
|
+
try {
|
780
|
+
await targetDb.getDocument(targetDbId, targetCollectionId, doc.$id);
|
781
|
+
MessageFormatter.info(`Document ${doc.$id} already exists, skipping`, { prefix: "Transfer" });
|
782
|
+
return;
|
783
|
+
} catch (error) {
|
784
|
+
// Document doesn't exist, proceed with creation
|
785
|
+
}
|
786
|
+
|
787
|
+
// Create document in target
|
788
|
+
const { $id, $createdAt, $updatedAt, $permissions, $databaseId, $collectionId, ...docData } = doc;
|
789
|
+
|
790
|
+
await tryAwaitWithRetry(async () =>
|
791
|
+
targetDb.createDocument(
|
792
|
+
targetDbId,
|
793
|
+
targetCollectionId,
|
794
|
+
doc.$id,
|
795
|
+
docData,
|
796
|
+
doc.$permissions
|
797
|
+
)
|
798
|
+
);
|
799
|
+
|
800
|
+
totalTransferred++;
|
801
|
+
MessageFormatter.success(`Transferred document ${doc.$id}`, { prefix: "Transfer" });
|
802
|
+
} catch (error) {
|
803
|
+
MessageFormatter.error(`Failed to transfer document ${doc.$id}`, error instanceof Error ? error : new Error(String(error)), { prefix: "Transfer" });
|
804
|
+
}
|
805
|
+
})
|
806
|
+
);
|
807
|
+
|
808
|
+
await Promise.all(transferTasks);
|
809
|
+
|
810
|
+
if (documents.documents.length < 50) {
|
811
|
+
break;
|
812
|
+
}
|
813
|
+
|
814
|
+
lastId = documents.documents[documents.documents.length - 1].$id;
|
815
|
+
}
|
816
|
+
|
817
|
+
MessageFormatter.info(`Transferred ${totalTransferred} documents from ${sourceCollectionId} to ${targetCollectionId}`, { prefix: "Transfer" });
|
818
|
+
}
|
819
|
+
|
486
820
|
private printSummary(): void {
|
487
821
|
const duration = Math.round((Date.now() - this.startTime) / 1000);
|
488
822
|
|