db-init.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. var argv = require('minimist')(process.argv.slice(2));
  2. var async = require('async');
  3. var mysql = require('mysql');
  4. var fs = require('fs');
  5. var util = require('util');
  6. var conn = mysql.createConnection({
  7. host : argv.host || 'localhost',
  8. user : argv.user || 'root',
  9. password : argv.pass || 'pass',
  10. multipleStatements: true,
  11. });
  12. function quit(err) {
  13. if(err) console.log(err);
  14. conn.end();
  15. }
  16. function errcb(cb) {
  17. return function(err) {
  18. if(err) console.log(err);
  19. cb(err);
  20. }
  21. }
  22. function usedb(db) {
  23. return function(cb) {
  24. conn.query('use `'+db+'`;', errcb(cb))
  25. }
  26. }
  27. function createdb(db) {
  28. return function(cb) {
  29. console.log("creating database " + db);
  30. conn.query('CREATE DATABASE IF NOT EXISTS `'+db+'`;', errcb(cb))
  31. console.log(' done');
  32. }
  33. }
  34. function dropdb(db) {
  35. return function(cb) {
  36. console.log('dropping CES tables in database ' + db);
  37. conn.query(
  38. 'DROP TABLE IF EXISTS `components`; ' +
  39. 'DROP TABLE IF EXISTS `entities`; ' +
  40. 'DROP TABLE IF EXISTS `types`; '
  41. 'DROP TABLE IF EXISTS `user_claims`; '
  42. 'DROP TABLE IF EXISTS `users`; '
  43. , errcb(cb))
  44. console.log(' done');
  45. }
  46. }
  47. function initdb() {
  48. return function(cb) {
  49. console.log('initializing database...');
  50. var schema = fs.readFileSync('./schema.sql', 'utf-8');
  51. conn.query(schema, errcb(cb));
  52. console.log(' done');
  53. }
  54. }
  55. function intType(row) {
  56. if(row.is_double) return 'double';
  57. if(row.is_int) return 'int';
  58. if(row.is_string) return 'string';
  59. if(row.is_date) return 'date';
  60. return 'unknown';
  61. }
  62. function dumpData(filename) {
  63. return function(cb) {
  64. console.log('dumping database...');
  65. var data = {};
  66. async.series([
  67. function(acb) {
  68. // dump types
  69. conn.query('SELECT * from types;', function(err, rows) {
  70. if(err) return acb(err);
  71. data.types = {};
  72. for(var i = 0; i < rows.length; i++) {
  73. var r = rows[i];
  74. data.types[r.typeID] = {
  75. typeID: r.typeID,
  76. name: r.name,
  77. internalType: intType(r),
  78. externalType: r.externalType,
  79. };
  80. }
  81. acb();
  82. });
  83. },
  84. function(acb) {
  85. // dump types
  86. conn.query('SELECT * from components;', function(err, rows) {
  87. if(err) return acb(err);
  88. data.components = {};
  89. function extractData(row, typeid) {
  90. var t = data.types[typeid].internalType;
  91. if(t == 'double') return row.data_double;
  92. if(t == 'int') return row.data_int;
  93. if(t == 'string') return row.data_string;
  94. if(t == 'date') return row.data_date;
  95. }
  96. for(var i = 0; i < rows.length; i++) {
  97. var r = rows[i];
  98. data.components[r.typeID] = {
  99. eid: r.eid,
  100. typeID: r.typeID,
  101. data: extractData(r, r.typeID),
  102. };
  103. }
  104. acb();
  105. });
  106. },
  107. ],
  108. function(err) {
  109. if(err) return cb(err);
  110. fs.writeFileSync(filename, JSON.stringify(data, false, 2));
  111. console.log(' done');
  112. cb();
  113. });
  114. }
  115. }
  116. function loadData(filename) {
  117. return function(cb) {
  118. console.log('loading database...');
  119. var data = JSON.parse(fs.readFileSync(filename, 'utf-8'));
  120. async.series([
  121. function(acb) {
  122. // load types
  123. async.mapSeries(data.types, function(type, bcb) {
  124. var d = [
  125. type.typeID,
  126. type.name,
  127. type.internalType == 'double',
  128. type.internalType == 'int',
  129. type.internalType == 'string',
  130. type.internalType == 'date',
  131. type.externalType
  132. ];
  133. var q = 'REPLACE INTO types (`typeID`, `name`, `is_double`, `is_int`, `is_string`, `is_date`, `externalType`) VALUES (?,?,?,?,?,?,?);'
  134. conn.query(q, d, bcb);
  135. }, acb);
  136. },
  137. function(acb) {
  138. // load components
  139. async.mapSeries(data.components, function(comp, bcb) {
  140. var d = [
  141. comp.eid,
  142. comp.typeID,
  143. comp.data,
  144. ];
  145. var type = data.types[comp.typeID]
  146. var q = 'REPLACE INTO components (`eid`, `typeID`, `data_'+type.internalType+'`) VALUES (?,?,?);'
  147. conn.query(q, d, bcb);
  148. }, acb);
  149. },
  150. ],
  151. function(err) {
  152. console.log(' done');
  153. cb()
  154. });
  155. }
  156. }
  157. if(argv.create) {
  158. async.series([
  159. createdb(argv.create),
  160. usedb(argv.create),
  161. initdb(),
  162. ], quit);
  163. }
  164. if(argv.reset) {
  165. async.series([
  166. dropdb(argv.reset),
  167. createdb(argv.reset),
  168. usedb(argv.reset),
  169. initdb(),
  170. ], quit);
  171. }
  172. if(argv.dump) {
  173. async.series([
  174. usedb(argv.dump),
  175. dumpData(argv._[0]),
  176. ], quit);
  177. }
  178. if(argv.load) {
  179. async.series([
  180. usedb(argv.load),
  181. loadData(argv._[0]),
  182. ], quit);
  183. }