Storage Migration
Migrate data between storage formats
JavaScript Storage Migration Program
This program helps you to learn the fundamental structure and syntax of JavaScript programming.
// Method 1: Simple migration
function migrateStorage() {
const oldData = localStorage.getItem('oldKey');
if (oldData) {
// Migrate to new format
const newData = {
version: 2,
data: JSON.parse(oldData)
};
localStorage.setItem('newKey', JSON.stringify(newData));
localStorage.removeItem('oldKey');
console.log('Migration complete');
}
}
// Method 2: Version-based migration
const STORAGE_VERSION = 2;
function checkAndMigrate() {
const currentVersion = localStorage.getItem('storageVersion');
if (!currentVersion || parseInt(currentVersion) < STORAGE_VERSION) {
console.log('Migration needed');
migrateToVersion(parseInt(currentVersion) || 1);
localStorage.setItem('storageVersion', STORAGE_VERSION.toString());
}
}
function migrateToVersion(fromVersion) {
if (fromVersion === 1) {
// Migrate from v1 to v2
const v1Data = localStorage.getItem('userData');
if (v1Data) {
const data = JSON.parse(v1Data);
const v2Data = {
...data,
metadata: {
createdAt: new Date().toISOString(),
version: 2
}
};
localStorage.setItem('userData', JSON.stringify(v2Data));
}
}
}
checkAndMigrate();
// Method 3: Migration with backup
function migrateWithBackup() {
// Create backup
const backup = {};
for (let i = 0; i < localStorage.length; i++) {
const key = localStorage.key(i);
backup[key] = localStorage.getItem(key);
}
localStorage.setItem('backup_' + Date.now(), JSON.stringify(backup));
// Perform migration
try {
migrateStorage();
console.log('Migration successful');
} catch (e) {
console.error('Migration failed, restoring backup');
// Restore from backup
restoreBackup(backup);
}
}
function restoreBackup(backup) {
localStorage.clear();
Object.keys(backup).forEach(key => {
localStorage.setItem(key, backup[key]);
});
}
// Method 4: Progressive migration
function progressiveMigration() {
const migrationSteps = [
() => migrateStep1(),
() => migrateStep2(),
() => migrateStep3()
];
migrationSteps.forEach((step, index) => {
try {
step();
console.log(`Step ${index + 1} complete`);
} catch (e) {
console.error(`Step ${index + 1} failed:`, e);
throw e;
}
});
}
function migrateStep1() {
// Step 1 logic
}
function migrateStep2() {
// Step 2 logic
}
function migrateStep3() {
// Step 3 logic
}
// Method 5: Data transformation
function transformData(oldFormat) {
// Transform old format to new format
return {
id: oldFormat.userId,
name: oldFormat.fullName,
email: oldFormat.emailAddress,
settings: {
theme: oldFormat.theme || 'light',
notifications: oldFormat.notify || true
}
};
}
function migrateUserData() {
const oldData = localStorage.getItem('oldUserData');
if (oldData) {
const parsed = JSON.parse(oldData);
const newData = transformData(parsed);
localStorage.setItem('userData', JSON.stringify(newData));
localStorage.removeItem('oldUserData');
}
}Migration complete Migration needed Step 1 complete Step 2 complete Step 3 complete
Understanding Storage Migration
Migration updates storage format.
Migration Strategies
Best Practices
Use Cases
Important
Let us now understand every line and the components of the above program.
Note: To write and run JavaScript programs, you need to set up the local environment on your computer. Refer to the complete article Setting up JavaScript Development Environment. If you do not want to set up the local environment on your computer, you can also use online IDE to write and run your JavaScript programs.
Practical Learning Notes for Storage Migration
This JavaScript program is part of the "LocalStorage/SessionStorage" topic and is designed to help you build real problem-solving confidence, not just memorize syntax. Start by understanding the goal of the program in plain language, then trace the logic line by line with a custom input of your own. Once you can predict the output before running the code, your understanding becomes much stronger.
A reliable practice pattern is to run the original version first, then modify only one condition or variable at a time. Observe how that single change affects control flow and output. This deliberate style helps you understand loops, conditions, and data movement much faster than copying full solutions repeatedly.
For interview preparation, explain this solution in three layers: the high-level approach, the step-by-step execution, and the time-space tradeoff. If you can teach these three layers clearly, you are ready to solve close variations of this problem under time pressure.