clickhouse-migrations 0.1.9 → 0.1.10

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
@@ -1,7 +1,7 @@
1
1
 
2
2
  # clickhouse-migrations
3
3
 
4
- > CLI NodeJS ClickHouse Migrations.
4
+ > ClickHouse Migrations CLI
5
5
 
6
6
  ## Install
7
7
 
@@ -13,9 +13,9 @@ npm install --global clickhouse-migrations
13
13
 
14
14
  Create a directory, where migrations will be stored. It will be used as the value for the `--migrations-home` option.
15
15
 
16
- In the directory, create migrations - which should be named like: `1_`some_text`.sql`, `2_`other_text`.sql`, ... `10_`more_test`.sql`. What is important here: migration versions followed by underscore - `1_`, `2_`, ...`10_`..., and file extension `.sql`. Any text can follow by version number. The version number should increase for every next migration.
16
+ In the directory, create migrations files - which should be named like: `1_`some_text`.sql`, `2_`other_text`.sql`, ... `10_`more_test`.sql`. What is important here: migration versions followed by underscore - `1_`, `2_`, ...`10_`..., and file extension `.sql`. Any text can follow by version number. The version number should increase for every next migration.
17
17
 
18
- For migrations' content should be used correct SQL ClickHouse queries. A few queries can be used in one migration file. Every query should end with `;`. The queries could be idempotent - for example: `CREATE TABLE IF NOT EXISTS table ...;` For adding comments should be used `--`, `# `, `#!` at the beginning of the line.
18
+ For migrations' content should be used correct SQL ClickHouse queries. A few queries can be used in one migration file. Every query should end with `;`. The queries could be idempotent - for example: `CREATE TABLE IF NOT EXISTS table ...;` For adding comments should be used `--`, `# `, `#!` at the beginning of the line. Can be used ClickHouse settings - that can be applied on query level, in the format: `SET allow_experimental_object_type = 1;` A migration file can't be changed or removed after applying.
19
19
 
20
20
  ```
21
21
  Usage
@@ -37,9 +37,19 @@ For migrations' content should be used correct SQL ClickHouse queries. A few que
37
37
  CH_MIGRATIONS_DB Database name (--db)
38
38
  CH_MIGRATIONS_HOME Migrations' directory (--migrations-home)
39
39
 
40
- Examples
40
+ CLI examples
41
41
  clickhouse-migrations migrate --host=http://localhost:8123
42
42
  --user=default --password='' --db=analytics
43
43
  --migrations-home=/app/clickhouse/migrations
44
44
 
45
45
  clickhouse-migrations migrate
46
+
47
+ Migration file example (could be named 'init-1.sql' and stored in the
48
+ directory provided in --migrations-home)
49
+
50
+ SET allow_experimental_object_type = 1;
51
+
52
+ CREATE TABLE IF NOT EXISTS events (
53
+ event JSON
54
+ );
55
+ ```
package/lib/migrate.d.ts CHANGED
@@ -1 +1,3 @@
1
- export declare const migrate: () => void;
1
+ declare const migration: (migrations_home: string, host: string, username: string, password: string, db_name: string) => Promise<void>;
2
+ declare const migrate: () => void;
3
+ export { migrate, migration };
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
- // Extract SQL queries from migrations.
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.9');
303
+ program.name('clickhouse-migrations').description('ClickHouse migrations.').version('0.1.10');
307
304
  program
308
305
  .command('migrate')
309
306
  .description('Apply migrations.')
@@ -0,0 +1,5 @@
1
+ declare const sql_queries: (content: string) => string[];
2
+ declare const sql_sets: (content: string) => {
3
+ [key: string]: string;
4
+ };
5
+ export { sql_queries, sql_sets };
@@ -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.9",
4
- "description": "CLI NodeJS ClickHouse Migrations",
3
+ "version": "0.1.10",
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.10",
24
- "commander": "^9.4.1"
23
+ "@clickhouse/client": "^0.0.13",
24
+ "commander": "^10.0.0"
25
25
  },
26
26
  "devDependencies": {
27
- "@types/jest": "^29.2.3",
28
- "@types/node": "^18.11.9",
29
- "@typescript-eslint/parser": "^5.44.0",
30
- "@typescript-eslint/eslint-plugin": "^5.44.0",
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.28.0",
33
- "eslint-config-prettier": "^8.5.0",
34
- "jest": "^29.3.1",
35
- "prettier": "^2.8.0",
36
- "ts-jest": "^29.0.3",
37
- "typescript": "^4.9.3"
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/**/*"