clickhouse-migrations 0.2.2 → 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 +24 -5
- package/lib/migrate.d.ts +1 -1
- package/lib/migrate.js +51 -34
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -32,7 +32,8 @@ 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
|
-
--
|
|
35
|
+
--db-engine=<value> ON CLUSTER and/or ENGINE (default: 'ENGINE=Atomic') for db
|
|
36
|
+
--timeout=<value> Client request timeout (milliseconds, default: 30000)
|
|
36
37
|
|
|
37
38
|
Environment variables
|
|
38
39
|
Instead of options can be used environment variables.
|
|
@@ -41,15 +42,26 @@ If the database provided in the `--db` option (or in `CH_MIGRATIONS_DB`) doesn't
|
|
|
41
42
|
CH_MIGRATIONS_PASSWORD Password (--password)
|
|
42
43
|
CH_MIGRATIONS_DB Database name (--db)
|
|
43
44
|
CH_MIGRATIONS_HOME Migrations' directory (--migrations-home)
|
|
44
|
-
|
|
45
|
+
|
|
46
|
+
CH_MIGRATIONS_DB_ENGINE (optional) DB engine (--db-engine)
|
|
47
|
+
CH_MIGRATIONS_TIMEOUT (optional) Client request timeout (--timeout)
|
|
45
48
|
|
|
46
|
-
CLI examples
|
|
47
|
-
|
|
49
|
+
CLI executions examples
|
|
50
|
+
settings are passed as command-line options
|
|
51
|
+
clickhouse-migrations migrate --host=http://localhost:8123
|
|
48
52
|
--user=default --password='' --db=analytics
|
|
49
53
|
--migrations-home=/app/clickhouse/migrations
|
|
50
54
|
|
|
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
|
|
51
62
|
clickhouse-migrations migrate
|
|
52
63
|
|
|
64
|
+
settings provided partially through options and environment variables
|
|
53
65
|
clickhouse-migrations migrate --timeout=60000
|
|
54
66
|
```
|
|
55
67
|
|
|
@@ -60,6 +72,13 @@ Migration file example:
|
|
|
60
72
|
SET allow_experimental_object_type = 1;
|
|
61
73
|
|
|
62
74
|
CREATE TABLE IF NOT EXISTS events (
|
|
75
|
+
timestamp DateTime('UTC'),
|
|
76
|
+
session_id UInt64,
|
|
63
77
|
event JSON
|
|
64
|
-
)
|
|
78
|
+
)
|
|
79
|
+
ENGINE=AggregatingMergeTree
|
|
80
|
+
PARTITION BY toYYYYMM(timestamp)
|
|
81
|
+
SAMPLE BY session_id
|
|
82
|
+
ORDER BY (session_id)
|
|
83
|
+
SETTINGS index_granularity = 8192;
|
|
65
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, 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 (
|
|
73
|
-
var
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
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.
|
|
322
|
+
program.name('clickhouse-migrations').description('ClickHouse migrations.').version('1.0.0');
|
|
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*/];
|