From 1f6b18a1974c1b53a079b21b6be39af86deb2432 Mon Sep 17 00:00:00 2001 From: Kumar Rishabh Date: Thu, 29 Jun 2017 11:54:06 +0530 Subject: VNF_Catalogue Codebase Catalogue of Open Source VNFs consist in helping the end users to get information of the VNF we can deploy on top of an OPNFV solution [Deepak]: Removed all swp files. Change-Id: Ib2ea7330e964f1b684f32aedf631accd580df968 Signed-off-by: Kumar Rishabh Signed-off-by: Deepak S --- VNF_Catalogue/migration/migrate.js | 100 +++++++++++++++++++++++++++++++++++++ 1 file changed, 100 insertions(+) create mode 100644 VNF_Catalogue/migration/migrate.js (limited to 'VNF_Catalogue/migration/migrate.js') diff --git a/VNF_Catalogue/migration/migrate.js b/VNF_Catalogue/migration/migrate.js new file mode 100644 index 00000000..24332fd9 --- /dev/null +++ b/VNF_Catalogue/migration/migrate.js @@ -0,0 +1,100 @@ +/******************************************************************************* + * Copyright (c) 2017 Kumar Rishabh(penguinRaider) and others. + * + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Apache License, Version 2.0 + * which accompanies this distribution, and is available at + * http://www.apache.org/licenses/LICENSE-2.0 + *******************************************************************************/ + +var knex = require('knex')({ + client: 'mysql', + connection: { + host : process.env.DB_HOST, + user : process.env.DB_USER, + password : process.env.DB_PASSWORD, + database : process.env.DB_DATABASE, + charset : 'utf8' + } +}); +var Schema = require('./schema'); +var sequence = require('when/sequence'); +var _ = require('lodash'); +var moment = require('moment'); + +function createTable(tableName) { + return knex.schema.createTable(tableName, function (table) { + var column; + var columnKeys = _.keys(Schema[tableName]); + _.each(columnKeys, function (key) { + if (Schema[tableName][key].type === 'text' && Schema[tableName][key].hasOwnProperty('fieldtype')) { + column = table[Schema[tableName][key].type](key, Schema[tableName][key].fieldtype); + } + else if (Schema[tableName][key].type === 'enum' && Schema[tableName][key].hasOwnProperty('values') && Schema[tableName][key].nullable === true) { + console.log(Schema[tableName][key].values); + column = table[Schema[tableName][key].type](key, Schema[tableName][key].values).nullable(); + } + else if (Schema[tableName][key].type === 'enum' && Schema[tableName][key].hasOwnProperty('values')) { + console.log(Schema[tableName][key].values); + column = table[Schema[tableName][key].type](key, Schema[tableName][key].values).notNullable(); + } + else if (Schema[tableName][key].type === 'string' && Schema[tableName][key].hasOwnProperty('maxlength')) { + column = table[Schema[tableName][key].type](key, Schema[tableName][key].maxlength); + } + else { + column = table[Schema[tableName][key].type](key); + } + if (Schema[tableName][key].hasOwnProperty('nullable') && Schema[tableName][key].nullable === true) { + column.nullable(); + } + else { + column.notNullable(); + } + if (Schema[tableName][key].hasOwnProperty('primary') && Schema[tableName][key].primary === true) { + column.primary(); + } + if (Schema[tableName][key].hasOwnProperty('unique') && Schema[tableName][key].unique) { + column.unique(); + } + if (Schema[tableName][key].hasOwnProperty('unsigned') && Schema[tableName][key].unsigned) { + column.unsigned(); + } + if (Schema[tableName][key].hasOwnProperty('references')) { + column.references(Schema[tableName][key].references); + } + if (Schema[tableName][key].hasOwnProperty('defaultTo')) { + column.defaultTo(Schema[tableName][key].defaultTo); + } + }); + }); +} +function createTables () { + var tables = []; + var tableNames = _.keys(Schema); + tables = _.map(tableNames, function (tableName) { + return function () { + return createTable(tableName); + }; + }); + return sequence(tables); +} + +function mysql_datetime() { + return moment(new Date()).format('YYYY-MM-DD HH:mm:ss'); +} + +createTables() +.then(function() { + console.log('Tables created!!'); + var current_time = mysql_datetime(); + console.log(current_time); + + knex.insert([{user_name: 'admin', password: 'admin', email_id: 'admin@opnfv.org', company: 'opnfv', introduction: 'hello world', + created_at: current_time}]).into('user').then(function() { + process.exit(0)});; +}) +.catch(function (error) { + console.log('error creating the database perhaps it exists?(If yes congrats the persistance of mysql works :-D)'); + process.exit(0); + //throw error; +}); -- cgit 1.2.3-korg