|
1 | 1 | 'use strict'; |
2 | 2 | /* eslint-disable no-console */ |
3 | 3 | const pg = require('pg'); |
| 4 | +const Promise = require('bluebird'); |
4 | 5 | const cleaner = require('postgres-cleaner'); |
5 | 6 |
|
6 | 7 | const dbArgument = () => (process.argv[3] === 'dev') |
7 | 8 | ? 'dev_db' |
8 | 9 | : 'test_db'; |
9 | 10 |
|
| 11 | +const dbUser = () => (process.argv[3] === 'dev') |
| 12 | + ? 'dev_user' |
| 13 | + : 'test_user'; |
| 14 | + |
10 | 15 | function recreateDatabase() { |
11 | | - var connectionString = 'postgres://postgres@localhost/postgres'; |
| 16 | + var connectionString = |
| 17 | + `postgres://${dbUser()}:ecp1950@ecp_${dbArgument()}/${dbArgument()}`; |
12 | 18 |
|
13 | 19 | pg.connect(connectionString) |
14 | | - .then( connection => |
15 | | - connection.query(`drop database ${dbArgument()}`).then( () => connection) |
16 | | - ) |
17 | | - .then( connection => |
18 | | - connection.query(`create database ${dbArgument()}`) |
19 | | - .then( () => process.exit(0)) |
20 | | - ) |
| 20 | + .then( connection => { |
| 21 | + return connection.query( |
| 22 | + 'SELECT table_name FROM information_schema.tables ' + |
| 23 | + 'WHERE table_schema = \'public\'' |
| 24 | + ) |
| 25 | + .then( tables => { |
| 26 | + if (tables.rows.length === 0) return; |
| 27 | + |
| 28 | + let promises = tables.rows.map(table => |
| 29 | + connection.query(`DROP TABLE "${table.table_name}" CASCADE`) |
| 30 | + ); |
| 31 | + |
| 32 | + return Promise.all(promises); |
| 33 | + }); |
| 34 | + }) |
| 35 | + .then( () => process.exit(0)) |
21 | 36 | .catch( err => { |
22 | 37 | console.log(err); |
23 | 38 | process.exit(1); |
24 | 39 | }); |
25 | 40 | } |
26 | 41 |
|
27 | 42 | function cleanDatabase() { |
28 | | - var connectionString = `postgres://postgres@localhost/${dbArgument()}`; |
| 43 | + var connectionString = |
| 44 | + `postgres://${dbUser()}:ecp1950@ecp_${dbArgument()}/${dbArgument()}`; |
29 | 45 |
|
30 | 46 | pg.connect(connectionString, (err, connection) => { |
31 | 47 | if (err) throw err; |
|
0 commit comments