From 41c21c74a1b45b4d428ca8906d6776da39f66ad3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Anders=20H=C3=B6ckersten?= Date: Tue, 13 Oct 2015 19:42:58 +0200 Subject: [PATCH] Use yasp db in production and yasp_test in test --- config.js | 2 +- package.json | 2 +- sql/create.sh | 7 +++ sql/{create.sql => create_tables.sql} | 10 +--- test/test.js | 66 +++++++++++++++++++-------- 5 files changed, 57 insertions(+), 30 deletions(-) create mode 100644 sql/create.sh rename sql/{create.sql => create_tables.sql} (93%) diff --git a/config.js b/config.js index 20fc34d49..2fee03c86 100644 --- a/config.js +++ b/config.js @@ -24,7 +24,7 @@ var defaults = { "PARSER_PORT": "5200", "PROXY_PORT": "5300", "WORK_PORT": "5400", - "POSTGRES_URL": "postgresql://postgres:postgres@localhost/postgres", + "POSTGRES_URL": "postgresql://yasp:yasp@localhost/yasp", "REDIS_URL": "redis://127.0.0.1:6379/0", "RETRIEVER_HOST": "localhost:5100", "PARSER_HOST": "localhost:5200", diff --git a/package.json b/package.json index 4534f769f..8e5db88a6 100644 --- a/package.json +++ b/package.json @@ -17,7 +17,7 @@ "maven": "mvn -q -f java_parser/pom.xml clean install -U", "build": "npm install && npm run maven && npm run webpack", "startup": "sudo pm2 startup ubuntu -u yasp", - "create": "sudo -u postgres psql -f sql/create.sql", + "create": "bash sql/create.sh", "deploy-core": "node deploy core", "deploy-parser": "git pull origin master && npm run maven && node deploy parser" }, diff --git a/sql/create.sh b/sql/create.sh new file mode 100644 index 000000000..8175314bc --- /dev/null +++ b/sql/create.sh @@ -0,0 +1,7 @@ +#!/bin/bash +sudo -u postgres dropdb yasp +sudo -u postgres dropuser yasp +sudo -u postgres createuser yasp +sudo -u postgres psql -c "ALTER USER yasp WITH PASSWORD 'yasp';" +sudo -u postgres createdb yasp --owner yasp +cat sql/create_tables.sql | sudo -u postgres psql -d postgresql://yasp:yasp@localhost/yasp -f - diff --git a/sql/create.sql b/sql/create_tables.sql similarity index 93% rename from sql/create.sql rename to sql/create_tables.sql index 24920138c..12a4469a6 100644 --- a/sql/create.sql +++ b/sql/create_tables.sql @@ -1,9 +1,3 @@ ---postgres user by default connects to postgres db -ALTER USER postgres WITH PASSWORD 'postgres'; ---default schema is public -drop schema public cascade; -create schema public; - CREATE TABLE matches ( match_id bigint PRIMARY KEY, match_seq_num bigint, @@ -145,7 +139,7 @@ CREATE TABLE player_ratings ( account_id bigint REFERENCES players(account_id) ON DELETE CASCADE, match_id bigint, solo_competitive_rank integer, - competitive_rank integer, + competitive_rank integer, time timestamp with time zone ); @@ -154,4 +148,4 @@ CREATE INDEX on matches(version); CREATE INDEX on players(full_history_time); CREATE INDEX on players(last_login); CREATE INDEX on players(cheese); -CREATE INDEX on player_ratings(account_id, time); \ No newline at end of file +CREATE INDEX on player_ratings(account_id, time); diff --git a/test/test.js b/test/test.js index 8923e6ede..203386c1c 100644 --- a/test/test.js +++ b/test/test.js @@ -1,7 +1,7 @@ var config = require('../config'); config.PORT = ""; //use service defaults config.MONGO_URL = "mongodb://localhost/test"; -config.POSTGRES_URL = "postgres://postgres:postgres@localhost/template1"; +config.POSTGRES_URL = "postgres://yasp_test:yasp_test@localhost/yasp_test"; config.REDIS_URL = "redis://localhost:6379/1"; config.SESSION_SECRET = "testsecretvalue"; config.NODE_ENV = "test"; @@ -27,11 +27,10 @@ var replay_dir = "./testfiles/"; var pg = require('pg'); var fs = require('fs'); var wait = 90000; -var db = require('../db'); -var app = require('../web'); -var queries = require('../queries'); -var insertMatch = queries.insertMatch; -var insertPlayer = queries.insertPlayer; +// these are loaded later, as the database needs to be created when these are required +var db; +var app; +var queries; //nock.disableNetConnect(); //nock.enableNetConnect(); //fake api response @@ -61,22 +60,46 @@ before(function(done) { this.timeout(wait); async.series([ function(cb) { - console.log('connecting to pg'); + console.log('removing old test database'); + pg.connect("postgres://postgres:postgres@localhost/postgres", function(err, client) { + if (err) { + return cb(err); + } + client.query('DROP ROLE IF EXISTS yasp_test;', function(err, result) { + console.log('cleaning database role for testing'); + }); + client.query('DROP DATABASE IF EXISTS yasp_test;', function(err, result) { + console.log('cleaning test database', config.POSTGRES_URL); + cb(err); + }); + }); + }, + function(cb) { + console.log('creating test database'); + pg.connect("postgres://postgres:postgres@localhost/postgres", function(err, client) { + if (err) { + return cb(err); + } + client.query('CREATE ROLE yasp_test WITH LOGIN PASSWORD \'yasp_test\';', function(err, result) { + console.log('creation of database role for testing'); + }); + client.query('CREATE DATABASE yasp_test OWNER yasp_test;', function(err, result) { + console.log('creation of test database', config.POSTGRES_URL); + cb(err); + }); + }); + }, + function(cb) { + console.log('connecting to test database and creating tables'); pg.connect(config.POSTGRES_URL, function(err, client) { if (err) { return cb(err); } - console.log('cleaning db'); - //clean the db - client.query('drop schema public cascade;create schema public;', function() { - //databaseCleaner.clean(client, function() { - console.log('cleaned %s', config.POSTGRES_URL); - //set up db - var query = fs.readFileSync("./sql/create.sql", "utf8"); - client.query(query, function(err, result) { - console.log('set up %s', config.POSTGRES_URL); - cb(err); - }); + // create tables + var query = fs.readFileSync("./sql/create_tables.sql", "utf8"); + client.query(query, function(err, result) { + console.log('set up %s', config.POSTGRES_URL); + cb(err); }); }); }, @@ -85,9 +108,12 @@ before(function(done) { redis.flushdb(cb); }, function(cb) { + db = require('../db'); + app = require('../web'); + queries = require('../queries'); console.log("loading matches"); async.mapSeries([require('./details_api.json').result], function(m, cb) { - insertMatch(db, redis, queue, m, { + queries.insertMatch(db, redis, queue, m, { type: "api" }, cb); }, cb); @@ -95,7 +121,7 @@ before(function(done) { function(cb) { console.log("loading players"); async.mapSeries(require('./summaries_api').response.players, function(p, cb) { - insertPlayer(db, p, cb); + queries.insertPlayer(db, p, cb); }, cb); }], function(err) { require('../workServer');