| 1 | package Wiki::Toolkit::Setup::Database; |
|---|
| 2 | |
|---|
| 3 | use strict; |
|---|
| 4 | |
|---|
| 5 | use vars qw( $VERSION ); |
|---|
| 6 | |
|---|
| 7 | $VERSION = 0.08; |
|---|
| 8 | |
|---|
| 9 | =head1 NAME |
|---|
| 10 | |
|---|
| 11 | Wiki::Toolkit::Setup::Database - parent class for database storage setup |
|---|
| 12 | classes for Wiki::Toolkit |
|---|
| 13 | |
|---|
| 14 | =cut |
|---|
| 15 | |
|---|
| 16 | # Fetch from the old style database, ready for an upgrade to db version 8 |
|---|
| 17 | sub fetch_upgrade_old_to_8 { |
|---|
| 18 | # Compatible with old_to_9 |
|---|
| 19 | fetch_upgrade_old_to_9(@_); |
|---|
| 20 | } |
|---|
| 21 | |
|---|
| 22 | # Fetch from the old style database, ready for an upgrade to db version 9 |
|---|
| 23 | sub fetch_upgrade_old_to_9 { |
|---|
| 24 | my $dbh = shift; |
|---|
| 25 | my %nodes; |
|---|
| 26 | my %metadatas; |
|---|
| 27 | my %contents; |
|---|
| 28 | my @internal_links; |
|---|
| 29 | my %ids; |
|---|
| 30 | |
|---|
| 31 | print "Grabbing and upgrading old data... "; |
|---|
| 32 | |
|---|
| 33 | # Grab all the nodes, and give them an ID |
|---|
| 34 | my $sth = $dbh->prepare("SELECT name,version,text,modified FROM node"); |
|---|
| 35 | $sth->execute; |
|---|
| 36 | my $id = 0; |
|---|
| 37 | while( my($name,$version,$text,$modified) = $sth->fetchrow_array) { |
|---|
| 38 | my %node; |
|---|
| 39 | $id++; |
|---|
| 40 | $node{'name'} = $name; |
|---|
| 41 | $node{'version'} = $version; |
|---|
| 42 | $node{'text'} = $text; |
|---|
| 43 | $node{'modified'} = $modified; |
|---|
| 44 | $node{'id'} = $id; |
|---|
| 45 | $node{'moderate'} = 0; |
|---|
| 46 | $nodes{$name} = \%node; |
|---|
| 47 | $ids{$name} = $id; |
|---|
| 48 | } |
|---|
| 49 | print " read $id nodes... "; |
|---|
| 50 | |
|---|
| 51 | # Grab all the content, and upgrade to ID from name |
|---|
| 52 | $sth = $dbh->prepare("SELECT name,version,text,modified,comment FROM content"); |
|---|
| 53 | $sth->execute; |
|---|
| 54 | while ( my($name,$version,$text,$modified,$comment) = $sth->fetchrow_array) { |
|---|
| 55 | my $id = $ids{$name}; |
|---|
| 56 | if($id) { |
|---|
| 57 | my %content; |
|---|
| 58 | $content{'node_id'} = $id; |
|---|
| 59 | $content{'version'} = $version; |
|---|
| 60 | $content{'text'} = $text; |
|---|
| 61 | $content{'modified'} = $modified; |
|---|
| 62 | $content{'comment'} = $comment; |
|---|
| 63 | $content{'moderated'} = 1; |
|---|
| 64 | $contents{$id."-".$version} = \%content; |
|---|
| 65 | } else { |
|---|
| 66 | warn("There was no node entry for content with name '$name', unable to migrate it!"); |
|---|
| 67 | } |
|---|
| 68 | } |
|---|
| 69 | print " read ".(scalar keys %contents)." contents... "; |
|---|
| 70 | |
|---|
| 71 | # Grab all the metadata, and upgrade to ID from node |
|---|
| 72 | $sth = $dbh->prepare("SELECT node,version,metadata_type,metadata_value FROM metadata"); |
|---|
| 73 | $sth->execute; |
|---|
| 74 | my $i = 0; |
|---|
| 75 | while( my($node,$version,$metadata_type,$metadata_value) = $sth->fetchrow_array) { |
|---|
| 76 | my $id = $ids{$node}; |
|---|
| 77 | if($id) { |
|---|
| 78 | my %metadata; |
|---|
| 79 | $metadata{'node_id'} = $id; |
|---|
| 80 | $metadata{'version'} = $version; |
|---|
| 81 | $metadata{'metadata_type'} = $metadata_type; |
|---|
| 82 | $metadata{'metadata_value'} = $metadata_value; |
|---|
| 83 | $metadatas{$id."-".($i++)} = \%metadata; |
|---|
| 84 | } else { |
|---|
| 85 | warn("There was no node entry for metadata with name (node) '$node', unable to migrate it!"); |
|---|
| 86 | } |
|---|
| 87 | } |
|---|
| 88 | |
|---|
| 89 | # Grab all the internal links |
|---|
| 90 | $sth = $dbh->prepare("SELECT link_from,link_to FROM internal_links"); |
|---|
| 91 | $sth->execute; |
|---|
| 92 | while( my($link_from,$link_to) = $sth->fetchrow_array) { |
|---|
| 93 | my %il; |
|---|
| 94 | $il{'link_from'} = $link_from; |
|---|
| 95 | $il{'link_to'} = $link_to; |
|---|
| 96 | push @internal_links, \%il; |
|---|
| 97 | } |
|---|
| 98 | |
|---|
| 99 | print "done\n"; |
|---|
| 100 | |
|---|
| 101 | # Return it all |
|---|
| 102 | return (\%nodes,\%contents,\%metadatas,\@internal_links,\%ids); |
|---|
| 103 | } |
|---|
| 104 | |
|---|
| 105 | # Fetch from schema version 8, and upgrade to version 9 |
|---|
| 106 | sub fetch_upgrade_8_to_9 { |
|---|
| 107 | my $dbh = shift; |
|---|
| 108 | my %nodes; |
|---|
| 109 | my %metadatas; |
|---|
| 110 | my %contents; |
|---|
| 111 | my @internal_links; |
|---|
| 112 | |
|---|
| 113 | print "Grabbing and upgrading old data... "; |
|---|
| 114 | |
|---|
| 115 | # Grab all the nodes |
|---|
| 116 | my $sth = $dbh->prepare("SELECT id,name,version,text,modified FROM node"); |
|---|
| 117 | $sth->execute; |
|---|
| 118 | while( my($id,$name,$version,$text,$modified) = $sth->fetchrow_array) { |
|---|
| 119 | my %node; |
|---|
| 120 | $node{'name'} = $name; |
|---|
| 121 | $node{'version'} = $version; |
|---|
| 122 | $node{'text'} = $text; |
|---|
| 123 | $node{'modified'} = $modified; |
|---|
| 124 | $node{'id'} = $id; |
|---|
| 125 | $node{'moderate'} = 0; |
|---|
| 126 | $nodes{$name} = \%node; |
|---|
| 127 | } |
|---|
| 128 | |
|---|
| 129 | # Grab all the content |
|---|
| 130 | $sth = $dbh->prepare("SELECT node_id,version,text,modified,comment FROM content"); |
|---|
| 131 | $sth->execute; |
|---|
| 132 | while ( my($node_id,$version,$text,$modified,$comment) = $sth->fetchrow_array) { |
|---|
| 133 | my %content; |
|---|
| 134 | $content{'node_id'} = $node_id; |
|---|
| 135 | $content{'version'} = $version; |
|---|
| 136 | $content{'text'} = $text; |
|---|
| 137 | $content{'modified'} = $modified; |
|---|
| 138 | $content{'comment'} = $comment; |
|---|
| 139 | $content{'moderated'} = 1; |
|---|
| 140 | $contents{$node_id."-".$version} = \%content; |
|---|
| 141 | } |
|---|
| 142 | |
|---|
| 143 | # Grab all the metadata |
|---|
| 144 | $sth = $dbh->prepare("SELECT node_id,version,metadata_type,metadata_value FROM metadata"); |
|---|
| 145 | $sth->execute; |
|---|
| 146 | my $i = 0; |
|---|
| 147 | while( my($node_id,$version,$metadata_type,$metadata_value) = $sth->fetchrow_array) { |
|---|
| 148 | my %metadata; |
|---|
| 149 | $metadata{'node_id'} = $node_id; |
|---|
| 150 | $metadata{'version'} = $version; |
|---|
| 151 | $metadata{'metadata_type'} = $metadata_type; |
|---|
| 152 | $metadata{'metadata_value'} = $metadata_value; |
|---|
| 153 | $metadatas{$node_id."-".($i++)} = \%metadata; |
|---|
| 154 | } |
|---|
| 155 | |
|---|
| 156 | # Grab all the internal links |
|---|
| 157 | $sth = $dbh->prepare("SELECT link_from,link_to FROM internal_links"); |
|---|
| 158 | $sth->execute; |
|---|
| 159 | while( my($link_from,$link_to) = $sth->fetchrow_array) { |
|---|
| 160 | my %il; |
|---|
| 161 | $il{'link_from'} = $link_from; |
|---|
| 162 | $il{'link_to'} = $link_to; |
|---|
| 163 | push @internal_links, \%il; |
|---|
| 164 | } |
|---|
| 165 | |
|---|
| 166 | print "done\n"; |
|---|
| 167 | |
|---|
| 168 | # Return it all |
|---|
| 169 | return (\%nodes,\%contents,\%metadatas,\@internal_links); |
|---|
| 170 | } |
|---|
| 171 | |
|---|
| 172 | # Get the version of the database schema |
|---|
| 173 | sub get_database_version { |
|---|
| 174 | my $dbh = shift; |
|---|
| 175 | my $sql = "SELECT version FROM schema_info"; |
|---|
| 176 | my $sth; |
|---|
| 177 | eval{ $sth = $dbh->prepare($sql) }; |
|---|
| 178 | if($@) { return "old"; } |
|---|
| 179 | eval{ $sth->execute }; |
|---|
| 180 | if($@) { return "old"; } |
|---|
| 181 | |
|---|
| 182 | my ($cur_schema) = $sth->fetchrow_array; |
|---|
| 183 | unless($cur_schema) { return "old"; } |
|---|
| 184 | |
|---|
| 185 | return $cur_schema; |
|---|
| 186 | } |
|---|
| 187 | |
|---|
| 188 | # Is an upgrade to the database required? |
|---|
| 189 | sub get_database_upgrade_required { |
|---|
| 190 | my ($dbh,$VERSION) = @_; |
|---|
| 191 | |
|---|
| 192 | # Get the schema version |
|---|
| 193 | my $schema_version = get_database_version($dbh); |
|---|
| 194 | |
|---|
| 195 | # Compare it |
|---|
| 196 | my $new_ver = $VERSION * 100; |
|---|
| 197 | if($schema_version eq $new_ver) { |
|---|
| 198 | # At latest version |
|---|
| 199 | return undef; |
|---|
| 200 | } else { |
|---|
| 201 | return $schema_version."_to_".$new_ver; |
|---|
| 202 | } |
|---|
| 203 | } |
|---|
| 204 | |
|---|
| 205 | # Put the latest data into the latest database structure |
|---|
| 206 | sub bulk_data_insert { |
|---|
| 207 | my ($dbh, $nodesref, $contentsref, $metadataref, $internallinksref) = @_; |
|---|
| 208 | |
|---|
| 209 | print "Bulk inserting upgraded data... "; |
|---|
| 210 | |
|---|
| 211 | # Add nodes |
|---|
| 212 | my $sth = $dbh->prepare("INSERT INTO node (id,name,version,text,modified,moderate) VALUES (?,?,?,?,?,?)"); |
|---|
| 213 | foreach my $name (keys %$nodesref) { |
|---|
| 214 | my %node = %{$nodesref->{$name}}; |
|---|
| 215 | $sth->execute($node{'id'}, |
|---|
| 216 | $node{'name'}, |
|---|
| 217 | $node{'version'}, |
|---|
| 218 | $node{'text'}, |
|---|
| 219 | $node{'modified'}, |
|---|
| 220 | $node{'moderate'}); |
|---|
| 221 | } |
|---|
| 222 | print "added ".(scalar keys %$nodesref)." nodes... "; |
|---|
| 223 | |
|---|
| 224 | # Add content |
|---|
| 225 | $sth = $dbh->prepare("INSERT INTO content (node_id,version,text,modified,comment,moderated) VALUES (?,?,?,?,?,?)"); |
|---|
| 226 | foreach my $key (keys %$contentsref) { |
|---|
| 227 | my %content = %{$contentsref->{$key}}; |
|---|
| 228 | $sth->execute($content{'node_id'}, |
|---|
| 229 | $content{'version'}, |
|---|
| 230 | $content{'text'}, |
|---|
| 231 | $content{'modified'}, |
|---|
| 232 | $content{'comment'}, |
|---|
| 233 | $content{'moderated'}); |
|---|
| 234 | } |
|---|
| 235 | |
|---|
| 236 | # Add metadata |
|---|
| 237 | $sth = $dbh->prepare("INSERT INTO metadata (node_id,version,metadata_type,metadata_value) VALUES (?,?,?,?)"); |
|---|
| 238 | foreach my $key (keys %$metadataref) { |
|---|
| 239 | my %metadata = %{$metadataref->{$key}}; |
|---|
| 240 | $sth->execute($metadata{'node_id'}, |
|---|
| 241 | $metadata{'version'}, |
|---|
| 242 | $metadata{'metadata_type'}, |
|---|
| 243 | $metadata{'metadata_value'}); |
|---|
| 244 | } |
|---|
| 245 | |
|---|
| 246 | # Add internal links |
|---|
| 247 | $sth = $dbh->prepare("INSERT INTO internal_links (link_from,link_to) VALUES (?,?)"); |
|---|
| 248 | foreach my $ilr (@$internallinksref) { |
|---|
| 249 | my %il = %{$ilr}; |
|---|
| 250 | $sth->execute($il{'link_from'}, |
|---|
| 251 | $il{'link_to'}); |
|---|
| 252 | } |
|---|
| 253 | |
|---|
| 254 | print "done\n"; |
|---|
| 255 | } |
|---|
| 256 | |
|---|
| 257 | sub perm_check { |
|---|
| 258 | my $dbh = shift; |
|---|
| 259 | # If we can do all this, we'll be able to do a bulk upgrade too |
|---|
| 260 | eval { |
|---|
| 261 | my $sth = $dbh->prepare("CREATE TABLE dbtest (test int)"); |
|---|
| 262 | $sth->execute; |
|---|
| 263 | |
|---|
| 264 | $sth = $dbh->prepare("CREATE INDEX dbtest_index ON dbtest (test)"); |
|---|
| 265 | $sth->execute; |
|---|
| 266 | |
|---|
| 267 | $sth = $dbh->prepare("DROP TABLE dbtest"); |
|---|
| 268 | $sth->execute; |
|---|
| 269 | }; |
|---|
| 270 | return $@; |
|---|
| 271 | } |
|---|