mirror of
https://github.com/Stirling-Tools/Stirling-PDF.git
synced 2025-09-18 01:19:24 +00:00
File migration
This commit is contained in:
parent
bd6c9042d3
commit
d7f698ead7
@ -87,45 +87,135 @@ class IndexedDBManager {
|
|||||||
request.onupgradeneeded = (event) => {
|
request.onupgradeneeded = (event) => {
|
||||||
const db = request.result;
|
const db = request.result;
|
||||||
const oldVersion = event.oldVersion;
|
const oldVersion = event.oldVersion;
|
||||||
|
const transaction = request.transaction;
|
||||||
|
|
||||||
console.log(`Upgrading ${config.name} from v${oldVersion} to v${config.version}`);
|
console.log(`Upgrading ${config.name} from v${oldVersion} to v${config.version}`);
|
||||||
|
|
||||||
// Create or update object stores
|
// Create or update object stores
|
||||||
config.stores.forEach(storeConfig => {
|
config.stores.forEach(storeConfig => {
|
||||||
|
let store: IDBObjectStore | undefined;
|
||||||
|
|
||||||
if (db.objectStoreNames.contains(storeConfig.name)) {
|
if (db.objectStoreNames.contains(storeConfig.name)) {
|
||||||
// Store exists - for now, just continue (could add migration logic here)
|
// Store exists - get reference for migration
|
||||||
console.log(`Object store '${storeConfig.name}' already exists`);
|
console.log(`Object store '${storeConfig.name}' already exists`);
|
||||||
return;
|
store = transaction?.objectStore(storeConfig.name);
|
||||||
|
|
||||||
|
// Add new indexes if they don't exist
|
||||||
|
if (storeConfig.indexes && store) {
|
||||||
|
storeConfig.indexes.forEach(indexConfig => {
|
||||||
|
if (!store?.indexNames.contains(indexConfig.name)) {
|
||||||
|
store?.createIndex(
|
||||||
|
indexConfig.name,
|
||||||
|
indexConfig.keyPath,
|
||||||
|
{ unique: indexConfig.unique }
|
||||||
|
);
|
||||||
|
console.log(`Created index '${indexConfig.name}' on '${storeConfig.name}'`);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// Create new object store
|
||||||
|
const options: IDBObjectStoreParameters = {};
|
||||||
|
if (storeConfig.keyPath) {
|
||||||
|
options.keyPath = storeConfig.keyPath;
|
||||||
|
}
|
||||||
|
if (storeConfig.autoIncrement) {
|
||||||
|
options.autoIncrement = storeConfig.autoIncrement;
|
||||||
|
}
|
||||||
|
|
||||||
|
store = db.createObjectStore(storeConfig.name, options);
|
||||||
|
console.log(`Created object store '${storeConfig.name}'`);
|
||||||
|
|
||||||
|
// Create indexes
|
||||||
|
if (storeConfig.indexes) {
|
||||||
|
storeConfig.indexes.forEach(indexConfig => {
|
||||||
|
store?.createIndex(
|
||||||
|
indexConfig.name,
|
||||||
|
indexConfig.keyPath,
|
||||||
|
{ unique: indexConfig.unique }
|
||||||
|
);
|
||||||
|
console.log(`Created index '${indexConfig.name}' on '${storeConfig.name}'`);
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create new object store
|
// Perform data migration for files database
|
||||||
const options: IDBObjectStoreParameters = {};
|
if (config.name === 'stirling-pdf-files' && storeConfig.name === 'files' && store) {
|
||||||
if (storeConfig.keyPath) {
|
this.migrateFileHistoryFields(store, oldVersion);
|
||||||
options.keyPath = storeConfig.keyPath;
|
|
||||||
}
|
|
||||||
if (storeConfig.autoIncrement) {
|
|
||||||
options.autoIncrement = storeConfig.autoIncrement;
|
|
||||||
}
|
|
||||||
|
|
||||||
const store = db.createObjectStore(storeConfig.name, options);
|
|
||||||
console.log(`Created object store '${storeConfig.name}'`);
|
|
||||||
|
|
||||||
// Create indexes
|
|
||||||
if (storeConfig.indexes) {
|
|
||||||
storeConfig.indexes.forEach(indexConfig => {
|
|
||||||
store.createIndex(
|
|
||||||
indexConfig.name,
|
|
||||||
indexConfig.keyPath,
|
|
||||||
{ unique: indexConfig.unique }
|
|
||||||
);
|
|
||||||
console.log(`Created index '${indexConfig.name}' on '${storeConfig.name}'`);
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Migrate existing file records to include new file history fields
|
||||||
|
*/
|
||||||
|
private migrateFileHistoryFields(store: IDBObjectStore, oldVersion: number): void {
|
||||||
|
// Only migrate if upgrading from a version before file history was added (version < 3)
|
||||||
|
if (oldVersion >= 3) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log('Starting file history migration for existing records...');
|
||||||
|
|
||||||
|
const cursor = store.openCursor();
|
||||||
|
let migratedCount = 0;
|
||||||
|
|
||||||
|
cursor.onsuccess = (event) => {
|
||||||
|
const cursor = (event.target as IDBRequest).result;
|
||||||
|
if (cursor) {
|
||||||
|
const record = cursor.value;
|
||||||
|
let needsUpdate = false;
|
||||||
|
|
||||||
|
// Add missing file history fields with sensible defaults
|
||||||
|
if (record.isLeaf === undefined) {
|
||||||
|
record.isLeaf = true; // Existing files are unprocessed, should appear in recent files
|
||||||
|
needsUpdate = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (record.versionNumber === undefined) {
|
||||||
|
record.versionNumber = 1; // Existing files are first version
|
||||||
|
needsUpdate = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (record.originalFileId === undefined) {
|
||||||
|
record.originalFileId = record.id; // Existing files are their own root
|
||||||
|
needsUpdate = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (record.parentFileId === undefined) {
|
||||||
|
record.parentFileId = undefined; // No parent for existing files
|
||||||
|
needsUpdate = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (record.toolHistory === undefined) {
|
||||||
|
record.toolHistory = []; // No history for existing files
|
||||||
|
needsUpdate = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Update the record if any fields were missing
|
||||||
|
if (needsUpdate) {
|
||||||
|
try {
|
||||||
|
cursor.update(record);
|
||||||
|
migratedCount++;
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Failed to migrate record:', record.id, error);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
cursor.continue();
|
||||||
|
} else {
|
||||||
|
// Migration complete
|
||||||
|
console.log(`File history migration completed. Migrated ${migratedCount} records.`);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
cursor.onerror = (event) => {
|
||||||
|
console.error('File history migration failed:', (event.target as IDBRequest).error);
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get database connection (must be already opened)
|
* Get database connection (must be already opened)
|
||||||
*/
|
*/
|
||||||
|
@ -28,8 +28,8 @@ export interface BaseFileMetadata {
|
|||||||
createdAt?: number; // When file was added to system
|
createdAt?: number; // When file was added to system
|
||||||
|
|
||||||
// File history tracking
|
// File history tracking
|
||||||
isLeaf?: boolean; // True if this file hasn't been processed yet
|
isLeaf: boolean; // True if this file hasn't been processed yet
|
||||||
originalFileId?: string; // Root file ID for grouping versions
|
originalFileId: string; // Root file ID for grouping versions
|
||||||
versionNumber: number; // Version number in chain
|
versionNumber: number; // Version number in chain
|
||||||
parentFileId?: FileId; // Immediate parent file ID
|
parentFileId?: FileId; // Immediate parent file ID
|
||||||
toolHistory?: Array<{
|
toolHistory?: Array<{
|
||||||
|
@ -169,6 +169,7 @@ export function createNewStirlingFileStub(
|
|||||||
size: file.size,
|
size: file.size,
|
||||||
type: file.type,
|
type: file.type,
|
||||||
lastModified: file.lastModified,
|
lastModified: file.lastModified,
|
||||||
|
originalFileId: fileId,
|
||||||
quickKey: createQuickKey(file),
|
quickKey: createQuickKey(file),
|
||||||
createdAt: Date.now(),
|
createdAt: Date.now(),
|
||||||
isLeaf: true, // New files are leaf nodes by default
|
isLeaf: true, // New files are leaf nodes by default
|
||||||
|
Loading…
x
Reference in New Issue
Block a user