| 1 | package Wiki::Toolkit::Setup::Pg; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | 2 | ▶ | use vars qw( @ISA $VERSION $SCHEMA_VERSION ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 2 | ▶ | use Wiki::Toolkit::Setup::Database; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 2 | @ISA = qw( Wiki::Toolkit::Setup::Database ); | |||||||||||||||||||||||||||
| 10 | 2 | $VERSION = '0.11'; | |||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 2 | ▶ | use DBI; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 2 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 2 | $SCHEMA_VERSION = $VERSION*100; | |||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 2 | my $create_sql = { | |||||||||||||||||||||||||||
| 18 | 10 => { | ||||||||||||||||||||||||||||
| 19 | schema_info => [ qq| | ||||||||||||||||||||||||||||
| 20 | CREATE TABLE schema_info ( | ||||||||||||||||||||||||||||
| 21 | version integer NOT NULL default 0 | ||||||||||||||||||||||||||||
| 22 | ) | ||||||||||||||||||||||||||||
| 23 | |, qq| | ||||||||||||||||||||||||||||
| 24 | INSERT INTO schema_info VALUES (10) | ||||||||||||||||||||||||||||
| 25 | | ], | ||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | node => [ qq| | ||||||||||||||||||||||||||||
| 28 | CREATE SEQUENCE node_seq | ||||||||||||||||||||||||||||
| 29 | |, qq| | ||||||||||||||||||||||||||||
| 30 | CREATE TABLE node ( | ||||||||||||||||||||||||||||
| 31 | id integer NOT NULL DEFAULT NEXTVAL('node_seq'), | ||||||||||||||||||||||||||||
| 32 | name varchar(200) NOT NULL DEFAULT '', | ||||||||||||||||||||||||||||
| 33 | version integer NOT NULL default 0, | ||||||||||||||||||||||||||||
| 34 | text text NOT NULL default '', | ||||||||||||||||||||||||||||
| 35 | modified timestamp without time zone default NULL, | ||||||||||||||||||||||||||||
| 36 | moderate boolean NOT NULL default '0', | ||||||||||||||||||||||||||||
| 37 | CONSTRAINT pk_id PRIMARY KEY (id) | ||||||||||||||||||||||||||||
| 38 | ) | ||||||||||||||||||||||||||||
| 39 | |, qq| | ||||||||||||||||||||||||||||
| 40 | CREATE UNIQUE INDEX node_name ON node (name) | ||||||||||||||||||||||||||||
| 41 | | ], | ||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | content => [ qq| | ||||||||||||||||||||||||||||
| 44 | CREATE TABLE content ( | ||||||||||||||||||||||||||||
| 45 | node_id integer NOT NULL, | ||||||||||||||||||||||||||||
| 46 | version integer NOT NULL default 0, | ||||||||||||||||||||||||||||
| 47 | text text NOT NULL default '', | ||||||||||||||||||||||||||||
| 48 | modified timestamp without time zone default NULL, | ||||||||||||||||||||||||||||
| 49 | comment text NOT NULL default '', | ||||||||||||||||||||||||||||
| 50 | moderated boolean NOT NULL default '1', | ||||||||||||||||||||||||||||
| 51 | verified timestamp without time zone default NULL, | ||||||||||||||||||||||||||||
| 52 | verified_info text NOT NULL default '', | ||||||||||||||||||||||||||||
| 53 | CONSTRAINT pk_node_id PRIMARY KEY (node_id,version), | ||||||||||||||||||||||||||||
| 54 | CONSTRAINT fk_node_id FOREIGN KEY (node_id) REFERENCES node (id) | ||||||||||||||||||||||||||||
| 55 | ) | ||||||||||||||||||||||||||||
| 56 | | ], | ||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | internal_links => [ qq| | ||||||||||||||||||||||||||||
| 59 | CREATE TABLE internal_links ( | ||||||||||||||||||||||||||||
| 60 | link_from varchar(200) NOT NULL default '', | ||||||||||||||||||||||||||||
| 61 | link_to varchar(200) NOT NULL default '' | ||||||||||||||||||||||||||||
| 62 | ) | ||||||||||||||||||||||||||||
| 63 | |, qq| | ||||||||||||||||||||||||||||
| 64 | CREATE UNIQUE INDEX internal_links_pkey ON internal_links (link_from, link_to) | ||||||||||||||||||||||||||||
| 65 | | ], | ||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | metadata => [ qq| | ||||||||||||||||||||||||||||
| 68 | CREATE TABLE metadata ( | ||||||||||||||||||||||||||||
| 69 | node_id integer NOT NULL, | ||||||||||||||||||||||||||||
| 70 | version integer NOT NULL default 0, | ||||||||||||||||||||||||||||
| 71 | metadata_type varchar(200) NOT NULL DEFAULT '', | ||||||||||||||||||||||||||||
| 72 | metadata_value text NOT NULL DEFAULT '', | ||||||||||||||||||||||||||||
| 73 | CONSTRAINT fk_node_id FOREIGN KEY (node_id) REFERENCES node (id) | ||||||||||||||||||||||||||||
| 74 | ) | ||||||||||||||||||||||||||||
| 75 | |, qq| | ||||||||||||||||||||||||||||
| 76 | CREATE INDEX metadata_index ON metadata (node_id, version, metadata_type, metadata_value) | ||||||||||||||||||||||||||||
| 77 | | ] | ||||||||||||||||||||||||||||
| 78 | }, | ||||||||||||||||||||||||||||
| 79 | 11 => { | ||||||||||||||||||||||||||||
| 80 | schema_info => [ qq| | ||||||||||||||||||||||||||||
| 81 | CREATE TABLE schema_info ( | ||||||||||||||||||||||||||||
| 82 | version integer NOT NULL default 0 | ||||||||||||||||||||||||||||
| 83 | ) | ||||||||||||||||||||||||||||
| 84 | |, qq| | ||||||||||||||||||||||||||||
| 85 | INSERT INTO schema_info VALUES (11) | ||||||||||||||||||||||||||||
| 86 | | ], | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | node => [ qq| | ||||||||||||||||||||||||||||
| 89 | CREATE SEQUENCE node_seq | ||||||||||||||||||||||||||||
| 90 | |, qq| | ||||||||||||||||||||||||||||
| 91 | CREATE TABLE node ( | ||||||||||||||||||||||||||||
| 92 | id integer NOT NULL DEFAULT NEXTVAL('node_seq'), | ||||||||||||||||||||||||||||
| 93 | name varchar(200) NOT NULL DEFAULT '', | ||||||||||||||||||||||||||||
| 94 | version integer NOT NULL default 0, | ||||||||||||||||||||||||||||
| 95 | text text NOT NULL, | ||||||||||||||||||||||||||||
| 96 | modified timestamp without time zone default NULL, | ||||||||||||||||||||||||||||
| 97 | moderate boolean NOT NULL default '0', | ||||||||||||||||||||||||||||
| 98 | CONSTRAINT pk_id PRIMARY KEY (id) | ||||||||||||||||||||||||||||
| 99 | ) | ||||||||||||||||||||||||||||
| 100 | |, qq| | ||||||||||||||||||||||||||||
| 101 | CREATE UNIQUE INDEX node_name ON node (name) | ||||||||||||||||||||||||||||
| 102 | | ], | ||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | content => [ qq| | ||||||||||||||||||||||||||||
| 105 | CREATE TABLE content ( | ||||||||||||||||||||||||||||
| 106 | node_id integer NOT NULL, | ||||||||||||||||||||||||||||
| 107 | version integer NOT NULL default 0, | ||||||||||||||||||||||||||||
| 108 | text text NOT NULL, | ||||||||||||||||||||||||||||
| 109 | modified timestamp without time zone default NULL, | ||||||||||||||||||||||||||||
| 110 | comment text, | ||||||||||||||||||||||||||||
| 111 | moderated boolean NOT NULL default '1', | ||||||||||||||||||||||||||||
| 112 | verified timestamp without time zone default NULL, | ||||||||||||||||||||||||||||
| 113 | verified_info text, | ||||||||||||||||||||||||||||
| 114 | CONSTRAINT pk_node_id PRIMARY KEY (node_id,version), | ||||||||||||||||||||||||||||
| 115 | CONSTRAINT fk_node_id FOREIGN KEY (node_id) REFERENCES node (id) | ||||||||||||||||||||||||||||
| 116 | ) | ||||||||||||||||||||||||||||
| 117 | | ], | ||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | internal_links => [ qq| | ||||||||||||||||||||||||||||
| 120 | CREATE TABLE internal_links ( | ||||||||||||||||||||||||||||
| 121 | link_from varchar(200) NOT NULL default '', | ||||||||||||||||||||||||||||
| 122 | link_to varchar(200) NOT NULL default '' | ||||||||||||||||||||||||||||
| 123 | ) | ||||||||||||||||||||||||||||
| 124 | |, qq| | ||||||||||||||||||||||||||||
| 125 | CREATE UNIQUE INDEX internal_links_pkey ON internal_links (link_from, link_to) | ||||||||||||||||||||||||||||
| 126 | | ], | ||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | metadata => [ qq| | ||||||||||||||||||||||||||||
| 129 | CREATE TABLE metadata ( | ||||||||||||||||||||||||||||
| 130 | node_id integer NOT NULL, | ||||||||||||||||||||||||||||
| 131 | version integer NOT NULL default 0, | ||||||||||||||||||||||||||||
| 132 | metadata_type varchar(200) NOT NULL DEFAULT '', | ||||||||||||||||||||||||||||
| 133 | metadata_value text NOT NULL, | ||||||||||||||||||||||||||||
| 134 | CONSTRAINT fk_node_id FOREIGN KEY (node_id) REFERENCES node (id) | ||||||||||||||||||||||||||||
| 135 | ) | ||||||||||||||||||||||||||||
| 136 | |, qq| | ||||||||||||||||||||||||||||
| 137 | CREATE INDEX metadata_index ON metadata (node_id, version, metadata_type, metadata_value) | ||||||||||||||||||||||||||||
| 138 | | ] | ||||||||||||||||||||||||||||
| 139 | }, | ||||||||||||||||||||||||||||
| 140 | }; | ||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | 2 | my %upgrades = ( | |||||||||||||||||||||||||||
| 143 | '10_to_11' => [ | ||||||||||||||||||||||||||||
| 144 | qq| | ||||||||||||||||||||||||||||
| 145 | ALTER TABLE node ALTER COLUMN text DROP DEFAULT | ||||||||||||||||||||||||||||
| 146 | |, qq| | ||||||||||||||||||||||||||||
| 147 | ALTER TABLE content ALTER COLUMN text DROP DEFAULT | ||||||||||||||||||||||||||||
| 148 | |, qq| | ||||||||||||||||||||||||||||
| 149 | ALTER TABLE content ALTER COLUMN comment DROP DEFAULT | ||||||||||||||||||||||||||||
| 150 | |, qq| | ||||||||||||||||||||||||||||
| 151 | ALTER TABLE content ALTER COLUMN comment DROP NOT NULL | ||||||||||||||||||||||||||||
| 152 | |, qq| | ||||||||||||||||||||||||||||
| 153 | ALTER TABLE content ALTER COLUMN verified_info DROP DEFAULT | ||||||||||||||||||||||||||||
| 154 | |, qq| | ||||||||||||||||||||||||||||
| 155 | ALTER TABLE content ALTER COLUMN verified_info DROP NOT NULL | ||||||||||||||||||||||||||||
| 156 | |, qq| | ||||||||||||||||||||||||||||
| 157 | ALTER TABLE metadata ALTER COLUMN metadata_value DROP DEFAULT | ||||||||||||||||||||||||||||
| 158 | |, qq| | ||||||||||||||||||||||||||||
| 159 | UPDATE schema_info SET version = 11 | ||||||||||||||||||||||||||||
| 160 | |, | ||||||||||||||||||||||||||||
| 161 | ], | ||||||||||||||||||||||||||||
| 162 | ); | ||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||
| 164 | =head1 NAME | ||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | Wiki::Toolkit::Setup::Pg - Set up tables for a Wiki::Toolkit store in a Postgres database. | ||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||
| 170 | use Wiki::Toolkit::Setup::Pg; | ||||||||||||||||||||||||||||
| 171 | Wiki::Toolkit::Setup::Pg::setup($dbname, $dbuser, $dbpass, $dbhost); | ||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||
| 173 | Omit $dbhost if the database is local. | ||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | Set up a Postgres database for use as a Wiki::Toolkit store. | ||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||
| 179 | =head1 FUNCTIONS | ||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||
| 181 | =over 4 | ||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||
| 183 | =item B<setup> | ||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||
| 185 | use Wiki::Toolkit::Setup::Pg; | ||||||||||||||||||||||||||||
| 186 | Wiki::Toolkit::Setup::Pg::setup($dbname, $dbuser, $dbpass, $dbhost); | ||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||
| 188 | or | ||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||
| 190 | Wiki::Toolkit::Setup::Pg::setup( $dbh ); | ||||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||
| 192 | You can either provide an active database handle C<$dbh> or connection | ||||||||||||||||||||||||||||
| 193 | parameters. | ||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||
| 195 | If you provide connection parameters the following arguments are | ||||||||||||||||||||||||||||
| 196 | mandatory -- the database name, the username and the password. The | ||||||||||||||||||||||||||||
| 197 | username must be able to create and drop tables in the database. | ||||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||
| 199 | The $dbhost argument is optional -- omit it if the database is local. | ||||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||
| 201 | B<NOTE:> If a table that the module wants to create already exists, | ||||||||||||||||||||||||||||
| 202 | C<setup> will leave it alone. This means that you can safely run this | ||||||||||||||||||||||||||||
| 203 | on an existing L<Wiki::Toolkit> database to bring the schema up to date | ||||||||||||||||||||||||||||
| 204 | with the current L<Wiki::Toolkit> version. If you wish to completely start | ||||||||||||||||||||||||||||
| 205 | again with a fresh database, run C<cleardb> first. | ||||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||
| 207 | =cut | ||||||||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||||||
| 209 | 0 | ▶ | sub setup { | ||||||||||||||||||||||||||
|
Subroutine
setup: not called CC=17 SCAR=57.2
Pod
documented
| |||||||||||||||||||||||||||||
| 210 | 0 | my @args = @_; | |||||||||||||||||||||||||||
| 211 | 0 | my $dbh = _get_dbh( @args ); | |||||||||||||||||||||||||||
| 212 | 0 | my $disconnect_required = _disconnect_required( @args ); | |||||||||||||||||||||||||||
| 213 | 0 | ▶ | my $wanted_schema = _get_wanted_schema( @args ) || $SCHEMA_VERSION; | ||||||||||||||||||||||||||
|
Condition0%0/3
_get_wanted_schema(@args) || $Wiki::Toolkit::Setup::Pg::SCHEMA_VERSION
MC/DC0%0/2
_get_wanted_schema(@args) || $Wiki::Toolkit::Setup::Pg::SCHEMA_VERSION
_get_wanted_schema(@args)
$Wiki::Toolkit::Setup::Pg::SCHEMA_VERSION
Truth table0%0/3
A || B
A= _get_wanted_schema(@args)B= $Wiki::Toolkit::Setup::Pg::SCHEMA_VERSION
| |||||||||||||||||||||||||||||
| 214 | |||||||||||||||||||||||||||||
| 215 | die "No schema information for requested schema version $wanted_schema\n" | ||||||||||||||||||||||||||||
| 216 | 0 | ▶ | unless $create_sql->{$wanted_schema}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 217 | |||||||||||||||||||||||||||||
| 218 | # Check whether tables exist | ||||||||||||||||||||||||||||
| 219 | my $sql = "SELECT tablename FROM pg_tables | ||||||||||||||||||||||||||||
| 220 | WHERE tablename in (" | ||||||||||||||||||||||||||||
| 221 | 0 | . join( ",", map { $dbh->quote($_) } keys %{$create_sql->{$wanted_schema}} ) . ")"; | |||||||||||||||||||||||||||
| 222 | 0 | ▶ | my $sth = $dbh->prepare($sql) or croak $dbh->errstr; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 223 | 0 | $sth->execute; | |||||||||||||||||||||||||||
| 224 | 0 | my %tables; | |||||||||||||||||||||||||||
| 225 | 0 | ▶ | while ( my $table = $sth->fetchrow_array ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 226 | 0 | ▶ | exists $create_sql->{$wanted_schema}->{$table} and $tables{$table} = 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 227 | } | ||||||||||||||||||||||||||||
| 228 | |||||||||||||||||||||||||||||
| 229 | # Do we need to upgrade the schema of existing tables? | ||||||||||||||||||||||||||||
| 230 | # (Don't check if no tables currently exist) | ||||||||||||||||||||||||||||
| 231 | 0 | my $upgrade_schema; | |||||||||||||||||||||||||||
| 232 | 0 | ▶ | if(scalar keys %tables > 0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 233 | 0 | $upgrade_schema = Wiki::Toolkit::Setup::Database::get_database_upgrade_required($dbh,$wanted_schema); | |||||||||||||||||||||||||||
| 234 | } else { | ||||||||||||||||||||||||||||
| 235 | 0 | print "Skipping schema upgrade check - no tables found\n"; | |||||||||||||||||||||||||||
| 236 | } | ||||||||||||||||||||||||||||
| 237 | |||||||||||||||||||||||||||||
| 238 | # Set up tables if not found | ||||||||||||||||||||||||||||
| 239 | 0 | foreach my $required ( reverse sort keys %{$create_sql->{$wanted_schema}} ) { | |||||||||||||||||||||||||||
| 240 | 0 | ▶ | if ( $tables{$required} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 241 | 0 | print "Table $required already exists... skipping...\n"; | |||||||||||||||||||||||||||
| 242 | } else { | ||||||||||||||||||||||||||||
| 243 | 0 | print "Creating table $required... done\n"; | |||||||||||||||||||||||||||
| 244 | 0 | foreach my $sql ( @{ $create_sql->{$wanted_schema}->{$required} } ) { | |||||||||||||||||||||||||||
| 245 | 0 | ▶ | $dbh->do($sql) or croak $dbh->errstr; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 246 | } | ||||||||||||||||||||||||||||
| 247 | } | ||||||||||||||||||||||||||||
| 248 | } | ||||||||||||||||||||||||||||
| 249 | |||||||||||||||||||||||||||||
| 250 | # Do the upgrade if required | ||||||||||||||||||||||||||||
| 251 | 0 | ▶ | if($upgrade_schema) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 252 | 0 | print "Upgrading schema: $upgrade_schema\n"; | |||||||||||||||||||||||||||
| 253 | 0 | my @updates = @{$upgrades{$upgrade_schema}}; | |||||||||||||||||||||||||||
| 254 | 0 | foreach my $update (@updates) { | |||||||||||||||||||||||||||
| 255 | 0 | ▶ | if(ref($update) eq "CODE") { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 256 | 0 | &$update($dbh); | |||||||||||||||||||||||||||
| 257 | } elsif(ref($update) eq "ARRAY") { | ||||||||||||||||||||||||||||
| 258 | 0 | foreach my $nupdate (@$update) { | |||||||||||||||||||||||||||
| 259 | 0 | $dbh->do($nupdate); | |||||||||||||||||||||||||||
| 260 | } | ||||||||||||||||||||||||||||
| 261 | } else { | ||||||||||||||||||||||||||||
| 262 | 0 | $dbh->do($update); | |||||||||||||||||||||||||||
| 263 | } | ||||||||||||||||||||||||||||
| 264 | } | ||||||||||||||||||||||||||||
| 265 | } | ||||||||||||||||||||||||||||
| 266 | |||||||||||||||||||||||||||||
| 267 | # Clean up if we made our own dbh. | ||||||||||||||||||||||||||||
| 268 | 0 | ▶ | $dbh->disconnect if $disconnect_required; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 269 | } | ||||||||||||||||||||||||||||
| 270 | |||||||||||||||||||||||||||||
| 271 | =item B<cleardb> | ||||||||||||||||||||||||||||
| 272 | |||||||||||||||||||||||||||||
| 273 | use Wiki::Toolkit::Setup::Pg; | ||||||||||||||||||||||||||||
| 274 | |||||||||||||||||||||||||||||
| 275 | # Clear out all Wiki::Toolkit tables from the database. | ||||||||||||||||||||||||||||
| 276 | Wiki::Toolkit::Setup::Pg::cleardb($dbname, $dbuser, $dbpass, $dbhost); | ||||||||||||||||||||||||||||
| 277 | |||||||||||||||||||||||||||||
| 278 | or | ||||||||||||||||||||||||||||
| 279 | |||||||||||||||||||||||||||||
| 280 | Wiki::Toolkit::Setup::Pg::cleardb( $dbh ); | ||||||||||||||||||||||||||||
| 281 | |||||||||||||||||||||||||||||
| 282 | You can either provide an active database handle C<$dbh> or connection | ||||||||||||||||||||||||||||
| 283 | parameters. | ||||||||||||||||||||||||||||
| 284 | |||||||||||||||||||||||||||||
| 285 | If you provide connection parameters the following arguments are | ||||||||||||||||||||||||||||
| 286 | mandatory -- the database name, the username and the password. The | ||||||||||||||||||||||||||||
| 287 | username must be able to drop tables in the database. | ||||||||||||||||||||||||||||
| 288 | |||||||||||||||||||||||||||||
| 289 | The $dbhost argument is optional -- omit it if the database is local. | ||||||||||||||||||||||||||||
| 290 | |||||||||||||||||||||||||||||
| 291 | Clears out all L<Wiki::Toolkit> store tables from the database. B<NOTE> | ||||||||||||||||||||||||||||
| 292 | that this will lose all your data; you probably only want to use this | ||||||||||||||||||||||||||||
| 293 | for testing purposes or if you really screwed up somewhere. Note also | ||||||||||||||||||||||||||||
| 294 | that it doesn't touch any L<Wiki::Toolkit> search backend tables; if you | ||||||||||||||||||||||||||||
| 295 | have any of those in the same or a different database see | ||||||||||||||||||||||||||||
| 296 | L<Wiki::Toolkit::Setup::DBIxFTS> or L<Wiki::Toolkit::Setup::SII>, depending on | ||||||||||||||||||||||||||||
| 297 | which search backend you're using. | ||||||||||||||||||||||||||||
| 298 | |||||||||||||||||||||||||||||
| 299 | =cut | ||||||||||||||||||||||||||||
| 300 | |||||||||||||||||||||||||||||
| 301 | 0 | ▶ | sub cleardb { | ||||||||||||||||||||||||||
|
Subroutine
cleardb: not called CC=6 SCAR=37.4
Pod
documented
| |||||||||||||||||||||||||||||
| 302 | 0 | my @args = @_; | |||||||||||||||||||||||||||
| 303 | 0 | my $dbh = _get_dbh( @args ); | |||||||||||||||||||||||||||
| 304 | 0 | my $disconnect_required = _disconnect_required( @args ); | |||||||||||||||||||||||||||
| 305 | |||||||||||||||||||||||||||||
| 306 | 0 | print "Dropping tables... "; | |||||||||||||||||||||||||||
| 307 | my $sql = "SELECT tablename FROM pg_tables | ||||||||||||||||||||||||||||
| 308 | WHERE tablename in (" | ||||||||||||||||||||||||||||
| 309 | 0 | . join( ",", map { $dbh->quote($_) } keys %{$create_sql->{$SCHEMA_VERSION}} ) . ")"; | |||||||||||||||||||||||||||
| 310 | 0 | foreach my $tableref (@{$dbh->selectall_arrayref($sql)}) { | |||||||||||||||||||||||||||
| 311 | 0 | ▶ | $dbh->do("DROP TABLE $tableref->[0] CASCADE") or croak $dbh->errstr; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 312 | } | ||||||||||||||||||||||||||||
| 313 | |||||||||||||||||||||||||||||
| 314 | 0 | $sql = "SELECT relname FROM pg_statio_all_sequences | |||||||||||||||||||||||||||
| 315 | WHERE relname = 'node_seq'"; | ||||||||||||||||||||||||||||
| 316 | 0 | foreach my $seqref (@{$dbh->selectall_arrayref($sql)}) { | |||||||||||||||||||||||||||
| 317 | 0 | ▶ | $dbh->do("DROP SEQUENCE $seqref->[0]") or croak $dbh->errstr; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 318 | } | ||||||||||||||||||||||||||||
| 319 | |||||||||||||||||||||||||||||
| 320 | 0 | print "done\n"; | |||||||||||||||||||||||||||
| 321 | |||||||||||||||||||||||||||||
| 322 | # Clean up if we made our own dbh. | ||||||||||||||||||||||||||||
| 323 | 0 | ▶ | $dbh->disconnect if $disconnect_required; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 324 | } | ||||||||||||||||||||||||||||
| 325 | |||||||||||||||||||||||||||||
| 326 | 0 | ▶ | sub _get_dbh { | ||||||||||||||||||||||||||
|
Subroutine
_get_dbh: not called CC=6 SCAR=37.4
| |||||||||||||||||||||||||||||
| 327 | # Database handle passed in. | ||||||||||||||||||||||||||||
| 328 | 0 | ▶ | if ( ref $_[0] and ref $_[0] eq 'DBI::db' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
ref $_[0] and ref $_[0] eq "DBI::db"
MC/DC0%0/2
ref $_[0] and ref $_[0] eq "DBI::db"
ref $_[0]
ref $_[0] eq "DBI::db"
Truth table0%0/3
A and B
A= ref $_[0]B= ref $_[0] eq "DBI::db"
| |||||||||||||||||||||||||||||
| 329 | 0 | return $_[0]; | |||||||||||||||||||||||||||
| 330 | } | ||||||||||||||||||||||||||||
| 331 | |||||||||||||||||||||||||||||
| 332 | # Args passed as hashref. | ||||||||||||||||||||||||||||
| 333 | 0 | ▶ | if ( ref $_[0] and ref $_[0] eq 'HASH' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
ref $_[0] and ref $_[0] eq "HASH"
MC/DC0%0/2
ref $_[0] and ref $_[0] eq "HASH"
ref $_[0]
ref $_[0] eq "HASH"
Truth table0%0/3
A and B
A= ref $_[0]B= ref $_[0] eq "HASH"
| |||||||||||||||||||||||||||||
| 334 | 0 | my %args = %{$_[0]}; | |||||||||||||||||||||||||||
| 335 | 0 | ▶ | if ( $args{dbh} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 336 | 0 | return $args{dbh}; | |||||||||||||||||||||||||||
| 337 | } else { | ||||||||||||||||||||||||||||
| 338 | 0 | return _make_dbh( %args ); | |||||||||||||||||||||||||||
| 339 | } | ||||||||||||||||||||||||||||
| 340 | } | ||||||||||||||||||||||||||||
| 341 | |||||||||||||||||||||||||||||
| 342 | # Args passed as list of connection details. | ||||||||||||||||||||||||||||
| 343 | 0 | return _make_dbh( | |||||||||||||||||||||||||||
| 344 | dbname => $_[0], | ||||||||||||||||||||||||||||
| 345 | dbuser => $_[1], | ||||||||||||||||||||||||||||
| 346 | dbpass => $_[2], | ||||||||||||||||||||||||||||
| 347 | dbhost => $_[3], | ||||||||||||||||||||||||||||
| 348 | ); | ||||||||||||||||||||||||||||
| 349 | } | ||||||||||||||||||||||||||||
| 350 | |||||||||||||||||||||||||||||
| 351 | 0 | ▶ | sub _get_wanted_schema { | ||||||||||||||||||||||||||
|
Subroutine
_get_wanted_schema: not called CC=5 SCAR=34.0
| |||||||||||||||||||||||||||||
| 352 | # Database handle passed in. | ||||||||||||||||||||||||||||
| 353 | 0 | ▶ | if ( ref $_[0] and ref $_[0] eq 'DBI::db' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
ref $_[0] and ref $_[0] eq "DBI::db"
MC/DC0%0/2
ref $_[0] and ref $_[0] eq "DBI::db"
ref $_[0]
ref $_[0] eq "DBI::db"
Truth table0%0/3
A and B
A= ref $_[0]B= ref $_[0] eq "DBI::db"
| |||||||||||||||||||||||||||||
| 354 | 0 | return undef; | |||||||||||||||||||||||||||
| 355 | } | ||||||||||||||||||||||||||||
| 356 | |||||||||||||||||||||||||||||
| 357 | # Args passed as hashref. | ||||||||||||||||||||||||||||
| 358 | 0 | ▶ | if ( ref $_[0] and ref $_[0] eq 'HASH' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
ref $_[0] and ref $_[0] eq "HASH"
MC/DC0%0/2
ref $_[0] and ref $_[0] eq "HASH"
ref $_[0]
ref $_[0] eq "HASH"
Truth table0%0/3
A and B
A= ref $_[0]B= ref $_[0] eq "HASH"
| |||||||||||||||||||||||||||||
| 359 | 0 | my %args = %{$_[0]}; | |||||||||||||||||||||||||||
| 360 | 0 | return $args{wanted_schema}; | |||||||||||||||||||||||||||
| 361 | } | ||||||||||||||||||||||||||||
| 362 | } | ||||||||||||||||||||||||||||
| 363 | |||||||||||||||||||||||||||||
| 364 | 0 | ▶ | sub _disconnect_required { | ||||||||||||||||||||||||||
|
Subroutine
_disconnect_required: not called CC=6 SCAR=37.4
| |||||||||||||||||||||||||||||
| 365 | # Database handle passed in. | ||||||||||||||||||||||||||||
| 366 | 0 | ▶ | if ( ref $_[0] and ref $_[0] eq 'DBI::db' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
ref $_[0] and ref $_[0] eq "DBI::db"
MC/DC0%0/2
ref $_[0] and ref $_[0] eq "DBI::db"
ref $_[0]
ref $_[0] eq "DBI::db"
Truth table0%0/3
A and B
A= ref $_[0]B= ref $_[0] eq "DBI::db"
| |||||||||||||||||||||||||||||
| 367 | 0 | return 0; | |||||||||||||||||||||||||||
| 368 | } | ||||||||||||||||||||||||||||
| 369 | |||||||||||||||||||||||||||||
| 370 | # Args passed as hashref. | ||||||||||||||||||||||||||||
| 371 | 0 | ▶ | if ( ref $_[0] and ref $_[0] eq 'HASH' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
ref $_[0] and ref $_[0] eq "HASH"
MC/DC0%0/2
ref $_[0] and ref $_[0] eq "HASH"
ref $_[0]
ref $_[0] eq "HASH"
Truth table0%0/3
A and B
A= ref $_[0]B= ref $_[0] eq "HASH"
| |||||||||||||||||||||||||||||
| 372 | 0 | my %args = %{$_[0]}; | |||||||||||||||||||||||||||
| 373 | 0 | ▶ | if ( $args{dbh} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 374 | 0 | return 0; | |||||||||||||||||||||||||||
| 375 | } else { | ||||||||||||||||||||||||||||
| 376 | 0 | return 1; | |||||||||||||||||||||||||||
| 377 | } | ||||||||||||||||||||||||||||
| 378 | } | ||||||||||||||||||||||||||||
| 379 | |||||||||||||||||||||||||||||
| 380 | # Args passed as list of connection details. | ||||||||||||||||||||||||||||
| 381 | 0 | return 1; | |||||||||||||||||||||||||||
| 382 | } | ||||||||||||||||||||||||||||
| 383 | |||||||||||||||||||||||||||||
| 384 | 0 | ▶ | sub _make_dbh { | ||||||||||||||||||||||||||
|
Subroutine
_make_dbh: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 385 | 0 | my %args = @_; | |||||||||||||||||||||||||||
| 386 | 0 | my $dsn = "dbi:Pg:dbname=$args{dbname}"; | |||||||||||||||||||||||||||
| 387 | 0 | ▶ | $dsn .= ";host=$args{dbhost}" if $args{dbhost}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 388 | my $dbh = DBI->connect($dsn, $args{dbuser}, $args{dbpass}, | ||||||||||||||||||||||||||||
| 389 | 0 | ▶ | { PrintError => 1, RaiseError => 1, | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 390 | AutoCommit => 1 } ) | ||||||||||||||||||||||||||||
| 391 | or croak DBI::errstr; | ||||||||||||||||||||||||||||
| 392 | 0 | return $dbh; | |||||||||||||||||||||||||||
| 393 | } | ||||||||||||||||||||||||||||
| 394 | |||||||||||||||||||||||||||||
| 395 | =back | ||||||||||||||||||||||||||||
| 396 | |||||||||||||||||||||||||||||
| 397 | =head1 ALTERNATIVE CALLING SYNTAX | ||||||||||||||||||||||||||||
| 398 | |||||||||||||||||||||||||||||
| 399 | As requested by Podmaster. Instead of passing arguments to the methods as | ||||||||||||||||||||||||||||
| 400 | |||||||||||||||||||||||||||||
| 401 | ($dbname, $dbuser, $dbpass, $dbhost) | ||||||||||||||||||||||||||||
| 402 | |||||||||||||||||||||||||||||
| 403 | you can pass them as | ||||||||||||||||||||||||||||
| 404 | |||||||||||||||||||||||||||||
| 405 | ( { dbname => $dbname, | ||||||||||||||||||||||||||||
| 406 | dbuser => $dbuser, | ||||||||||||||||||||||||||||
| 407 | dbpass => $dbpass, | ||||||||||||||||||||||||||||
| 408 | dbhost => $dbhost | ||||||||||||||||||||||||||||
| 409 | } | ||||||||||||||||||||||||||||
| 410 | ) | ||||||||||||||||||||||||||||
| 411 | |||||||||||||||||||||||||||||
| 412 | or indeed as | ||||||||||||||||||||||||||||
| 413 | |||||||||||||||||||||||||||||
| 414 | ( { dbh => $dbh } ) | ||||||||||||||||||||||||||||
| 415 | |||||||||||||||||||||||||||||
| 416 | Note that's a hashref, not a hash. | ||||||||||||||||||||||||||||
| 417 | |||||||||||||||||||||||||||||
| 418 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 419 | |||||||||||||||||||||||||||||
| 420 | Kake Pugh (kake@earth.li). | ||||||||||||||||||||||||||||
| 421 | |||||||||||||||||||||||||||||
| 422 | =head1 COPYRIGHT | ||||||||||||||||||||||||||||
| 423 | |||||||||||||||||||||||||||||
| 424 | Copyright (C) 2002-2004 Kake Pugh. All Rights Reserved. | ||||||||||||||||||||||||||||
| 425 | Copyright (C) 2006-2008 the Wiki::Toolkit team. All Rights Reserved. | ||||||||||||||||||||||||||||
| 426 | |||||||||||||||||||||||||||||
| 427 | This module is free software; you can redistribute it and/or modify it | ||||||||||||||||||||||||||||
| 428 | under the same terms as Perl itself. | ||||||||||||||||||||||||||||
| 429 | |||||||||||||||||||||||||||||
| 430 | =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 431 | |||||||||||||||||||||||||||||
| 432 | L<Wiki::Toolkit>, L<Wiki::Toolkit::Setup::DBIxFTS>, L<Wiki::Toolkit::Setup::SII> | ||||||||||||||||||||||||||||
| 433 | |||||||||||||||||||||||||||||
| 434 | =cut | ||||||||||||||||||||||||||||
| 435 | |||||||||||||||||||||||||||||
| 436 | 2 | 1; | |||||||||||||||||||||||||||