Commit cf5090de authored by Andre Natal's avatar Andre Natal
Browse files

Replace psql by mysql

parent c6d05d86
dist: trusty
sudo: required
language: node_js
node_js:
- "7"
services:
- postgresql
addons:
postgresql: "9.5"
before_install:
- mysql -e "SET PASSWORD FOR 'root'@'localhost' = PASSWORD('root');"
before_script:
- npm install -g gulp-cli
script:
......
{
"PROD": false,
"DB_ROOT_USER": "",
"DB_ROOT_PASS": "",
"PGUSER": "",
"PGPASS": "",
"PGNAME": ""
"DB_ROOT_USER": "root",
"DB_ROOT_PASS": "root",
"MSQLUSER": "voicecommons",
"MSQLPASS": "voicecommons",
"MSQLDBNAME": "voiceweb",
"MSQLHOST": "localhost",
"MYSQLPORT": "3306"
}
import Postgres from './db/postgres';
import Mysql from './db/mysql';
import UserDB from './db/user-db';
const Promise = require('bluebird');
export default class DB {
pg: Postgres;
mysql: Mysql;
user: UserDB;
constructor() {
this.pg = new Postgres();
this.user = new UserDB(this.pg);
this.mysql = new Mysql();
this.user = new UserDB(this.mysql);
}
createAll() {
......@@ -19,6 +19,6 @@ export default class DB {
}
end() {
this.pg.end();
this.mysql.end();
}
}
import Postgres from './postgres';
import promisify from '../../promisify';
import Mysql from './mysql';
const Promise = require('bluebird');
/**
......@@ -9,7 +7,7 @@ const Promise = require('bluebird');
export default class BaseDB {
constructor(
public pg: Postgres,
public mysql: Mysql,
public name: string,
public columns: Object
) {}
......@@ -18,8 +16,15 @@ export default class BaseDB {
* Query database, but using promises.
*/
q(text: string, values?: any[]) {
return promisify(this.pg, this.pg.query, [text, values]);
}
return new Promise((resolve, reject) => {
this.mysql.query(text,null,(error, results) => {
error ? reject(error) : resolve(results);
});
})
};
/**
* Create the postgres table this object represents.
......
const mysql = require('mysql');
const config = require('../../../../config.json');
type MysqlOptions = {
user: string;
database: string;
password: string;
host: string;
port: number;
max: number;
idleTimeoutMillis: number;
};
// Default configuration values, notice we dont have password.
const DEFAULTS = {
user: 'voiceweb',
database: 'voiceweb',
password: '',
host: 'localhost',
port: 3306,
max: 10,
idleTimeoutMillis: 30000
};
export default class Mysql {
pool: any;
constructor(options?: MysqlOptions) {
options = options || Object.create(null);
// For configuring, use the following order of priority:
// 1. passed in options
// 2. options in config.json
// 3. hard coded DEFAULTS
var myConfig = {
user: options.user || config.MSQLUSER || DEFAULTS.user,
database: options.database || config.MYSQLDB || DEFAULTS.database,
password: options.password || config.MSQLPASS || DEFAULTS.password,
host: options.host || config.MSQLHOST || DEFAULTS.host,
port: options.port || config.MYSQLPORT || DEFAULTS.port,
max: options.max || DEFAULTS.max,
idleTimeoutMillis: options.idleTimeoutMillis ||
DEFAULTS.idleTimeoutMillis,
};
this.pool = mysql.createPool({
connectionLimit : 20,
host : myConfig.host,
user : myConfig.user,
password : myConfig.password,
database : myConfig.database
});
this.pool.on('error', this.handleIdleError.bind(this));
}
private handleIdleError(err: ErrorEvent) {
console.error('idle client error', err.message);
}
query(text: string, values: any[], callback: Function) {
this.pool.query(text, function (error, results, fields) {
error ? callback(error.message, null) : callback(null, results);
});
}
connect(callback: Function) {
return this.pool.connect(callback);
}
end() {
this.pool.end();
}
}
import Postgres from './postgres';
import Mysql from './mysql';
import BaseDB from './base-db';
const NAME = 'users';
const COLUMNS = {
'id': 'text primary key',
'email': 'text unique',
'birthyear': 'smallserial'
'id': 'BIGINT NOT NULL AUTO_INCREMENT primary key',
'email': 'varchar(200) unique',
'birthyear': 'smallint',
'accent': 'varchar(100)',
'gender': 'varchar(1)',
'name': 'varchar(50)',
'userid': 'bigint'
};
export default class UserDB extends BaseDB {
constructor(pg: Postgres) {
constructor(pg: Mysql) {
super(pg, NAME, COLUMNS);
}
}
const ff = require('ff');
const config = require('../config.json');
const Postgres = require('../server/js/lib/db/postgres').default;
const Mysql = require('../server/js/lib/db/mysql').default;
const DB = require('../server/js/lib/db').default;
const DEFAULT = 'voiceweb';
const USERNAME = config.PGUSER || DEFAULT;
const PASSWORD = config.PGPASS || DEFAULT;
const DBNAME = config.PGNAME || DEFAULT;
const USERNAME = config.MSQLUSER || DEFAULT;
const PASSWORD = config.MSQLPASS || DEFAULT;
const DBNAME = config.MSQLDBNAME || DEFAULT;
/**
* Create the database.
*/
function run(callback) {
// Make sure we have a superuser username and password
let user = config.DB_ROOT_USER || 'postgres';
let user = config.DB_ROOT_USER || 'root';
let pass = config.DB_ROOT_PASS || '';
if (!user) {
......@@ -23,61 +23,76 @@ function run(callback) {
}
// Log in as superuser to create user and tables for voice-web.
let pg = new Postgres({
let mysql = new Mysql({
user: user,
password: pass,
database: 'template1' // Default db for postgres.
database: 'mysql'
});
let db; // db object, for when we connect through the server api.
let f = ff(
() => {
pg.query('SELECT usename FROM pg_shadow ' +
'WHERE usename = \'' + USERNAME + '\';', f());
mysql.query('SELECT User FROM mysql.user ' +
'WHERE user = \'' + USERNAME + '\';', null, f.slot());
},
result => {
if (result && result.rowCount < 1) {
pg.query(`CREATE USER ${USERNAME} WITH PASSWORD '${PASSWORD}';`, f());
if (result && result.length < 1) {
mysql.query(`CREATE USER '${USERNAME}'@'localhost' IDENTIFIED BY '${PASSWORD}';`, null, f.slot());
} else {
f.fail('User already exists');
}
},
() => {
pg.query('SELECT datname FROM pg_catalog.pg_database ' +
'WHERE datname = \'' + DBNAME + '\';', f());
mysql.query('show databases like ' +
'\'' + DBNAME + '\';', null, f.slot());
},
(result) => {
if (result && result.rowCount < 1) {
pg.query(`CREATE DATABASE ${DBNAME} ENCODING UTF8;`, f());
if (result && result.length < 1) {
mysql.query(`CREATE DATABASE ${DBNAME} CHARACTER SET utf8 COLLATE utf8_general_ci;`, null, f.slot());
} else {
f.fail('Database already exists');
}
},
(result) => {
mysql.query(`GRANT ALL PRIVILEGES ON ${DBNAME}.* TO '${USERNAME}'@'localhost';`, null, f.slot());
},
(result) => {
// Now let's use the user we just created to create the schema.
// To do so, we will need to as the server (through db object).
pg.end();
pg = null;
mysql.end();
mysql = null;
db = new DB();
db.createAll().then(f.slotPlain())
db.createAll().then( () => {
db.end();
f.slot();
})
// Forward promise errors to ff.
.catch(f.fail.bind(f));
.catch((err) => {
db.end();
f.fail();
console.error('database create error', err);
process.exitCode = 1;
callback(err);
});
})
.onComplete((err, result) => {
if (mysql) {
mysql.end();
mysql = null;
}
if (err) {
console.error('database create error', err);
process.exitCode = 1;
}
// Clean up any open connections.
db && db.end();
pg && pg.end();
callback(err);
});
})
}
// Allow running as a script and module.
......
const ff = require('ff');
const config = require('../config.json');
const Postgres = require('../server/js/lib/db/postgres').default;
const Mysql = require('../server/js/lib/db/mysql').default;
const DBNAME = 'voiceweb' || config.PBDATABASE;
const USERNAME = 'voiceweb' || config.PGUSER;
const DBNAME = '' || config.MSQLDBNAME;
const USERNAME = '' || config.MSQLUSER;
/**
* Create the database.
* Drop the database.
*/
function run(callback) {
// Make sure we have a superuser username and password
let user = config.DB_ROOT_USER || 'postgres';
let user = config.DB_ROOT_USER || 'root';
let pass = config.DB_ROOT_PASS || '';
if (!user) {
......@@ -19,16 +19,16 @@ function run(callback) {
return;
}
let pg = new Postgres({
let mysql = new Mysql({
user: user,
password: pass,
database: 'template1'
database: 'sys'
});
let f = ff(() => {
pg.query(`DROP DATABASE IF EXISTS ${DBNAME};`, f());
mysql.query(`DROP DATABASE IF EXISTS ${DBNAME};`, null, f.slot());
}, () => {
pg.query(`DROP USER IF EXISTS ${USERNAME};`, f());
mysql.query(`DROP USER IF EXISTS ${USERNAME}@localhost;`, null, f.slot());
}).onComplete((err, result) => {
if (err) {
console.error('drop error', err);
......@@ -37,7 +37,7 @@ function run(callback) {
}
console.log('database dropped.');
pg.end();
mysql.end();
callback();
});
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment