Skip to content

Commit

Permalink
fix: auto re-init data.json when schema updated (microsoft#2316)
Browse files Browse the repository at this point in the history
* auto update data.json when schema updated

* fix lint

* add a version number and use generic migration to do re-init
  • Loading branch information
boydc2014 authored Mar 20, 2020
1 parent bd6567c commit f6f0081
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 46 deletions.
26 changes: 0 additions & 26 deletions Composer/packages/server/src/store/abh-template.json

This file was deleted.

1 change: 1 addition & 0 deletions Composer/packages/server/src/store/data.template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import settings from '../settings';

export default {
version: 1,
storageConnections: [
{
id: 'default',
Expand Down
7 changes: 7 additions & 0 deletions Composer/packages/server/src/store/migrations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import set from 'lodash/set';
import log from '../logger';
import settings from '../settings';

import initData from './data.template';

interface Migration {
/**
* Migration label. Will be printed to the console in debug.
Expand All @@ -33,6 +35,11 @@ const migrations: Migration[] = [
condition: data => get(data, 'storageConnections.0.defaultPath') !== settings.botsFolder,
run: data => set(data, 'storageConnections[0].defaultPath', settings.botsFolder),
},
{
name: 'Re-init when version update',
condition: data => !data.version || data.version != initData.version,
run: data => initData,
},
];

export function runMigrations(initialData: any): any {
Expand Down
30 changes: 10 additions & 20 deletions Composer/packages/server/src/store/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,30 +7,20 @@ import path from 'path';
import log from '../logger';
import settings from '../settings';

import localInitData from './data.template';
import abhInitData from './abh-template.json';
import initData from './data.template';
import { runMigrations } from './migrations';

const isHostedInAzure = !!process.env.WEBSITE_NODE_DEFAULT_VERSION;
const dataStorePath =
isHostedInAzure && process.env.HOME ? path.resolve(process.env.HOME, './site/data.json') : settings.appDataPath;
const dataStorePath = settings.appDataPath;

let initData = isHostedInAzure ? abhInitData : localInitData;

if (fs.existsSync(dataStorePath)) {
const userData = JSON.parse(fs.readFileSync(dataStorePath, 'utf-8'));
initData = runMigrations(userData);
} else {
log('Database not found. Seeding data.json with: %O', initData);
}

fs.writeFileSync(dataStorePath, JSON.stringify(initData, null, 2) + '\n');
const migrateStore = () => {
if (fs.existsSync(dataStorePath)) {
const userData = JSON.parse(fs.readFileSync(dataStorePath, 'utf-8'));
const migratedData = runMigrations(userData);
fs.writeFileSync(dataStorePath, JSON.stringify(migratedData, null, 2) + '\n');
}
};

if (isHostedInAzure) {
// for some very odd reason on Azure webapp, fs.readFileSync after writeFileSync doesn't
// always find the file, add one more io to kick the virtual file system
fs.appendFileSync(dataStorePath, ' ');
}
migrateStore();

interface KVStore {
get(key: string, defaultValue?: any): any;
Expand Down

0 comments on commit f6f0081

Please sign in to comment.