Zonemaster/Backend/DB/SQLite.pm untested

filter: Statementstmt 0.0% 0 / 99 Branchbran 0.0% 0 / 6 Conditioncond 0.0% 0 / 13 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 15 Podpod 0.0% 0 / 8 totaltotal 0.0% 0 / 141 CC 4 SCAR 12.0
CC
4
Cov
0%
DEMOLISH
6.0
create_schema
6.0
add_batch_job
6.0
CRAP 3.3
1 package Zonemaster::Backend::DB::SQLite;
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  
12  
13 use Zonemaster::Backend::Errors;
14  
15 with 'Zonemaster::Backend::DB';
16  
17 =head1 CLASS METHODS
18  
19 =head2 from_config
20  
21 Construct a new instance from a Zonemaster::Backend::Config.
22  
23 my $db = Zonemaster::Backend::DB::SQLite->from_config( $config );
24  
25 =cut
26  
27 sub from_config {
28 my ( $class, $config ) = @_;
29  
30 my $file = $config->SQLITE_database_file;
31  
32 my $data_source_name = "DBI:SQLite:dbname=$file";
33  
34 return $class->new(
35 {
36 data_source_name => $data_source_name,
37 user => '',
38 password => '',
39 dbhandle => undef,
40 }
41 );
42 }
43  
44 sub DEMOLISH {
45 my ( $self ) = @_;
46 $self->dbh->disconnect() if defined $self->dbhandle && $self->dbhandle->ping;
47 }
48  
49 sub get_dbh_specific_attributes {
50 return { sqlite_extended_result_codes => 1 };
51 }
52  
53 sub create_schema {
54 my ( $self ) = @_;
55  
56 my $dbh = $self->dbh;
57  
58 # enable FOREIGN KEY support
59 $dbh->do( 'PRAGMA foreign_keys = ON;' );
60  
61 ####################################################################
62 # TEST RESULTS
63 ####################################################################
64 $dbh->do(
65 'CREATE TABLE IF NOT EXISTS test_results (
66 id integer PRIMARY KEY AUTOINCREMENT,
67 hash_id VARCHAR(16) NOT NULL,
68 domain VARCHAR(255) NOT NULL,
69 batch_id integer NULL,
70 created_at DATETIME NOT NULL,
71 started_at DATETIME DEFAULT NULL,
72 ended_at DATETIME DEFAULT NULL,
73 priority integer DEFAULT 10,
74 queue integer DEFAULT 0,
75 progress integer DEFAULT 0,
76 fingerprint character varying(32),
77 params text NOT NULL,
78 results text DEFAULT NULL,
79 undelegated boolean NOT NULL DEFAULT false,
80  
81 UNIQUE (hash_id)
82 )
83 '
84 ) or die Zonemaster::Backend::Error::Internal->new( reason => "SQLite error, could not create 'test_results' table", data => $dbh->errstr() );
85  
86 $dbh->do(
87 'CREATE INDEX IF NOT EXISTS test_results__hash_id ON test_results (hash_id)'
88 );
89 $self->dbh->do(
90 'CREATE INDEX IF NOT EXISTS test_results__fingerprint ON test_results (fingerprint)'
91 );
92 $dbh->do(
93 'CREATE INDEX IF NOT EXISTS test_results__batch_id_progress ON test_results (batch_id, progress)'
94 );
95 $dbh->do(
96 'CREATE INDEX IF NOT EXISTS test_results__progress ON test_results (progress)'
97 );
98 $dbh->do(
99 'CREATE INDEX IF NOT EXISTS test_results__domain_undelegated ON test_results (domain, undelegated)'
100 );
101  
102 ####################################################################
103 # LOG LEVEL
104 ####################################################################
105 $dbh->do(
106 "CREATE TABLE IF NOT EXISTS log_level (
107 value INTEGER,
108 level VARCHAR(15),
109  
110 UNIQUE (value)
111 )
112 "
113 ) or die Zonemaster::Backend::Error::Internal->new( reason => "SQLite error, could not create 'log_level' table", data => $dbh->errstr() );
114  
115 my ( $c ) = $dbh->selectrow_array( "SELECT count(*) FROM log_level" );
116 if ( $c == 0 ) {
117 $dbh->do(
118 "INSERT INTO log_level (value, level)
119 VALUES
120 (-2, 'DEBUG3'),
121 (-1, 'DEBUG2'),
122 ( 0, 'DEBUG'),
123 ( 1, 'INFO'),
124 ( 2, 'NOTICE'),
125 ( 3, 'WARNING'),
126 ( 4, 'ERROR'),
127 ( 5, 'CRITICAL')
128 "
129 );
130 }
131  
132 ####################################################################
133 # RESULT ENTRIES
134 ####################################################################
135 $dbh->do(
136 'CREATE TABLE IF NOT EXISTS result_entries (
137 hash_id VARCHAR(16) NOT NULL,
138 level INT NOT NULL,
139 module VARCHAR(255) NOT NULL,
140 testcase VARCHAR(255) NOT NULL,
141 tag VARCHAR(255) NOT NULL,
142 timestamp REAL NOT NULL,
143 args BLOB NOT NULL,
144  
145 FOREIGN KEY(hash_id) REFERENCES test_results(hash_id),
146 FOREIGN KEY(level) REFERENCES log_level(value)
147 )
148 '
149 ) or die Zonemaster::Backend::Error::Internal->new( reason => "SQLite error, could not create 'result_entries' table", data => $dbh->errstr() );
150  
151 $dbh->do(
152 'CREATE INDEX IF NOT EXISTS result_entries__hash_id ON result_entries (hash_id)'
153 );
154  
155 $dbh->do(
156 'CREATE INDEX IF NOT EXISTS result_entries__level ON result_entries (level)'
157 );
158  
159 ####################################################################
160 # BATCH JOBS
161 ####################################################################
162 $dbh->do(
163 'CREATE TABLE IF NOT EXISTS batch_jobs (
164 id integer PRIMARY KEY,
165 username character varying(50) NOT NULL,
166 created_at DATETIME NOT NULL
167 )
168 '
169 ) or die Zonemaster::Backend::Error::Internal->new( reason => "SQLite error, could not create 'batch_jobs' table", data => $dbh->errstr() );
170  
171  
172 ####################################################################
173 # USERS
174 ####################################################################
175 $dbh->do(
176 'CREATE TABLE IF NOT EXISTS users (
177 id INTEGER PRIMARY KEY AUTOINCREMENT,
178 username varchar(128),
179 api_key varchar(512),
180  
181 UNIQUE (username)
182 )
183 '
184 ) or die Zonemaster::Backend::Error::Internal->new( reason => "SQLite error, could not create 'users' table", data => $dbh->errstr() );
185  
186 return;
187 }
188  
189 =head2 drop_tables
190  
191 Drop all the tables if they exist.
192  
193 =cut
194  
195 sub drop_tables {
196 my ( $self ) = @_;
197  
198 $self->dbh->do( "DROP TABLE IF EXISTS test_results" );
199 $self->dbh->do( "DROP TABLE IF EXISTS result_entries" );
200 $self->dbh->do( "DROP TABLE IF EXISTS log_level" );
201 $self->dbh->do( "DROP TABLE IF EXISTS users" );
202 $self->dbh->do( "DROP TABLE IF EXISTS batch_jobs" );
203  
204 return;
205 }
206  
207 sub add_batch_job {
208 my ( $self, $params ) = @_;
209 my $batch_id;
210  
211 my $dbh = $self->dbh;
212  
213 if ( $self->user_authorized( $params->{username}, $params->{api_key} ) ) {
214 $batch_id = $self->create_new_batch_job( $params->{username} );
215  
216 my $test_params = $params->{test_params};
217 my $priority = $test_params->{priority};
218 my $queue_label = $test_params->{queue};
219  
220 $dbh->{AutoCommit} = 0;
221 eval {$dbh->do( "DROP INDEX IF EXISTS test_results__hash_id " );};
222 eval {$dbh->do( "DROP INDEX IF EXISTS test_results__fingerprint " );};
223 eval {$dbh->do( "DROP INDEX IF EXISTS test_results__batch_id_progress " );};
224 eval {$dbh->do( "DROP INDEX IF EXISTS test_results__progress " );};
225 eval {$dbh->do( "DROP INDEX IF EXISTS test_results__domain_undelegated " );};
226  
227 my $sth = $dbh->prepare( '
228 INSERT INTO test_results (
229 hash_id,
230 domain,
231 batch_id,
232 created_at,
233 priority,
234 queue,
235 fingerprint,
236 params,
237 undelegated
238 ) VALUES (?,?,?,?,?,?,?,?,?)'
239 );
240 foreach my $domain ( @{$params->{domains}} ) {
241 $test_params->{domain} = _normalize_domain( $domain );
242  
243 my $fingerprint = $self->generate_fingerprint( $test_params );
244 my $encoded_params = $self->encode_params( $test_params );
245 my $undelegated = $self->undelegated ( $test_params );
246  
247 my $hash_id = substr(md5_hex(time().rand()), 0, 16);
248 $sth->execute(
249 $hash_id,
250 $test_params->{domain},
251 $batch_id,
252 $self->format_time( time() ),
253 $priority,
254 $queue_label,
255 $fingerprint,
256 $encoded_params,
257 $undelegated,
258 );
259 }
260 $dbh->do( "CREATE INDEX test_results__hash_id ON test_results (hash_id, created_at)" );
261 $dbh->do( "CREATE INDEX test_results__fingerprint ON test_results (fingerprint)" );
262 $dbh->do( "CREATE INDEX test_results__batch_id_progress ON test_results (batch_id, progress)" );
263 $dbh->do( "CREATE INDEX test_results__progress ON test_results (progress)" );
264 $dbh->do( "CREATE INDEX test_results__domain_undelegated ON test_results (domain, undelegated)" );
265  
266 $dbh->commit();
267 $dbh->{AutoCommit} = 1;
268 }
269 else {
270 die Zonemaster::Backend::Error::PermissionDenied->new( message => 'User not authorized to use batch mode', data => { username => $params->{username}} );
271 }
272  
273 return $batch_id;
274 }
275  
276 sub get_relative_start_time {
277 my ( $self, $hash_id ) = @_;
278  
279 return $self->dbh->selectrow_array(
280 q[
281 SELECT (julianday(?) - julianday(started_at)) * 3600 * 24
282 FROM test_results
283 WHERE hash_id = ?
284 ],
285 undef,
286 $self->format_time( time() ),
287 $hash_id,
288 );
289 }
290  
291 sub is_duplicate {
292 my ( $self ) = @_;
293  
294 # for the list of codes see: https://sqlite.org/rescode.html
295 return ( $self->dbh->err == 2067 );
296 }
297  
298 no Moose;
299 __PACKAGE__->meta()->make_immutable();
300  
301 1;