lib/Monitoring/Spooler/DB.pm untested

filter: Statementstmt 0.0% 0 / 114 Branchbran 0.0% 0 / 38 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 17 Podpod 0.0% 0 / 10 totaltotal 0.0% 0 / 179 CC 20 SCAR 38.8
CC
20
Cov
0%
_check_db_version
110.0
_check_tables
72.0
prepexec
12.0
CRAP 48.6
1 package Monitoring::Spooler::DB;
2 $Monitoring::Spooler::DB::VERSION = '0.05';
3 BEGIN {
4 $Monitoring::Spooler::DB::AUTHORITY = 'cpan:TEX';
5 }
6 # ABSTRACT: DB handling
7  
8 use 5.010_000;
9 use mro 'c3';
10 use feature ':5.10';
11  
12 use Moose;
13 use namespace::autoclean;
14  
15 # use IO::Handle;
16 # use autodie;
17 # use MooseX::Params::Validate;
18 # use Carp;
19 # use English qw( -no_match_vars );
20 # use Try::Tiny;
21 use DBI;
22  
23 # extends ...
24 # has ...
25 has 'dbh' => (
26 'is' => 'rw',
27 'isa' => 'DBI::db',
28 'lazy' => 1,
29 'builder' => '_init_dbh',
30 );
31  
32 has 'config' => (
33 'is' => 'rw',
34 'isa' => 'Config::Yak',
35 'required' => 1,
36 );
37  
38 has 'logger' => (
39 'is' => 'rw',
40 'isa' => 'Log::Tree',
41 'required' => 1,
42 );
43 # with ...
44 # initializers ...
45 sub _init_dbh {
46 my $self = shift;
47  
48 my $db_file = $self->config()->get('Monitoring::Spooler::DBFile', { Default => '/var/lib/mon-spooler/db.sqlite3'});
49  
50 my $dsn = 'DBI:SQLite:dbname='.$db_file;
51  
52 # see http://search.cpan.org/~adamk/DBD-SQLite-1.35/lib/DBD/SQLite.pm#Transaction_and_Database_Locking
53 my $dbh = DBI->connect($dsn, '', '', { sqlite_use_immediate_transaction => 1, });
54  
55 if($dbh) {
56 $self->_check_tables($dbh);
57 }
58  
59 return $dbh;
60 }
61  
62 sub DEMOLISH {
63 my $self = shift;
64  
65 $self->dbh()->disconnect();
66  
67 return;
68 }
69  
70 # your code here ...
71 sub prepare {
72 my $self = shift;
73 my $sqlstr = shift;
74  
75 return $self->dbh()->prepare($sqlstr);
76 }
77  
78 sub prepexec {
79 my ( $self, $sqlstr, @params ) = @_;
80  
81 my $sth = $self->dbh()->prepare($sqlstr);
82  
83 if(!$sth) {
84 $self->logger()->log( message => 'Failed to prepare statement from SQL: '.$sqlstr.' w/ error: '.$self->dbh()->errstr, level => 'warning', );
85 return;
86 }
87 if($sth->execute(@params)) {
88 return $sth;
89 } else {
90 $self->logger()->log( message => 'Failed to execute statement w/ error: '.$sth->errstr, level => 'warning', );
91 return;
92 }
93 }
94  
95 sub do {
96 my $self = shift;
97  
98 return $self->dbh()->do(@_);
99 }
100  
101 sub last_insert_id {
102 my $self = shift;
103  
104 return $self->dbh()->last_insert_id(@_);
105 }
106  
107 sub errstr {
108 my $self = shift;
109 return $self->dbh()->errstr();
110 }
111  
112 sub _check_tables {
113 my $self = shift;
114 my $dbh = shift;
115  
116 # TODO LOW handle corrupted DB somehow
117 # see http://www.sqlite.org/faq.html#q21 for possible approaches
118  
119 # TODO LOW add archive table to archive sent messages for some time
120 # see http://www.sqlite.org/pragma.html#pragma_auto_vacuum for keeping the
121 # disk usage low
122  
123 my $sql_meta = <<EOS;
124 CREATE TABLE IF NOT EXISTS meta (
125 key TEXT,
126 value TEXT,
127 CONSTRAINT ukey UNIQUE (key) ON CONFLICT ABORT
128 );
129 EOS
130 if($dbh->do($sql_meta)) {
131 #$self->logger()->log( message => 'Table meta OK', level => 'debug', );
132 } else {
133 $self->logger()->log( message => 'Failed to create table meta: '.$dbh->errstr, level => 'error', );
134 }
135  
136 # check that all required tables exist and create them if they don't
137 my $sql_groups = <<EOS;
138 CREATE TABLE IF NOT EXISTS groups (
139 id INTEGER PRIMARY KEY ASC,
140 name TEXT
141 );
142 EOS
143  
144 if($dbh->do($sql_groups)) {
145 #$self->logger()->log( message => 'Table groups OK', level => 'debug', );
146 } else {
147 $self->logger()->log( message => 'Failed to create table groups: '.$dbh->errstr, level => 'error', );
148 }
149  
150 my $sql_msgq = <<EOS;
151 CREATE TABLE IF NOT EXISTS msg_queue (
152 id INTEGER PRIMARY KEY ASC,
153 group_id INTEGER,
154 type TEXT,
155 message TEXT,
156 ts INTEGER,
157 event TEXT,
158 trigger_id INTEGER,
159 CONSTRAINT fk_gid FOREIGN KEY (group_id) REFERENCES groups (id) ON DELETE CASCADE
160 );
161 EOS
162  
163 if($dbh->do($sql_msgq)) {
164 #$self->logger()->log( message => 'Table msg_queue OK', level => 'debug', );
165 } else {
166 $self->logger()->log( message => 'Failed to create table msg_queue: '.$dbh->errstr, level => 'error', );
167 }
168  
169 my $sql_nq = <<EOS;
170 CREATE TABLE IF NOT EXISTS notify_order (
171 id INTEGER PRIMARY KEY ASC,
172 group_id INTEGER,
173 name TEXT,
174 number TEXT,
175 CONSTRAINT fk_gid FOREIGN KEY (group_id) REFERENCES groups (id) ON DELETE CASCADE
176 );
177 EOS
178  
179 if($dbh->do($sql_nq)) {
180 #$self->logger()->log( message => 'Table notify_order OK', level => 'debug', );
181 } else {
182 $self->logger()->log( message => 'Failed to create table notify_order: '.$dbh->errstr, level => 'error', );
183 }
184  
185 my $sql_ni = <<EOS;
186 CREATE TABLE IF NOT EXISTS notify_interval (
187 id INTEGER PRIMARY KEY ASC,
188 group_id INTEGER,
189 type TEXT,
190 notify_from TEXT,
191 notify_to TEXT,
192 CONSTRAINT fk_gid FOREIGN KEY (group_id) REFERENCES groups (id) ON DELETE CASCADE
193 );
194 EOS
195  
196 if($dbh->do($sql_ni)) {
197 #$self->logger()->log( message => 'Table notify_interval OK', level => 'debug', );
198 } else {
199 $self->logger()->log( message => 'Failed to create table notify_interval: '.$dbh->errstr, level => 'error', );
200 }
201  
202 my $sql_pg = <<EOS;
203 CREATE TABLE IF NOT EXISTS paused_groups (
204 id INTEGER PRIMARY KEY ASC,
205 group_id INTEGER,
206 until INTEGER,
207 CONSTRAINT fk_gid FOREIGN KEY (group_id) REFERENCES groups (id) ON DELETE CASCADE
208 );
209 EOS
210  
211 if($dbh->do($sql_pg)) {
212 #$self->logger()->log( message => 'Table paused_groups OK', level => 'debug', );
213 } else {
214 $self->logger()->log( message => 'Failed to create table paused_groups: '.$dbh->errstr, level => 'error', );
215 }
216  
217 my $sql_pr = <<EOS;
218 CREATE TABLE IF NOT EXISTS running_procs (
219 pid INTEGER PRIMARY KEY ASC,
220 type TEXT,
221 name TEXT
222 );
223 EOS
224  
225 if($dbh->do($sql_pr)) {
226 #$self->logger()->log( message => 'Table running_procs OK', level => 'debug', );
227 } else {
228 $self->logger()->log( message => 'Failed to create table running_procs: '.$dbh->errstr, level => 'error', );
229 }
230  
231 # SQLite honors FK constraints only if the are explicitly turned on ...
232 # http://www.sqlite.org/foreignkeys.html#fk_enable
233 $dbh->do('PRAGMA foreign_keys = ON;');
234  
235 # Speed up a bit, if we encounter a crash we have more to worry about than
236 # just a messed up spooler queue ...
237 # http://www.sqlite.org/pragma.html#pragma_synchronous
238 $dbh->do('PRAGMA synchronous = OFF;');
239  
240 $self->_check_db_version($dbh);
241  
242 return 1;
243 }
244  
245 sub _check_db_version {
246 my $self = shift;
247 my $dbh = shift;
248  
249 $dbh->do('BEGIN TRANSACTION');
250  
251 my $db_version = 0; # set default
252 my $sql = 'SELECT value FROM meta WHERE `key` = ?';
253 my $sth = $dbh->prepare($sql);
254 if($sth) {
255 if($sth->execute('version')) {
256 $db_version = $sth->fetchrow_array();
257 } else {
258 $self->logger()->log( message => 'Failed to execute statement w/ error: '.$sth->errstr, level => 'warning', );
259 }
260 $sth->finish();
261 } else {
262 $self->logger()->log( message => 'Failed to prepare statement from SQL: '.$sql.' w/ error: '.$dbh->errstr, level => 'warning', );
263 }
264  
265 # drop table paused_users regardless of db version
266 $sql = 'DROP TABLE IF EXISTS paused_users;';
267 if(!$dbh->do($sql)) {
268 $self->logger()->log( message => 'Failed to drop table paused_users', level => 'warning', );
269 }
270  
271 # do other kinds of upgrades
272 if(defined($db_version)) {
273 if($db_version < 2) {
274 # col ts was added in version 2
275 $sql = 'ALTER TABLE msg_queue ADD COLUMN ts INTEGER';
276 if(!$dbh->do($sql)) {
277 $self->logger()->log( message => 'Failed to add column ts to msq_queue table', level => 'warning', );
278 }
279 }
280 if($db_version < 3) {
281 # cols event and trigger_id were added in version 3
282 $sql = 'ALTER TABLE msg_queue ADD COLUMN event TEXT';
283 if(!$dbh->do($sql)) {
284 $self->logger()->log( message => 'Failed to add column event to msq_queue table', level => 'warning', );
285 }
286 $sql = 'ALTER TABLE msg_queue ADD COLUMN trigger_id INTEGER';
287 if(!$dbh->do($sql)) {
288 $self->logger()->log( message => 'Failed to add column trigger_id to msq_queue table', level => 'warning', );
289 }
290 }
291 }
292  
293 # finally we set the current version which we've reached by upgrading (or not)
294 $sql = "INSERT OR REPLACE INTO meta ('key','value') VALUES('version',3);";
295 $dbh->do($sql);
296  
297 $dbh->do('COMMIT');
298 return 1;
299 }
300  
301 no Moose;
302 __PACKAGE__->meta->make_immutable;
303  
304 1;
305  
306 __END__