Skip to content

Commit

Permalink
Run database setup via migration scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
erssebaggala committed Jul 29, 2019
1 parent f4f327f commit 3df1200
Show file tree
Hide file tree
Showing 23 changed files with 6,326 additions and 858 deletions.
4 changes: 3 additions & 1 deletion background/background-process.html
Original file line number Diff line number Diff line change
Expand Up @@ -727,7 +727,9 @@ <h1>Background process</h1>
//Setup database --
try{
if(task === 'setup_database'){
setupBodaDatabase(options.hostname, options.port, options.username, options.password);
//setupBodaDatabase(options.hostname, options.port, options.username, options.password);
const result = await utils.runMigrations(options.hostname, options.port, options.username, options.password);
sendLogToUI('setup_database',result.status, result.message);
}
}catch(err){
sendLogToUI('setup_database','error', err);
Expand Down
77 changes: 76 additions & 1 deletion background/background-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -588,11 +588,86 @@ function getPathToPsqlOnMacOSX(){

}

/*
* Run database migrations
*
* @param string hostname
* @param string port
* @param string username
* @param string password
*
* @since 0.3.0
*/
async function runMigrations(hostname, port, username, password){

const connectionString = `postgresql://${username}:${password}@${hostname}:${port}/postgres`;
const client = new Client({
connectionString: connectionString,
});

client.connect((err) => {
if(err){
return err;
}
});

try{
let results = await
new Promise( async (resolve, reject) => {
let res = await client.query("SELECT pg_terminate_backend(pid) FROM pg_stat_activity WHERE pid <> pg_backend_pid() AND datname = 'boda'");
res = await client.query("DROP DATABASE IF EXISTS boda");
res = await client.query("DROP ROLE IF EXISTS bodastage");
res = await client.query("CREATE USER bodastage WITH PASSWORD 'password'");
res = await client.query("CREATE DATABASE boda owner bodastage");

client.end();
if(typeof res.err !== 'undefined') reject("Error occured"); else resolve("Database and role created successfully.");

});
}catch(e){
return {status: 'error', message: 'Error occurred while running migrations. See log for details'}
}

//Get app base path
let basePath = app.getAppPath();
if (!isDev) basePath = process.resourcesPath;

//Create boda database
const dbCon = await getSQLiteDBConnectionDetails('boda');
const bodaConnStr = `postgresql://${dbCon.username}:${dbCon.password}@${dbCon.hostname}:${dbCon.port}/boda`;
const migrationDir = path.join(basePath,'db','migrations');
const options = {
databaseUrl: bodaConnStr ,
dir: migrationDir,
direction: 'up',
count: Infinity,
migrationsTable: 'pgmigrations',
log: log.log
};

log.info(`Migration directory: ${migrationDir}`)


const migrationRunner = window.require('node-pg-migrate');

try {
await migrationRunner(options);
} catch(e) {
log.error(e.toString());
return {status: 'error', message: 'Error occurred while running migrations. See log for details'}
}

return {status: 'success', message: 'Database setup/upgrade completed successfully'}


}

exports.SQLITE3_DB_PATH = SQLITE3_DB_PATH;
exports.getSQLiteDBConnectionDetails = getSQLiteDBConnectionDetails;
exports.getSQLiteReportInfo = getSQLiteReportInfo;
exports.runQuery = runQuery;
exports.generateCSVFromQuery = generateCSVFromQuery;
exports.loadCMDataViaStream = loadCMDataViaStream;
exports.generateExcelOrCSV = generateExcelOrCSV;
exports.getPathToPsqlOnMacOSX = getPathToPsqlOnMacOSX;
exports.getPathToPsqlOnMacOSX = getPathToPsqlOnMacOSX;
exports.runMigrations = runMigrations;
23 changes: 23 additions & 0 deletions db/migrations/1564257260302_create-boda-database.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
exports.shorthands = undefined;

//@TODO: Run these migrations from postgres account separately
exports.up = (pgm) => {
// pgm.noTransaction();
// pgm.sql("SELECT pg_terminate_backend(pid) FROM pg_stat_activity WHERE pid <> pg_backend_pid() AND datname = 'boda';");
// pgm.sql("DROP DATABASE IF EXISTS boda");
//
// pgm.createRole( "bodastage", {
// "password": "password",
// "login": true
// } );
//
// pgm.sql( "CREATE DATABASE boda owner bodastage");

};

exports.down = (pgm) => {
// pgm.noTransaction();
// pgm.sql("DROP DATABASE IF EXISTS boda");
// pgm.dropRole("bodastage");

};
62 changes: 62 additions & 0 deletions db/migrations/1564262109531_create-hex-to-int-function.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
exports.shorthands = undefined;
const hexToIntParams = [
{mode: 'in', name: "hexval", type: "varchar"}
];

exports.up = (pgm) => {
//Create hex to int function
pgm.createFunction(
//name
"hex_to_int",

//function_params
hexToIntParams,

//function_options
{
returns: "integer",
language: "plpgsql"
},

//definiton
`
DECLARE
result int;
BEGIN
EXECUTE 'SELECT x''' || hexval || '''::int' INTO result;
RETURN result;
END;
`
);

//Create hex to char function
pgm.createFunction(
//name
"hex_to_char",

//function_params
hexToIntParams,

//function_options
{
returns: "varchar",
language: "plpgsql"
},

//definiton
`
DECLARE
result varchar;
BEGIN
EXECUTE 'SELECT x''' || hexval || '''::int' INTO result;
RETURN result;
END;
`
);

};

exports.down = (pgm) => {
pgm.dropFunction("hex_to_int", hexToIntParams);
pgm.dropFunction("hex_to_char", hexToIntParams);
};
15 changes: 15 additions & 0 deletions db/migrations/1564263700671_create-vendor-cm-schemas.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
exports.shorthands = undefined;

exports.up = (pgm) => {
pgm.createSchema("ericsson_cm", {ifNotExists : true} );
pgm.createSchema("huawei_cm", {ifNotExists : true} );
pgm.createSchema("zte_cm", {ifNotExists : true} );
pgm.createSchema("nokia_cm", {ifNotExists : true} );
};

exports.down = (pgm) => {
pgm.dropSchema("ericsson_cm", {ifExists : true} );
pgm.dropSchema("huawei_cm", {ifExists : true} );
pgm.dropSchema("zte_cm", {ifExists : true} );
pgm.dropSchema("nokia_cm", {ifExists : true} );
};
50 changes: 50 additions & 0 deletions db/migrations/1564264136564_create-ericssom-cm-cnaiv2-tables.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
const PgLiteral = window.require('node-pg-migrate').PgLiteral;

exports.shorthands = {
idx: { type: 'uuid', primaryKey: true},
load_datetime: {
type: "timestamp",
notNull: true,
default: new PgLiteral('current_timestamp')
},
data: {type: "jsonb", notNull: true},
createdAt: {
type: "timestamp",
notNull: true,
default: new PgLiteral('current_timestamp')
}
};

exports.up = (pgm) => {
pgm.createTable({schema: "ericsson_cm", name: "UTRAN_NREL"}, {id: "id", load_datetime: "load_datetime", data: "data"});
pgm.createTable({schema: "ericsson_cm", name: "UTRAN_EXTERNAL_CELL"}, {id: "id", load_datetime: "load_datetime", data: "data"});
pgm.createTable({schema: "ericsson_cm", name: "TG"}, {id: "id", load_datetime: "load_datetime", data: "data"});
pgm.createTable({schema: "ericsson_cm", name: "SITE"}, {id: "id", load_datetime: "load_datetime", data: "data"});
pgm.createTable({schema: "ericsson_cm", name: "PRIORITY_PROFILE"}, {id: "id", load_datetime: "load_datetime", data: "data"});
pgm.createTable({schema: "ericsson_cm", name: "OVERLAID_CELL"}, {id: "id", load_datetime: "load_datetime", data: "data"});
pgm.createTable({schema: "ericsson_cm", name: "OUTER_CELL"}, {id: "id", load_datetime: "load_datetime", data: "data"});
pgm.createTable({schema: "ericsson_cm", name: "NREL"}, {id: "id", load_datetime: "load_datetime", data: "data"});
pgm.createTable({schema: "ericsson_cm", name: "MSC"}, {id: "id", load_datetime: "load_datetime", data: "data"});
pgm.createTable({schema: "ericsson_cm", name: "INTERNAL_CELL"}, {id: "id", load_datetime: "load_datetime", data: "data"});
pgm.createTable({schema: "ericsson_cm", name: "INNER_CELL"}, {id: "id", load_datetime: "load_datetime", data: "data"});
pgm.createTable({schema: "ericsson_cm", name: "EXTERNAL_CELL"}, {id: "id", load_datetime: "load_datetime", data: "data"});
pgm.createTable({schema: "ericsson_cm", name: "CHANNEL_GROUP"}, {id: "id", load_datetime: "load_datetime", data: "data"});
pgm.createTable({schema: "ericsson_cm", name: "BSC"}, {id: "id", load_datetime: "load_datetime", data: "data"});
};

exports.down = (pgm) => {
pgm.dropTable({schema: "ericsson_cm", name: 'UTRAN_NREL'});
pgm.dropTable({schema: "ericsson_cm", name: 'UTRAN_EXTERNAL_CELL'});
pgm.dropTable({schema: "ericsson_cm", name: 'TG'});
pgm.dropTable({schema: "ericsson_cm", name: 'SITE'});
pgm.dropTable({schema: "ericsson_cm", name: 'PRIORITY_PROFILE'});
pgm.dropTable({schema: "ericsson_cm", name: 'OVERLAID_CELL'});
pgm.dropTable({schema: "ericsson_cm", name: 'OUTER_CELL'});
pgm.dropTable({schema: "ericsson_cm", name: 'NREL'});
pgm.dropTable({schema: "ericsson_cm", name: 'MSC'});
pgm.dropTable({schema: "ericsson_cm", name: 'INTERNAL_CELL'});
pgm.dropTable({schema: "ericsson_cm", name: 'INNER_CELL'});
pgm.dropTable({schema: "ericsson_cm", name: 'EXTERNAL_CELL'});
pgm.dropTable({schema: "ericsson_cm", name: 'CHANNEL_GROUP'});
pgm.dropTable({schema: "ericsson_cm", name: 'BSC'});
};
Loading

0 comments on commit 3df1200

Please sign in to comment.