-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathpretest.js
112 lines (98 loc) · 3.16 KB
/
pretest.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
// Copyright IBM Corp. 2017,2019. All Rights Reserved.
// Node module: loopback-connector-db2
// This file is licensed under the Artistic License 2.0.
// License text available at https://opensource.org/licenses/Artistic-2.0
'use strict';
require('./test/init.js');
const async = require('async');
const fs = require('fs');
const path = require('path');
const _ = require('lodash');
const db = global.getDataSource();
const schemaName = global.config.schema;
const template = path.resolve(__dirname, 'test', 'table.template');
if (!process.env.CI)
return console.log('Skipping pre-test ...');
console.log('> Seeding the database ...');
const listSchemaCmd = `SELECT SCHEMANAME FROM SYSCAT.SCHEMATA WHERE
SCHEMANAME LIKE 'SCHEMA%_DB2_%_%'`;
const createViewCmd = `CREATE OR REPLACE VIEW "${schemaName}"."INVENTORY_VIEW"
AS SELECT P.name AS product, L.name AS location, I.available FROM
"${schemaName}"."INVENTORY" I,
"${schemaName}"."PRODUCT" P,
"${schemaName}"."LOCATION" L
WHERE
p.id = I.product_id AND l.id = I.location_id;`;
const contents = fs.readFileSync(template, {encoding: 'utf-8'});
const lines = contents.trim().split(';');
async.series([
function dropExistingSchemas(cb) {
db.connector.execute(listSchemaCmd, function(err, schema) {
if (err) cb(err);
console.log('> Cleaning up existing schemas ...');
async.each(schema, dropSchema, cb);
});
},
function createSchema(cb) {
db.connector.execute(`CREATE SCHEMA "${schemaName}";`, function(err) {
if (err) cb(err);
console.log('> Created schema: ' + schemaName);
cb(err);
});
},
function createTables(cb) {
async.each(lines, createTable, cb);
},
function createView(cb) {
db.connector.execute(createViewCmd, cb);
},
], function(err) {
if (err) throw err;
console.log('> Done seeding the database.');
});
function dropSchema(schema, cb) {
schema = schema['SCHEMANAME'];
const findTables = `SELECT TABNAME FROM SYSCAT.TABLES WHERE TABSCHEMA =
'${schema}' and type='T';`;
const findViews = `SELECT TABNAME FROM SYSCAT.TABLES WHERE TABSCHEMA =
'${schema}' and type='V';`;
async.series([
function dropViews(cb) {
db.connector.execute(findViews, function(err, views) {
async.each(views, dropView, cb);
});
},
function dropTables(cb) {
db.connector.execute(findTables, function(err, tables) {
async.each(tables, dropTable, cb);
});
},
function dropSchema(cb) {
db.connector.execute('DROP SCHEMA "' + schema +
'" RESTRICT;', cb);
},
], function(err) {
cb(err);
});
function dropView(view, cb) {
const viewName = view.TABNAME;
db.connector.execute(`DROP VIEW
"${schema}"."${viewName}";`, function(err, result) {
cb(err);
});
};
function dropTable(table, cb) {
const tableName = table.TABNAME;
db.connector.execute(`DROP TABLE
"${schema}"."${tableName}";`, function(err, result) {
cb(err);
});
};
};
function createTable(command, cb) {
command = (command + ';').trim();
if (command !== ';') {
const cmd = command.replace(/"\?"/g, `"${schemaName}"`);
db.connector.execute(cmd, cb);
} else cb();
};