-
Notifications
You must be signed in to change notification settings - Fork 0
/
clickhouse.ts
48 lines (48 loc) · 1.63 KB
/
clickhouse.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import { promises as fs } from 'fs';
import { inject, Types } from '@biorate/inversion';
import { Migration } from './migration';
import {
IClickhouseConnector,
IClickhouseConnection,
IClickhouseConfig,
} from '@biorate/clickhouse';
/**
* @description Clickhouse migration class
*/
export class Clickhouse extends Migration {
@inject(Types.Clickhouse) protected connector: IClickhouseConnector;
/**
* @description Clickhouse process method realization
*/
protected async process() {
await this.forEach<IClickhouseConfig, IClickhouseConnection>(
'Clickhouse',
async (config, connection, paths) => {
const tableName = this.config.get<string>('migrations.tableName', 'migrations');
const createQuery = `
CREATE TABLE IF NOT EXISTS {tableName:Identifier} (
name String
)
ENGINE = MergeTree()
PRIMARY KEY (name);
`;
await connection.query(createQuery, { params: { tableName } }).toPromise();
await this.forEachPath(paths, async (file, name) => {
const item = await connection
.query(`SELECT * FROM {tableName:Identifier} WHERE name = {name:String};`, {
params: { name, tableName },
})
.toPromise();
if (item.length) return;
await connection.query(await fs.readFile(file, 'utf8')).toPromise();
await connection
.query(`INSERT INTO {tableName:Identifier} (name) VALUES ({name:String})`, {
params: { name, tableName },
})
.toPromise();
this.log(config.name, name);
});
},
);
}
}