Merge "KVMFORNFV: Uploading logs to artifacts for debugging."
[releng.git] / utils / test / vnfcatalogue / helpers / 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     : 'localhost',
14         user     : '*',
15         password : '*',
16         database : 'vnf_catalogue',
17         charset  : 'utf8'
18     }
19 });
20 var Schema = require('./schema');
21 var sequence = require('when/sequence');
22 var _ = require('lodash');
23 function createTable(tableName) {
24     return knex.schema.createTable(tableName, function (table) {
25     var column;
26     var columnKeys = _.keys(Schema[tableName]);
27     _.each(columnKeys, function (key) {
28         if (Schema[tableName][key].type === 'text' && Schema[tableName][key].hasOwnProperty('fieldtype')) {
29         column = table[Schema[tableName][key].type](key, Schema[tableName][key].fieldtype);
30         }
31         else if (Schema[tableName][key].type === 'string' && Schema[tableName][key].hasOwnProperty('maxlength')) {
32         column = table[Schema[tableName][key].type](key, Schema[tableName][key].maxlength);
33         }
34         else {
35         column = table[Schema[tableName][key].type](key);
36         }
37         if (Schema[tableName][key].hasOwnProperty('nullable') && Schema[tableName][key].nullable === true) {
38         column.nullable();
39         }
40         else {
41         column.notNullable();
42         }
43         if (Schema[tableName][key].hasOwnProperty('primary') && Schema[tableName][key].primary === true) {
44         column.primary();
45         }
46         if (Schema[tableName][key].hasOwnProperty('unique') && Schema[tableName][key].unique) {
47         column.unique();
48         }
49         if (Schema[tableName][key].hasOwnProperty('unsigned') && Schema[tableName][key].unsigned) {
50         column.unsigned();
51         }
52         if (Schema[tableName][key].hasOwnProperty('references')) {
53         column.references(Schema[tableName][key].references);
54         }
55         if (Schema[tableName][key].hasOwnProperty('defaultTo')) {
56         column.defaultTo(Schema[tableName][key].defaultTo);
57         }
58     });
59     });
60 }
61 function createTables () {
62     var tables = [];
63     var tableNames = _.keys(Schema);
64     tables = _.map(tableNames, function (tableName) {
65     return function () {
66         return createTable(tableName);
67     };
68     });
69     return sequence(tables);
70 }
71 createTables()
72 .then(function() {
73     console.log('Tables created!!');
74     process.exit(0);
75 })
76 .catch(function (error) {
77     throw error;
78 });