clickhouse-migrations 0.1.14 → 1.0.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
@@ -31,6 +31,10 @@ If the database provided in the `--db` option (or in `CH_MIGRATIONS_DB`) doesn't
31
31
  --db=<name> Database name
32
32
  --migrations-home=<dir> Migrations' directory
33
33
 
34
+ Optional options
35
+ --db-engine=<value> ON CLUSTER and/or ENGINE (default: 'ENGINE=Atomic') for db
36
+ --timeout=<value> Client request timeout (milliseconds, default: 30000)
37
+
34
38
  Environment variables
35
39
  Instead of options can be used environment variables.
36
40
  CH_MIGRATIONS_HOST Clickhouse hostname (--host)
@@ -38,13 +42,27 @@ If the database provided in the `--db` option (or in `CH_MIGRATIONS_DB`) doesn't
38
42
  CH_MIGRATIONS_PASSWORD Password (--password)
39
43
  CH_MIGRATIONS_DB Database name (--db)
40
44
  CH_MIGRATIONS_HOME Migrations' directory (--migrations-home)
45
+
46
+ CH_MIGRATIONS_DB_ENGINE (optional) DB engine (--db-engine)
47
+ CH_MIGRATIONS_TIMEOUT (optional) Client request timeout (--timeout)
41
48
 
42
- CLI examples
43
- clickhouse-migrations migrate --host=http://localhost:8123
49
+ CLI executions examples
50
+ settings are passed as command-line options
51
+ clickhouse-migrations migrate --host=http://localhost:8123
44
52
  --user=default --password='' --db=analytics
45
53
  --migrations-home=/app/clickhouse/migrations
46
54
 
47
- clickhouse-migrations migrate
55
+ settings provided as options, including timeout and db-engine
56
+ clickhouse-migrations migrate --host=http://localhost:8123
57
+ --user=default --password='' --db=analytics
58
+ --migrations-home=/app/clickhouse/migrations --timeout=60000
59
+ --db-engine="ON CLUSTER default ENGINE=Replicated('{replica}')"
60
+
61
+ settings provided as environment variables
62
+ clickhouse-migrations migrate
63
+
64
+ settings provided partially through options and environment variables
65
+ clickhouse-migrations migrate --timeout=60000
48
66
  ```
49
67
 
50
68
  Migration file example:
@@ -54,6 +72,13 @@ Migration file example:
54
72
  SET allow_experimental_object_type = 1;
55
73
 
56
74
  CREATE TABLE IF NOT EXISTS events (
75
+ timestamp DateTime('UTC'),
76
+ session_id UInt64,
57
77
  event JSON
58
- );
78
+ )
79
+ ENGINE=AggregatingMergeTree
80
+ PARTITION BY toYYYYMM(timestamp)
81
+ SAMPLE BY session_id
82
+ ORDER BY (session_id)
83
+ SETTINGS index_granularity = 8192;
59
84
  ```
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) => Promise<void>;
1
+ declare const migration: (migrations_home: string, host: string, username: string, password: string, db_name: string, db_engine?: string, timeout?: string) => Promise<void>;
2
2
  declare const migrate: () => void;
3
3
  export { migrate, migration };
package/lib/migrate.js CHANGED
@@ -35,6 +35,15 @@ 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
+ };
38
47
  var __importDefault = (this && this.__importDefault) || function (mod) {
39
48
  return (mod && mod.__esModule) ? mod : { "default": mod };
40
49
  };
@@ -54,9 +63,9 @@ var log = function (type, message, error) {
54
63
  console.error('\x1b[36m', "clickhouse-migrations :", '\x1b[31m', "Error: ".concat(message), error ? "\n\n ".concat(error) : '');
55
64
  }
56
65
  };
57
- var connect = function (host, username, password, db_name) {
66
+ var connect = function (url, username, password, db_name, timeout) {
58
67
  var db_params = {
59
- host: host,
68
+ url: url,
60
69
  username: username,
61
70
  password: password,
62
71
  application: 'clickhouse-migrations',
@@ -64,45 +73,55 @@ var connect = function (host, username, password, db_name) {
64
73
  if (db_name) {
65
74
  db_params.database = db_name;
66
75
  }
76
+ if (timeout) {
77
+ db_params.request_timeout = Number(timeout);
78
+ }
67
79
  return (0, client_1.createClient)(db_params);
68
80
  };
69
- var create_db = function (host, username, password, db_name) { return __awaiter(void 0, void 0, void 0, function () {
70
- var client, q, e_1;
71
- return __generator(this, function (_a) {
72
- switch (_a.label) {
73
- case 0:
74
- client = connect(host, username, password);
75
- q = "CREATE DATABASE IF NOT EXISTS ".concat(db_name, " ENGINE = Atomic");
76
- _a.label = 1;
77
- case 1:
78
- _a.trys.push([1, 3, , 4]);
79
- return [4 /*yield*/, client.exec({
80
- query: q,
81
- clickhouse_settings: {
82
- wait_end_of_query: 1,
83
- },
84
- })];
85
- case 2:
86
- _a.sent();
87
- return [3 /*break*/, 4];
88
- case 3:
89
- e_1 = _a.sent();
90
- log('error', "can't create the database ".concat(db_name, "."), e_1.message);
91
- process.exit(1);
92
- return [3 /*break*/, 4];
93
- case 4: return [4 /*yield*/, client.close()];
94
- case 5:
95
- _a.sent();
96
- return [2 /*return*/];
97
- }
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
+ });
98
117
  });
99
- }); };
118
+ };
100
119
  var init_migration_table = function (client) { return __awaiter(void 0, void 0, void 0, function () {
101
120
  var q, e_2;
102
121
  return __generator(this, function (_a) {
103
122
  switch (_a.label) {
104
123
  case 0:
105
- q = "CREATE TABLE IF NOT EXISTS _migrations (\n uid UUID DEFAULT generateUUIDv4(), \n version UInt32,\n checksum String, \n migration_name String, \n applied_at DateTime DEFAULT now()\n ) \n ENGINE = MergeTree \n ORDER BY tuple(applied_at)";
124
+ q = "CREATE TABLE IF NOT EXISTS _migrations (\n uid UUID DEFAULT generateUUIDv4(),\n version UInt32,\n checksum String,\n migration_name String,\n applied_at DateTime DEFAULT now()\n )\n ENGINE = MergeTree\n ORDER BY tuple(applied_at)";
106
125
  _a.label = 1;
107
126
  case 1:
108
127
  _a.trys.push([1, 3, , 4]);
@@ -274,16 +293,16 @@ var apply_migrations = function (client, migrations, migrations_home) { return _
274
293
  }
275
294
  });
276
295
  }); };
277
- var migration = function (migrations_home, host, username, password, db_name) { return __awaiter(void 0, void 0, void 0, function () {
296
+ var migration = function (migrations_home, host, username, password, db_name, db_engine, timeout) { return __awaiter(void 0, void 0, void 0, function () {
278
297
  var migrations, client;
279
298
  return __generator(this, function (_a) {
280
299
  switch (_a.label) {
281
300
  case 0:
282
301
  migrations = get_migrations(migrations_home);
283
- return [4 /*yield*/, create_db(host, username, password, db_name)];
302
+ return [4 /*yield*/, create_db(host, username, password, db_name, db_engine)];
284
303
  case 1:
285
304
  _a.sent();
286
- client = connect(host, username, password, db_name);
305
+ client = connect(host, username, password, db_name, timeout);
287
306
  return [4 /*yield*/, init_migration_table(client)];
288
307
  case 2:
289
308
  _a.sent();
@@ -300,7 +319,7 @@ var migration = function (migrations_home, host, username, password, db_name) {
300
319
  exports.migration = migration;
301
320
  var migrate = function () {
302
321
  var program = new commander_1.Command();
303
- program.name('clickhouse-migrations').description('ClickHouse migrations.').version('0.1.13');
322
+ program.name('clickhouse-migrations').description('ClickHouse migrations.').version('1.0.0');
304
323
  program
305
324
  .command('migrate')
306
325
  .description('Apply migrations.')
@@ -309,10 +328,12 @@ var migrate = function () {
309
328
  .requiredOption('--password <password>', 'Password', process.env.CH_MIGRATIONS_PASSWORD)
310
329
  .requiredOption('--db <name>', 'Database name', process.env.CH_MIGRATIONS_DB)
311
330
  .requiredOption('--migrations-home <dir>', "Migrations' directory", process.env.CH_MIGRATIONS_HOME)
331
+ .option('--db-engine <value>', 'ON CLUSTER and/or ENGINE clauses for database (default: "ENGINE=Atomic")', process.env.CH_MIGRATIONS_DB_ENGINE)
332
+ .option('--timeout <value>', 'Client request timeout (milliseconds, default value 30000)', process.env.CH_MIGRATIONS_TIMEOUT)
312
333
  .action(function (options) { return __awaiter(void 0, void 0, void 0, function () {
313
334
  return __generator(this, function (_a) {
314
335
  switch (_a.label) {
315
- case 0: return [4 /*yield*/, migration(options.migrationsHome, options.host, options.user, options.password, options.db)];
336
+ case 0: return [4 /*yield*/, migration(options.migrationsHome, options.host, options.user, options.password, options.db, options.dbEngine, options.timeout)];
316
337
  case 1:
317
338
  _a.sent();
318
339
  return [2 /*return*/];
package/package.json CHANGED
@@ -1,12 +1,19 @@
1
1
  {
2
2
  "name": "clickhouse-migrations",
3
- "version": "0.1.14",
3
+ "version": "1.0.0",
4
4
  "description": "ClickHouse Migrations",
5
5
  "bin": {
6
6
  "clickhouse-migrations": "lib/cli.js"
7
7
  },
8
8
  "main": "lib/migrate.js",
9
9
  "types": "lib/migrate.d.ts",
10
+ "engines": {
11
+ "node": ">=16"
12
+ },
13
+ "private": false,
14
+ "license": "MIT",
15
+ "author": "VVVi",
16
+ "repository": "VVVi/clickhouse-migrations",
10
17
  "scripts": {
11
18
  "build": "rm lib/*; tsc",
12
19
  "test": "jest --config jestconfig.json",
@@ -18,25 +25,22 @@
18
25
  "version": "npm run format && git add -A src",
19
26
  "postversion": "git push && git push --tags"
20
27
  },
21
- "author": "",
22
- "license": "MIT",
23
- "repository": "VVVi/clickhouse-migrations",
24
28
  "dependencies": {
25
- "@clickhouse/client": "^0.2.9",
26
- "commander": "^11.1.0"
29
+ "@clickhouse/client": "^1.5.0",
30
+ "commander": "^12.1.0"
27
31
  },
28
32
  "devDependencies": {
29
33
  "@types/jest": "^29.5.12",
30
- "@types/node": "^18.15.11",
31
- "@typescript-eslint/parser": "^6.20.0",
32
- "@typescript-eslint/eslint-plugin": "^6.20.0",
33
- "eslint-plugin-prettier": "^5.1.3",
34
- "eslint": "^8.56.0",
34
+ "@types/node": "^22.5.4",
35
+ "@typescript-eslint/eslint-plugin": "^8.4.0",
36
+ "@typescript-eslint/parser": "^8.4.0",
37
+ "eslint": "^8.57.0",
35
38
  "eslint-config-prettier": "^9.1.0",
39
+ "eslint-plugin-prettier": "^5.2.1",
36
40
  "jest": "^29.7.0",
37
- "prettier": "^3.2.4",
38
- "ts-jest": "^29.1.2",
39
- "typescript": "^5.3.3"
41
+ "prettier": "^3.3.3",
42
+ "ts-jest": "^29.2.5",
43
+ "typescript": "^5.5.4"
40
44
  },
41
45
  "files": [
42
46
  "lib/**/*"