Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add docker database support #61

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file added .env.list
Empty file.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:_
Expand Down
2 changes: 2 additions & 0 deletions client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
3 changes: 0 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,5 @@
"mocha": "^5.2.0",
"request": "^2.88.0",
"supertest": "^3.1.0"
},
"engines": {
"yarn": "1.18.0"
}
}
5 changes: 4 additions & 1 deletion scripts/initDb.js
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
28 changes: 18 additions & 10 deletions server/db/manage.js
Original file line number Diff line number Diff line change
Expand Up @@ -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));',
Expand All @@ -46,7 +54,7 @@ const tables = {
// Creates each table if needed
function createTables (pool) {
const queries = Object.values(tables);

return executeInSequence(pool, queries);
}

Expand Down
1 change: 1 addition & 0 deletions server/db/migrations.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 14 additions & 0 deletions setup-postgres.js
Original file line number Diff line number Diff line change
@@ -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}"`