Merge pull request #1 from shackbarth/keith1
[xtuple] / scripts / lib / util / inspect_database.js
1 /*jshint node:true, indent:2, curly:false, eqeqeq:true, immed:true, latedef:true, newcap:true, noarg:true,
2 regexp:true, undef:true, strict:true, trailing:true, white:true */
3 /*global _:true */
4
5 (function () {
6   "use strict";
7
8   var path = require('path'),
9     dataSource = require('../../../node-datasource/lib/ext/datasource').dataSource,
10     defaultExtensions = require("./default_extensions").extensions;
11
12   var pathFromExtension = function (name, location) {
13     if (location === '/core-extensions') {
14       return path.join(__dirname, "../../../enyo-client/extensions/source/", name);
15     } else if (location === '/xtuple-extensions') {
16       return path.join(__dirname, "../../../../xtuple-extensions/source", name);
17     } else if (location === '/private-extensions') {
18       return path.join(__dirname, "../../../../private-extensions/source", name);
19     } else if (location === 'npm') {
20       return path.join(__dirname, "../../../node_modules", name);
21     }
22   };
23
24   //
25   // Looks in a database to see which extensions are registered, and
26   // tacks onto that list the core directories.
27   //
28   var inspectMobilizedDatabase = function (creds, done) {
29     var extSql = "SELECT * FROM xt.ext ORDER BY ext_load_order";
30     dataSource.query(extSql, creds, function (err, res) {
31       if (err) {
32         return done(err);
33       }
34
35       var paths = _.map(_.compact(res.rows), function (row) {
36         return pathFromExtension(row.ext_name, row.ext_location);
37       });
38
39       paths.unshift(path.join(__dirname, "../../../enyo-client")); // core path
40       paths.unshift(path.join(__dirname, "../../../lib/orm")); // lib path
41       paths.unshift(path.join(__dirname, "../../../foundation-database")); // foundation path
42       done(null, _.compact(paths));
43     });
44   };
45
46   var inspectUnmobilizedDatabase = function (creds, done) {
47     var extSql = "select * from public.pkghead where pkghead_name in ('xtmfg', 'xwd');",
48       editionMap = {
49         xtmfg: ["inventory", "manufacturing"],
50         xwd: ["inventory", "distribution"]
51       };
52     dataSource.query(extSql, creds, function (err, res) {
53       if (err) {
54         return done(err);
55       }
56       var extensions = _.unique(_.flatten(_.map(res.rows, function (row) {
57         return _.map(editionMap[row.pkghead_name], function (ext) {
58           return path.join(__dirname, "../../../../private-extensions/source", ext);
59         });
60       })));
61       done(err, defaultExtensions.concat(extensions));
62     });
63   };
64
65   var inspectDatabaseExtensions = function (creds, done) {
66     var isMobilizedSql = "select * from information_schema.tables where table_schema = 'xt' and table_name = 'ext';";
67
68     dataSource.query(isMobilizedSql, creds, function (err, res) {
69       if (res.rowCount === 0) {
70         inspectUnmobilizedDatabase(creds, done);
71       } else {
72         inspectMobilizedDatabase(creds, done);
73       }
74     });
75   };
76
77   exports.inspectDatabaseExtensions = inspectDatabaseExtensions;
78
79 }());