knex 0.21.20 → 0.21.21

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (141) hide show
  1. package/CHANGELOG.md +6 -0
  2. package/CONTRIBUTING.md +184 -184
  3. package/LICENSE +22 -22
  4. package/README.md +95 -95
  5. package/bin/cli.js +414 -414
  6. package/bin/utils/cli-config-utils.js +151 -151
  7. package/bin/utils/constants.js +7 -7
  8. package/bin/utils/migrationsLister.js +37 -37
  9. package/knex.js +8 -8
  10. package/lib/client.js +413 -413
  11. package/lib/config-resolver.js +61 -61
  12. package/lib/constants.js +44 -44
  13. package/lib/dialects/mssql/index.js +390 -390
  14. package/lib/dialects/mssql/query/compiler.js +444 -444
  15. package/lib/dialects/mssql/schema/columncompiler.js +103 -103
  16. package/lib/dialects/mssql/schema/compiler.js +59 -59
  17. package/lib/dialects/mssql/schema/tablecompiler.js +245 -245
  18. package/lib/dialects/mssql/transaction.js +97 -97
  19. package/lib/dialects/mysql/index.js +191 -191
  20. package/lib/dialects/mysql/query/compiler.js +142 -142
  21. package/lib/dialects/mysql/schema/columncompiler.js +171 -171
  22. package/lib/dialects/mysql/schema/compiler.js +60 -60
  23. package/lib/dialects/mysql/schema/tablecompiler.js +262 -262
  24. package/lib/dialects/mysql/transaction.js +48 -48
  25. package/lib/dialects/mysql2/index.js +35 -35
  26. package/lib/dialects/mysql2/transaction.js +46 -46
  27. package/lib/dialects/oracle/DEAD_CODE.md +5 -5
  28. package/lib/dialects/oracle/formatter.js +20 -20
  29. package/lib/dialects/oracle/index.js +79 -79
  30. package/lib/dialects/oracle/query/compiler.js +327 -327
  31. package/lib/dialects/oracle/schema/columnbuilder.js +18 -18
  32. package/lib/dialects/oracle/schema/columncompiler.js +139 -139
  33. package/lib/dialects/oracle/schema/compiler.js +81 -81
  34. package/lib/dialects/oracle/schema/tablecompiler.js +165 -165
  35. package/lib/dialects/oracle/schema/trigger.js +126 -126
  36. package/lib/dialects/oracle/utils.js +86 -86
  37. package/lib/dialects/oracledb/index.js +489 -489
  38. package/lib/dialects/oracledb/query/compiler.js +363 -363
  39. package/lib/dialects/oracledb/schema/columncompiler.js +35 -35
  40. package/lib/dialects/oracledb/transaction.js +76 -76
  41. package/lib/dialects/oracledb/utils.js +14 -14
  42. package/lib/dialects/postgres/index.js +319 -319
  43. package/lib/dialects/postgres/query/compiler.js +206 -206
  44. package/lib/dialects/postgres/schema/columncompiler.js +125 -125
  45. package/lib/dialects/postgres/schema/compiler.js +109 -109
  46. package/lib/dialects/postgres/schema/tablecompiler.js +183 -183
  47. package/lib/dialects/redshift/index.js +73 -73
  48. package/lib/dialects/redshift/query/compiler.js +119 -119
  49. package/lib/dialects/redshift/schema/columnbuilder.js +20 -20
  50. package/lib/dialects/redshift/schema/columncompiler.js +60 -60
  51. package/lib/dialects/redshift/schema/compiler.js +14 -14
  52. package/lib/dialects/redshift/schema/tablecompiler.js +123 -123
  53. package/lib/dialects/redshift/transaction.js +18 -18
  54. package/lib/dialects/sqlite3/formatter.js +21 -21
  55. package/lib/dialects/sqlite3/index.js +169 -169
  56. package/lib/dialects/sqlite3/query/compiler.js +222 -222
  57. package/lib/dialects/sqlite3/schema/columncompiler.js +27 -27
  58. package/lib/dialects/sqlite3/schema/compiler.js +49 -49
  59. package/lib/dialects/sqlite3/schema/ddl.js +525 -525
  60. package/lib/dialects/sqlite3/schema/tablecompiler.js +238 -238
  61. package/lib/formatter.js +295 -295
  62. package/lib/functionhelper.js +14 -14
  63. package/lib/helpers.js +92 -92
  64. package/lib/index.js +3 -3
  65. package/lib/interface.js +115 -115
  66. package/lib/knex.js +42 -42
  67. package/lib/logger.js +76 -76
  68. package/lib/migrate/MigrationGenerator.js +82 -82
  69. package/lib/migrate/Migrator.js +611 -611
  70. package/lib/migrate/configuration-merger.js +60 -60
  71. package/lib/migrate/migrate-stub.js +17 -17
  72. package/lib/migrate/migration-list-resolver.js +36 -36
  73. package/lib/migrate/sources/fs-migrations.js +99 -99
  74. package/lib/migrate/stub/cjs.stub +15 -15
  75. package/lib/migrate/stub/coffee.stub +13 -13
  76. package/lib/migrate/stub/eg.stub +14 -14
  77. package/lib/migrate/stub/js.stub +15 -15
  78. package/lib/migrate/stub/knexfile-coffee.stub +34 -34
  79. package/lib/migrate/stub/knexfile-eg.stub +43 -43
  80. package/lib/migrate/stub/knexfile-js.stub +44 -44
  81. package/lib/migrate/stub/knexfile-ls.stub +35 -35
  82. package/lib/migrate/stub/knexfile-ts.stub +44 -44
  83. package/lib/migrate/stub/ls.stub +14 -14
  84. package/lib/migrate/stub/ts.stub +21 -21
  85. package/lib/migrate/table-creator.js +67 -67
  86. package/lib/migrate/table-resolver.js +27 -27
  87. package/lib/query/builder.js +1372 -1372
  88. package/lib/query/compiler.js +889 -889
  89. package/lib/query/constants.js +13 -13
  90. package/lib/query/joinclause.js +263 -263
  91. package/lib/query/methods.js +92 -92
  92. package/lib/query/string.js +190 -190
  93. package/lib/raw.js +188 -188
  94. package/lib/ref.js +39 -39
  95. package/lib/runner.js +285 -285
  96. package/lib/schema/builder.js +82 -82
  97. package/lib/schema/columnbuilder.js +117 -117
  98. package/lib/schema/columncompiler.js +177 -177
  99. package/lib/schema/compiler.js +101 -101
  100. package/lib/schema/helpers.js +51 -51
  101. package/lib/schema/tablebuilder.js +288 -288
  102. package/lib/schema/tablecompiler.js +296 -296
  103. package/lib/seed/Seeder.js +203 -203
  104. package/lib/seed/seed-stub.js +13 -13
  105. package/lib/seed/stub/coffee.stub +9 -9
  106. package/lib/seed/stub/eg.stub +11 -11
  107. package/lib/seed/stub/js.stub +13 -13
  108. package/lib/seed/stub/ls.stub +11 -11
  109. package/lib/seed/stub/ts.stub +13 -13
  110. package/lib/transaction.js +363 -363
  111. package/lib/util/batchInsert.js +59 -59
  112. package/lib/util/delay.js +6 -6
  113. package/lib/util/fake-client.js +9 -9
  114. package/lib/util/finally-mixin.js +13 -13
  115. package/lib/util/fs.js +76 -76
  116. package/lib/util/import-file.js +13 -13
  117. package/lib/util/is-module-type.js +14 -14
  118. package/lib/util/is.js +32 -32
  119. package/lib/util/make-knex.js +338 -338
  120. package/lib/util/nanoid.js +29 -29
  121. package/lib/util/noop.js +1 -1
  122. package/lib/util/parse-connection.js +66 -66
  123. package/lib/util/save-async-stack.js +14 -14
  124. package/lib/util/template.js +52 -52
  125. package/lib/util/timeout.js +29 -29
  126. package/lib/util/timestamp.js +16 -16
  127. package/package.json +1 -1
  128. package/scripts/build.js +125 -125
  129. package/scripts/docker-compose.yml +111 -111
  130. package/scripts/next-release-howto.md +24 -24
  131. package/scripts/release.sh +34 -34
  132. package/scripts/runkit-example.js +34 -34
  133. package/scripts/stress-test/README.txt +18 -18
  134. package/scripts/stress-test/docker-compose.yml +47 -47
  135. package/scripts/stress-test/knex-stress-test.js +196 -196
  136. package/scripts/stress-test/mysql2-random-hanging-every-now-and-then.js +145 -145
  137. package/scripts/stress-test/mysql2-sudden-exit-without-error.js +100 -100
  138. package/scripts/stress-test/reconnect-test-mysql-based-drivers.js +184 -184
  139. package/types/index.d.ts +2249 -2249
  140. package/types/result.d.ts +27 -27
  141. package/types/tables.d.ts +4 -4
@@ -1,18 +1,18 @@
1
- # Test scripts to evaluate stability of drivers / pool etc.
2
-
3
- # To run this test you need to be in this directory + have node >= 8
4
- # and startup docker containers with proxy and sql servers
5
-
6
- docker-compose up --no-start
7
- docker-compose start
8
-
9
- # Select different test script to run:
10
-
11
- node mysql2-random-hanging-every-now-and-then.js 2> /dev/null | grep -B500 -A2 -- "- STATS"
12
- node mysql2-sudden-exit-without-error
13
- node knex-stress-test.js | grep -A 3 -- "- STATS "
14
- node reconnect-test-mysql-based-drivers.js 2> /dev/null | grep -A 3 -- "- STATS "
15
-
16
- # Shut down docker instances when done:
17
-
18
- docker-compose down
1
+ # Test scripts to evaluate stability of drivers / pool etc.
2
+
3
+ # To run this test you need to be in this directory + have node >= 8
4
+ # and startup docker containers with proxy and sql servers
5
+
6
+ docker-compose up --no-start
7
+ docker-compose start
8
+
9
+ # Select different test script to run:
10
+
11
+ node mysql2-random-hanging-every-now-and-then.js 2> /dev/null | grep -B500 -A2 -- "- STATS"
12
+ node mysql2-sudden-exit-without-error
13
+ node knex-stress-test.js | grep -A 3 -- "- STATS "
14
+ node reconnect-test-mysql-based-drivers.js 2> /dev/null | grep -A 3 -- "- STATS "
15
+
16
+ # Shut down docker instances when done:
17
+
18
+ docker-compose down
@@ -1,47 +1,47 @@
1
- version: '3'
2
-
3
- services:
4
- toxiproxy:
5
- image: shopify/toxiproxy
6
- ports:
7
- - "8474:8474"
8
- - "23306:23306"
9
- - "25432:25432"
10
- - "21521:21521"
11
- - "21433:21433"
12
- links:
13
- - "mysql"
14
- - "postgresql"
15
- - "oracledbxe"
16
- - "mssql"
17
-
18
- mysql:
19
- image: mysql:5.7
20
- ports:
21
- - "33306:3306"
22
- environment:
23
- - TZ=UTC
24
- - MYSQL_ROOT_PASSWORD=mysqlrootpassword
25
-
26
- postgresql:
27
- image: mdillon/postgis
28
- ports:
29
- - "35432:5432"
30
- environment:
31
- - POSTGRES_PASSWORD=postgresrootpassword
32
- - POSTGRES_USER=postgres
33
-
34
- oracledbxe:
35
- image: wnameless/oracle-xe-11g
36
- ports:
37
- - "31521:1521"
38
- environment:
39
- - ORACLE_ALLOW_REMOTE=true
40
-
41
- mssql:
42
- image: microsoft/mssql-server-linux:2017-latest
43
- ports:
44
- - "31433:1433"
45
- environment:
46
- - ACCEPT_EULA=Y
47
- - SA_PASSWORD=S0meVeryHardPassword
1
+ version: '3'
2
+
3
+ services:
4
+ toxiproxy:
5
+ image: shopify/toxiproxy
6
+ ports:
7
+ - "8474:8474"
8
+ - "23306:23306"
9
+ - "25432:25432"
10
+ - "21521:21521"
11
+ - "21433:21433"
12
+ links:
13
+ - "mysql"
14
+ - "postgresql"
15
+ - "oracledbxe"
16
+ - "mssql"
17
+
18
+ mysql:
19
+ image: mysql:5.7
20
+ ports:
21
+ - "33306:3306"
22
+ environment:
23
+ - TZ=UTC
24
+ - MYSQL_ROOT_PASSWORD=mysqlrootpassword
25
+
26
+ postgresql:
27
+ image: mdillon/postgis
28
+ ports:
29
+ - "35432:5432"
30
+ environment:
31
+ - POSTGRES_PASSWORD=postgresrootpassword
32
+ - POSTGRES_USER=postgres
33
+
34
+ oracledbxe:
35
+ image: wnameless/oracle-xe-11g
36
+ ports:
37
+ - "31521:1521"
38
+ environment:
39
+ - ORACLE_ALLOW_REMOTE=true
40
+
41
+ mssql:
42
+ image: microsoft/mssql-server-linux:2017-latest
43
+ ports:
44
+ - "31433:1433"
45
+ environment:
46
+ - ACCEPT_EULA=Y
47
+ - SA_PASSWORD=S0meVeryHardPassword
@@ -1,196 +1,196 @@
1
- const Knex = require('../../lib');
2
-
3
- const toxiproxy = require('toxiproxy-node-client');
4
- const toxicli = new toxiproxy.Toxiproxy('http://localhost:8474');
5
- const rp = require('request-promise-native');
6
- const delay = require('../../lib/util/delay');
7
-
8
- // init instances
9
- const pg = Knex({
10
- client: 'pg',
11
- connection:
12
- 'postgres://postgres:postgresrootpassword@localhost:25432/postgres',
13
- pool: { max: 50 },
14
- });
15
-
16
- const mysql2 = Knex({
17
- client: 'mysql2',
18
- connection:
19
- 'mysql://root:mysqlrootpassword@localhost:23306/?charset=utf8&connectTimeout=500',
20
- pool: { max: 50 },
21
- });
22
-
23
- const mysql = Knex({
24
- client: 'mysql',
25
- connection:
26
- 'mysql://root:mysqlrootpassword@localhost:23306/?charset=utf8&connectTimeout=500',
27
- pool: { max: 50 },
28
- });
29
-
30
- const mssql = Knex({
31
- client: 'mssql',
32
- connection: {
33
- port: 21433,
34
- user: 'sa',
35
- password: 'S0meVeryHardPassword',
36
- server: 'localhost',
37
- requestTimeout: 500,
38
- },
39
- pool: { max: 50 },
40
- });
41
-
42
- /* TODO: figure out how to nicely install oracledb node driver on osx
43
- const oracledb = Knex({
44
- client: 'oracledb',
45
- connection: {
46
- user : "travis",
47
- password : "travis",
48
- connectString : "localhost/XE",
49
- // https://github.com/oracle/node-oracledb/issues/525
50
- stmtCacheSize : 0
51
- },
52
- pool: { max: 50 }
53
- });
54
- */
55
-
56
- const counters = {};
57
-
58
- function setQueryCounters(instance, name) {
59
- const counts = (counters[name] = { queries: 0, results: 0, errors: 0 });
60
- instance.on('query', () => (counts.queries += 1));
61
- instance.on('query-response', () => (counts.results += 1));
62
- instance.on('query-error', () => (counts.errors += 1));
63
- }
64
-
65
- setQueryCounters(pg, 'pg');
66
- setQueryCounters(mysql, 'mysql');
67
- setQueryCounters(mysql2, 'mysql2');
68
- setQueryCounters(mssql, 'mssql');
69
-
70
- const _ = require('lodash');
71
-
72
- // start printing out counters
73
- let lastCounters = _.cloneDeep(counters);
74
-
75
- setInterval(() => {
76
- const reqsPerSec = {};
77
- for (let key of Object.keys(counters)) {
78
- reqsPerSec[key] = {
79
- queries: (counters[key].queries - lastCounters[key].queries) / 2,
80
- results: (counters[key].results - lastCounters[key].results) / 2,
81
- errors: (counters[key].errors - lastCounters[key].errors) / 2,
82
- };
83
- }
84
- console.log(
85
- '------------------------ STATS PER SECOND ------------------------'
86
- );
87
- console.dir(reqsPerSec, { colors: true });
88
- console.log(
89
- '------------------------------- EOS ------------------------------'
90
- );
91
- lastCounters = _.cloneDeep(counters);
92
- }, 2000);
93
-
94
- async function killConnectionsPg() {
95
- return pg.raw(`SELECT pg_terminate_backend(pg_stat_activity.pid)
96
- FROM pg_stat_activity
97
- WHERE pg_stat_activity.datname = 'postgres'
98
- AND pid <> pg_backend_pid()`);
99
- }
100
-
101
- async function killConnectionsMyslq(client) {
102
- const [rows, colDefs] = await client.raw(`SHOW FULL PROCESSLIST`);
103
- await Promise.all(rows.map((row) => client.raw(`KILL ${row.Id}`)));
104
- }
105
-
106
- async function killConnectionsMssql() {
107
- const rows = await mssql('sys.dm_exec_sessions').select('session_id');
108
- await Promise.all(rows.map((row) => mssql.raw(`KILL ${row.session_id}`)));
109
- }
110
-
111
- async function main() {
112
- async function loopQueries(prefix, query) {
113
- const queries = () => [...Array(50).fill(query)];
114
-
115
- while (true) {
116
- try {
117
- await Promise.all(queries());
118
- } catch (err) {
119
- console.log(prefix, err);
120
- }
121
- }
122
- }
123
-
124
- async function recreateProxy(serviceName, listenPort, proxyToPort) {
125
- try {
126
- await rp.delete({
127
- url: `${toxicli.host}/proxies/${serviceName}`,
128
- });
129
- } catch (err) {}
130
-
131
- const proxy = await toxicli.createProxy({
132
- name: serviceName,
133
- listen: `0.0.0.0:${listenPort}`,
134
- upstream: `${serviceName}:${proxyToPort}`,
135
- });
136
-
137
- // add some latency
138
- await proxy.addToxic(
139
- new toxiproxy.Toxic(proxy, {
140
- type: 'latency',
141
- attributes: { latency: 1, jitter: 1 },
142
- })
143
- );
144
-
145
- // cause connections to be closed every 500 bytes
146
- await proxy.addToxic(
147
- new toxiproxy.Toxic(proxy, {
148
- type: 'limit_data',
149
- attributes: { bytes: 5000 },
150
- })
151
- );
152
- }
153
-
154
- // create TCP proxies for simulating bad connections etc.
155
- async function recreateProxies() {
156
- await recreateProxy('postgresql', 25432, 5432);
157
- await recreateProxy('mysql', 23306, 3306);
158
- await recreateProxy('oracledbxe', 21521, 1521);
159
- await recreateProxy('mssql', 21433, 1433);
160
- }
161
-
162
- await recreateProxies();
163
-
164
- loopQueries('PSQL:', pg.raw('select 1'));
165
- loopQueries('PSQL TO:', pg.raw('select 1').timeout(20));
166
-
167
- loopQueries('MYSQL:', mysql.raw('select 1'));
168
- loopQueries('MYSQL TO:', mysql.raw('select 1').timeout(20));
169
-
170
- // mysql2 still crashes app (without connection killer nor timeouts)
171
- // https://github.com/sidorares/node-mysql2/issues/731
172
- // loopQueries('MYSQL2:', mysql2.raw('select 1'));
173
- // loopQueries('MYSQL2 TO:', mysql2.raw('select 1').timeout(20));
174
-
175
- loopQueries('MSSQL:', mssql.raw('select 1'));
176
- loopQueries('MSSQL TO:', mssql.raw('select 1').timeout(20));
177
-
178
- setInterval(recreateProxies, 2000);
179
-
180
- while (true) {
181
- await delay(20); // kill everything every quite often from server side
182
- try {
183
- await Promise.all([
184
- killConnectionsPg(),
185
- killConnectionsMyslq(mysql),
186
- // killConnectionsMyslq(mysql2),
187
- killConnectionsMssql(),
188
- ]);
189
- } catch (err) {
190
- console.log('KILLER ERROR:', err);
191
- }
192
- }
193
- }
194
-
195
- process.on('exit', () => console.log('- STATS PRINT NEAR END LOGS ')); // marker for grep...
196
- main();
1
+ const Knex = require('../../lib');
2
+
3
+ const toxiproxy = require('toxiproxy-node-client');
4
+ const toxicli = new toxiproxy.Toxiproxy('http://localhost:8474');
5
+ const rp = require('request-promise-native');
6
+ const delay = require('../../lib/util/delay');
7
+
8
+ // init instances
9
+ const pg = Knex({
10
+ client: 'pg',
11
+ connection:
12
+ 'postgres://postgres:postgresrootpassword@localhost:25432/postgres',
13
+ pool: { max: 50 },
14
+ });
15
+
16
+ const mysql2 = Knex({
17
+ client: 'mysql2',
18
+ connection:
19
+ 'mysql://root:mysqlrootpassword@localhost:23306/?charset=utf8&connectTimeout=500',
20
+ pool: { max: 50 },
21
+ });
22
+
23
+ const mysql = Knex({
24
+ client: 'mysql',
25
+ connection:
26
+ 'mysql://root:mysqlrootpassword@localhost:23306/?charset=utf8&connectTimeout=500',
27
+ pool: { max: 50 },
28
+ });
29
+
30
+ const mssql = Knex({
31
+ client: 'mssql',
32
+ connection: {
33
+ port: 21433,
34
+ user: 'sa',
35
+ password: 'S0meVeryHardPassword',
36
+ server: 'localhost',
37
+ requestTimeout: 500,
38
+ },
39
+ pool: { max: 50 },
40
+ });
41
+
42
+ /* TODO: figure out how to nicely install oracledb node driver on osx
43
+ const oracledb = Knex({
44
+ client: 'oracledb',
45
+ connection: {
46
+ user : "travis",
47
+ password : "travis",
48
+ connectString : "localhost/XE",
49
+ // https://github.com/oracle/node-oracledb/issues/525
50
+ stmtCacheSize : 0
51
+ },
52
+ pool: { max: 50 }
53
+ });
54
+ */
55
+
56
+ const counters = {};
57
+
58
+ function setQueryCounters(instance, name) {
59
+ const counts = (counters[name] = { queries: 0, results: 0, errors: 0 });
60
+ instance.on('query', () => (counts.queries += 1));
61
+ instance.on('query-response', () => (counts.results += 1));
62
+ instance.on('query-error', () => (counts.errors += 1));
63
+ }
64
+
65
+ setQueryCounters(pg, 'pg');
66
+ setQueryCounters(mysql, 'mysql');
67
+ setQueryCounters(mysql2, 'mysql2');
68
+ setQueryCounters(mssql, 'mssql');
69
+
70
+ const _ = require('lodash');
71
+
72
+ // start printing out counters
73
+ let lastCounters = _.cloneDeep(counters);
74
+
75
+ setInterval(() => {
76
+ const reqsPerSec = {};
77
+ for (let key of Object.keys(counters)) {
78
+ reqsPerSec[key] = {
79
+ queries: (counters[key].queries - lastCounters[key].queries) / 2,
80
+ results: (counters[key].results - lastCounters[key].results) / 2,
81
+ errors: (counters[key].errors - lastCounters[key].errors) / 2,
82
+ };
83
+ }
84
+ console.log(
85
+ '------------------------ STATS PER SECOND ------------------------'
86
+ );
87
+ console.dir(reqsPerSec, { colors: true });
88
+ console.log(
89
+ '------------------------------- EOS ------------------------------'
90
+ );
91
+ lastCounters = _.cloneDeep(counters);
92
+ }, 2000);
93
+
94
+ async function killConnectionsPg() {
95
+ return pg.raw(`SELECT pg_terminate_backend(pg_stat_activity.pid)
96
+ FROM pg_stat_activity
97
+ WHERE pg_stat_activity.datname = 'postgres'
98
+ AND pid <> pg_backend_pid()`);
99
+ }
100
+
101
+ async function killConnectionsMyslq(client) {
102
+ const [rows, colDefs] = await client.raw(`SHOW FULL PROCESSLIST`);
103
+ await Promise.all(rows.map((row) => client.raw(`KILL ${row.Id}`)));
104
+ }
105
+
106
+ async function killConnectionsMssql() {
107
+ const rows = await mssql('sys.dm_exec_sessions').select('session_id');
108
+ await Promise.all(rows.map((row) => mssql.raw(`KILL ${row.session_id}`)));
109
+ }
110
+
111
+ async function main() {
112
+ async function loopQueries(prefix, query) {
113
+ const queries = () => [...Array(50).fill(query)];
114
+
115
+ while (true) {
116
+ try {
117
+ await Promise.all(queries());
118
+ } catch (err) {
119
+ console.log(prefix, err);
120
+ }
121
+ }
122
+ }
123
+
124
+ async function recreateProxy(serviceName, listenPort, proxyToPort) {
125
+ try {
126
+ await rp.delete({
127
+ url: `${toxicli.host}/proxies/${serviceName}`,
128
+ });
129
+ } catch (err) {}
130
+
131
+ const proxy = await toxicli.createProxy({
132
+ name: serviceName,
133
+ listen: `0.0.0.0:${listenPort}`,
134
+ upstream: `${serviceName}:${proxyToPort}`,
135
+ });
136
+
137
+ // add some latency
138
+ await proxy.addToxic(
139
+ new toxiproxy.Toxic(proxy, {
140
+ type: 'latency',
141
+ attributes: { latency: 1, jitter: 1 },
142
+ })
143
+ );
144
+
145
+ // cause connections to be closed every 500 bytes
146
+ await proxy.addToxic(
147
+ new toxiproxy.Toxic(proxy, {
148
+ type: 'limit_data',
149
+ attributes: { bytes: 5000 },
150
+ })
151
+ );
152
+ }
153
+
154
+ // create TCP proxies for simulating bad connections etc.
155
+ async function recreateProxies() {
156
+ await recreateProxy('postgresql', 25432, 5432);
157
+ await recreateProxy('mysql', 23306, 3306);
158
+ await recreateProxy('oracledbxe', 21521, 1521);
159
+ await recreateProxy('mssql', 21433, 1433);
160
+ }
161
+
162
+ await recreateProxies();
163
+
164
+ loopQueries('PSQL:', pg.raw('select 1'));
165
+ loopQueries('PSQL TO:', pg.raw('select 1').timeout(20));
166
+
167
+ loopQueries('MYSQL:', mysql.raw('select 1'));
168
+ loopQueries('MYSQL TO:', mysql.raw('select 1').timeout(20));
169
+
170
+ // mysql2 still crashes app (without connection killer nor timeouts)
171
+ // https://github.com/sidorares/node-mysql2/issues/731
172
+ // loopQueries('MYSQL2:', mysql2.raw('select 1'));
173
+ // loopQueries('MYSQL2 TO:', mysql2.raw('select 1').timeout(20));
174
+
175
+ loopQueries('MSSQL:', mssql.raw('select 1'));
176
+ loopQueries('MSSQL TO:', mssql.raw('select 1').timeout(20));
177
+
178
+ setInterval(recreateProxies, 2000);
179
+
180
+ while (true) {
181
+ await delay(20); // kill everything every quite often from server side
182
+ try {
183
+ await Promise.all([
184
+ killConnectionsPg(),
185
+ killConnectionsMyslq(mysql),
186
+ // killConnectionsMyslq(mysql2),
187
+ killConnectionsMssql(),
188
+ ]);
189
+ } catch (err) {
190
+ console.log('KILLER ERROR:', err);
191
+ }
192
+ }
193
+ }
194
+
195
+ process.on('exit', () => console.log('- STATS PRINT NEAR END LOGS ')); // marker for grep...
196
+ main();