forked from sqlpad/sqlpad
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate-test-db-fixture.js
More file actions
308 lines (284 loc) · 7.14 KB
/
generate-test-db-fixture.js
File metadata and controls
308 lines (284 loc) · 7.14 KB
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
/* eslint-disable no-console */
/* eslint-disable no-await-in-loop */
const fs = require('fs');
const path = require('path');
const moment = require('moment');
const sqliteDriver = require('./drivers/sqlite');
/**
* This script generates server/test/fixtures/sales.sqlite file for testing and dev purposes
*
* The idea of this sqlite database file is to be used for development and testing,
* to replace the `mock` driver that parses SQL comments and generates some output.
*
* This db can be extended to potentially have better views that simulate larger amounts of data.
*
* The generated file should try to remain a reasonable size (a few MBs).
* If always distributed it can serve as a good debug tool for others.
*/
const filename = path.join(__dirname, 'test/fixtures/sales.sqlite');
const client = new sqliteDriver.Client({
filename,
});
const regions = [
{ region: 'Northeast', multiplier: 1.5 },
{ region: 'West', multiplier: 1.4 },
{ region: 'South', multiplier: 1.1 },
{ region: 'Midwest', multiplier: 1.2 },
{ region: 'East', multiplier: 1.2 },
];
// This is just another number to use as a metric for charts
const hypes = [1, 3, 3, 4, 4, 4, 4, 4, 4, 1, 1, 1, 3];
const products = [
{
name: 'Awesome Wooden Ball',
category: 'toy',
color: 'azure',
price: 3.99,
},
{
name: 'Awesome Wooden Ball',
category: 'toy',
color: 'lime',
price: 4.99,
},
{
name: 'Fantastic Rubber Tuna',
category: 'toy',
color: 'azure',
price: 8.99,
},
{
name: 'Fantastic Rubber Tuna',
category: 'toy',
color: 'indigo',
price: 7.99,
},
{
name: 'Fantastic Rubber Tuna',
category: 'toy',
color: 'cyan',
price: 9.99,
},
{
name: 'Generic Wooden Keyboard',
category: 'tech',
color: 'azure',
price: 29.99,
},
{
name: 'Generic Wooden Keyboard',
category: 'tech',
color: 'lime',
price: 25.99,
},
{
name: 'Generic Wooden Keyboard',
category: 'tech',
color: 'red',
price: 31.99,
},
{
name: 'Generic Wooden Keyboard',
category: 'tech',
color: 'azure',
price: 31.99,
},
{
name: 'Handmade Granite Monitor',
category: 'tech',
color: 'cyan',
price: 99.99,
},
{
name: 'Incredible Rubber ball',
category: 'toy',
color: 'lime',
price: 12.99,
},
{
name: 'Refined Frozen Fish',
category: 'food',
color: 'cyan',
price: 22.99,
},
{
name: 'Rustic Concrete Chips',
category: 'food',
color: 'azure',
price: 2.99,
},
{
name: 'Rustic Metal Bacon',
category: 'food',
color: 'red',
price: 7.99,
},
{
name: 'Rustic Metal Bacon',
category: 'food',
color: 'azure',
price: 8.99,
},
{
name: 'Unbranded Granite laptop',
category: 'tech',
color: 'indigo',
price: 149.99,
},
];
async function main() {
if (fs.existsSync(filename)) {
console.log('removing existing file %s', filename);
fs.unlinkSync(filename);
}
await client.connect();
console.log('creating regions');
await client.runQuery(
'CREATE TABLE regions (id INTEGER PRIMARY KEY, region TEXT)'
);
for (let i = 0; i < regions.length; i++) {
await client.runQuery(
`INSERT INTO regions
(id, region)
VALUES
(${i + 1}, '${regions[i].region}')`
);
}
console.log('creating products');
await client.runQuery(
`CREATE TABLE products (
id INTEGER PRIMARY KEY,
name TEXT,
category TEXT,
color TEXT,
price REAL
)`
);
for (let i = 0; i < products.length; i++) {
const product = products[i];
await client.runQuery(
`INSERT INTO products (
id,
name,
category,
color,
price
) VALUES (
${i + 1},
'${product.name}',
'${product.category}',
'${product.color}',
${product.price}
);`
);
}
console.log('creating sales');
await client.runQuery(
`CREATE TABLE sales (
id INTEGER PRIMARY KEY,
order_timestamp TEXT,
region_id INT,
product_id INT,
hype INT,
cost REAL,
revenue REAL
);`
);
const NUM_BATCHES = 100;
const ROWS_PER_BATCH = 100;
for (let batch = 0; batch < NUM_BATCHES; batch++) {
if (batch % 10 === 0) {
console.log(
((batch / NUM_BATCHES) * 100).toFixed(1) + '% test db rows inserted'
);
}
const values = [];
for (let batchRow = 0; batchRow < ROWS_PER_BATCH; batchRow++) {
const rowId = batch * ROWS_PER_BATCH + batchRow;
const regionId = (rowId % regions.length) + 1;
const regionMultiplier = regions[rowId % regions.length].multiplier;
const productId = (rowId % products.length) + 1;
const product = products[rowId % products.length];
const hype = hypes[rowId % hypes.length];
const orderTimestamp = moment
.utc('2020-01-01')
.add(rowId * 43, 'minute')
.add(rowId * 37, 'seconds')
.toISOString();
const dayOfMonthMultiplier =
(moment(orderTimestamp).date() / 31) * 0.45 + 1;
const weekOfYearMultiplier =
(moment(orderTimestamp).week() / 52) * 0.25 + 1;
const minuteMultipler = (moment(orderTimestamp).minute() / 60) * 0.75 + 1;
const secondMultipler = (moment(orderTimestamp).second() / 60) * 0.75 + 1;
const multiplier =
regionMultiplier *
dayOfMonthMultiplier *
weekOfYearMultiplier *
minuteMultipler *
secondMultipler;
const cost = (product.price * 0.7 * multiplier).toFixed(2);
const revenue = (product.price * multiplier).toFixed(2);
values.push(`(
${rowId + 1},
'${orderTimestamp}',
${regionId},
${productId},
${hype},
${cost},
${revenue}
)`);
}
await client.runQuery(
`INSERT INTO sales (
id,
order_timestamp,
region_id,
product_id,
hype,
cost,
revenue
) VALUES ${values.join(', ')};`
);
}
console.log('creating sales view');
await client.runQuery(`
CREATE VIEW vw_sales AS
SELECT
sales.id,
date(sales.order_timestamp) order_date,
time(sales.order_timestamp) order_time,
strftime('%H:00:00', sales.order_timestamp) AS order_hour,
sales.order_timestamp,
regions.region,
products.name,
products.category,
products.color,
sales.hype,
sales.cost,
sales.revenue
FROM
sales
JOIN regions ON sales.region_id = regions.id
JOIN products ON sales.product_id = products.id
`);
// Lastly create a bunch of junk tables so simulate large schemas
console.log('creating dummy tables for large schema');
for (let i = 0; i < 500; i++) {
await client.runQuery(`CREATE TABLE z_fake_table_products_${i} (
id INTEGER PRIMARY KEY,
name TEXT,
category TEXT,
color TEXT,
price REAL,
quantity INT,
created_at DATETIME,
updated_at DATETIME
)`);
}
console.log('%s creation finished', filename);
}
main().catch((error) => {
console.error(error);
process.exit(1);
});