Welcome to mirror list, hosted at ThFree Co, Russian Federation.

Migration.ts « src - github.com/jsxc/jsxc.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 9dfded2e91bcccf7f66cea7cdb8fc2ef64ca8081 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import Storage from './Storage';
import Log from '@util/Log';

const VERSION_KEY = 'version';

export default class Migration {
   public static run(currentVersion: string, storage: Storage) {
      new Migration(currentVersion, storage);
   }

   private keys: string[];

   private constructor(currentVersion: string, private storage: Storage) {
      let lastVersion = storage.getItem(VERSION_KEY);

      if (lastVersion !== currentVersion) {
         Log.debug('Apply migrations');

         this.keys = Object.keys(storage.getBackend());

         this.runV3Migration();

         storage.setItem(VERSION_KEY, currentVersion);
      }
   }

   private runV3Migration() {
      let backend = this.storage.getBackend();

      if (!backend.getItem('jsxc:version')) {
         return;
      }

      Log.debug('Run migration for 3.x');

      this.keys.forEach(key => {
         let matches = key.match(/^jsxc:([^:]+):key$/);

         if (matches) {
            let newKey = this.storage.generateKey(matches[1], 'plugin', 'otr', 'key');

            this.storage.setItem(newKey, backend.getItem(key));
         }
      });
   }
}