blib/lib/App/Netdisco/DB/StepFile.pm untested

filter: Statementstmt 0.0% 0 / 81 Branchbran 0.0% 0 / 28 Conditioncond 0.0% 0 / 18 MC/DCmcdc 0.0% 0 / 11 Subroutinesub 0.0% 0 / 12 Podpod 0.0% 0 / 8 totaltotal 0.0% 0 / 158 CC 15 SCAR 31.0
CC
15
Cov
0%
inline_comment_lines
42.0
_tokenize
30.0
parse_step_file
30.0
CRAP 22.3
1 package App::Netdisco::DB::StepFile;
2  
3 use strict;
4 use warnings;
5  
6 use Carp ();
7  
8 use base 'Exporter';
9 our @EXPORT_OK = qw(
10 parse_step_file legacy_statements inline_comment_lines step_file_path
11 );
12  
13 my $NO_TRANSACTION_DIRECTIVE = '-- netdisco: no-transaction';
14  
15 sub step_file_path {
16 my ($share_dir, $from) = @_;
17 return sprintf '%s/App-Netdisco-DB-%d-%d-PostgreSQL.sql', $share_dir, $from, $from + 1;
18 }
19  
20 sub _slurp {
21 my $path = shift;
22 open my $fh, '<', $path or Carp::croak("Cannot read schema step file $path: $!");
23 local $/;
24 my $text = <$fh>;
25 close $fh;
26 return $text;
27 }
28  
29 # Single left-to-right scan. Returns [ kind, text ] pairs where kind is one of
30 # code, string, dollar, comment or split. Comment text excludes its newline.
31 sub _tokenize {
32 my $sql = shift;
33 my @tokens;
34 pos($sql) = 0;
35 while (pos($sql) < length $sql) {
36 if ($sql =~ /\G(--[^\n]*)/gc) { push @tokens, [comment => $1] }
37 elsif ($sql =~ /\G('(?:[^']|'')*'?)/gc) { push @tokens, [string => $1] }
38 elsif ($sql =~ /\G(\$[A-Za-z_]*\$)/gc) { push @tokens, [dollar => _dollar_body(\$sql, $1)] }
39 elsif ($sql =~ /\G;/gc) { push @tokens, [split => ';'] }
40 else { $sql =~ /\G([^-';\$]+|.)/gcs; push @tokens, [code => $1] }
41 }
42 return \@tokens;
43 }
44  
45 # An unterminated dollar quote swallows the rest of the file, as Postgres would.
46 sub _dollar_body {
47 my ($sql_ref, $opening_tag) = @_;
48 my $start = pos($$sql_ref);
49 my $close_at = index($$sql_ref, $opening_tag, $start);
50 my $end = $close_at < 0 ? length $$sql_ref : $close_at + length $opening_tag;
51 my $body = $opening_tag . substr($$sql_ref, $start, $end - $start);
52 pos($$sql_ref) = $end;
53 return $body;
54 }
55  
56 sub parse_step_file {
57 my $path = shift;
58 my $text = _slurp($path);
59 my ($first_line) = split /\n/, $text, 2;
60 $first_line = '' unless defined $first_line;
61 $first_line =~ s/\s+$//;
62  
63 my (@statements, $current);
64 $current = '';
65 foreach my $token (@{ _tokenize($text) }) {
66 my ($kind, $chunk) = @$token;
67 next if $kind eq 'comment';
68 if ($kind eq 'split') {
69 push @statements, $current;
70 $current = '';
71 next;
72 }
73 $current .= $chunk;
74 }
75 push @statements, $current;
76  
77 return {
78 no_transaction => ($first_line eq $NO_TRANSACTION_DIRECTIVE ? 1 : 0),
79 statements => [ _meaningful(@statements) ],
80 };
81 }
82  
83 sub _meaningful {
84 return grep { length && !/^(?:BEGIN|BEGIN TRANSACTION|COMMIT)$/i }
85 map { s/^\s+//r =~ s/\s+$//r } @_;
86 }
87  
88 # Must stay byte-for-byte what DBIx::Class::Schema::Versioned::_read_sql_file
89 # does: it is the fallback for steps below the baseline, which ran this way.
90 sub legacy_statements {
91 my $path = shift;
92 my @lines = split /\n/, _slurp($path);
93 return [ split /;/, join '', grep {
94 $_ && !/^--/ && !/^(BEGIN|BEGIN TRANSACTION|COMMIT)/mi
95 } @lines ];
96 }
97  
98 sub inline_comment_lines {
99 my $path = shift;
100 my ($line, $code_on_line, @reported) = (1, 0);
101 foreach my $token (@{ _tokenize(_slurp($path)) }) {
102 my ($kind, $chunk) = @$token;
103 push @reported, $line if $kind eq 'comment' && $code_on_line;
104 next if $kind eq 'comment';
105 my $newlines = () = $chunk =~ /\n/g;
106 if ($newlines) {
107 $line += $newlines;
108 $code_on_line = ($chunk =~ /\n([^\n]*)\z/)[0] =~ /\S/ ? 1 : 0;
109 }
110 else {
111 $code_on_line ||= $chunk =~ /\S/ ? 1 : 0;
112 }
113 }
114 return \@reported;
115 }
116  
117 1;