forked from neurotech/canvas-data-warehouse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
64 lines (60 loc) · 2.14 KB
/
index.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
'use strict';
const config = require('./config');
const generate = require('./lib/schema/generate');
const manifest = require('./lib/manifest');
const tables = require('./lib/tables');
const cleanup = require('./lib/cleanup');
function checkSetup () {
cleanup();
// Generate and execute DDL against the PostgreSQL database if `db.setup` flag is false
if (typeof config.get('db.setup') === 'boolean' && config.get('db.setup') === false) {
console.log('Database schema has not been setup! Beginning schema generation and execution.\n');
// Generate DDL based on the Canvas Data API '/schema/latest' endpoint
generate()
.then(results => {
console.log(`DDL generation complete.`);
console.log(`${results.join(' | ')} \n`);
// Execute DDL against the PostgreSQL database.
const execute = require('./lib/schema/execute');
execute();
checkSeed();
}, error => {
console.error(error);
});
} else {
console.log('Database schema has been setup - skipping.');
checkSeed();
}
}
function checkSeed () {
// Seed PostgreSQL database if `db.seeded `flag is false
if (typeof config.get('db.seeded') === 'boolean' && config.get('db.seeded') === false) {
console.log(`Database has not been seeded! Beginning seed process.\n`);
// Fetch the list of resources from the Canvas Data API endpoint /account/self/file/sync
manifest.fetch()
.then(results => {
tables.init();
const diff = require('./lib/tables/diff');
const seed = require('./lib/tables/seed');
const manifestItems = results['files'];
const actions = diff(manifestItems);
const series = function (table) {
if (table) {
seed(table, function () {
return series(actions.shift());
});
} else {
config.set('db.seeded', true);
console.log(`\nDatabase seeding completed.`);
cleanup();
}
};
series(actions.shift());
}, error => {
console.error(error);
});
} else {
console.log('Database has been seeded - skipping.');
}
}
checkSetup();