Skip to content

Commit

Permalink
separated generateDeclaration function from initDb, and moved all db …
Browse files Browse the repository at this point in the history
…related tools to database/management folder
  • Loading branch information
bleucitron committed Jan 21, 2016
1 parent a2fcd6e commit b142f9c
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 71 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ This will also create a `latest.sql` backup file of the db in the `backup` folde
If you run a service without an initialized db, you need to

```
node tools/init-db.js
node database/management/init-db.js
```

* Backups and restore : TO BE REWRITTEN
Expand Down
2 changes: 1 addition & 1 deletion compose-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ broker:
- NODE_ENV=test
volumes:
- ./:/pheromon
#log_driver: "none"
log_driver: "none"

db:
image: postgres:9.4
Expand Down
File renamed without changes.
42 changes: 42 additions & 0 deletions database/management/generateDeclarations.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
'use strict';

require('es6-shim');

var fs = require('fs');
var path = require('path');
var generateSqlDefinition = require('sql-generate');

var PRIVATE = require('../../PRIVATE/secret.json');

var isTest = process.env.NODE_ENV === 'test';

var pg_user = isTest ? process.env.POSTGRES_USER : PRIVATE.pg_user;
var pg_pwd = isTest ? process.env.POSTGRES_PASSWORD : PRIVATE.pg_pwd;
var pg_addr = isTest ? process.env.DB_PORT_5432_TCP_ADDR : 'localhost';
var pg_dbName = isTest ? 'postgres' : PRIVATE.db_name;

var conString = [
'postgres://',
pg_user,
':',
pg_pwd,
'@',
pg_addr,
':5432/',
pg_dbName
].join('');

console.log('CONSTRING', conString);

module.exports = function() {
return new Promise(function(resolve, reject) {
generateSqlDefinition({ dsn: conString, omitComments: true }, function(err, definitions) {
if (err) {
console.error(err);
reject(err);
}
fs.writeFileSync(path.join(__dirname, './declarations.js'), definitions.buffer);
resolve();
});
});
};
32 changes: 5 additions & 27 deletions tools/init-db.js → database/management/init-db.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,34 +4,12 @@

require('es6-shim');

var fs = require('fs');
var path = require('path');

var generateSqlDefinition = require('sql-generate');

var PRIVATE = require('../PRIVATE/secret.json');

// when ready, drop and create tables
var databaseClientP = require('../database/management/databaseClientP');
var dropAllTables = require('../database/management/dropAllTables.js');
var createTables = require('../database/management/createTables.js');
var databaseClientP = require('./databaseClientP');
var dropAllTables = require('./dropAllTables.js');
var createTables = require('./createTables.js');

var conString = 'postgres://'+ PRIVATE.pg_user + ':' + PRIVATE.pg_pwd + '@localhost:5432/' + PRIVATE.db_name;

console.log('Init-db connection string', conString);

function generateDefinitions() {
return new Promise(function(resolve, reject) {
generateSqlDefinition({ dsn: conString, omitComments: true }, function(err, definitions) {
if (err) {
console.error(err);
reject(err);
}
fs.writeFileSync(path.join(__dirname, '../database/management/declarations.js'), definitions.buffer);
resolve();
});
});
}
var generateDeclarations = require('./generateDeclarations.js');

(function tryRebuildDatabase(){
console.log('Trying to rebuild database...');
Expand All @@ -51,7 +29,7 @@ function generateDefinitions() {
})
.then(function(){

generateDefinitions()
generateDeclarations()
.then(function(){
console.log('Dropped and created the tables.');
process.exit();
Expand Down
File renamed without changes.
45 changes: 3 additions & 42 deletions tests/init-db.js
Original file line number Diff line number Diff line change
@@ -1,50 +1,11 @@
'use strict';

var fs = require('fs');
var path = require('path');

var generateSqlDefinition = require('sql-generate');

// when ready, drop and create tables
var databaseClientP = require('../database/management/databaseClientP');

var dropAllTables = require('../database/management/dropAllTables.js');
var createTables = require('../database/management/createTables.js');

var PRIVATE = require('../PRIVATE/secret.json');

var isTest = process.env.NODE_ENV === 'test';

var pg_user = isTest ? process.env.POSTGRES_USER : PRIVATE.pg_user;
var pg_pwd = isTest ? process.env.POSTGRES_PASSWORD : PRIVATE.pg_pwd;
var pg_addr = isTest ? process.env.DB_PORT_5432_TCP_ADDR : 'localhost';
var pg_dbName = isTest ? 'postgres' : PRIVATE.db_name;

var conString = [
'postgres://',
pg_user,
':',
pg_pwd,
'@',
pg_addr,
':5432/',
pg_dbName
].join('');

console.log('conString for Test init-db', conString);

function generateDefinitions() {
return new Promise(function(resolve, reject) {
generateSqlDefinition({ dsn: conString, omitComments: true }, function(err, definitions) {
if (err) {
console.error(err);
reject(err);
}
fs.writeFileSync(path.join(__dirname, '../database/management/declarations.js'), definitions.buffer);
resolve();
});
});
}
var generateDeclarations = require('../database/management/generateDeclarations.js');

module.exports = function(){

Expand All @@ -71,7 +32,7 @@ module.exports = function(){
.then(function(){
if (!process.env.BACKUP) {
console.log('no backup file');
generateDefinitions()
generateDeclarations()
.then(function(){
console.log('Dropped and created the tables.');

Expand All @@ -84,7 +45,7 @@ module.exports = function(){
});
}
else {
generateDefinitions()
generateDeclarations()
.then(function(){
console.log('definitions generated');

Expand Down

0 comments on commit b142f9c

Please sign in to comment.