-
Notifications
You must be signed in to change notification settings - Fork 0
/
MigrationStore.js
47 lines (42 loc) · 1.49 KB
/
MigrationStore.js
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
47
const OpenrecordStore = require('./OpenrecordStore');
module.exports = function() {
return {
load: async callback => {
const store = await OpenrecordStore.getInstance();
async function createMigrationsTableIfNeeded() {
const result = await store.connection.raw(
"SELECT EXISTS (SELECT 1 FROM information_schema.tables WHERE table_name = 'migrations');"
);
const exists = result.rows[0].exists;
if (!exists) {
await store.connection.raw('CREATE TABLE migrations (version varchar PRIMARY KEY);');
}
}
await createMigrationsTableIfNeeded();
store.Model('migrations', function() {
this.attributes.version.primary = false;
});
await store.ready();
const Migrations = store.Model('migrations');
try {
const migrations = await Migrations;
const mapped = migrations.map(m => {
return { title: m.version, timestamp: Date.now() };
});
callback(null, { migrations: mapped });
} catch (error) {
callback(error);
}
},
save: async (set, callback) => {
const store = await OpenrecordStore.getInstance();
store.Model('migrations', () => {});
await store.ready();
const Migrations = store.Model('migrations');
const inserts = set.migrations.filter(m => !!m.timestamp).map(m => ({ version: m.title }));
await Migrations.deleteAll();
await Migrations.create(inserts);
callback();
}
};
};