Toolkit/Setup/Database.pm

filter: Statementstmt 12.2% 11 / 90 Branchbran 0.0% 0 / 18 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 33.3% 3 / 9 Podpod 0.0% 0 / 6 totaltotal 10.9% 14 / 128 CC 15 SCAR 30.7
CC
15
Cov
10%
fetch_upgrade_10_to_11
30.0
get_database_version
30.0
bulk_data_insert
30.0
CRAP 21.4
« Toolkit/Feed/Listing.pm ↑ Summary Toolkit/Store/Pg.pm »
1 package Wiki::Toolkit::Setup::Database;
2  
3 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 5 ▶use Carp qw( croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 5 ▶use vars qw( $VERSION @SUPPORTED_SCHEMAS);
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 5 $VERSION = 0.11;
10 5 @SUPPORTED_SCHEMAS = qw( 10 11 );
11  
12 =head1 NAME
13  
14 Wiki::Toolkit::Setup::Database - parent class for database storage setup
15 classes for Wiki::Toolkit
16  
17 =cut
18  
19 # Fetch from schema version 10, and upgrade to version 11
20 0 ▶sub fetch_upgrade_10_to_11 {
Subroutine fetch_upgrade_10_to_11: not called CC=5 SCAR=34.0
Pod undocumented
21 0 my $dbh = shift;
22 0 my %nodes;
23 my %metadatas;
24 0 my %contents;
25 0 my @internal_links;
26  
27 0 print "Grabbing and upgrading old data... ";
28  
29 # Grab all the nodes
30 0 my $sth = $dbh->prepare( "SELECT id,name,version,text,modified,moderate"
31 . " FROM node" );
32 0 $sth->execute;
33 0 ▶ while( my( $id, $name, $version, $text, $modified, $moderate) =
Branch: 1 branch
BranchTrueFalseTotal
if (() = $sth->fetchrow_array) 0 0 0
34 $sth->fetchrow_array ) {
35 0 $nodes{$name} = {
36 name => $name,
37 version => $version,
38 text => $text,
39 modified => $modified,
40 id => $id,
41 moderate => $moderate,
42 };
43 }
44  
45 # Grab all the content
46 0 $sth = $dbh->prepare( "SELECT node_id,version,text,modified,comment,"
47 . "moderated FROM content" );
48 0 $sth->execute;
49 0 ▶ while ( my( $node_id, $version, $text, $modified, $comment, $moderated) =
Branch: 1 branch
BranchTrueFalseTotal
if (() = $sth->fetchrow_array) 0 0 0
50 $sth->fetchrow_array ) {
51 0 $contents{$node_id."-".$version} = {
52 node_id => $node_id,
53 version => $version,
54 text => $text,
55 modified => $modified,
56 comment => $comment,
57 moderated => $moderated,
58 };
59 }
60  
61 # Grab all the metadata
62 0 $sth = $dbh->prepare( "SELECT node_id,version,metadata_type,metadata_value"
63 . " FROM metadata" );
64 0 $sth->execute;
65 0 my $i = 0;
66 0 ▶ while( my ( $node_id, $version, $metadata_type, $metadata_value ) =
Branch: 1 branch
BranchTrueFalseTotal
if (() = $sth->fetchrow_array) 0 0 0
67 $sth->fetchrow_array) {
68 0 $metadatas{$node_id."-".($i++)} = {
69 node_id => $node_id,
70 version => $version,
71 metadata_type => $metadata_type,
72 metadata_value => $metadata_value,
73 };
74 }
75  
76 # Grab all the internal links
77 0 $sth = $dbh->prepare( "SELECT link_from,link_to FROM internal_links" );
78 0 $sth->execute;
79 0 ▶ while( my ( $link_from, $link_to ) = $sth->fetchrow_array ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = $sth->fetchrow_array) 0 0 0
80 0 push @internal_links, {
81 link_from => $link_from,
82 link_to => $link_to,
83 };
84 }
85  
86 0 print "done\n";
87  
88 # Return it all
89 0 return ( \%nodes, \%contents, \%metadatas, \@internal_links );
90 }
91  
92 # Get the version of the database schema
93 0 ▶sub get_database_version {
Subroutine get_database_version: not called CC=5 SCAR=34.0
Pod undocumented
94 0 my $dbh = shift;
95 0 my $sql = "SELECT version FROM schema_info";
96 0 my $sth;
97 0 eval{ $sth = $dbh->prepare($sql) };
98 0 ▶ if($@) { croak_too_old(); }
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
99 0 eval{ $sth->execute };
100 0 ▶ if($@) { croak_too_old(); }
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
101  
102 0 my ($cur_schema) = $sth->fetchrow_array;
103 0 ▶ if ( !$cur_schema || $cur_schema < $SUPPORTED_SCHEMAS[0] ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $cur_schema or $cur_schema < $Wiki::Toolkit::Setup::Database::SUPPORTED_SCHEMAS[0]) 0 0 0
Condition0%0/3
not $cur_schema or $cur_schema < $Wiki::Toolkit::Setup::Database::SUPPORTED_SCHEMAS[0]
l!l&&r!l&&!r
000
MC/DC0%0/2
not $cur_schema or $cur_schema < $Wiki::Toolkit::Setup::Database::SUPPORTED_SCHEMAS[0]
not $cur_schema $cur_schema < $Wiki::Toolkit::Setup::Database::SUPPORTED_SCHEMAS[0]
Truth table0%0/3
not(A) or B
A= not $cur_schemaB= $cur_schema < $Wiki::Toolkit::Setup::Database::SUPPORTED_SCHEMAS[0]
ABresult
1X1
011
000
104 0 croak_too_old();
105 }
106  
107 0 return $cur_schema;
108 }
109  
110 0 ▶sub croak_too_old {
Subroutine croak_too_old: not called CC=1 SCAR=6.9
Pod undocumented
111 0 croak "Database schema too old — must be at least version "
112 . $SUPPORTED_SCHEMAS[0];
113 }
114  
115 # Is an upgrade to the database required?
116 0 ▶sub get_database_upgrade_required {
Subroutine get_database_upgrade_required: not called CC=3 SCAR=24.8
Pod undocumented
117 0 my ($dbh,$new_version) = @_;
118  
119 # Get the schema version
120 0 my $schema_version = get_database_version($dbh);
121  
122 # Compare it
123 0 ▶ if($schema_version eq $new_version) {
Branch: 2 branches
BranchTrueFalseTotal
if ($schema_version eq $new_version) { } 0 0 0
elsif ($schema_version < $new_version) { } 0 0 0
124 # At latest version
125 0 return undef;
126 } elsif ( $schema_version < $new_version ) {
127 0 return $schema_version."_to_".$new_version;
128 } else {
129 0 die "Aiee! We seem to be trying to downgrade the database schema from $schema_version to $new_version. Aborting.\n";
130 }
131 }
132  
133 # Put the latest data into the latest database structure
134 0 ▶sub bulk_data_insert {
Subroutine bulk_data_insert: not called CC=5 SCAR=34.0
Pod undocumented
135 0 my ($dbh, $nodesref, $contentsref, $metadataref, $internallinksref) = @_;
136  
137 0 print "Bulk inserting upgraded data... ";
138  
139 # Add nodes
140 0 my $sth = $dbh->prepare("INSERT INTO node (id,name,version,text,modified,moderate) VALUES (?,?,?,?,?,?)");
141 0 foreach my $name (keys %$nodesref) {
142 0 my %node = %{$nodesref->{$name}};
143 $sth->execute($node{'id'},
144 $node{'name'},
145 $node{'version'},
146 $node{'text'},
147 $node{'modified'},
148 0 $node{'moderate'});
149 }
150 0 print "added ".(scalar keys %$nodesref)." nodes... ";
151  
152 # Add content
153 0 $sth = $dbh->prepare("INSERT INTO content (node_id,version,text,modified,comment,moderated) VALUES (?,?,?,?,?,?)");
154 0 foreach my $key (keys %$contentsref) {
155 0 my %content = %{$contentsref->{$key}};
156 $sth->execute($content{'node_id'},
157 $content{'version'},
158 $content{'text'},
159 $content{'modified'},
160 $content{'comment'},
161 0 $content{'moderated'});
162 }
163  
164 # Add metadata
165 0 $sth = $dbh->prepare("INSERT INTO metadata (node_id,version,metadata_type,metadata_value) VALUES (?,?,?,?)");
166 0 foreach my $key (keys %$metadataref) {
167 0 my %metadata = %{$metadataref->{$key}};
168 $sth->execute($metadata{'node_id'},
169 $metadata{'version'},
170 $metadata{'metadata_type'},
171 0 $metadata{'metadata_value'});
172 }
173  
174 # Add internal links
175 0 $sth = $dbh->prepare("INSERT INTO internal_links (link_from,link_to) VALUES (?,?)");
176 0 foreach my $ilr (@$internallinksref) {
177 0 my %il = %{$ilr};
178 $sth->execute($il{'link_from'},
179 0 $il{'link_to'});
180 }
181  
182 0 print "done\n";
183 }
184  
185 0 ▶sub perm_check {
Subroutine perm_check: not called CC=1 SCAR=6.9
Pod undocumented
186 0 my $dbh = shift;
187 # If we can do all this, we'll be able to do a bulk upgrade too
188 0 eval {
189 0 my $sth = $dbh->prepare("CREATE TABLE dbtest (test int)");
190 0 $sth->execute;
191  
192 0 $sth = $dbh->prepare("CREATE INDEX dbtest_index ON dbtest (test)");
193 0 $sth->execute;
194  
195 0 $sth = $dbh->prepare("DROP TABLE dbtest");
196 0 $sth->execute;
197 };
198 0 return $@;
199 }
« Toolkit/Feed/Listing.pm ↑ Summary Toolkit/Store/Pg.pm »