Skip to content

Commit

Permalink
feat: add request timeout support (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
shini4i authored Jun 28, 2024
1 parent 8187dba commit 12d03a7
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 39 deletions.
28 changes: 15 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,27 +38,29 @@ If the database provided in the `--db` option (or in `CH_MIGRATIONS_DB`) doesn't
$ clickhouse-migrations migrate <options>
Required flags
--url=<url> Clickhouse URL (ex. https://clickhouse:8123)
--user=<name> Username
--db=<name> Database name
--migrations-home=<dir> Migrations' directory
--url=<url> Clickhouse URL (ex. https://clickhouse:8123)
--user=<name> Username
--db=<name> Database name
--migrations-home=<dir> Migrations' directory
Optional flags
--password=<password> Password
--engine=<engine> The engine to use for DB creation
--password=<password> Password
--engine=<engine> The engine to use for DB creation
--request-timeout=<milliseconds> Request timeout in milliseconds
Environment variables
Instead of options can be used environment variables.
CH_MIGRATIONS_URL Clickhouse hostname (--url)
CH_MIGRATIONS_USER Username (--user)
CH_MIGRATIONS_PASSWORD Password (--password)
CH_MIGRATIONS_DB Database name (--db)
CH_MIGRATIONS_HOME Migrations' directory (--migrations-home)
CH_MIGRATIONS_ENGINE The engine to use for DB creation (optional)
CH_MIGRATIONS_URL Clickhouse hostname (--url)
CH_MIGRATIONS_USER Username (--user)
CH_MIGRATIONS_PASSWORD Password (--password)
CH_MIGRATIONS_DB Database name (--db)
CH_MIGRATIONS_HOME Migrations' directory (--migrations-home)
CH_MIGRATIONS_ENGINE The engine to use for DB creation (optional)
CH_MIGRATIONS_REQUEST_TIMEOUT Clickhouse client request timeout (optional)
CLI examples
clickhouse-migrations migrate --url=http://localhost:8123
--user=default -db=analytics
--user=default --db=analytics
--migrations-home=/app/clickhouse/migrations
clickhouse-migrations migrate
Expand Down
29 changes: 21 additions & 8 deletions src/migrate.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { ClickHouseClient } from '@clickhouse/client';
import { ClickHouseClientConfigOptions } from '@clickhouse/client';
import { createClient } from '@clickhouse/client';
import { Command } from 'commander';
import fs from 'fs';
Expand All @@ -14,8 +15,8 @@ const log = (type: 'info' | 'error' = 'info', message: string, error?: string) =
}
};

const connect = (url: string, username: string, password?: string, db_name?: string): ClickHouseClient => {
const db_params: ClickhouseDbParams = {
const connect = (url: string, username: string, password?: string, db_name?: string, requestTimeout?: number): ClickHouseClient => {
const db_params: ClickHouseClientConfigOptions = {
url,
username,
password,
Expand All @@ -25,11 +26,16 @@ const connect = (url: string, username: string, password?: string, db_name?: str
if (db_name) {
db_params.database = db_name;
}

if (requestTimeout) {
db_params.request_timeout = requestTimeout;
}

return createClient(db_params);
};

const create_db = async (url: string, username: string, db_name: string, password?: string, engine?: string): Promise<void> => {
const client = connect(url, username, password);
const create_db = async (url: string, username: string, db_name: string, password?: string, engine?: string, requestTimeout?: number): Promise<void> => {
const client = connect(url, username, password, "", requestTimeout);

let q = `CREATE DATABASE IF NOT EXISTS "${db_name}"`;
q += engine ? ` ENGINE = ${engine}` : '';
Expand Down Expand Up @@ -222,13 +228,14 @@ const migration = async (
username: string,
db_name: string,
password?: string,
engine?: string
engine?: string,
requestTimeout?: number
): Promise<void> => {
const migrations = get_migrations(migrations_home);

await create_db(url, username, db_name, password, engine);
await create_db(url, username, db_name, password, engine, requestTimeout);

const client = connect(url, username, password, db_name);
const client = connect(url, username, password, db_name, requestTimeout);

await init_migration_table(client);

Expand All @@ -251,8 +258,14 @@ const migrate = () => {
.requiredOption('--migrations-home <dir>', "Migrations' directory", process.env.CH_MIGRATIONS_HOME)
.option('--password <password>', 'Password', process.env.CH_MIGRATIONS_PASSWORD)
.option('--engine <name>', 'Engine name', process.env.CH_MIGRATIONS_ENGINE)
.option(
"--request-timeout <milliseconds>",
"Request timeout",
(value) => Number(value),
process.env.CH_MIGRATIONS_REQUEST_TIMEOUT ? Number(process.env.CH_MIGRATIONS_REQUEST_TIMEOUT) : undefined,
)
.action(async (options: CliParameters) => {
await migration(options.migrationsHome, options.url, options.user, options.db, options.password, options.engine);
await migration(options.migrationsHome, options.url, options.user, options.db, options.password, options.engine, options.requestTimeout);
});

program.parse();
Expand Down
21 changes: 3 additions & 18 deletions src/types/cli.d.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,5 @@
/// <reference types="node" />

type ClickhouseDbParams = {
url: string;
connect_timeout?: number;
request_timeout?: number;
max_open_connections?: number;
compression?: { response?: boolean; request?: boolean };
username: string;
password?: string;
application?: string;
database?: string;
clickhouse_settings?: ClickHouseSettings;
log?: { enable?: boolean; LoggerClass?: Logger };
tls?: { ca_cert: Buffer; cert?: Buffer; key?: Buffer };
session_id?: string;
};

type MigrationBase = {
version: number;
file: string;
Expand All @@ -25,7 +9,7 @@ type MigrationsRowData = {
version: number;
checksum: string;
migration_name: string;
}
};

type CliParameters = {
migrationsHome: string;
Expand All @@ -34,8 +18,9 @@ type CliParameters = {
db: string;
password?: string;
engine?: string;
requestTimeout?: number;
};

type QueryError = {
message: string;
}
};

0 comments on commit 12d03a7

Please sign in to comment.