clickhouse-migrations 0.1.9 → 0.1.11
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 +19 -5
- package/lib/migrate.d.ts +3 -1
- package/lib/migrate.js +12 -15
- package/lib/sql-parse.d.ts +5 -0
- package/lib/sql-parse.js +31 -0
- package/package.json +14 -14
package/README.md
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
|
|
2
2
|
# clickhouse-migrations
|
|
3
3
|
|
|
4
|
-
>
|
|
4
|
+
> ClickHouse Migrations CLI
|
|
5
5
|
|
|
6
6
|
## Install
|
|
7
7
|
|
|
@@ -11,11 +11,13 @@ npm install --global clickhouse-migrations
|
|
|
11
11
|
|
|
12
12
|
## Usage
|
|
13
13
|
|
|
14
|
-
Create a directory, where migrations will be stored. It will be used as the value for the `--migrations-home` option.
|
|
14
|
+
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
15
|
|
|
16
|
-
In the directory, create
|
|
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.
|
|
17
17
|
|
|
18
|
-
For migrations' content should be used correct SQL ClickHouse queries.
|
|
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 `--`, `# `, `#!` at the beginning of the line.
|
|
19
|
+
|
|
20
|
+
If the database provided in the `--db` option (or in `CH_MIGRATIONS_DB`) doesn't exist, it will be created automatically.
|
|
19
21
|
|
|
20
22
|
```
|
|
21
23
|
Usage
|
|
@@ -37,9 +39,21 @@ For migrations' content should be used correct SQL ClickHouse queries. A few que
|
|
|
37
39
|
CH_MIGRATIONS_DB Database name (--db)
|
|
38
40
|
CH_MIGRATIONS_HOME Migrations' directory (--migrations-home)
|
|
39
41
|
|
|
40
|
-
|
|
42
|
+
CLI examples
|
|
41
43
|
clickhouse-migrations migrate --host=http://localhost:8123
|
|
42
44
|
--user=default --password='' --db=analytics
|
|
43
45
|
--migrations-home=/app/clickhouse/migrations
|
|
44
46
|
|
|
45
47
|
clickhouse-migrations migrate
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
Migration file example:
|
|
51
|
+
```
|
|
52
|
+
-- an example of migration file 1_init.sql
|
|
53
|
+
|
|
54
|
+
SET allow_experimental_object_type = 1;
|
|
55
|
+
|
|
56
|
+
CREATE TABLE IF NOT EXISTS events (
|
|
57
|
+
event JSON
|
|
58
|
+
);
|
|
59
|
+
```
|
package/lib/migrate.d.ts
CHANGED
package/lib/migrate.js
CHANGED
|
@@ -39,22 +39,12 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
39
39
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
40
40
|
};
|
|
41
41
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
42
|
-
exports.migrate = void 0;
|
|
42
|
+
exports.migration = exports.migrate = void 0;
|
|
43
43
|
var client_1 = require("@clickhouse/client");
|
|
44
44
|
var commander_1 = require("commander");
|
|
45
45
|
var fs_1 = __importDefault(require("fs"));
|
|
46
46
|
var crypto_1 = __importDefault(require("crypto"));
|
|
47
|
-
|
|
48
|
-
var sql_queries = function (content) {
|
|
49
|
-
var queries = content
|
|
50
|
-
.replace(/^(--|#!|# ).*(\n|\r\n|\r)$/gm, '')
|
|
51
|
-
.replace(/(\n|\r\n|\r)/gm, ' ')
|
|
52
|
-
.replace(/\s+/g, ' ')
|
|
53
|
-
.split(';')
|
|
54
|
-
.map(function (el) { return el.trim(); })
|
|
55
|
-
.filter(function (el) { return el.length != 0; });
|
|
56
|
-
return queries;
|
|
57
|
-
};
|
|
47
|
+
var sql_parse_1 = require("./sql-parse");
|
|
58
48
|
var log = function (type, message, error) {
|
|
59
49
|
if (type === void 0) { type = 'info'; }
|
|
60
50
|
if (type === 'info') {
|
|
@@ -146,6 +136,10 @@ var get_migrations = function (migrations_home) {
|
|
|
146
136
|
var migrations = [];
|
|
147
137
|
files.forEach(function (file) {
|
|
148
138
|
var version = Number(file.split('_')[0]);
|
|
139
|
+
if (!version) {
|
|
140
|
+
log('error', "a migration name should start from number, example: 1_init.sql. Please check, if the migration ".concat(file, " is named correctly"));
|
|
141
|
+
process.exit(1);
|
|
142
|
+
}
|
|
149
143
|
// Manage only .sql files.
|
|
150
144
|
if (!file.endsWith('.sql'))
|
|
151
145
|
return;
|
|
@@ -162,7 +156,7 @@ var get_migrations = function (migrations_home) {
|
|
|
162
156
|
return migrations;
|
|
163
157
|
};
|
|
164
158
|
var apply_migrations = function (client, migrations, migrations_home) { return __awaiter(void 0, void 0, void 0, function () {
|
|
165
|
-
var migration_query_result, resultSet, e_3, migrations_applied, applied_migrations, _i, migrations_1, migration_1, content, checksum, queries, _a, queries_1, query, e_4, e_5;
|
|
159
|
+
var migration_query_result, resultSet, e_3, migrations_applied, applied_migrations, _i, migrations_1, migration_1, content, checksum, queries, sets, _a, queries_1, query, e_4, e_5;
|
|
166
160
|
return __generator(this, function (_b) {
|
|
167
161
|
switch (_b.label) {
|
|
168
162
|
case 0:
|
|
@@ -220,7 +214,8 @@ var apply_migrations = function (client, migrations, migrations_home) { return _
|
|
|
220
214
|
// Skip if a migration is already applied.
|
|
221
215
|
return [3 /*break*/, 16];
|
|
222
216
|
}
|
|
223
|
-
queries = sql_queries(content);
|
|
217
|
+
queries = (0, sql_parse_1.sql_queries)(content);
|
|
218
|
+
sets = (0, sql_parse_1.sql_sets)(content);
|
|
224
219
|
_a = 0, queries_1 = queries;
|
|
225
220
|
_b.label = 7;
|
|
226
221
|
case 7:
|
|
@@ -231,6 +226,7 @@ var apply_migrations = function (client, migrations, migrations_home) { return _
|
|
|
231
226
|
_b.trys.push([8, 10, , 11]);
|
|
232
227
|
return [4 /*yield*/, client.exec({
|
|
233
228
|
query: query,
|
|
229
|
+
clickhouse_settings: sets,
|
|
234
230
|
})];
|
|
235
231
|
case 9:
|
|
236
232
|
_b.sent();
|
|
@@ -301,9 +297,10 @@ var migration = function (migrations_home, host, username, password, db_name) {
|
|
|
301
297
|
}
|
|
302
298
|
});
|
|
303
299
|
}); };
|
|
300
|
+
exports.migration = migration;
|
|
304
301
|
var migrate = function () {
|
|
305
302
|
var program = new commander_1.Command();
|
|
306
|
-
program.name('clickhouse-migrations').description('ClickHouse migrations.').version('0.1.
|
|
303
|
+
program.name('clickhouse-migrations').description('ClickHouse migrations.').version('0.1.11');
|
|
307
304
|
program
|
|
308
305
|
.command('migrate')
|
|
309
306
|
.description('Apply migrations.')
|
package/lib/sql-parse.js
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.sql_sets = exports.sql_queries = void 0;
|
|
4
|
+
// Extract sql queries from migrations.
|
|
5
|
+
var sql_queries = function (content) {
|
|
6
|
+
var queries = content
|
|
7
|
+
.replace(/^(--|#!|#\s|SET\s).*(\n|\r\n|\r|$)/gm, '')
|
|
8
|
+
.replace(/(\n|\r\n|\r)/gm, ' ')
|
|
9
|
+
.replace(/\s+/g, ' ')
|
|
10
|
+
.split(';')
|
|
11
|
+
.map(function (el) { return el.trim(); })
|
|
12
|
+
.filter(function (el) { return el.length != 0; });
|
|
13
|
+
return queries;
|
|
14
|
+
};
|
|
15
|
+
exports.sql_queries = sql_queries;
|
|
16
|
+
// Extract query settings from migrations.
|
|
17
|
+
var sql_sets = function (content) {
|
|
18
|
+
var sets = {};
|
|
19
|
+
var sets_arr = content
|
|
20
|
+
.replace(/^(?!SET\s).*(\n|\r\n|\r|$)/gm, '')
|
|
21
|
+
.replace(/^(SET\s)/gm, '')
|
|
22
|
+
.replace(/(\n|\r\n|\r)/gm, ' ')
|
|
23
|
+
.replace(/\s+/g, '')
|
|
24
|
+
.split(';');
|
|
25
|
+
sets_arr.forEach(function (set_full) {
|
|
26
|
+
var set = set_full.split('=');
|
|
27
|
+
sets[set[0]] = set[1];
|
|
28
|
+
});
|
|
29
|
+
return sets;
|
|
30
|
+
};
|
|
31
|
+
exports.sql_sets = sql_sets;
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "clickhouse-migrations",
|
|
3
|
-
"version": "0.1.
|
|
4
|
-
"description": "
|
|
3
|
+
"version": "0.1.11",
|
|
4
|
+
"description": "ClickHouse Migrations",
|
|
5
5
|
"bin": {
|
|
6
6
|
"clickhouse-migrations": "lib/cli.js"
|
|
7
7
|
},
|
|
@@ -20,21 +20,21 @@
|
|
|
20
20
|
"license": "MIT",
|
|
21
21
|
"repository": "VVVi/clickhouse-migrations",
|
|
22
22
|
"dependencies": {
|
|
23
|
-
"@clickhouse/client": "^0.0.
|
|
24
|
-
"commander": "^
|
|
23
|
+
"@clickhouse/client": "^0.0.13",
|
|
24
|
+
"commander": "^10.0.0"
|
|
25
25
|
},
|
|
26
26
|
"devDependencies": {
|
|
27
|
-
"@types/jest": "^29.
|
|
28
|
-
"@types/node": "^18.
|
|
29
|
-
"@typescript-eslint/parser": "^5.
|
|
30
|
-
"@typescript-eslint/eslint-plugin": "^5.
|
|
27
|
+
"@types/jest": "^29.4.1",
|
|
28
|
+
"@types/node": "^18.15.3",
|
|
29
|
+
"@typescript-eslint/parser": "^5.55.0",
|
|
30
|
+
"@typescript-eslint/eslint-plugin": "^5.55.0",
|
|
31
31
|
"eslint-plugin-prettier": "^4.2.1",
|
|
32
|
-
"eslint": "^8.
|
|
33
|
-
"eslint-config-prettier": "^8.
|
|
34
|
-
"jest": "^29.
|
|
35
|
-
"prettier": "^2.8.
|
|
36
|
-
"ts-jest": "^29.0.
|
|
37
|
-
"typescript": "^4.9.
|
|
32
|
+
"eslint": "^8.36.0",
|
|
33
|
+
"eslint-config-prettier": "^8.7.0",
|
|
34
|
+
"jest": "^29.5.0",
|
|
35
|
+
"prettier": "^2.8.4",
|
|
36
|
+
"ts-jest": "^29.0.5",
|
|
37
|
+
"typescript": "^4.9.5"
|
|
38
38
|
},
|
|
39
39
|
"files": [
|
|
40
40
|
"lib/**/*"
|