clickhouse-migrations 1.0.5 → 1.1.1

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.
package/README.md CHANGED
@@ -19,6 +19,8 @@ For migrations' content should be used correct SQL ClickHouse queries. Multiple
19
19
 
20
20
  If the database provided in the `--db` option (or in `CH_MIGRATIONS_DB`) doesn't exist, it will be created automatically.
21
21
 
22
+ For TLS/HTTPS connections, you can provide a custom CA certificate and optional client certificate/key via the `--ca-cert`, `--cert`, and `--key` options (or the `CH_MIGRATIONS_CA_CERT`, `CH_MIGRATIONS_CERT`, and `CH_MIGRATIONS_KEY` environment variables).
23
+
22
24
  ```
23
25
  Usage
24
26
  $ clickhouse-migrations migrate <options>
@@ -36,6 +38,9 @@ If the database provided in the `--db` option (or in `CH_MIGRATIONS_DB`) doesn't
36
38
  (default: 'ENGINE=Atomic')
37
39
  --timeout=<value> Client request timeout
38
40
  (milliseconds, default: 30000)
41
+ --ca-cert=<path> CA certificate file path
42
+ --cert=<path> Client certificate file path
43
+ --key=<path> Client key file path
39
44
 
40
45
  Environment variables
41
46
  Instead of options can be used environment variables.
@@ -48,6 +53,10 @@ If the database provided in the `--db` option (or in `CH_MIGRATIONS_DB`) doesn't
48
53
  CH_MIGRATIONS_DB_ENGINE (optional) DB engine (--db-engine)
49
54
  CH_MIGRATIONS_TIMEOUT (optional) Client request timeout
50
55
  (--timeout)
56
+ CH_MIGRATIONS_CA_CERT (optional) CA certificate file path
57
+ CH_MIGRATIONS_CERT (optional) Client certificate file path
58
+ CH_MIGRATIONS_KEY (optional) Client key file path
59
+
51
60
 
52
61
  CLI executions examples
53
62
  settings are passed as command-line options
@@ -66,6 +75,12 @@ If the database provided in the `--db` option (or in `CH_MIGRATIONS_DB`) doesn't
66
75
 
67
76
  settings provided partially through options and environment variables
68
77
  clickhouse-migrations migrate --timeout=60000
78
+
79
+ settings provided as options with TLS certificates
80
+ clickhouse-migrations migrate --host=https://localhost:8443
81
+ --user=default --password='' --db=analytics
82
+ --migrations-home=/app/clickhouse/migrations
83
+ --ca-cert=/app/certs/ca.pem --cert=/app/certs/client.crt --key=/app/certs/client.key
69
84
  ```
70
85
 
71
86
  Migration file example:
package/lib/migrate.d.ts CHANGED
@@ -1,3 +1,3 @@
1
- declare const migration: (migrations_home: string, host: string, username: string, password: string, db_name: string, db_engine?: string, timeout?: string) => Promise<void>;
1
+ declare const migration: (migrations_home: string, host: string, username: string, password: string, db_name: string, db_engine?: string, timeout?: string, ca_cert?: string | undefined, cert?: string | undefined, key?: string | undefined) => Promise<void>;
2
2
  declare const migrate: () => void;
3
3
  export { migrate, migration };
package/lib/migrate.js CHANGED
@@ -35,15 +35,6 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
35
35
  if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
36
36
  }
37
37
  };
38
- var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
39
- if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
40
- if (ar || !(i in from)) {
41
- if (!ar) ar = Array.prototype.slice.call(from, 0, i);
42
- ar[i] = from[i];
43
- }
44
- }
45
- return to.concat(ar || Array.prototype.slice.call(from));
46
- };
47
38
  var __importDefault = (this && this.__importDefault) || function (mod) {
48
39
  return (mod && mod.__esModule) ? mod : { "default": mod };
49
40
  };
@@ -63,7 +54,7 @@ var log = function (type, message, error) {
63
54
  console.error('\x1b[36m', "clickhouse-migrations :", '\x1b[31m', "Error: ".concat(message), error ? "\n\n ".concat(error) : '');
64
55
  }
65
56
  };
66
- var connect = function (url, username, password, db_name, timeout) {
57
+ var connect = function (url, username, password, db_name, timeout, ca_cert, cert, key) {
67
58
  var db_params = {
68
59
  url: url,
69
60
  username: username,
@@ -76,48 +67,76 @@ var connect = function (url, username, password, db_name, timeout) {
76
67
  if (timeout) {
77
68
  db_params.request_timeout = Number(timeout);
78
69
  }
70
+ if (ca_cert) {
71
+ try {
72
+ if (cert && key) {
73
+ db_params.tls = {
74
+ ca_cert: fs_1.default.readFileSync(ca_cert),
75
+ cert: fs_1.default.readFileSync(cert),
76
+ key: fs_1.default.readFileSync(key),
77
+ };
78
+ }
79
+ else {
80
+ db_params.tls = {
81
+ ca_cert: fs_1.default.readFileSync(ca_cert),
82
+ };
83
+ }
84
+ }
85
+ catch (e) {
86
+ log('error', 'Failed to read CA certificate file for TLS connection.', e instanceof Error ? e.message : String(e));
87
+ process.exit(1);
88
+ }
89
+ }
79
90
  return (0, client_1.createClient)(db_params);
80
91
  };
81
- var create_db = function (host_1, username_1, password_1, db_name_1) {
82
- var args_1 = [];
83
- for (var _i = 4; _i < arguments.length; _i++) {
84
- args_1[_i - 4] = arguments[_i];
85
- }
86
- return __awaiter(void 0, __spreadArray([host_1, username_1, password_1, db_name_1], args_1, true), void 0, function (host, username, password, db_name, db_engine) {
87
- var client, q, e_1;
88
- if (db_engine === void 0) { db_engine = 'ENGINE=Atomic'; }
89
- return __generator(this, function (_a) {
90
- switch (_a.label) {
91
- case 0:
92
- client = connect(host, username, password);
93
- q = "CREATE DATABASE IF NOT EXISTS \"".concat(db_name, "\" ").concat(db_engine);
94
- _a.label = 1;
95
- case 1:
96
- _a.trys.push([1, 3, , 4]);
97
- return [4 /*yield*/, client.exec({
98
- query: q,
99
- clickhouse_settings: {
100
- wait_end_of_query: 1,
101
- },
102
- })];
103
- case 2:
104
- _a.sent();
105
- return [3 /*break*/, 4];
106
- case 3:
107
- e_1 = _a.sent();
108
- log('error', "can't create the database ".concat(db_name, "."), e_1.message);
109
- process.exit(1);
110
- return [3 /*break*/, 4];
111
- case 4: return [4 /*yield*/, client.close()];
112
- case 5:
113
- _a.sent();
114
- return [2 /*return*/];
115
- }
116
- });
92
+ var create_db = function (host, username, password, db_name, db_engine, timeout, ca_cert, cert, key) { return __awaiter(void 0, void 0, void 0, function () {
93
+ var client, e_1, q, e_2;
94
+ return __generator(this, function (_a) {
95
+ switch (_a.label) {
96
+ case 0:
97
+ client = connect(host, username, password, undefined, timeout, ca_cert, cert, key);
98
+ _a.label = 1;
99
+ case 1:
100
+ _a.trys.push([1, 3, , 4]);
101
+ return [4 /*yield*/, client.ping()];
102
+ case 2:
103
+ _a.sent();
104
+ return [3 /*break*/, 4];
105
+ case 3:
106
+ e_1 = _a.sent();
107
+ log('error', "Failed to connect to ClickHouse", e_1.message);
108
+ process.exit(1);
109
+ return [3 /*break*/, 4];
110
+ case 4:
111
+ q = db_engine
112
+ ? "CREATE DATABASE IF NOT EXISTS \"".concat(db_name, "\" ").concat(db_engine)
113
+ : "CREATE DATABASE IF NOT EXISTS \"".concat(db_name, "\"");
114
+ _a.label = 5;
115
+ case 5:
116
+ _a.trys.push([5, 7, , 8]);
117
+ return [4 /*yield*/, client.exec({
118
+ query: q,
119
+ clickhouse_settings: {
120
+ wait_end_of_query: 1,
121
+ },
122
+ })];
123
+ case 6:
124
+ _a.sent();
125
+ return [3 /*break*/, 8];
126
+ case 7:
127
+ e_2 = _a.sent();
128
+ log('error', "can't create the database ".concat(db_name, "."), e_2.message);
129
+ process.exit(1);
130
+ return [3 /*break*/, 8];
131
+ case 8: return [4 /*yield*/, client.close()];
132
+ case 9:
133
+ _a.sent();
134
+ return [2 /*return*/];
135
+ }
117
136
  });
118
- };
137
+ }); };
119
138
  var init_migration_table = function (client) { return __awaiter(void 0, void 0, void 0, function () {
120
- var q, e_2;
139
+ var q, e_3;
121
140
  return __generator(this, function (_a) {
122
141
  switch (_a.label) {
123
142
  case 0:
@@ -135,8 +154,8 @@ var init_migration_table = function (client) { return __awaiter(void 0, void 0,
135
154
  _a.sent();
136
155
  return [3 /*break*/, 4];
137
156
  case 3:
138
- e_2 = _a.sent();
139
- log('error', "can't create the _migrations table.", e_2.message);
157
+ e_3 = _a.sent();
158
+ log('error', "can't create the _migrations table.", e_3.message);
140
159
  process.exit(1);
141
160
  return [3 /*break*/, 4];
142
161
  case 4: return [2 /*return*/];
@@ -176,7 +195,7 @@ var get_migrations = function (migrations_home) {
176
195
  return migrations;
177
196
  };
178
197
  var apply_migrations = function (client, migrations, migrations_home) { return __awaiter(void 0, void 0, void 0, function () {
179
- var migration_query_result, resultSet, e_3, migrations_applied, applied_migrations, _i, migrations_1, migration_1, content, checksum, queries, sets, _a, queries_1, query, e_4, e_5;
198
+ var migration_query_result, resultSet, e_4, migrations_applied, applied_migrations, _i, migrations_1, migration_1, content, checksum, queries, sets, _a, queries_1, query, e_5, e_6;
180
199
  return __generator(this, function (_b) {
181
200
  switch (_b.label) {
182
201
  case 0:
@@ -195,8 +214,8 @@ var apply_migrations = function (client, migrations, migrations_home) { return _
195
214
  migration_query_result = _b.sent();
196
215
  return [3 /*break*/, 5];
197
216
  case 4:
198
- e_3 = _b.sent();
199
- log('error', "can't select data from the _migrations table.", e_3.message);
217
+ e_4 = _b.sent();
218
+ log('error', "can't select data from the _migrations table.", e_4.message);
200
219
  process.exit(1);
201
220
  return [3 /*break*/, 5];
202
221
  case 5:
@@ -252,11 +271,11 @@ var apply_migrations = function (client, migrations, migrations_home) { return _
252
271
  _b.sent();
253
272
  return [3 /*break*/, 11];
254
273
  case 10:
255
- e_4 = _b.sent();
274
+ e_5 = _b.sent();
256
275
  if (applied_migrations) {
257
276
  log('info', "The migration(s) ".concat(applied_migrations, " was successfully applied!"));
258
277
  }
259
- log('error', "the migrations ".concat(migration_1.file, " has an error. Please, fix it (be sure that already executed parts of the migration would not be run second time) and re-run migration script."), e_4.message);
278
+ log('error', "the migrations ".concat(migration_1.file, " has an error. Please, fix it (be sure that already executed parts of the migration would not be run second time) and re-run migration script."), e_5.message);
260
279
  process.exit(1);
261
280
  return [3 /*break*/, 11];
262
281
  case 11:
@@ -273,8 +292,8 @@ var apply_migrations = function (client, migrations, migrations_home) { return _
273
292
  _b.sent();
274
293
  return [3 /*break*/, 15];
275
294
  case 14:
276
- e_5 = _b.sent();
277
- log('error', "can't insert a data into the table _migrations.", e_5.message);
295
+ e_6 = _b.sent();
296
+ log('error', "can't insert a data into the table _migrations.", e_6.message);
278
297
  process.exit(1);
279
298
  return [3 /*break*/, 15];
280
299
  case 15:
@@ -294,16 +313,16 @@ var apply_migrations = function (client, migrations, migrations_home) { return _
294
313
  }
295
314
  });
296
315
  }); };
297
- var migration = function (migrations_home, host, username, password, db_name, db_engine, timeout) { return __awaiter(void 0, void 0, void 0, function () {
316
+ var migration = function (migrations_home, host, username, password, db_name, db_engine, timeout, ca_cert, cert, key) { return __awaiter(void 0, void 0, void 0, function () {
298
317
  var migrations, client;
299
318
  return __generator(this, function (_a) {
300
319
  switch (_a.label) {
301
320
  case 0:
302
321
  migrations = get_migrations(migrations_home);
303
- return [4 /*yield*/, create_db(host, username, password, db_name, db_engine)];
322
+ return [4 /*yield*/, create_db(host, username, password, db_name, db_engine, timeout, ca_cert, cert, key)];
304
323
  case 1:
305
324
  _a.sent();
306
- client = connect(host, username, password, db_name, timeout);
325
+ client = connect(host, username, password, db_name, timeout, ca_cert, cert, key);
307
326
  return [4 /*yield*/, init_migration_table(client)];
308
327
  case 2:
309
328
  _a.sent();
@@ -320,7 +339,7 @@ var migration = function (migrations_home, host, username, password, db_name, db
320
339
  exports.migration = migration;
321
340
  var migrate = function () {
322
341
  var program = new commander_1.Command();
323
- program.name('clickhouse-migrations').description('ClickHouse migrations.').version('1.0.5');
342
+ program.name('clickhouse-migrations').description('ClickHouse migrations.').version('1.1.1');
324
343
  program
325
344
  .command('migrate')
326
345
  .description('Apply migrations.')
@@ -331,10 +350,13 @@ var migrate = function () {
331
350
  .requiredOption('--migrations-home <dir>', "Migrations' directory", process.env.CH_MIGRATIONS_HOME)
332
351
  .option('--db-engine <value>', 'ON CLUSTER and/or ENGINE clauses for database (default: "ENGINE=Atomic")', process.env.CH_MIGRATIONS_DB_ENGINE)
333
352
  .option('--timeout <value>', 'Client request timeout (milliseconds, default value 30000)', process.env.CH_MIGRATIONS_TIMEOUT)
353
+ .option('--ca-cert <path>', 'CA certificate file path', process.env.CH_MIGRATIONS_CA_CERT)
354
+ .option('--cert <path>', 'Client certificate file path', process.env.CH_MIGRATIONS_CERT)
355
+ .option('--key <path>', 'Client key file path', process.env.CH_MIGRATIONS_KEY)
334
356
  .action(function (options) { return __awaiter(void 0, void 0, void 0, function () {
335
357
  return __generator(this, function (_a) {
336
358
  switch (_a.label) {
337
- case 0: return [4 /*yield*/, migration(options.migrationsHome, options.host, options.user, options.password, options.db, options.dbEngine, options.timeout)];
359
+ case 0: return [4 /*yield*/, migration(options.migrationsHome, options.host, options.user, options.password, options.db, options.dbEngine, options.timeout, options.caCert, options.cert, options.key)];
338
360
  case 1:
339
361
  _a.sent();
340
362
  return [2 /*return*/];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "clickhouse-migrations",
3
- "version": "1.0.5",
3
+ "version": "1.1.1",
4
4
  "description": "ClickHouse Migrations",
5
5
  "bin": {
6
6
  "clickhouse-migrations": "lib/cli.js"
@@ -17,6 +17,8 @@
17
17
  "scripts": {
18
18
  "build": "rm lib/*; tsc",
19
19
  "test": "jest --config jestconfig.json",
20
+ "test:unit": "jest --config jestconfig.json --testPathPatterns=\"\\.(unit|integration)\\.test\\.ts$\"",
21
+ "test:e2e": "jest --config jestconfig.json --testPathPatterns=\"\\.e2e\\.test\\.ts$\"",
20
22
  "format": "prettier --write \"src/**/*.ts\" \"src/**/*.js\"",
21
23
  "lint": "eslint ./src",
22
24
  "prepare": "npm run build",