-
Notifications
You must be signed in to change notification settings - Fork 21
/
setupDB.js
71 lines (61 loc) · 2.37 KB
/
setupDB.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
65
66
67
68
69
70
71
/**
* This file is the script that is used to set up the databases. To run it
* simply type 'node setupDB.js' in the console. You'll want to run it to
* initialize the database or to set up a test database. The databases are
* created in the 'db/' folder.
*
* created by: Eric Kryski March 4, 2011
* Reviwed by: Umer March 9, 2011
**/
var sys = require('sys'),
path = require('path');
var exec = require('child_process').exec;
var DB = 'db/CivicConnect.db';
var TEST_DB = 'db/CivicConnect-test.db';
// Callback function that prints out the stdout, stderr, and checks
// for an exec error.
function puts(error, stdout, stderr) {
console.log('stdout: ' + stdout);
console.log('stderr: ' + stderr);
if (error !== null) {
console.log('exec error: ' + error);
}
else{
console.log("*****************************************************************");
console.log("Your Database: is set up! You are good to go!!");
}
}
// This checks to see if the -t flag has been passed in. If a test db
// already exists we remove it and set up a new one with the sample data
// in the fixtures folder. If a test db didn't already exist we create one.
if (process.argv.length == 3) {
if (process.argv[2] == '-t')
{
var filename = path.join(process.cwd(), TEST_DB);
path.exists(filename, function(exists) {
if(!exists) {
exec("echo cd db; cd db; echo sqlite3 CivicConnect-test.db <fixtures/dbSetup.sql; sqlite3 CivicConnect-test.db <fixtures/dbSetup.sql;", puts);
}
else
{
exec("echo rm "+TEST_DB+"; rm "+TEST_DB+"; echo cd db; cd db; echo sqlite3 CivicConnect.db <fixtures/dbSetup.sql; sqlite3 CivicConnect.db <fixtures/dbSetup.sql;", puts);
}
});
}
}
// No arguments were passed so we'll set up the production db. If it
// already exists we make a backup and create a fresh copy with the
// sample data in the fixtures folder. If the db doesn't exist we
// create and populate it.
else {
var filename = path.join(process.cwd(), DB);
path.exists(filename, function(exists) {
if(!exists) {
exec("echo cd db; cd db; echo sqlite3 CivicConnect.db <fixtures/dbSetup.sql; sqlite3 CivicConnect.db <fixtures/dbSetup.sql;", puts);
}
else
{
exec("echo cd db; cd db; echo cp CivicConnect.db CivicConnect-back.db; cp CivicConnect.db CivicConnect-back.db; echo sqlite3 CivicConnect.db <fixtures/dbSetup.sql; sqlite3 CivicConnect.db <fixtures/dbSetup.sql;", puts);
}
});
}