diff --git a/.env.list b/.env.list new file mode 100644 index 00000000..e69de29b diff --git a/README.md b/README.md index 2c51281d..8ec186b1 100644 --- a/README.md +++ b/README.md @@ -4,6 +4,13 @@ * Requires postgresql >=9.2 to be installed and running * `yarn setup` installs dependencies, configures database, runs the tests, and finally runs the app +#### Setup with docker +* Requires Docker to be installed and running +* Run `docker run --rm --name pg-docker --env-file .env.list -d -p 5432:5432 -v $HOME/docker/volumes/postgres:/var/lib/postgresql/data postgres` +* Run `QUEER_DOCKER=ofc npm run db:init` + * Note: the 'QUEER_DOCKER' environment variable can have any value, we only check if it exists + * Note: You can use "npm run db:init" or "yarn run db:init" + #### To run locally _Development:_ diff --git a/client/package.json b/client/package.json index 5d8a8c4e..6d62e46a 100644 --- a/client/package.json +++ b/client/package.json @@ -42,7 +42,9 @@ "webpack-core": "^0.6.9" }, "devDependencies": { + "babel-core": "^6.26.3", "jest-dom": "^3.1.3", + "prop-types": "^15.7.2", "react-test-renderer": "^16.8.3", "react-testing-library": "^6.0.3", "react-toolbox-themr": "^1.0.2" diff --git a/package.json b/package.json index 132b5f2c..9010e104 100644 --- a/package.json +++ b/package.json @@ -25,8 +25,5 @@ "mocha": "^5.2.0", "request": "^2.88.0", "supertest": "^3.1.0" - }, - "engines": { - "yarn": "1.18.0" } } diff --git a/scripts/initDb.js b/scripts/initDb.js index d219cd03..c3fc437b 100755 --- a/scripts/initDb.js +++ b/scripts/initDb.js @@ -24,7 +24,10 @@ function initialize(config) { return createTables(pool) .then(() => console.log('Running Migrations...')) - .then(() => addActionToRequested(pool)) .then(() => addReportTable(pool)) + .then(() => addActionToRequested(pool)) + .then(() => console.log('added action to requested')) + .then(() => addReportTable(pool)) + .then(() => console.log("Added report table")) .then(() => console.log('Running seeds...')) .then(() => seedActions(pool)) diff --git a/server/db/manage.js b/server/db/manage.js index ab39dbbe..4ee0e032 100644 --- a/server/db/manage.js +++ b/server/db/manage.js @@ -3,39 +3,47 @@ const { executeInSequence } = require('./util'); const { execSync } = require('child_process'); -const psql = query => `psql -h localhost -p 5432 -U postgres -d postgres -tAc "${query}"`; +let psql_shell_command = query => `psql -h localhost -p 5432 -U postgres -d postgres -tAc "${query}"`; + +if (process.env['QUEER_DOCKER']) { + psql_shell_command = query => `docker exec pg-docker psql -h localhost -p 5432 -U postgres -d postgres -tAc "${query}"`; +} + + const exec = command => execSync(command).toString().trim(); function createUser (user, pass) { const doesUserExist = `SELECT 'exists' FROM pg_roles WHERE rolname='${user}'`; - if (exec(psql(doesUserExist)) !== 'exists') { + if (exec(psql_shell_command(doesUserExist)) !== 'exists') { console.log(`Creating user ${user}...`); - console.log(exec(psql(`CREATE USER ${user} WITH PASSWORD '${pass}'`))); - } + console.log(exec(psql_shell_command(`CREATE USER ${user} WITH PASSWORD '${pass}'`))); + } } function deleteUser (user) { console.log(`Deleting user ${user}...`); - console.log(exec(psql(`DROP USER IF EXISTS ${user}`))); + console.log(exec(psql_shell_command(`DROP USER IF EXISTS ${user}`))); } function createDb (name) { const doesDbExist = `SELECT 'exists' FROM pg_database WHERE datname='${name}'`; - if (exec(psql(doesDbExist)) !== 'exists') { + if (exec(psql_shell_command(doesDbExist)) !== 'exists') { console.log(`Creating database ${name}...`); - console.log(exec(psql(`CREATE DATABASE ${name}`))); + console.log(exec(psql_shell_command(`CREATE DATABASE ${name}`))); + } else { + console.log("Db already exists") } } function deleteDb (name) { console.log(`Deleting database ${name}...`); - console.log(exec(psql(`DROP DATABASE IF EXISTS ${name}`))); + console.log(exec(psql_shell_command(`DROP DATABASE IF EXISTS ${name}`))); } const tables = { - 'term': 'CREATE TABLE IF NOT EXISTS term(term VARCHAR (60) PRIMARY KEY);', + 'term': `CREATE TABLE IF NOT EXISTS term(term VARCHAR (60) PRIMARY KEY);`, 'synonym': 'CREATE TABLE IF NOT EXISTS synonym( term VARCHAR (60) NOT NULL, sort_as VARCHAR (60) NOT NULL, PRIMARY KEY (term, sort_as));', 'author': 'CREATE TABLE IF NOT EXISTS author( author_id SERIAL PRIMARY KEY, name VARCHAR (60), identity VARCHAR (3000));', 'action': 'CREATE TABLE IF NOT EXISTS action(id SERIAL PRIMARY KEY, title VARCHAR(20));', @@ -46,7 +54,7 @@ const tables = { // Creates each table if needed function createTables (pool) { const queries = Object.values(tables); - + return executeInSequence(pool, queries); } diff --git a/server/db/migrations.js b/server/db/migrations.js index b60536ea..8ec55662 100644 --- a/server/db/migrations.js +++ b/server/db/migrations.js @@ -3,6 +3,7 @@ const addActionToRequested = db => db.query( `ALTER TABLE requested ADD COLUMN action INTEGER NOT NULL REFERENCES action(id)` ).catch(e => { + console.log('caught an error e ' + e) // ignore the error when we have already run the migration successfully if (e.message !== 'column "action" of relation "requested" already exists') { throw e diff --git a/setup-postgres.js b/setup-postgres.js new file mode 100644 index 00000000..eeef5b6e --- /dev/null +++ b/setup-postgres.js @@ -0,0 +1,14 @@ +function createUser (user, pass) { + const doesUserExist = `SELECT 'exists' FROM pg_roles WHERE rolname='${user}'`; + + if (exec(psql(doesUserExist)) !== 'exists') { + console.log(`Creating user ${user}...`); + console.log(exec(psql(`CREATE USER ${user} WITH PASSWORD '${pass}'`))); + } else { + console.log(`user exists ${user}`) + } + } + +const docker_prequery = 'docker exec pg-docker' +const psql = query => `${docker_prequery} psql -h localhost -p 5432 -U postgres -d postgres -tAc "${query}"` +