Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#100 - Bug/local storage version control - Fikret Ellek #116

Merged
merged 8 commits into from
Jan 19, 2025

Conversation

fikretellek
Copy link
Collaborator

Naming Rules

Name your PR like this: ISSUENUMBER-TITLE-YOURNAME

Description

I have created service functions for getting and setting meeting local storage data. Within this implementation we ensure that local storage updates need to be handled with version control.

Related to

Make sure you include the issue number with a hash sign # so Github can link this PR to the right issue, like this:

Fixes #100

Checklist:

  • My code follows the style guidelines of this project
  • I have carefully reviewed my own code
  • I have commented my code
  • I have updated any documentation

@OracPrime
Copy link
Collaborator

I would suggest we should use semantic versioning https://semver.org/ and behave appropriately. So if you add a new field which has a sensible default, then you don't throw away existing data, but if there is a breaking change you do throw away existing data.

@OracPrime
Copy link
Collaborator

I'm not convinced this new version implements semantic versioning. Where is the "major version changed, ignore old: minor version change, try to merge: tiny version changed try to ignore" ??

@OracPrime
Copy link
Collaborator

OK this is a ChatGPT quickie, but it's about right I think:

const fs = require('fs');
const semver = require('semver');

function readVersionedJson(filePath, requiredVersion) {
try {
// Read the JSON file
const fileContent = fs.readFileSync(filePath, 'utf8');
const jsonData = JSON.parse(fileContent);

// Extract and parse the version
const fileVersion = jsonData.version;
if (!fileVersion) {
  throw new Error("Missing 'version' field in JSON file.");
}

// Check for major version compatibility
if (semver.major(fileVersion) !== semver.major(requiredVersion)) {
  throw new Error(`Incompatible major version: ${fileVersion}. Requires major version ${semver.major(requiredVersion)}.`);
}

// Graceful upgrade for minor version differences
if (semver.minor(fileVersion) < semver.minor(requiredVersion)) {
  console.warn(
    `Warning: Minor version ${semver.minor(fileVersion)} is lower than required ${semver.minor(requiredVersion)}. Using default upgrades.`
  );
  applyDefaultUpgrades(jsonData, requiredVersion);
}

console.log(`File version ${fileVersion} is compatible with required version ${requiredVersion}.`);
return jsonData.data;

} catch (error) {
console.error(Error reading JSON file: ${error.message});
return null;
}
}

function applyDefaultUpgrades(jsonData, targetVersion) {
// Example: Add new properties introduced in newer minor versions
if (!jsonData.data.newFeature) {
jsonData.data.newFeature = "This is a default value for new features.";
}
console.log(Upgraded JSON data to be compatible with version ${targetVersion}.);
}

// Example Usage
const requiredVersion = "1.1.0"; // Required version
const filePath = "data_v1.0.0.json";

const data = readVersionedJson(filePath, requiredVersion);
if (data) {
console.log("JSON Data:", data);
} else {
console.log("Failed to load JSON file.");
}

…s differently. Created subfunctionalities to support semantic versioning.
@OracPrime
Copy link
Collaborator

That last commit looks as though it should do the job. How confident are you? Sufficiently confident not to have some tests?

@fikretellek fikretellek merged commit 031b470 into main Jan 19, 2025
0 of 5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Local storage version control
2 participants