Zonemaster/Backend/DB/PostgreSQL.pm untested

filter: Statementstmt 0.0% 0 / 109 Branchbran 0.0% 0 / 4 Conditioncond 0.0% 0 / 10 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 16 Podpod 0.0% 0 / 7 totaltotal 0.0% 0 / 146 CC 3 SCAR 10.6
CC
3
Cov
0%
create_schema
6.0
add_batch_job
6.0
from_config
2.0
CRAP 2.9
1 package Zonemaster::Backend::DB::PostgreSQL;
2  
3 our $VERSION = '1.1.0';
4  
5 use Moose;
6 use 5.14.2;
7  
8 use DBI qw(:utils);
9 use Digest::MD5 qw(md5_hex);
10 use JSON::PP;
11 use Try::Tiny;
12  
13 use Zonemaster::Backend::DB;
14 use Zonemaster::Backend::Errors;
15  
16 with 'Zonemaster::Backend::DB';
17  
18 =head1 CLASS METHODS
19  
20 =head2 from_config
21  
22 Construct a new instance from a Zonemaster::Backend::Config.
23  
24 my $db = Zonemaster::Backend::DB::PostgreSQL->from_config( $config );
25  
26 =cut
27  
28 sub from_config {
29 my ( $class, $config ) = @_;
30  
31 my $database = $config->POSTGRESQL_database;
32 my $host = $config->POSTGRESQL_host;
33 my $port = $config->POSTGRESQL_port;
34 my $user = $config->POSTGRESQL_user;
35 my $password = $config->POSTGRESQL_password;
36  
37 my $data_source_name = "DBI:Pg:dbname=$database;host=$host;port=$port";
38  
39 return $class->new(
40 {
41 data_source_name => $data_source_name,
42 user => $user,
43 password => $password,
44 dbhandle => undef,
45 }
46 );
47 }
48  
49 sub get_dbh_specific_attributes {
50 return { pg_enable_utf8 => 0 };
51 }
52  
53 sub create_schema {
54 my ( $self ) = @_;
55  
56 my $dbh = $self->dbh;
57  
58 ####################################################################
59 # TEST RESULTS
60 ####################################################################
61 $dbh->do(
62 'CREATE TABLE IF NOT EXISTS test_results (
63 id BIGSERIAL PRIMARY KEY,
64 hash_id VARCHAR(16) NOT NULL,
65 domain VARCHAR(255) NOT NULL,
66 batch_id integer,
67 created_at TIMESTAMP NOT NULL,
68 started_at TIMESTAMP DEFAULT NULL,
69 ended_at TIMESTAMP DEFAULT NULL,
70 priority integer DEFAULT 10,
71 queue integer DEFAULT 0,
72 progress integer DEFAULT 0,
73 fingerprint varchar(32),
74 params json NOT NULL,
75 undelegated integer NOT NULL DEFAULT 0,
76 results json,
77  
78 UNIQUE (hash_id)
79 )
80 '
81 ) or die Zonemaster::Backend::Error::Internal->new( reason => "PostgreSQL error, could not create 'test_results' table", data => $dbh->errstr() );
82  
83 $dbh->do(
84 'CREATE INDEX IF NOT EXISTS test_results__hash_id ON test_results (hash_id)'
85 );
86 $dbh->do(
87 'CREATE INDEX IF NOT EXISTS test_results__fingerprint ON test_results (fingerprint)'
88 );
89 $dbh->do(
90 'CREATE INDEX IF NOT EXISTS test_results__batch_id_progress ON test_results (batch_id, progress)'
91 );
92 $dbh->do(
93 'CREATE INDEX IF NOT EXISTS test_results__progress ON test_results (progress)'
94 );
95 $dbh->do(
96 'CREATE INDEX IF NOT EXISTS test_results__domain_undelegated ON test_results (domain, undelegated)'
97 );
98 # this index helps speed up query time to retrieve the next test to
99 # perform when using batches
100 $dbh->do(
101 'CREATE INDEX IF NOT EXISTS test_results__progress_priority_id ON test_results (progress, priority DESC, id) WHERE (progress = 0)'
102 );
103  
104 ####################################################################
105 # LOG LEVEL
106 ####################################################################
107 $dbh->do(
108 "CREATE TABLE IF NOT EXISTS log_level (
109 value INT,
110 level VARCHAR(15),
111  
112 UNIQUE (value)
113 )
114 "
115 ) or die Zonemaster::Backend::Error::Internal->new( reason => "PostgreSQL error, could not create 'log_level' table", data => $dbh->errstr() );
116 my ( $c ) = $dbh->selectrow_array( "SELECT count(*) FROM log_level" );
117 if ( $c == 0 ) {
118 $dbh->do(
119 "INSERT INTO log_level (value, level)
120 VALUES
121 (-2, 'DEBUG3'),
122 (-1, 'DEBUG2'),
123 ( 0, 'DEBUG'),
124 ( 1, 'INFO'),
125 ( 2, 'NOTICE'),
126 ( 3, 'WARNING'),
127 ( 4, 'ERROR'),
128 ( 5, 'CRITICAL')
129 "
130 );
131 }
132  
133 ####################################################################
134 # RESULT ENTRIES
135 ####################################################################
136 $dbh->do(
137 'CREATE TABLE IF NOT EXISTS result_entries (
138 hash_id VARCHAR(16) NOT NULL,
139 level INT NOT NULL,
140 module VARCHAR(255) NOT NULL,
141 testcase VARCHAR(255) NOT NULL,
142 tag VARCHAR(255) NOT NULL,
143 timestamp REAL NOT NULL,
144 args JSONb NOT NULL,
145  
146 CONSTRAINT fk_hash_id FOREIGN KEY (hash_id) REFERENCES test_results(hash_id),
147 CONSTRAINT fk_level FOREIGN KEY(level) REFERENCES log_level(value)
148 )
149 '
150 ) or die Zonemaster::Backend::Error::Internal->new( reason => "PostgreSQL error, could not create 'result_entries' table", data => $dbh->errstr() );
151  
152 $dbh->do(
153 'CREATE INDEX IF NOT EXISTS result_entries__hash_id ON result_entries (hash_id)'
154 );
155  
156 $dbh->do(
157 'CREATE INDEX IF NOT EXISTS result_entries__level ON result_entries (level)'
158 );
159  
160 ####################################################################
161 # BATCH JOBS
162 ####################################################################
163 $dbh->do(
164 'CREATE TABLE IF NOT EXISTS batch_jobs (
165 id serial PRIMARY KEY,
166 username varchar(50) NOT NULL,
167 created_at TIMESTAMP NOT NULL
168 )
169 '
170 ) or die Zonemaster::Backend::Error::Internal->new( reason => "PostgreSQL error, could not create 'batch_jobs' table", data => $dbh->errstr() );
171  
172  
173 ####################################################################
174 # USERS
175 ####################################################################
176 $dbh->do(
177 'CREATE TABLE IF NOT EXISTS users (
178 id serial PRIMARY KEY,
179 username VARCHAR(128),
180 api_key VARCHAR(512),
181  
182 UNIQUE (username)
183 )
184 '
185 ) or die Zonemaster::Backend::Error::Internal->new( reason => "PostgreSQL error, could not create 'users' table", data => $dbh->errstr() );
186  
187 return;
188 }
189  
190 =head2 drop_tables
191  
192 Drop all the tables if they exist.
193  
194 =cut
195  
196 sub drop_tables {
197 my ( $self ) = @_;
198  
199 # Temporarily set the message level just above "notice" to mute messages when the tables don't
200 # exist.
201 # Without setting this level we run the risk of tripping up Test::NoWarnings in unit tests.
202 my ( $old_client_min_messages ) = $self->dbh->selectrow_array( "SHOW client_min_messages" );
203 $self->dbh->do( "SET client_min_messages = warning" );
204  
205 try {
206 $self->dbh->do( "DROP TABLE IF EXISTS test_results CASCADE" );
207 $self->dbh->do( "DROP TABLE IF EXISTS result_entries CASCADE" );
208 $self->dbh->do( "DROP TABLE IF EXISTS log_level" );
209 $self->dbh->do( "DROP TABLE IF EXISTS users" );
210 $self->dbh->do( "DROP TABLE IF EXISTS batch_jobs" );
211 }
212 finally {
213 $self->dbh->do( "SET client_min_messages = ?", undef, $old_client_min_messages );
214 };
215  
216 return;
217 }
218  
219 sub add_batch_job {
220 my ( $self, $params ) = @_;
221 my $batch_id;
222  
223 my $dbh = $self->dbh;
224  
225 if ( $self->user_authorized( $params->{username}, $params->{api_key} ) ) {
226 $batch_id = $self->create_new_batch_job( $params->{username} );
227  
228 my $test_params = $params->{test_params};
229  
230 my $priority = $test_params->{priority};
231 my $queue_label = $test_params->{queue};
232  
233 my $created_at = $self->format_time( time() );
234  
235 $dbh->begin_work();
236 $dbh->do( "ALTER TABLE test_results DROP CONSTRAINT IF EXISTS test_results_pkey" );
237 $dbh->do( "DROP INDEX IF EXISTS test_results__hash_id" );
238 $dbh->do( "DROP INDEX IF EXISTS test_results__fingerprint" );
239 $dbh->do( "DROP INDEX IF EXISTS test_results__batch_id_progress" );
240 $dbh->do( "DROP INDEX IF EXISTS test_results__progress" );
241 $dbh->do( "DROP INDEX IF EXISTS test_results__domain_undelegated" );
242  
243 $dbh->do(
244 q[
245 COPY test_results (
246 hash_id,
247 domain,
248 batch_id,
249 created_at,
250 priority,
251 queue,
252 fingerprint,
253 params,
254 undelegated
255 )
256 FROM STDIN
257 ]
258 );
259  
260 foreach my $domain ( @{$params->{domains}} ) {
261 $test_params->{domain} = _normalize_domain( $domain );
262  
263 my $fingerprint = $self->generate_fingerprint( $test_params );
264 my $encoded_params = $self->encode_params( $test_params );
265 my $undelegated = $self->undelegated ( $test_params );
266  
267 my $hash_id = substr(md5_hex(time().rand()), 0, 16);
268 $dbh->pg_putcopydata(
269 "$hash_id\t$test_params->{domain}\t$batch_id\t$created_at\t$priority\t$queue_label\t$fingerprint\t$encoded_params\t$undelegated\n"
270 );
271 }
272 $dbh->pg_putcopyend();
273 $dbh->do( "ALTER TABLE test_results ADD PRIMARY KEY (id)" );
274 $dbh->do( "CREATE INDEX test_results__hash_id ON test_results (hash_id, created_at)" );
275 $dbh->do( "CREATE INDEX test_results__fingerprint ON test_results (fingerprint)" );
276 $dbh->do( "CREATE INDEX test_results__batch_id_progress ON test_results (batch_id, progress)" );
277 $dbh->do( "CREATE INDEX test_results__progress ON test_results (progress)" );
278 $dbh->do( "CREATE INDEX test_results__domain_undelegated ON test_results (domain, undelegated)" );
279  
280 $dbh->commit();
281 }
282 else {
283 die Zonemaster::Backend::Error::PermissionDenied->new( message => 'User not authorized to use batch mode', data => { username => $params->{username}} );
284 }
285  
286 return $batch_id;
287 }
288  
289 sub get_relative_start_time {
290 my ( $self, $hash_id ) = @_;
291  
292 return $self->dbh->selectrow_array(
293 q[
294 SELECT EXTRACT(EPOCH FROM ? - started_at)
295 FROM test_results
296 WHERE hash_id=?
297 ],
298 undef,
299 $self->format_time( time() ),
300 $hash_id,
301 );
302 }
303  
304 sub is_duplicate {
305 my ( $self ) = @_;
306  
307 # for the list of codes see:
308 # https://www.postgresql.org/docs/current/errcodes-appendix.html
309 return ( $self->dbh->state == 23505 );
310 }
311  
312 no Moose;
313 __PACKAGE__->meta()->make_immutable();
314  
315 1;