Skip to content

Commit 18901af

Browse files
author
Konrad Bucheli
authored
Merge pull request #3 from open-ch/feature/quote_cq_names
quote cq names
2 parents 1e8e9fc + 339356d commit 18901af

File tree

4 files changed

+95
-81
lines changed

4 files changed

+95
-81
lines changed

influxdb-schema-updater

Lines changed: 64 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -146,10 +146,10 @@ sub extract_database_updates {
146146
my ($db_schemas_in_influxdb, $db_schemas_in_config, $dryrun, $force) = @_;
147147

148148
my ($old_dbs, $eq_dbs, $new_dbs) = get_Ldifference_intersection_Rdifference([keys %{$db_schemas_in_influxdb}], [keys %{$db_schemas_in_config}]);
149-
149+
150150
my %rp_updates;
151151
for my $db (@$eq_dbs, @$new_dbs) {
152-
my ($old, $updated, $new) = extract_retention_policy_updates($db, $db_schemas_in_influxdb->{$db}, $db_schemas_in_config->{$db}->{rps}, $dryrun, $force);
152+
my ($old, $updated, $new) = extract_retention_policy_updates($db, $db_schemas_in_influxdb->{$db}, $db_schemas_in_config->{$db}, $dryrun, $force);
153153
$rp_updates{old_rps}->{$db} = $old;
154154
$rp_updates{updated_rps}->{$db} = $updated;
155155
$rp_updates{new_rps}->{$db} = $new;
@@ -169,7 +169,7 @@ sub extract_database_updates {
169169
object => 'db',
170170
db => $db,
171171
name => $db,
172-
query => "DROP DATABASE $db;",
172+
query => qq{DROP DATABASE "$db";},
173173
skip => $dryrun || !$force,
174174
};
175175
}
@@ -180,7 +180,7 @@ sub extract_database_updates {
180180
object => 'db',
181181
db => $db,
182182
name => $db,
183-
query => $db_schemas_in_config->{$db}->{create_query},
183+
query => qq{CREATE DATABASE "$db";},
184184
skip => $dryrun,
185185
};
186186
}
@@ -209,7 +209,7 @@ sub extract_retention_policy_updates {
209209
object => 'rp',
210210
db => $db,
211211
name => $rp,
212-
query => "DROP RETENTION POLICY \"$rp\" ON $db;",
212+
query => qq{DROP RETENTION POLICY "$rp" ON "$db";},
213213
skip => $dryrun || !$force,
214214
};
215215
}
@@ -221,7 +221,7 @@ sub extract_retention_policy_updates {
221221
object => 'rp',
222222
db => $db,
223223
name => $rp,
224-
query => "ALTER RETENTION POLICY \"$rp\" ON $db DURATION $rps_in_config->{$rp}->{duration} REPLICATION 1 SHARD DURATION $rps_in_config->{$rp}->{shard_duration}" . ($rps_in_config->{$rp}->{default} ? ' DEFAULT;' : ';'),
224+
query => qq{ALTER RETENTION POLICY "$rp" ON "$db" DURATION $rps_in_config->{$rp}->{duration} REPLICATION 1 SHARD DURATION $rps_in_config->{$rp}->{shard_duration}} . ($rps_in_config->{$rp}->{default} ? ' DEFAULT;' : ';'),
225225
skip => $dryrun,
226226
};
227227
}
@@ -233,7 +233,7 @@ sub extract_retention_policy_updates {
233233
object => 'rp',
234234
db => $db,
235235
name => $rp,
236-
query => "CREATE RETENTION POLICY \"$rp\" ON $db DURATION $rps_in_config->{$rp}->{duration} REPLICATION $rps_in_config->{$rp}->{rp_replication} SHARD DURATION $rps_in_config->{$rp}->{shard_duration}" . ($rps_in_config->{$rp}->{default} ? ' DEFAULT;' : ';'),
236+
query => qq{CREATE RETENTION POLICY "$rp" ON "$db" DURATION $rps_in_config->{$rp}->{duration} REPLICATION $rps_in_config->{$rp}->{rp_replication} SHARD DURATION $rps_in_config->{$rp}->{shard_duration}} . ($rps_in_config->{$rp}->{default} ? ' DEFAULT;' : ';'),
237237
skip => $dryrun,
238238
};
239239
}
@@ -276,7 +276,7 @@ sub extract_continuous_query_updates {
276276
object => 'cq',
277277
db => $db,
278278
name => $cq,
279-
query => "DROP CONTINUOUS QUERY $cq ON $db;",
279+
query => qq{DROP CONTINUOUS QUERY "$cq" ON "$db";},
280280
skip => $dryrun || !$force,
281281
};
282282
}
@@ -287,7 +287,7 @@ sub extract_continuous_query_updates {
287287
object => 'cq',
288288
db => $db,
289289
name => $cq,
290-
query => "DROP CONTINUOUS QUERY $cq ON $db; $all_cqs_in_config->{$db}->{$cq};",
290+
query => qq{DROP CONTINUOUS QUERY "$cq" ON "$db"; $all_cqs_in_config->{$db}->{$cq};},
291291
skip => $dryrun,
292292
};
293293
}
@@ -346,7 +346,7 @@ sub load_db_schemas_in_influxdb {
346346

347347
my %db_schemas_in_influxdb;
348348
for my $db (@dbs_in_influxdb) {
349-
my $rp_query_res = query_influxql($influxdb_client, "SHOW RETENTION POLICIES ON $db");
349+
my $rp_query_res = query_influxql($influxdb_client, qq{SHOW RETENTION POLICIES ON "$db"});
350350
$db_schemas_in_influxdb{$db} = {
351351
map { $_->[0] => {
352352
duration => $_->[1],
@@ -369,13 +369,11 @@ sub load_db_schemas_in_influxdb {
369369
# $schema_dir string: the directory name where the config files are
370370
#
371371
# Returns:
372-
# a reference to a hash holding the parsed statements. This hash is structured as below.
372+
# $db_schemas: a reference to a hash holding the parsed statements. This hash is structured as below.
373373
#
374374
# {
375375
# <db> => {
376-
# create_query => '...',
377-
# rps => {
378-
# <rp> => {
376+
# <rp> => {
379377
# duration => ...,
380378
# shard_duration => ...,
381379
# default => ...,
@@ -389,21 +387,39 @@ sub load_db_schemas_in_influxdb {
389387
sub load_db_schemas_in_config {
390388
my ($schema_dir) = @_;
391389

392-
# this will be populated with all statements parsed from the files
393-
my %parsed_statements;
390+
my %db_schemas;
394391
my $db_files = get_schema_files_for_dir("$schema_dir/db");
395-
392+
396393
for my $db_file (@$db_files) {
397394
my $statements_in_file = read_text("$schema_dir/db/$db_file");
398-
my $parsed_statements_from_file = parse_statements($statements_in_file);
399-
400-
# add the statements from this file to the initial hash
401-
%parsed_statements = (%parsed_statements, %$parsed_statements_from_file);
395+
my ($databases, $rps) = parse_statements($statements_in_file);
396+
397+
# loop all the databases and add them to the hash
398+
for my $db (@$databases) {
399+
# make sure every database is only created once
400+
if (exists $db_schemas{$db}) {
401+
die "duplicate database $db in file $db_file detected\n";
402+
}
403+
$db_schemas{$db} = {};
404+
}
405+
# loop all the retention policies and assign them to the correct database
406+
for my $rp (@$rps) {
407+
my $db = $rp->{database};
408+
if (exists $db_schemas{$db}) {
409+
# make sure every retention policy is only created once
410+
if (exists $db_schemas{$db}->{$rp->{rp_name}}) {
411+
die "duplicate rp $rp on db $db in file $db_file detected\n";
412+
}
413+
$db_schemas{$db}->{$rp->{rp_name}} = $rp;
414+
}
415+
else {
416+
die "database $db specified in rp $rp from file $db_file does not exist\n";
417+
}
418+
}
402419
}
403-
return \%parsed_statements;
420+
return \%db_schemas;
404421
}
405422

406-
407423
#
408424
# Iterates over the lines of a config file, parses the valid statements and adds them to the given hash.
409425
# Valid statements are:
@@ -412,19 +428,20 @@ sub load_db_schemas_in_config {
412428
#
413429
# Arguments:
414430
# $string_to_parse string: the contents of the config file (loaded as string)
415-
# $parsed_statements reference: a reference to a hash. The function will populate it with the parsed statements.
416431
#
417432
# Returns:
418-
# a reference to a hash holding the parsed statements from the file.
433+
# $databases: reference to a list which contains all the database names
434+
# $rps: reference to a list which contains hashes of all the retention policies
419435
#
420436
sub parse_statements {
421437
my ($string_to_parse) = @_;
422438
# we want to iterate line-by-line
423439
my @splitted_lines = split "\n", $string_to_parse;
424-
my %parsed_statements;
440+
my @databases;
441+
my @rps;
425442

426443
# captures a 'create database' statement with an optional retention policy definition
427-
my $db_regex = qr/^\s*(create \s+ database \s+ (\w+)) # a create db statement, optionally starting with a space. Any valid word as db name. Captured group.
444+
my $db_regex = qr/^\s*(create \s+ database \s+ "?([\w.]+)"?) # a create db statement, optionally starting with a space. Any valid word as db name. Captured group.
428445
(
429446
\s+ with \s+ duration # optional statement to define a retention policy in the db creation statement
430447
\s+ ((?:\d+[smhdw])|(?:inf)) # captured policy duration as one or more numbers followed by a letter, or 'inf'
@@ -453,7 +470,7 @@ sub parse_statements {
453470
for my $line (@splitted_lines) {
454471
# ignore empty and commented lines
455472
next if $line =~ /^\s*(#.*)?$/;
456-
473+
457474
if ($line =~ /$integrated_regex/) {
458475
# capture groups from the regex
459476
my $create_db_statement = $1;
@@ -472,23 +489,30 @@ sub parse_statements {
472489

473490
# the line is a 'create database' statement...
474491
if ($create_db_statement) {
475-
check_if_db_statement_exists_and_die(\%parsed_statements, $db_name);
476-
$parsed_statements{$db_name}{create_query} = $create_db_statement . ';';
492+
push @databases, $db_name;
477493
# ...and has a retention policy defined inline
478494
if ($inline_rp_statement) {
479-
# flagged as the default policy (assume that the inline policy is the default)
480-
$parsed_statements{$db_name}{rps}{$inline_rp_name} = {duration => $inline_rp_duration,
481-
shard_duration => $inline_rp_shard_duration,
482-
rp_replication => $inline_rp_replication,
483-
default => 1};
495+
# flagged as the default policy (assume that the inline policy is the default)ƒ
496+
push @rps, {
497+
database => $db_name,
498+
rp_name => $inline_rp_name,
499+
duration => $inline_rp_duration,
500+
shard_duration => $inline_rp_shard_duration,
501+
rp_replication => $inline_rp_replication,
502+
default => 1,
503+
};
484504
}
485505
}
486506
# the line is a 'create retention policy' statement
487507
elsif ($rp_name) {
488-
$parsed_statements{$rp_db_name}{rps}{$rp_name} = {duration => $rp_duration,
489-
shard_duration => $rp_shard_duration,
490-
rp_replication => $rp_replication,
491-
default => $default_rp}
508+
push @rps, {
509+
database => $rp_db_name,
510+
rp_name => $rp_name,
511+
duration => $rp_duration,
512+
shard_duration => $rp_shard_duration,
513+
rp_replication => $rp_replication,
514+
default => $default_rp,
515+
}
492516
}
493517
else {
494518
die "error, should never reach this"
@@ -498,29 +522,9 @@ sub parse_statements {
498522
die "could not parse input: $line";
499523
}
500524
}
501-
return \%parsed_statements;
525+
return return \@databases, \@rps;
502526
}
503527

504-
505-
#
506-
# Checks if the db creation statement is already added in the parsed statements hash. If yes then it throws an exception.
507-
#
508-
# Arguments:
509-
# $parsed_statements reference: a reference to the hash containing the db statements
510-
# $db_name string:
511-
# Returns:
512-
# -
513-
#
514-
sub check_if_db_statement_exists_and_die {
515-
my ($parsed_statements, $db_name) = @_;
516-
517-
# there should be no 'create database' statement yet for this database
518-
if ($parsed_statements->{$db_name}{create_query}) {
519-
die "Create statement already defined for database $db_name. Fix the config file";
520-
}
521-
}
522-
523-
524528
# {
525529
# <db> => {
526530
# <cq_name> => <cq_create_query>,
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
CREATE CONTINUOUS QUERY "cq.test" ON "db.test" RESAMPLE EVERY 5m FOR 10m BEGIN SELECT LAST(a) AS b, c INTO "rp.test"."m.test" FROM "rp.test"."m.test" GROUP BY time(5m) END;
2+
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
CREATE DATABASE "db.test" WITH DURATION 260w REPLICATION 1 SHARD DURATION 12w NAME "rp.test";

0 commit comments

Comments
 (0)