1 /*******************************************************************************
2 * Copyright (c) 2017 Kumar Rishabh(penguinRaider) and others.
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Apache License, Version 2.0
6 * which accompanies this distribution, and is available at
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *******************************************************************************/
10 var knex = require('knex')({
13 host : process.env.DB_HOST,
14 user : process.env.DB_USER,
15 password : process.env.DB_PASSWORD,
16 database : process.env.DB_DATABASE,
20 var Schema = require('./schema');
21 var sequence = require('when/sequence');
22 var _ = require('lodash');
23 var moment = require('moment');
25 function createTable(tableName) {
26 return knex.schema.createTable(tableName, function (table) {
28 var columnKeys = _.keys(Schema[tableName]);
29 _.each(columnKeys, function (key) {
30 if (Schema[tableName][key].type === 'text' && Schema[tableName][key].hasOwnProperty('fieldtype')) {
31 column = table[Schema[tableName][key].type](key, Schema[tableName][key].fieldtype);
33 else if (Schema[tableName][key].type === 'enum' && Schema[tableName][key].hasOwnProperty('values') && Schema[tableName][key].nullable === true) {
34 console.log(Schema[tableName][key].values);
35 column = table[Schema[tableName][key].type](key, Schema[tableName][key].values).nullable();
37 else if (Schema[tableName][key].type === 'enum' && Schema[tableName][key].hasOwnProperty('values')) {
38 console.log(Schema[tableName][key].values);
39 column = table[Schema[tableName][key].type](key, Schema[tableName][key].values).notNullable();
41 else if (Schema[tableName][key].type === 'string' && Schema[tableName][key].hasOwnProperty('maxlength')) {
42 column = table[Schema[tableName][key].type](key, Schema[tableName][key].maxlength);
45 column = table[Schema[tableName][key].type](key);
47 if (Schema[tableName][key].hasOwnProperty('nullable') && Schema[tableName][key].nullable === true) {
53 if (Schema[tableName][key].hasOwnProperty('primary') && Schema[tableName][key].primary === true) {
56 if (Schema[tableName][key].hasOwnProperty('unique') && Schema[tableName][key].unique) {
59 if (Schema[tableName][key].hasOwnProperty('unsigned') && Schema[tableName][key].unsigned) {
62 if (Schema[tableName][key].hasOwnProperty('references')) {
63 column.references(Schema[tableName][key].references);
65 if (Schema[tableName][key].hasOwnProperty('defaultTo')) {
66 column.defaultTo(Schema[tableName][key].defaultTo);
71 function createTables () {
73 var tableNames = _.keys(Schema);
74 tables = _.map(tableNames, function (tableName) {
76 return createTable(tableName);
79 return sequence(tables);
82 function mysql_datetime() {
83 return moment(new Date()).format('YYYY-MM-DD HH:mm:ss');
88 console.log('Tables created!!');
89 var current_time = mysql_datetime();
90 console.log(current_time);
92 knex.insert([{user_name: 'admin', password: 'admin', email_id: 'admin@opnfv.org', company: 'opnfv', introduction: 'hello world',
93 created_at: current_time}]).into('user').then(function() {
96 .catch(function (error) {
97 console.log('error creating the database perhaps it exists?(If yes congrats the persistance of mysql works :-D)');