clickhouse-migrations 0.1.8 → 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/cli.js CHANGED
@@ -1,5 +1,5 @@
1
1
  #!/usr/bin/env node
2
2
  "use strict";
3
3
  Object.defineProperty(exports, "__esModule", { value: true });
4
- var migrate = require('./migrate').migrate;
5
- migrate();
4
+ var migrate_1 = require("./migrate");
5
+ (0, migrate_1.migrate)();
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
@@ -14,7 +14,7 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
14
14
  function verb(n) { return function (v) { return step([n, v]); }; }
15
15
  function step(op) {
16
16
  if (f) throw new TypeError("Generator is already executing.");
17
- while (_) try {
17
+ while (g && (g = 0, op[0] && (_ = 0)), _) try {
18
18
  if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
19
19
  if (y = 0, t) op = [op[0] & 2, t.value];
20
20
  switch (op[0]) {
@@ -35,23 +35,16 @@ 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
- Object.defineProperty(exports, "__esModule", { value: true });
39
- exports.migrate = void 0;
40
- var createClient = require('@clickhouse/client').createClient;
41
- var Command = require('commander').Command;
42
- var fs = require('fs');
43
- var crypto = require('crypto');
44
- // Extract SQL queries from migrations.
45
- var sql_queries = function (content) {
46
- var queries = content
47
- .replace(/^(--|#!|# ).*(\n|\r\n|\r)$/gm, '')
48
- .replace(/(\n|\r\n|\r)/gm, ' ')
49
- .replace(/\s+/g, ' ')
50
- .split(';')
51
- .map(function (el) { return el.trim(); })
52
- .filter(function (el) { return el.length != 0; });
53
- return queries;
38
+ var __importDefault = (this && this.__importDefault) || function (mod) {
39
+ return (mod && mod.__esModule) ? mod : { "default": mod };
54
40
  };
41
+ Object.defineProperty(exports, "__esModule", { value: true });
42
+ exports.migration = exports.migrate = void 0;
43
+ var client_1 = require("@clickhouse/client");
44
+ var commander_1 = require("commander");
45
+ var fs_1 = __importDefault(require("fs"));
46
+ var crypto_1 = __importDefault(require("crypto"));
47
+ var sql_parse_1 = require("./sql-parse");
55
48
  var log = function (type, message, error) {
56
49
  if (type === void 0) { type = 'info'; }
57
50
  if (type === 'info') {
@@ -71,7 +64,7 @@ var connect = function (host, username, password, db_name) {
71
64
  if (db_name) {
72
65
  db_params.database = db_name;
73
66
  }
74
- return new createClient(db_params);
67
+ return (0, client_1.createClient)(db_params);
75
68
  };
76
69
  var create_db = function (host, username, password, db_name) { return __awaiter(void 0, void 0, void 0, function () {
77
70
  var client, q, e_1;
@@ -85,6 +78,9 @@ var create_db = function (host, username, password, db_name) { return __awaiter(
85
78
  _a.trys.push([1, 3, , 4]);
86
79
  return [4 /*yield*/, client.exec({
87
80
  query: q,
81
+ clickhouse_settings: {
82
+ wait_end_of_query: 1,
83
+ },
88
84
  })];
89
85
  case 2:
90
86
  _a.sent();
@@ -112,6 +108,9 @@ var init_migration_table = function (client) { return __awaiter(void 0, void 0,
112
108
  _a.trys.push([1, 3, , 4]);
113
109
  return [4 /*yield*/, client.exec({
114
110
  query: q,
111
+ clickhouse_settings: {
112
+ wait_end_of_query: 1,
113
+ },
115
114
  })];
116
115
  case 2:
117
116
  _a.sent();
@@ -128,7 +127,7 @@ var init_migration_table = function (client) { return __awaiter(void 0, void 0,
128
127
  var get_migrations = function (migrations_home) {
129
128
  var files;
130
129
  try {
131
- files = fs.readdirSync(migrations_home);
130
+ files = fs_1.default.readdirSync(migrations_home);
132
131
  }
133
132
  catch (e) {
134
133
  log('error', "no migration directory ".concat(migrations_home, ". Please create it."));
@@ -137,6 +136,10 @@ var get_migrations = function (migrations_home) {
137
136
  var migrations = [];
138
137
  files.forEach(function (file) {
139
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
+ }
140
143
  // Manage only .sql files.
141
144
  if (!file.endsWith('.sql'))
142
145
  return;
@@ -153,7 +156,7 @@ var get_migrations = function (migrations_home) {
153
156
  return migrations;
154
157
  };
155
158
  var apply_migrations = function (client, migrations, migrations_home) { return __awaiter(void 0, void 0, void 0, function () {
156
- 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;
157
160
  return __generator(this, function (_b) {
158
161
  switch (_b.label) {
159
162
  case 0:
@@ -177,9 +180,10 @@ var apply_migrations = function (client, migrations, migrations_home) { return _
177
180
  process.exit(1);
178
181
  return [3 /*break*/, 5];
179
182
  case 5:
180
- migrations_applied = {};
183
+ migrations_applied = [];
181
184
  migration_query_result.forEach(function (row) {
182
185
  migrations_applied[row.version] = {
186
+ version: row.version,
183
187
  checksum: row.checksum,
184
188
  migration_name: row.migration_name,
185
189
  };
@@ -199,8 +203,8 @@ var apply_migrations = function (client, migrations, migrations_home) { return _
199
203
  case 6:
200
204
  if (!(_i < migrations_1.length)) return [3 /*break*/, 17];
201
205
  migration_1 = migrations_1[_i];
202
- content = fs.readFileSync(migrations_home + '/' + migration_1.file).toString();
203
- checksum = crypto.createHash('md5').update(content).digest('hex');
206
+ content = fs_1.default.readFileSync(migrations_home + '/' + migration_1.file).toString();
207
+ checksum = crypto_1.default.createHash('md5').update(content).digest('hex');
204
208
  if (migrations_applied[migration_1.version]) {
205
209
  // Check if migration file was not changed after apply.
206
210
  if (migrations_applied[migration_1.version].checksum !== checksum) {
@@ -210,7 +214,8 @@ var apply_migrations = function (client, migrations, migrations_home) { return _
210
214
  // Skip if a migration is already applied.
211
215
  return [3 /*break*/, 16];
212
216
  }
213
- queries = sql_queries(content);
217
+ queries = (0, sql_parse_1.sql_queries)(content);
218
+ sets = (0, sql_parse_1.sql_sets)(content);
214
219
  _a = 0, queries_1 = queries;
215
220
  _b.label = 7;
216
221
  case 7:
@@ -221,6 +226,7 @@ var apply_migrations = function (client, migrations, migrations_home) { return _
221
226
  _b.trys.push([8, 10, , 11]);
222
227
  return [4 /*yield*/, client.exec({
223
228
  query: query,
229
+ clickhouse_settings: sets,
224
230
  })];
225
231
  case 9:
226
232
  _b.sent();
@@ -291,9 +297,10 @@ var migration = function (migrations_home, host, username, password, db_name) {
291
297
  }
292
298
  });
293
299
  }); };
300
+ exports.migration = migration;
294
301
  var migrate = function () {
295
- var program = new Command();
296
- program.name('clickhouse-migrations').description('ClickHouse migrations.').version('0.1.8');
302
+ var program = new commander_1.Command();
303
+ program.name('clickhouse-migrations').description('ClickHouse migrations.').version('0.1.10');
297
304
  program
298
305
  .command('migrate')
299
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,15 +1,15 @@
1
1
  {
2
2
  "name": "clickhouse-migrations",
3
- "version": "0.1.8",
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
  },
8
8
  "scripts": {
9
- "build": "tsc",
9
+ "build": "rm lib/*; tsc",
10
10
  "test": "jest --config jestconfig.json",
11
11
  "format": "prettier --write \"src/**/*.ts\" \"src/**/*.js\"",
12
- "lint": "eslint ./src/*.ts",
12
+ "lint": "eslint ./src --ext .ts",
13
13
  "prepare": "npm run build",
14
14
  "prepublishOnly": "npm test && npm run lint",
15
15
  "preversion": "npm run lint",
@@ -20,19 +20,21 @@
20
20
  "license": "MIT",
21
21
  "repository": "VVVi/clickhouse-migrations",
22
22
  "dependencies": {
23
- "@clickhouse/client": "^0.0.9",
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.2",
28
- "@types/node": "^18.11.9",
29
- "@typescript-eslint/parser": "^5.43.0",
30
- "eslint": "^8.26.0",
31
- "eslint-config-prettier": "^8.5.0",
32
- "jest": "^29.2.2",
33
- "prettier": "^2.7.1",
34
- "ts-jest": "^29.0.3",
35
- "typescript": "^4.8.4"
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
+ "eslint-plugin-prettier": "^4.2.1",
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"
36
38
  },
37
39
  "files": [
38
40
  "lib/**/*"