clickhouse-migrations 0.2.2 → 1.0.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
@@ -32,7 +32,10 @@ If the database provided in the `--db` option (or in `CH_MIGRATIONS_DB`) doesn't
32
32
  --migrations-home=<dir> Migrations' directory
33
33
 
34
34
  Optional options
35
- --timeout=<value> Client request timeout (milliseconds, default value 30000)
35
+ --db-engine=<value> ON CLUSTER and/or ENGINE for DB
36
+ (default: 'ENGINE=Atomic')
37
+ --timeout=<value> Client request timeout
38
+ (milliseconds, default: 30000)
36
39
 
37
40
  Environment variables
38
41
  Instead of options can be used environment variables.
@@ -41,25 +44,45 @@ If the database provided in the `--db` option (or in `CH_MIGRATIONS_DB`) doesn't
41
44
  CH_MIGRATIONS_PASSWORD Password (--password)
42
45
  CH_MIGRATIONS_DB Database name (--db)
43
46
  CH_MIGRATIONS_HOME Migrations' directory (--migrations-home)
44
- CH_MIGRATIONS_TIMEOUT Client request timeout (--timeout)
45
47
 
46
- CLI examples
47
- clickhouse-migrations migrate --host=http://localhost:8123
48
+ CH_MIGRATIONS_DB_ENGINE (optional) DB engine (--db-engine)
49
+ CH_MIGRATIONS_TIMEOUT (optional) Client request timeout
50
+ (--timeout)
51
+
52
+ CLI executions examples
53
+ settings are passed as command-line options
54
+ clickhouse-migrations migrate --host=http://localhost:8123
48
55
  --user=default --password='' --db=analytics
49
56
  --migrations-home=/app/clickhouse/migrations
50
57
 
58
+ settings provided as options, including timeout and db-engine
59
+ clickhouse-migrations migrate --host=http://localhost:8123
60
+ --user=default --password='' --db=analytics
61
+ --migrations-home=/app/clickhouse/migrations --timeout=60000
62
+ --db-engine="ON CLUSTER default ENGINE=Replicated('{replica}')"
63
+
64
+ settings provided as environment variables
51
65
  clickhouse-migrations migrate
52
66
 
67
+ settings provided partially through options and environment variables
53
68
  clickhouse-migrations migrate --timeout=60000
54
69
  ```
55
70
 
56
71
  Migration file example:
72
+ (e.g., located at /app/clickhouse/migrations/1_init.sql)
57
73
  ```
58
74
  -- an example of migration file 1_init.sql
59
75
 
60
76
  SET allow_experimental_object_type = 1;
61
77
 
62
78
  CREATE TABLE IF NOT EXISTS events (
79
+ timestamp DateTime('UTC'),
80
+ session_id UInt64,
63
81
  event JSON
64
- );
82
+ )
83
+ ENGINE=AggregatingMergeTree
84
+ PARTITION BY toYYYYMM(timestamp)
85
+ SAMPLE BY session_id
86
+ ORDER BY (session_id)
87
+ SETTINGS index_granularity = 8192;
65
88
  ```
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, 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) => 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
  };
@@ -69,37 +78,44 @@ var connect = function (url, username, password, db_name, timeout) {
69
78
  }
70
79
  return (0, client_1.createClient)(db_params);
71
80
  };
72
- var create_db = function (host, username, password, db_name) { return __awaiter(void 0, void 0, void 0, function () {
73
- var client, q, e_1;
74
- return __generator(this, function (_a) {
75
- switch (_a.label) {
76
- case 0:
77
- client = connect(host, username, password);
78
- q = "CREATE DATABASE IF NOT EXISTS \"".concat(db_name, "\" ENGINE = Atomic");
79
- _a.label = 1;
80
- case 1:
81
- _a.trys.push([1, 3, , 4]);
82
- return [4 /*yield*/, client.exec({
83
- query: q,
84
- clickhouse_settings: {
85
- wait_end_of_query: 1,
86
- },
87
- })];
88
- case 2:
89
- _a.sent();
90
- return [3 /*break*/, 4];
91
- case 3:
92
- e_1 = _a.sent();
93
- log('error', "can't create the database ".concat(db_name, "."), e_1.message);
94
- process.exit(1);
95
- return [3 /*break*/, 4];
96
- case 4: return [4 /*yield*/, client.close()];
97
- case 5:
98
- _a.sent();
99
- return [2 /*return*/];
100
- }
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
+ });
101
117
  });
102
- }); };
118
+ };
103
119
  var init_migration_table = function (client) { return __awaiter(void 0, void 0, void 0, function () {
104
120
  var q, e_2;
105
121
  return __generator(this, function (_a) {
@@ -277,13 +293,13 @@ var apply_migrations = function (client, migrations, migrations_home) { return _
277
293
  }
278
294
  });
279
295
  }); };
280
- var migration = function (migrations_home, host, username, password, db_name, timeout) { 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 () {
281
297
  var migrations, client;
282
298
  return __generator(this, function (_a) {
283
299
  switch (_a.label) {
284
300
  case 0:
285
301
  migrations = get_migrations(migrations_home);
286
- return [4 /*yield*/, create_db(host, username, password, db_name)];
302
+ return [4 /*yield*/, create_db(host, username, password, db_name, db_engine)];
287
303
  case 1:
288
304
  _a.sent();
289
305
  client = connect(host, username, password, db_name, timeout);
@@ -303,7 +319,7 @@ var migration = function (migrations_home, host, username, password, db_name, ti
303
319
  exports.migration = migration;
304
320
  var migrate = function () {
305
321
  var program = new commander_1.Command();
306
- program.name('clickhouse-migrations').description('ClickHouse migrations.').version('0.1.13');
322
+ program.name('clickhouse-migrations').description('ClickHouse migrations.').version('1.0.1');
307
323
  program
308
324
  .command('migrate')
309
325
  .description('Apply migrations.')
@@ -312,11 +328,12 @@ var migrate = function () {
312
328
  .requiredOption('--password <password>', 'Password', process.env.CH_MIGRATIONS_PASSWORD)
313
329
  .requiredOption('--db <name>', 'Database name', process.env.CH_MIGRATIONS_DB)
314
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)
315
332
  .option('--timeout <value>', 'Client request timeout (milliseconds, default value 30000)', process.env.CH_MIGRATIONS_TIMEOUT)
316
333
  .action(function (options) { return __awaiter(void 0, void 0, void 0, function () {
317
334
  return __generator(this, function (_a) {
318
335
  switch (_a.label) {
319
- case 0: return [4 /*yield*/, migration(options.migrationsHome, options.host, options.user, options.password, options.db, options.timeout)];
336
+ case 0: return [4 /*yield*/, migration(options.migrationsHome, options.host, options.user, options.password, options.db, options.dbEngine, options.timeout)];
320
337
  case 1:
321
338
  _a.sent();
322
339
  return [2 /*return*/];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "clickhouse-migrations",
3
- "version": "0.2.2",
3
+ "version": "1.0.1",
4
4
  "description": "ClickHouse Migrations",
5
5
  "bin": {
6
6
  "clickhouse-migrations": "lib/cli.js"