App-Sqitch
view release on metacpan or search on metacpan
lib/App/Sqitch/Engine/clickhouse.pm view on Meta::CPAN
has registry_uri => (
is => 'ro',
isa => URIDB,
lazy => 1,
default => sub {
my $self = shift;
my $uri = $self->uri->clone;
$uri->dbname($self->registry);
return $uri;
},
);
sub registry_destination {
my $uri = shift->registry_uri;
if ($uri->password) {
$uri = $uri->clone;
$uri->password(undef);
}
return $uri->as_string;
}
sub _load_xml {
my $path = shift;
require XML::Tiny;
my $doc = XML::Tiny::parsefile($path->stringify);
return {} unless @{ $doc } > 0;
return _xml2hash($doc->[0]);
}
sub _xml2hash {
my $e = shift;
my $n = $e->{content};
# Return text if it's a text node.
return $n->[0]{content} if @{ $n } == 1 && $n->[0]{type} eq 't';
my $hash = {};
for my $c (@{ $n }) {
# We only care about element nodes.
next if $c->{type} ne 'e';
if (my $prev = $hash->{ $c->{name} }) {
# Convert to an array.
$hash->{ $c->{name} } = $prev = [$prev] unless ref $prev eq 'ARRAY';
push @{ $prev } => _xml2hash($c)
} else {
$hash->{ $c->{name} } = _xml2hash($c);
}
}
return $hash;
}
sub _is_true($) {
my $val = shift || return 0;
# https://github.com/ClickHouse/ClickHouse/blob/ce5a43c/base/poco/Util/src/AbstractConfiguration.cpp#L528C29-L547
return $val != 0 || 0 if looks_like_number $val;
$val = lc $val;
return $val eq 'true' || $val eq 'yes' || $val eq 'on' || 0;
}
# Connection name defaults to host name from url, or else hostname from config
# or else localhost. Then look for that name in a connection under
# `connections_credentials`. If it exists, copy/overwrite `hostname`, `port`,
# `secure`, `user`, `password`, and `database`. Fall back on root object
# values `host` (not `hostname`) `port`, `secure`, `user`, `password`, and
# `database`.
#
# https://github.com/ClickHouse/ClickHouse/blob/d0facf0/programs/client/Client.cpp#L139-L212
sub _conn_cfg {
my ($cfg, $host) = @_;
# Copy root-level configs.
my $conn = {
(exists $cfg->{secure} ? (secure => _is_true $cfg->{secure}) : ()),
map { ( $_ => $cfg->{$_}) } grep { $cfg->{$_} } qw(host port user password database),
};
# Copy client TLS config if exists.
if (my $tls = $cfg->{openSSL}) {
$conn->{tls} = $tls->{client} if $tls->{client};
}
# Copy connection credentials for this host if they exists.
$host ||= $cfg->{host} || 'localhost';
my $creds = $cfg->{connections_credentials} or return $conn;
my $conns = $creds->{connection} or return $conn;
for my $c (@{ ref $conns eq 'ARRAY' ? $conns : [$conns] }) {
next unless ($c->{name} || '') eq $host;
if (exists $c->{secure}) {
$conn->{secure} = _is_true $c->{secure}
}
$conn->{host} = $c->{hostname} if $c->{hostname};
$conn->{$_} = $c->{$_} for grep { $c->{$_} } qw(port user password database);
}
return $conn;
}
has _clickcnf => (
is => 'rw',
isa => HashRef,
lazy => 1,
default => \&_load_cfg,
);
sub _load_cfg {
my $self = shift;
# https://clickhouse.com/docs/interfaces/cli#configuration_files
# https://github.com/ClickHouse/ClickHouse/blob/master/src/Common/Config/getClientConfigPath.cpp
for my $spec (
['.', 'clickhouse-client'],
[App::Sqitch::Config->home_dir, '.clickhouse-client'],
['etc', 'clickhouse-client'],
) {
for my $ext (qw(xml yaml yml)) {
my $path = file $spec->[0], "$spec->[1].$ext";
next unless -f $path;
my $config = $ext eq 'xml' ? _load_xml $path : do {
require YAML::Tiny;
YAML::Tiny->read($path)->[0];
};
# We want the hostname specified by the user, if present.
my $host = $ENV{CLICKHOUSE_HOST} || $self->SUPER::uri->host;
return _conn_cfg $config, $host;
}
}
return {};
}
sub _def_user { $ENV{CLICKHOUSE_USER} || $_[0]->_clickcnf->{user} }
sub _def_pass { $ENV{CLICKHOUSE_PASSWORD} || shift->_clickcnf->{password} }
sub _dsn {
# Always set the host name to the default if it's not set. Otherwise
# URI::db::_odbc returns the DSN `dbi:ODBC:DSN=sqitch;Driver=ClickHouse`.
# We don't want that, because no such DSN exists. By setting the host
# name, it instead returns
# `dbi:ODBC:Server=localhost;Database=sqitch;Driver=ClickHouse`, almost
# certainly more correct.
my $uri = shift->registry_uri;
unless ($uri->host) {
$uri = $uri->clone;
$uri->host('localhost');
}
return $uri->dbi_dsn
}
( run in 1.235 second using v1.01-cache-2.11-cpan-cdf2f3d4e48 )