clickhouse-migrations 1.2.0 → 1.3.0

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
@@ -16,7 +16,7 @@ In the directory, create migration files, which should be named like this: `1_so
16
16
 
17
17
  For migrations' content should be used correct SQL ClickHouse queries. Multiple queries can be used in a single migration file, and each query should be terminated with a semicolon (;). The queries could be idempotent - for example: `CREATE TABLE IF NOT EXISTS table ...;` Clickhouse settings, that can be included at the query level, can be added like `SET allow_experimental_object_type = 1;`. For adding comments should be used `--`, `# `, `#!`.
18
18
 
19
- If the database provided in the `--db` option (or in `CH_MIGRATIONS_DB`) doesn't exist, it will be created automatically.
19
+ If the database provided in the `--db` option (or in `CH_MIGRATIONS_DB`) doesn't exist, it will be created automatically. To disable this behavior (for example, when the user has no privileges to create databases), use the `--skip-db-creation` option (or set `CH_MIGRATIONS_SKIP_DB_CREATION` to `'true'`); in that case the database must already exist.
20
20
 
21
21
  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).
22
22
 
@@ -42,6 +42,7 @@ For TLS/HTTPS connections, you can provide a custom CA certificate and optional
42
42
  --ca-cert=<path> CA certificate file path
43
43
  --cert=<path> Client certificate file path
44
44
  --key=<path> Client key file path
45
+ --skip-db-creation Skip database creation
45
46
 
46
47
  Environment variables
47
48
  Instead of options can be used environment variables.
@@ -58,6 +59,10 @@ For TLS/HTTPS connections, you can provide a custom CA certificate and optional
58
59
  CH_MIGRATIONS_CA_CERT (optional) CA certificate file path
59
60
  CH_MIGRATIONS_CERT (optional) Client certificate file path
60
61
  CH_MIGRATIONS_KEY (optional) Client key file path
62
+ CH_MIGRATIONS_SKIP_DB_CREATION
63
+ (optional) Skip database creation,
64
+ set to 'true' to enable
65
+ (--skip-db-creation)
61
66
 
62
67
 
63
68
  CLI executions examples
@@ -84,6 +89,11 @@ For TLS/HTTPS connections, you can provide a custom CA certificate and optional
84
89
  --user=default --password='' --db=analytics
85
90
  --migrations-home=/app/clickhouse/migrations
86
91
  --ca-cert=/app/certs/ca.pem --cert=/app/certs/client.crt --key=/app/certs/client.key
92
+
93
+ skip database creation (assumes database already exists)
94
+ clickhouse-migrations migrate --host=http://localhost:8123
95
+ --user=default --password='' --db=analytics
96
+ --migrations-home=/app/clickhouse/migrations --skip-db-creation
87
97
  ```
88
98
 
89
99
  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, table_engine?: string, timeout?: string, ca_cert?: string | undefined, cert?: string | undefined, key?: string | undefined) => Promise<void>;
1
+ declare const migration: (migrations_home: string, host: string, username: string, password: string, db_name: string, db_engine?: string, table_engine?: string, timeout?: string, ca_cert?: string | undefined, cert?: string | undefined, key?: string | undefined, skip_db_creation?: boolean) => Promise<void>;
2
2
  declare const migrate: () => void;
3
3
  export { migrate, migration };
package/lib/migrate.js CHANGED
@@ -99,8 +99,8 @@ var connect = function (url, username, password, db_name, timeout, ca_cert, cert
99
99
  }
100
100
  return (0, client_1.createClient)(db_params);
101
101
  };
102
- 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 () {
103
- var client, e_1, q, e_2;
102
+ var create_db = function (host, username, password, db_name, db_engine, timeout, ca_cert, cert, key, skip_db_creation) { return __awaiter(void 0, void 0, void 0, function () {
103
+ var client, e_1, exists, resultSet, rows, e_2, q, e_3;
104
104
  return __generator(this, function (_a) {
105
105
  switch (_a.label) {
106
106
  case 0:
@@ -118,28 +118,66 @@ var create_db = function (host, username, password, db_name, db_engine, timeout,
118
118
  process.exit(1);
119
119
  return [3 /*break*/, 4];
120
120
  case 4:
121
+ if (!skip_db_creation) return [3 /*break*/, 14];
122
+ exists = false;
123
+ _a.label = 5;
124
+ case 5:
125
+ _a.trys.push([5, 8, , 10]);
126
+ return [4 /*yield*/, client.query({
127
+ query: "SELECT 1 AS ok FROM system.databases WHERE name = {db_name:String}",
128
+ query_params: { db_name: db_name },
129
+ format: 'JSONEachRow',
130
+ })];
131
+ case 6:
132
+ resultSet = _a.sent();
133
+ return [4 /*yield*/, resultSet.json()];
134
+ case 7:
135
+ rows = (_a.sent());
136
+ exists = rows.length > 0;
137
+ return [3 /*break*/, 10];
138
+ case 8:
139
+ e_2 = _a.sent();
140
+ log('error', "can't verify that the database ".concat(db_name, " exists."), e_2.message);
141
+ return [4 /*yield*/, client.close()];
142
+ case 9:
143
+ _a.sent();
144
+ process.exit(1);
145
+ return [3 /*break*/, 10];
146
+ case 10:
147
+ if (!!exists) return [3 /*break*/, 12];
148
+ log('error', "the database ".concat(db_name, " doesn't exist and --skip-db-creation is set. Please create the database manually or remove --skip-db-creation."));
149
+ return [4 /*yield*/, client.close()];
150
+ case 11:
151
+ _a.sent();
152
+ process.exit(1);
153
+ _a.label = 12;
154
+ case 12: return [4 /*yield*/, client.close()];
155
+ case 13:
156
+ _a.sent();
157
+ return [2 /*return*/];
158
+ case 14:
121
159
  q = db_engine
122
160
  ? "CREATE DATABASE IF NOT EXISTS \"".concat(db_name, "\" ").concat(db_engine)
123
161
  : "CREATE DATABASE IF NOT EXISTS \"".concat(db_name, "\"");
124
- _a.label = 5;
125
- case 5:
126
- _a.trys.push([5, 7, , 8]);
162
+ _a.label = 15;
163
+ case 15:
164
+ _a.trys.push([15, 17, , 18]);
127
165
  return [4 /*yield*/, client.command({
128
166
  query: q,
129
167
  clickhouse_settings: {
130
168
  wait_end_of_query: 1,
131
169
  },
132
170
  })];
133
- case 6:
171
+ case 16:
134
172
  _a.sent();
135
- return [3 /*break*/, 8];
136
- case 7:
137
- e_2 = _a.sent();
138
- log('error', "can't create the database ".concat(db_name, "."), e_2.message);
173
+ return [3 /*break*/, 18];
174
+ case 17:
175
+ e_3 = _a.sent();
176
+ log('error', "can't create the database ".concat(db_name, "."), e_3.message);
139
177
  process.exit(1);
140
- return [3 /*break*/, 8];
141
- case 8: return [4 /*yield*/, client.close()];
142
- case 9:
178
+ return [3 /*break*/, 18];
179
+ case 18: return [4 /*yield*/, client.close()];
180
+ case 19:
143
181
  _a.sent();
144
182
  return [2 /*return*/];
145
183
  }
@@ -151,7 +189,7 @@ var init_migration_table = function (client_2) {
151
189
  args_1[_i - 1] = arguments[_i];
152
190
  }
153
191
  return __awaiter(void 0, __spreadArray([client_2], args_1, true), void 0, function (client, table_engine) {
154
- var q, e_3;
192
+ var q, e_4;
155
193
  if (table_engine === void 0) { table_engine = 'MergeTree'; }
156
194
  return __generator(this, function (_a) {
157
195
  switch (_a.label) {
@@ -170,8 +208,8 @@ var init_migration_table = function (client_2) {
170
208
  _a.sent();
171
209
  return [3 /*break*/, 4];
172
210
  case 3:
173
- e_3 = _a.sent();
174
- log('error', "can't create the _migrations table.", e_3.message);
211
+ e_4 = _a.sent();
212
+ log('error', "can't create the _migrations table.", e_4.message);
175
213
  process.exit(1);
176
214
  return [3 /*break*/, 4];
177
215
  case 4: return [2 /*return*/];
@@ -212,7 +250,7 @@ var get_migrations = function (migrations_home) {
212
250
  return migrations;
213
251
  };
214
252
  var apply_migrations = function (client, migrations, migrations_home) { return __awaiter(void 0, void 0, void 0, function () {
215
- 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;
253
+ var migration_query_result, resultSet, e_5, migrations_applied, applied_migrations, _i, migrations_1, migration_1, content, checksum, queries, sets, _a, queries_1, query, e_6, e_7;
216
254
  return __generator(this, function (_b) {
217
255
  switch (_b.label) {
218
256
  case 0:
@@ -231,8 +269,8 @@ var apply_migrations = function (client, migrations, migrations_home) { return _
231
269
  migration_query_result = _b.sent();
232
270
  return [3 /*break*/, 5];
233
271
  case 4:
234
- e_4 = _b.sent();
235
- log('error', "can't select data from the _migrations table.", e_4.message);
272
+ e_5 = _b.sent();
273
+ log('error', "can't select data from the _migrations table.", e_5.message);
236
274
  process.exit(1);
237
275
  return [3 /*break*/, 5];
238
276
  case 5:
@@ -288,11 +326,11 @@ var apply_migrations = function (client, migrations, migrations_home) { return _
288
326
  _b.sent();
289
327
  return [3 /*break*/, 11];
290
328
  case 10:
291
- e_5 = _b.sent();
329
+ e_6 = _b.sent();
292
330
  if (applied_migrations) {
293
331
  log('info', "The migration(s) ".concat(applied_migrations, " was successfully applied!"));
294
332
  }
295
- 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);
333
+ 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_6.message);
296
334
  process.exit(1);
297
335
  return [3 /*break*/, 11];
298
336
  case 11:
@@ -309,8 +347,8 @@ var apply_migrations = function (client, migrations, migrations_home) { return _
309
347
  _b.sent();
310
348
  return [3 /*break*/, 15];
311
349
  case 14:
312
- e_6 = _b.sent();
313
- log('error', "can't insert a data into the table _migrations.", e_6.message);
350
+ e_7 = _b.sent();
351
+ log('error', "can't insert a data into the table _migrations.", e_7.message);
314
352
  process.exit(1);
315
353
  return [3 /*break*/, 15];
316
354
  case 15:
@@ -330,13 +368,13 @@ var apply_migrations = function (client, migrations, migrations_home) { return _
330
368
  }
331
369
  });
332
370
  }); };
333
- var migration = function (migrations_home, host, username, password, db_name, db_engine, table_engine, timeout, ca_cert, cert, key) { return __awaiter(void 0, void 0, void 0, function () {
371
+ var migration = function (migrations_home, host, username, password, db_name, db_engine, table_engine, timeout, ca_cert, cert, key, skip_db_creation) { return __awaiter(void 0, void 0, void 0, function () {
334
372
  var migrations, client;
335
373
  return __generator(this, function (_a) {
336
374
  switch (_a.label) {
337
375
  case 0:
338
376
  migrations = get_migrations(migrations_home);
339
- return [4 /*yield*/, create_db(host, username, password, db_name, db_engine, timeout, ca_cert, cert, key)];
377
+ return [4 /*yield*/, create_db(host, username, password, db_name, db_engine, timeout, ca_cert, cert, key, skip_db_creation)];
340
378
  case 1:
341
379
  _a.sent();
342
380
  client = connect(host, username, password, db_name, timeout, ca_cert, cert, key);
@@ -371,10 +409,11 @@ var migrate = function () {
371
409
  .option('--ca-cert <path>', 'CA certificate file path', process.env.CH_MIGRATIONS_CA_CERT)
372
410
  .option('--cert <path>', 'Client certificate file path', process.env.CH_MIGRATIONS_CERT)
373
411
  .option('--key <path>', 'Client key file path', process.env.CH_MIGRATIONS_KEY)
412
+ .option('--skip-db-creation', 'Skip database creation', process.env.CH_MIGRATIONS_SKIP_DB_CREATION === 'true')
374
413
  .action(function (options) { return __awaiter(void 0, void 0, void 0, function () {
375
414
  return __generator(this, function (_a) {
376
415
  switch (_a.label) {
377
- case 0: return [4 /*yield*/, migration(options.migrationsHome, options.host, options.user, options.password, options.db, options.dbEngine, options.tableEngine, options.timeout, options.caCert, options.cert, options.key)];
416
+ case 0: return [4 /*yield*/, migration(options.migrationsHome, options.host, options.user, options.password, options.db, options.dbEngine, options.tableEngine, options.timeout, options.caCert, options.cert, options.key, options.skipDbCreation)];
378
417
  case 1:
379
418
  _a.sent();
380
419
  return [2 /*return*/];
package/lib/version.d.ts CHANGED
@@ -1 +1 @@
1
- export declare const VERSION = "1.2.0";
1
+ export declare const VERSION = "1.3.0";
package/lib/version.js CHANGED
@@ -1,4 +1,4 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.VERSION = void 0;
4
- exports.VERSION = '1.2.0'; // x-release-please-version
4
+ exports.VERSION = '1.3.0'; // x-release-please-version
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "clickhouse-migrations",
3
- "version": "1.2.0",
3
+ "version": "1.3.0",
4
4
  "description": "ClickHouse Migrations",
5
5
  "bin": {
6
6
  "clickhouse-migrations": "lib/cli.js"