Skip to content

Commit

Permalink
prevent running the migration process when there are not particular s…
Browse files Browse the repository at this point in the history
…cripts to run (teambit#1074)

* add a backup mechanism for the scope objects (it is not in use for now).
  • Loading branch information
davidfirst authored Jun 17, 2018
1 parent ef17407 commit 935c9ff
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 26 deletions.
1 change: 0 additions & 1 deletion .eslintignore-circle
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@ src/consumer/specs-results/specs-results.js
src/driver/driver.js
src/extensions/core-extensions/ext-docs-parser.js
src/logger/logger.js
src/migration/migration-helper.js
src/scope/objects/ref.js
src/scope/version-dependencies.js
e2e/commands/show.e2e.js
Expand Down
20 changes: 11 additions & 9 deletions src/migration/migration-helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,23 @@ import logger from '../logger/logger';

export type MigrationResult = {
run: boolean,
success: ?boolean
success?: ?boolean
};

export type MigrationDeclaration = {
name: string,
migrate: Fucntion
migrate: Function
};

type AbstractVersionMigrations = {
version: string,
migrations: MigrationDeclaration[]
[version: string]: MigrationDeclaration[]
};

/**
* A function which get a migration manifest and versions, and return a sorted array of the migrations to run
* We are taking also the current version to prevent cases which a developer specify a migration to run for a future release, and we don't want it
* to run now
*
* We are taking also the current version to prevent cases which a developer specify a migration to run for a
* future release, and we don't want it to run now
*
* @export
* @param {string} currentVersion - The current version of bit
* @param {string} storeVersion - The version of the store to check (for example scope version or .bit.map.json version)
Expand Down Expand Up @@ -49,9 +48,12 @@ export default function getMigrationVersions(
const sortedMigrationToRun = sortedMigrationVersionsToRun.map(migrationVersion => ({
[migrationVersion]: migratonManifest[migrationVersion]
}));
logger.debug(`Found the following versions which need migration ${sortedMigrationVersionsToRun.join(', ')}`);
const infoMessage = sortedMigrationVersionsToRun.length
? `Found the following versions that need migration ${sortedMigrationVersionsToRun.join(', ')}`
: 'none of the versions has migration to run';
logger.debug(infoMessage);
if (verbose) {
console.log(`Found the following versions which need migration ${sortedMigrationVersionsToRun.join(', ')}`); // eslint-disable-line no-console
console.log(infoMessage); // eslint-disable-line no-console
}
return sortedMigrationToRun;
}
22 changes: 11 additions & 11 deletions src/scope/migrations/scope-migrator.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
import R from 'ramda';
import { BitObject, BitRawObject, Ref } from '../objects';
import { BIT_VERSION } from '../../constants';
import getMigrationVersions, { MigrationDeclaration } from '../../migration/migration-helper';
import getMigrationVersions from '../../migration/migration-helper';
import type { MigrationDeclaration } from '../../migration/migration-helper';
import logger from '../../logger/logger';

export type ScopeMigrationResult = {
Expand All @@ -23,8 +24,7 @@ type VersionMigrationsDeclarations = {
};

type VersionMigrations = {
version: string,
migrations: VersionMigrationsDeclarations
[version: string]: VersionMigrationsDeclarations
};

let globalVerbose: boolean = false;
Expand All @@ -45,14 +45,14 @@ export default (async function migrate(
): Promise<ScopeMigrationResult> {
globalVerbose = verbose;
const migrations: VersionMigrations[] = getMigrationVersions(BIT_VERSION, scopeVersion, migratonManifest, verbose);
// We loop over the objects and not over the migration because we want to run the process even if there is no migrations at all
// The reason is that we might change the id calculation of an object without change the model itself.
// This will cause a change in the hash, so we need to delete the old object and persist the new one
// We also need to change all the refrences to this object (for example if we change the id of a version model)
R.forEach(_runAllMigrationsForObject(migrations), objects);

const result = { newObjects: {}, refsToRemove: [] };

if (R.isEmpty(migrations)) {
const noMigrationMsg = 'there are no migrations to run, leaving the scope as is with no changes';
logger.debug(noMigrationMsg);
if (verbose) console.log(noMigrationMsg); // eslint-disable-line
return result;
}
R.forEach(_runAllMigrationsForObject(migrations), objects);
R.forEach(_getRealObjectWithUpdatedRefs(result, refsIndex), objects);
result.newObjects = R.values(result.newObjects);
return result;
Expand Down Expand Up @@ -151,7 +151,7 @@ const _updateRefsForObjects = (index: { [string]: BitRawObject }, oldRef: string
const _getRealObjectWithUpdatedRefs = (
result: ScopeMigrationResultCache,
index: { [string]: BitRawObject }
): Funcion => (object: BitRawObject) => {
): Function => (object: BitRawObject) => {
// Make sure we got a rawObject (we might get a null object in case of corrupted object)
if (!object) {
return null;
Expand Down
17 changes: 16 additions & 1 deletion src/scope/objects/repository.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/** @flow */
import fs from 'fs';
import fs from 'fs-extra';
import path from 'path';
import BitObject from './object';
import BitRawObject from './raw-object';
Expand All @@ -11,6 +11,8 @@ import { Scope } from '../../scope';
import { Component, Symlink, ScopeMeta } from '../models';
import logger from '../../logger/logger';

const OBJECTS_BACKUP_DIR = `${OBJECTS_DIR}.bak`;

export default class Repository {
objects: BitObject[] = [];
_cache: { [string]: BitObject } = {};
Expand All @@ -36,6 +38,11 @@ export default class Repository {
return path.join(this.scope.getPath(), OBJECTS_DIR);
}

getBackupPath(dirName?: string): string {
const backupPath = path.join(this.scope.getPath(), OBJECTS_BACKUP_DIR);
return dirName ? path.join(backupPath, dirName) : backupPath;
}

getLicense(): Promise<string> {
return this.scope.scopeJson.getPopulatedLicense();
}
Expand Down Expand Up @@ -160,6 +167,14 @@ export default class Repository {
delete this._cache[ref.toString()];
}

backup(dirName?: string) {
const backupDir = this.getBackupPath(dirName);
const objectsDir = this.getPath();
logger.debug(`making a backup of all objects from ${objectsDir} to ${backupDir}`);
fs.emptyDirSync(backupDir);
fs.copySync(objectsDir, backupDir);
}

add(object: ?BitObject): Repository {
if (!object) return this;
// leave the following commented log message, it is very useful for debugging but too verbose when not needed.
Expand Down
11 changes: 7 additions & 4 deletions src/scope/scope.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ import { index } from '../search/indexer';
import loader from '../cli/loader';
import { MigrationResult } from '../migration/migration-helper';
import migratonManifest from './migrations/scope-migrator-manifest';
import migrate, { ScopeMigrationResult } from './migrations/scope-migrator';
import migrate from './migrations/scope-migrator';
import type { ScopeMigrationResult } from './migrations/scope-migrator';
import {
BEFORE_PERSISTING_PUT_ON_SCOPE,
BEFORE_IMPORT_PUT_ON_SCOPE,
Expand Down Expand Up @@ -179,15 +180,17 @@ export default class Scope {
* @returns {Object} - wether the process run and wether it successeded
* @memberof Consumer
*/
async migrate(verbose): MigrationResult {
async migrate(verbose: boolean): Promise<MigrationResult> {
logger.debug('running migration process for scope');
Analytics.addBreadCrumb('migrate', 'running migration process for scope');
if (verbose) console.log('running migration process for scope'); // eslint-disable-line
// We start to use this process after version 0.10.9, so we assume the scope is in the last production version
const scopeVersion = this.scopeJson.get('version') || '0.10.9';
if (semver.gte(scopeVersion, BIT_VERSION)) {
logger.debug('scope version is up to date');
Analytics.addBreadCrumb('migrate', 'scope version is up to date');
const upToDateMsg = 'scope version is up to date';
if (verbose) console.log(upToDateMsg); // eslint-disable-line
logger.debug(upToDateMsg);
Analytics.addBreadCrumb('migrate', upToDateMsg);
return {
run: false
};
Expand Down

0 comments on commit 935c9ff

Please sign in to comment.