]> git.donarmstrong.com Git - debbugs.git/blob - lib/Debbugs/DB/Util.pm
f7dc38f7a8e15715193de3cdd2fb00f3162d69e9
[debbugs.git] / lib / Debbugs / DB / Util.pm
1 # This module is part of debbugs, and is released
2 # under the terms of the GPL version 2, or any later version. See the
3 # file README and COPYING for more information.
4 # Copyright 2017 by Don Armstrong <don@donarmstrong.com>.
5
6 package Debbugs::DB::Util;
7
8 =head1 NAME
9
10 Debbugs::DB::Util -- Utility routines for the database
11
12 =head1 SYNOPSIS
13
14
15 =head1 DESCRIPTION
16
17
18 =head1 BUGS
19
20 None known.
21
22 =cut
23
24 use warnings;
25 use strict;
26
27 use base qw(DBIx::Class);
28
29 use Debbugs::Common qw(open_compressed_file);
30 use File::Find qw();
31
32 =head2 select
33
34 Routines for select requests
35
36 =over
37
38 =item select_one
39
40         $schema->select_one($sql,@bind_vals)
41
42 Returns the first column from the first row returned from a select statement
43
44 =cut
45
46 sub select_one {
47     my ($self,$sql,@bind_vals) = @_;
48     my $sth = $self->prepare_execute($sql,@bind_vals);
49     my $results = $sth->fetchall_arrayref([0]);
50     return (ref($results) and ref($results->[0]))?$results->[0][0]:undef;
51 }
52
53 =item prepare_execute
54
55         $schema->prepare_execute($sql,@bind_vals)
56
57 Prepares and executes a statement
58
59 =cut
60
61 sub prepare_execute {
62     my ($self,$sql,@bind_vals) = @_;
63     $self->storage->
64         dbh_do(sub {
65                    my ($s,$dbh) = @_;
66                    my $sth = $dbh->
67                        prepare_cached($sql,
68                       {dbi_dummy => __FILE__.__LINE__ })
69                        or die "Unable to prepare statement: $sql";
70                    $sth->execute(@bind_vals) or
71                        die "Unable to execute statement: ".$dbh->errstr();
72                    return $sth;
73                });
74 }
75
76 =item sql_file_in_txn
77
78 C<sql_file_in_txn();>
79
80
81
82 =cut
83 sub sql_file_in_txn {
84     my ($self,$fn) = @_;
85     my $fh = open_compressed_file($fn) or
86         die "Unable to open $fn for reading: $!";
87     local $/;
88     my $sql = <$fh>;
89     defined($sql) or die "Unable to read from file: $!";
90     $self->prepare_execute($sql);
91 }
92
93
94
95 =back
96
97 =head2 Database Upgrade and Version
98
99 =item db_version
100
101 C<db_version();>
102
103 Returns the current database version (integer)
104
105 =cut
106 sub db_version {
107     my ($self) = @_;
108     return $self->select_one('SELECT db_version()');
109 }
110
111 =item upgrades_to_run
112
113 C<upgrades_to_run();>
114
115 Returns the set of upgrades which will have to be run (in order) to upgrade the
116 database to the current version
117
118 =cut
119
120 sub upgrades_to_run {
121     my ($self,$deployment_dir) = @_;
122
123     my $current_version = $self->db_version();
124
125     my @files;
126     File::Find::find(sub {
127                          if (-f $_ and /^schema_(\d+)_to_(\d+)\.pl$/) {
128                              push @files, {file => $File::Find::name,
129                                            from => $1,
130                                            to => $2,
131                                           };
132                          }
133                      },
134                      $deployment_dir
135                     );
136     # sort the upgrades
137     use Data::Dumper;
138     print STDERR Dumper(@files);
139     @files = sort {$a->{from} <=> $b->{from}}
140         # strip out upgrades which don't need to be run
141         grep {$_->{from} >= $current_version } @files;
142
143     print STDERR Dumper(@files);
144     return @files;
145 }
146
147
148
149
150 =back
151
152 =cut
153
154 1;
155
156
157 __END__