clickhouse-migrations 1.1.3 → 1.2.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 +20 -16
- package/lib/migrate.d.ts +1 -1
- package/lib/migrate.js +48 -30
- package/lib/version.d.ts +1 -0
- package/lib/version.js +4 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
|
|
2
1
|
# clickhouse-migrations
|
|
3
2
|
|
|
4
3
|
> ClickHouse Migrations CLI
|
|
@@ -13,9 +12,9 @@ npm install clickhouse-migrations
|
|
|
13
12
|
|
|
14
13
|
Create a directory, where migrations will be stored. It will be used as the value for the `--migrations-home` option (or for environment variable `CH_MIGRATIONS_HOME`).
|
|
15
14
|
|
|
16
|
-
In the directory, create migration files, which should be named like this: `1_some_text.sql`, `2_other_text.sql`, `10_more_test.sql`. What's important here is that the migration version number should come first, followed by an underscore (`_`), and then any text can follow. The version number should increase for every next migration. Please note that once a migration file has been applied to the database, it cannot be modified or removed.
|
|
15
|
+
In the directory, create migration files, which should be named like this: `1_some_text.sql`, `2_other_text.sql`, `10_more_test.sql`. What's important here is that the migration version number should come first, followed by an underscore (`_`), and then any text can follow. The version number should increase for every next migration. Please note that once a migration file has been applied to the database, it cannot be modified or removed.
|
|
17
16
|
|
|
18
|
-
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 `--`, `# `, `#!`.
|
|
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 `--`, `# `, `#!`.
|
|
19
18
|
|
|
20
19
|
If the database provided in the `--db` option (or in `CH_MIGRATIONS_DB`) doesn't exist, it will be created automatically.
|
|
21
20
|
|
|
@@ -26,7 +25,7 @@ For TLS/HTTPS connections, you can provide a custom CA certificate and optional
|
|
|
26
25
|
$ clickhouse-migrations migrate <options>
|
|
27
26
|
|
|
28
27
|
Required options
|
|
29
|
-
--host=<name> Clickhouse hostname
|
|
28
|
+
--host=<name> Clickhouse hostname
|
|
30
29
|
(ex. https://clickhouse:8123)
|
|
31
30
|
--user=<name> Username
|
|
32
31
|
--password=<password> Password
|
|
@@ -36,11 +35,13 @@ For TLS/HTTPS connections, you can provide a custom CA certificate and optional
|
|
|
36
35
|
Optional options
|
|
37
36
|
--db-engine=<value> ON CLUSTER and/or ENGINE for DB
|
|
38
37
|
(default: 'ENGINE=Atomic')
|
|
39
|
-
--
|
|
38
|
+
--table-engine=<value> Engine for the _migrations table
|
|
39
|
+
(default: 'MergeTree')
|
|
40
|
+
--timeout=<value> Client request timeout
|
|
40
41
|
(milliseconds, default: 30000)
|
|
41
42
|
--ca-cert=<path> CA certificate file path
|
|
42
43
|
--cert=<path> Client certificate file path
|
|
43
|
-
--key=<path> Client key file path
|
|
44
|
+
--key=<path> Client key file path
|
|
44
45
|
|
|
45
46
|
Environment variables
|
|
46
47
|
Instead of options can be used environment variables.
|
|
@@ -51,7 +52,8 @@ For TLS/HTTPS connections, you can provide a custom CA certificate and optional
|
|
|
51
52
|
CH_MIGRATIONS_HOME Migrations' directory (--migrations-home)
|
|
52
53
|
|
|
53
54
|
CH_MIGRATIONS_DB_ENGINE (optional) DB engine (--db-engine)
|
|
54
|
-
|
|
55
|
+
CH_MIGRATIONS_TABLE_ENGINE (optional) Migrations table engine (--table-engine)
|
|
56
|
+
CH_MIGRATIONS_TIMEOUT (optional) Client request timeout
|
|
55
57
|
(--timeout)
|
|
56
58
|
CH_MIGRATIONS_CA_CERT (optional) CA certificate file path
|
|
57
59
|
CH_MIGRATIONS_CERT (optional) Client certificate file path
|
|
@@ -61,14 +63,15 @@ For TLS/HTTPS connections, you can provide a custom CA certificate and optional
|
|
|
61
63
|
CLI executions examples
|
|
62
64
|
settings are passed as command-line options
|
|
63
65
|
clickhouse-migrations migrate --host=http://localhost:8123
|
|
64
|
-
--user=default --password='' --db=analytics
|
|
66
|
+
--user=default --password='' --db=analytics
|
|
65
67
|
--migrations-home=/app/clickhouse/migrations
|
|
66
68
|
|
|
67
69
|
settings provided as options, including timeout and db-engine
|
|
68
|
-
clickhouse-migrations migrate --host=http://localhost:8123
|
|
69
|
-
--user=default --password='' --db=analytics
|
|
70
|
-
--migrations-home=/app/clickhouse/migrations --timeout=60000
|
|
71
|
-
--db-engine="ON CLUSTER default ENGINE=Replicated('{replica}')"
|
|
70
|
+
clickhouse-migrations migrate --host=http://localhost:8123
|
|
71
|
+
--user=default --password='' --db=analytics
|
|
72
|
+
--migrations-home=/app/clickhouse/migrations --timeout=60000
|
|
73
|
+
--db-engine="ON CLUSTER default ENGINE=Replicated('{replica}')"
|
|
74
|
+
--table-engine="ReplicatedMergeTree('/clickhouse/tables/{database}/migrations', '{replica}')"
|
|
72
75
|
|
|
73
76
|
settings provided as environment variables
|
|
74
77
|
clickhouse-migrations migrate
|
|
@@ -85,6 +88,7 @@ For TLS/HTTPS connections, you can provide a custom CA certificate and optional
|
|
|
85
88
|
|
|
86
89
|
Migration file example:
|
|
87
90
|
(e.g., located at /app/clickhouse/migrations/1_init.sql)
|
|
91
|
+
|
|
88
92
|
```
|
|
89
93
|
-- an example of migration file 1_init.sql
|
|
90
94
|
|
|
@@ -96,8 +100,8 @@ CREATE TABLE IF NOT EXISTS events (
|
|
|
96
100
|
event JSON
|
|
97
101
|
)
|
|
98
102
|
ENGINE=AggregatingMergeTree
|
|
99
|
-
PARTITION BY toYYYYMM(timestamp)
|
|
100
|
-
SAMPLE BY session_id
|
|
101
|
-
ORDER BY (session_id)
|
|
103
|
+
PARTITION BY toYYYYMM(timestamp)
|
|
104
|
+
SAMPLE BY session_id
|
|
105
|
+
ORDER BY (session_id)
|
|
102
106
|
SETTINGS index_granularity = 8192;
|
|
103
|
-
```
|
|
107
|
+
```
|
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, 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) => 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
|
};
|
|
@@ -45,6 +54,7 @@ var commander_1 = require("commander");
|
|
|
45
54
|
var fs_1 = __importDefault(require("fs"));
|
|
46
55
|
var crypto_1 = __importDefault(require("crypto"));
|
|
47
56
|
var sql_parse_1 = require("./sql-parse");
|
|
57
|
+
var version_1 = require("./version");
|
|
48
58
|
var log = function (type, message, error) {
|
|
49
59
|
if (type === void 0) { type = 'info'; }
|
|
50
60
|
if (type === 'info') {
|
|
@@ -135,33 +145,40 @@ var create_db = function (host, username, password, db_name, db_engine, timeout,
|
|
|
135
145
|
}
|
|
136
146
|
});
|
|
137
147
|
}); };
|
|
138
|
-
var init_migration_table = function (
|
|
139
|
-
var
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
148
|
+
var init_migration_table = function (client_2) {
|
|
149
|
+
var args_1 = [];
|
|
150
|
+
for (var _i = 1; _i < arguments.length; _i++) {
|
|
151
|
+
args_1[_i - 1] = arguments[_i];
|
|
152
|
+
}
|
|
153
|
+
return __awaiter(void 0, __spreadArray([client_2], args_1, true), void 0, function (client, table_engine) {
|
|
154
|
+
var q, e_3;
|
|
155
|
+
if (table_engine === void 0) { table_engine = 'MergeTree'; }
|
|
156
|
+
return __generator(this, function (_a) {
|
|
157
|
+
switch (_a.label) {
|
|
158
|
+
case 0:
|
|
159
|
+
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 = ".concat(table_engine, "\n ORDER BY tuple(applied_at)");
|
|
160
|
+
_a.label = 1;
|
|
161
|
+
case 1:
|
|
162
|
+
_a.trys.push([1, 3, , 4]);
|
|
163
|
+
return [4 /*yield*/, client.command({
|
|
164
|
+
query: q,
|
|
165
|
+
clickhouse_settings: {
|
|
166
|
+
wait_end_of_query: 1,
|
|
167
|
+
},
|
|
168
|
+
})];
|
|
169
|
+
case 2:
|
|
170
|
+
_a.sent();
|
|
171
|
+
return [3 /*break*/, 4];
|
|
172
|
+
case 3:
|
|
173
|
+
e_3 = _a.sent();
|
|
174
|
+
log('error', "can't create the _migrations table.", e_3.message);
|
|
175
|
+
process.exit(1);
|
|
176
|
+
return [3 /*break*/, 4];
|
|
177
|
+
case 4: return [2 /*return*/];
|
|
178
|
+
}
|
|
179
|
+
});
|
|
163
180
|
});
|
|
164
|
-
}
|
|
181
|
+
};
|
|
165
182
|
var get_migrations = function (migrations_home) {
|
|
166
183
|
var files;
|
|
167
184
|
try {
|
|
@@ -313,7 +330,7 @@ var apply_migrations = function (client, migrations, migrations_home) { return _
|
|
|
313
330
|
}
|
|
314
331
|
});
|
|
315
332
|
}); };
|
|
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 () {
|
|
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 () {
|
|
317
334
|
var migrations, client;
|
|
318
335
|
return __generator(this, function (_a) {
|
|
319
336
|
switch (_a.label) {
|
|
@@ -323,7 +340,7 @@ var migration = function (migrations_home, host, username, password, db_name, db
|
|
|
323
340
|
case 1:
|
|
324
341
|
_a.sent();
|
|
325
342
|
client = connect(host, username, password, db_name, timeout, ca_cert, cert, key);
|
|
326
|
-
return [4 /*yield*/, init_migration_table(client)];
|
|
343
|
+
return [4 /*yield*/, init_migration_table(client, table_engine)];
|
|
327
344
|
case 2:
|
|
328
345
|
_a.sent();
|
|
329
346
|
return [4 /*yield*/, apply_migrations(client, migrations, migrations_home)];
|
|
@@ -339,7 +356,7 @@ var migration = function (migrations_home, host, username, password, db_name, db
|
|
|
339
356
|
exports.migration = migration;
|
|
340
357
|
var migrate = function () {
|
|
341
358
|
var program = new commander_1.Command();
|
|
342
|
-
program.name('clickhouse-migrations').description('ClickHouse migrations.').version(
|
|
359
|
+
program.name('clickhouse-migrations').description('ClickHouse migrations.').version(version_1.VERSION);
|
|
343
360
|
program
|
|
344
361
|
.command('migrate')
|
|
345
362
|
.description('Apply migrations.')
|
|
@@ -349,6 +366,7 @@ var migrate = function () {
|
|
|
349
366
|
.requiredOption('--db <name>', 'Database name', process.env.CH_MIGRATIONS_DB)
|
|
350
367
|
.requiredOption('--migrations-home <dir>', "Migrations' directory", process.env.CH_MIGRATIONS_HOME)
|
|
351
368
|
.option('--db-engine <value>', 'ON CLUSTER and/or ENGINE clauses for database (default: "ENGINE=Atomic")', process.env.CH_MIGRATIONS_DB_ENGINE)
|
|
369
|
+
.option('--table-engine <value>', 'Engine for the _migrations table (default: "MergeTree")', process.env.CH_MIGRATIONS_TABLE_ENGINE)
|
|
352
370
|
.option('--timeout <value>', 'Client request timeout (milliseconds, default value 30000)', process.env.CH_MIGRATIONS_TIMEOUT)
|
|
353
371
|
.option('--ca-cert <path>', 'CA certificate file path', process.env.CH_MIGRATIONS_CA_CERT)
|
|
354
372
|
.option('--cert <path>', 'Client certificate file path', process.env.CH_MIGRATIONS_CERT)
|
|
@@ -356,7 +374,7 @@ var migrate = function () {
|
|
|
356
374
|
.action(function (options) { return __awaiter(void 0, void 0, void 0, function () {
|
|
357
375
|
return __generator(this, function (_a) {
|
|
358
376
|
switch (_a.label) {
|
|
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)];
|
|
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)];
|
|
360
378
|
case 1:
|
|
361
379
|
_a.sent();
|
|
362
380
|
return [2 /*return*/];
|
package/lib/version.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const VERSION = "1.2.0";
|
package/lib/version.js
ADDED