Fix the search algorithm
[samplevnf.git] / VNF_Catalogue / migration / migrate.js
1 /*******************************************************************************
2  * Copyright (c) 2017 Kumar Rishabh(penguinRaider) and others.
3  *
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  *******************************************************************************/
9
10 var knex = require('knex')({
11     client: 'mysql',
12     connection: {
13         host     : process.env.DB_HOST,
14         user     : process.env.DB_USER,
15         password : process.env.DB_PASSWORD,
16         database : process.env.DB_DATABASE,
17         charset  : 'utf8'
18     }
19 });
20 var Schema = require('./schema');
21 var sequence = require('when/sequence');
22 var _ = require('lodash');
23 var moment = require('moment');
24
25 function createTable(tableName) {
26     return knex.schema.createTable(tableName, function (table) {
27     var column;
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);
32         }
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();
36         }
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();
40         }
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);
43         }
44         else {
45         column = table[Schema[tableName][key].type](key);
46         }
47         if (Schema[tableName][key].hasOwnProperty('nullable') && Schema[tableName][key].nullable === true) {
48         column.nullable();
49         }
50         else {
51         column.notNullable();
52         }
53         if (Schema[tableName][key].hasOwnProperty('primary') && Schema[tableName][key].primary === true) {
54         column.primary();
55         }
56         if (Schema[tableName][key].hasOwnProperty('unique') && Schema[tableName][key].unique) {
57         column.unique();
58         }
59         if (Schema[tableName][key].hasOwnProperty('unsigned') && Schema[tableName][key].unsigned) {
60         column.unsigned();
61         }
62         if (Schema[tableName][key].hasOwnProperty('references')) {
63         column.references(Schema[tableName][key].references);
64         }
65         if (Schema[tableName][key].hasOwnProperty('defaultTo')) {
66         column.defaultTo(Schema[tableName][key].defaultTo);
67         }
68     });
69     });
70 }
71 function createTables () {
72     var tables = [];
73     var tableNames = _.keys(Schema);
74     tables = _.map(tableNames, function (tableName) {
75     return function () {
76         return createTable(tableName);
77     };
78     });
79     return sequence(tables);
80 }
81
82 function mysql_datetime() {
83     return moment(new Date()).format('YYYY-MM-DD HH:mm:ss');
84 }
85
86 createTables()
87 .then(function() {
88     console.log('Tables created!!');
89     var current_time = mysql_datetime();
90     console.log(current_time);
91
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() {                                                           
94                     process.exit(0)});;
95 })
96 .catch(function (error) {
97     console.log('error creating the database perhaps it exists?(If yes congrats the persistance of mysql works :-D)');
98     process.exit(0);
99     //throw error;
100 });