Changeset 440
- Timestamp:
- 05/12/08 01:21:15 (4 years ago)
- Location:
- wiki-toolkit/trunk
- Files:
-
- 1 added
- 7 modified
-
Changes (modified) (1 diff)
-
MANIFEST (modified) (1 diff)
-
lib/Wiki/Toolkit/Setup/Database.pm (modified) (1 diff)
-
lib/Wiki/Toolkit/Setup/MySQL.pm (modified) (7 diffs)
-
lib/Wiki/Toolkit/Setup/Pg.pm (modified) (6 diffs)
-
lib/Wiki/Toolkit/Setup/SQLite.pm (modified) (5 diffs)
-
lib/Wiki/Toolkit/TestLib.pm (modified) (1 diff)
-
t/400_upgrade.t (added)
Legend:
- Unmodified
- Added
- Removed
-
wiki-toolkit/trunk/Changes
r439 r440 1 1 0.76 2 2 Really add missing prereq of DBI! 3 Add testing of database schema upgrades 3 4 4 5 0.75 11 May 2008 -
wiki-toolkit/trunk/MANIFEST
r435 r440 81 81 t/303_feed_atom_node_all_versions.t 82 82 t/304_feed_atom_gen_node_dist_feed.t 83 t/400_upgrade.t 83 84 t/lib/Wiki/Toolkit/Plugin/Bar.pm 84 85 t/lib/Wiki/Toolkit/Plugin/Foo.pm -
wiki-toolkit/trunk/lib/Wiki/Toolkit/Setup/Database.pm
r438 r440 3 3 use strict; 4 4 5 use vars qw( $VERSION @SUPPORTED_SCHEMAS );5 use vars qw( $VERSION @SUPPORTED_SCHEMAS); 6 6 7 7 $VERSION = 0.08; 8 @SUPPORTED_SCHEMAS = qw( 9);8 @SUPPORTED_SCHEMAS = qw(8 9); 9 9 10 10 =head1 NAME -
wiki-toolkit/trunk/lib/Wiki/Toolkit/Setup/MySQL.pm
r438 r440 16 16 17 17 my $create_sql = { 18 8 => { 19 schema_info => [ qq| 20 CREATE TABLE schema_info ( 21 version int(10) NOT NULL default 0 22 ) 23 |, qq| 24 INSERT INTO schema_info VALUES (8) 25 | ], 26 27 node => [ qq| 28 CREATE TABLE node ( 29 id integer NOT NULL AUTO_INCREMENT, 30 name varchar(200) NOT NULL DEFAULT '', 31 version int(10) NOT NULL default 0, 32 text mediumtext NOT NULL default '', 33 modified datetime default NULL, 34 PRIMARY KEY (id) 35 ) 36 | ], 37 38 content => [ qq| 39 CREATE TABLE content ( 40 node_id integer NOT NULL, 41 version int(10) NOT NULL default 0, 42 text mediumtext NOT NULL default '', 43 modified datetime default NULL, 44 comment mediumtext NOT NULL default '', 45 PRIMARY KEY (node_id, version) 46 ) 47 | ], 48 internal_links => [ qq| 49 CREATE TABLE internal_links ( 50 link_from varchar(200) NOT NULL default '', 51 link_to varchar(200) NOT NULL default '', 52 PRIMARY KEY (link_from, link_to) 53 ) 54 | ], 55 metadata => [ qq| 56 CREATE TABLE metadata ( 57 node_id integer NOT NULL, 58 version int(10) NOT NULL default 0, 59 metadata_type varchar(200) NOT NULL DEFAULT '', 60 metadata_value mediumtext NOT NULL DEFAULT '' 61 ) 62 |, qq| 63 CREATE INDEX metadata_index ON metadata(node_id, version, metadata_type, metadata_value(10)) 64 | ] 65 }, 18 66 9 => { 19 67 schema_info => [ qq| … … 119 167 my $wanted_schema = _get_wanted_schema( @args ) || $SCHEMA_VERSION; 120 168 169 die "No schema information for requested schema version $wanted_schema\n" 170 unless $create_sql->{$wanted_schema}; 171 121 172 # Check whether tables exist 122 my %tables = fetch_tables_listing($dbh );173 my %tables = fetch_tables_listing($dbh, $wanted_schema); 123 174 124 175 # Do we need to upgrade the schema of existing tables? … … 127 178 my @cur_data; 128 179 if(scalar keys %tables > 0) { 129 $upgrade_schema = Wiki::Toolkit::Setup::Database::get_database_upgrade_required($dbh,$ SCHEMA_VERSION);180 $upgrade_schema = Wiki::Toolkit::Setup::Database::get_database_upgrade_required($dbh,$wanted_schema); 130 181 } 131 182 if($upgrade_schema) { … … 146 197 147 198 # Grab new list of tables 148 %tables = fetch_tables_listing($dbh );199 %tables = fetch_tables_listing($dbh, $wanted_schema); 149 200 } 150 201 151 202 # Set up tables if not found 152 foreach my $required ( keys %{$create_sql->{$ SCHEMA_VERSION}} ) {203 foreach my $required ( keys %{$create_sql->{$wanted_schema}} ) { 153 204 if ( $tables{$required} ) { 154 205 print "Table $required already exists... skipping...\n"; 155 206 } else { 156 207 print "Creating table $required... done\n"; 157 foreach my $sql ( @{$create_sql->{$ SCHEMA_VERSION}->{$required}} ) {208 foreach my $sql ( @{$create_sql->{$wanted_schema}->{$required}} ) { 158 209 $dbh->do($sql) or croak $dbh->errstr; 159 210 } … … 173 224 sub fetch_tables_listing { 174 225 my $dbh = shift; 226 my $wanted_schema = shift; 175 227 176 228 # Check what tables exist … … 179 231 my %tables; 180 232 while ( my $table = $sth->fetchrow_array ) { 181 exists $create_sql->{$ SCHEMA_VERSION}->{$table} and $tables{$table} = 1;233 exists $create_sql->{$wanted_schema}->{$table} and $tables{$table} = 1; 182 234 } 183 235 return %tables; … … 264 316 return $args{wanted_schema}; 265 317 } 266 267 # Args passed as list of connection details.268 return $_[1];269 318 } 270 319 -
wiki-toolkit/trunk/lib/Wiki/Toolkit/Setup/Pg.pm
r438 r440 16 16 17 17 my $create_sql = { 18 8 => { 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 (8) 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 CONSTRAINT pk_id PRIMARY KEY (id) 37 ) 38 |, qq| 39 CREATE UNIQUE INDEX node_name ON node (name) 40 | ], 41 42 content => [ qq| 43 CREATE TABLE content ( 44 node_id integer NOT NULL, 45 version integer NOT NULL default 0, 46 text text NOT NULL default '', 47 modified timestamp without time zone default NULL, 48 comment text NOT NULL default '', 49 CONSTRAINT pk_node_id PRIMARY KEY (node_id,version), 50 CONSTRAINT fk_node_id FOREIGN KEY (node_id) REFERENCES node (id) 51 ) 52 | ], 53 54 internal_links => [ qq| 55 CREATE TABLE internal_links ( 56 link_from varchar(200) NOT NULL default '', 57 link_to varchar(200) NOT NULL default '' 58 ) 59 |, qq| 60 CREATE UNIQUE INDEX internal_links_pkey ON internal_links (link_from, link_to) 61 | ], 62 63 metadata => [ qq| 64 CREATE TABLE metadata ( 65 node_id integer NOT NULL, 66 version integer NOT NULL default 0, 67 metadata_type varchar(200) NOT NULL DEFAULT '', 68 metadata_value text NOT NULL DEFAULT '', 69 CONSTRAINT fk_node_id FOREIGN KEY (node_id) REFERENCES node (id) 70 ) 71 |, qq| 72 CREATE INDEX metadata_index ON metadata (node_id, version, metadata_type, metadata_value) 73 | ] 74 75 }, 18 76 9 => { 19 77 schema_info => [ qq| … … 113 171 ALTER TABLE metadata ADD CONSTRAINT fk_node_id FOREIGN KEY (node_id) REFERENCES node (id); 114 172 CREATE INDEX metadata_index ON metadata (node_id, version, metadata_type, metadata_value) 173 |, 174 175 qq| 176 CREATE TABLE schema_info (version integer NOT NULL default 0); 177 INSERT INTO schema_info VALUES (8) 115 178 | 116 179 ], … … 126 189 ALTER TABLE content ALTER COLUMN moderated SET DEFAULT '1'; 127 190 ALTER TABLE content ALTER COLUMN moderated SET NOT NULL; 191 UPDATE schema_info SET version = 9; 128 192 | 129 193 ], … … 185 249 my $wanted_schema = _get_wanted_schema( @args ) || $SCHEMA_VERSION; 186 250 251 die "No schema information for requested schema version $wanted_schema\n" 252 unless $create_sql->{$wanted_schema}; 253 187 254 # Check whether tables exist 188 255 my $sql = "SELECT tablename FROM pg_tables 189 256 WHERE tablename in (" 190 . join( ",", map { $dbh->quote($_) } keys %{$create_sql->{$ SCHEMA_VERSION}} ) . ")";257 . join( ",", map { $dbh->quote($_) } keys %{$create_sql->{$wanted_schema}} ) . ")"; 191 258 my $sth = $dbh->prepare($sql) or croak $dbh->errstr; 192 259 $sth->execute; 193 260 my %tables; 194 261 while ( my $table = $sth->fetchrow_array ) { 195 exists $create_sql->{$ SCHEMA_VERSION}->{$table} and $tables{$table} = 1;262 exists $create_sql->{$wanted_schema}->{$table} and $tables{$table} = 1; 196 263 } 197 264 … … 206 273 207 274 # Set up tables if not found 208 foreach my $required ( reverse sort keys %{$create_sql->{$ SCHEMA_VERSION}} ) {275 foreach my $required ( reverse sort keys %{$create_sql->{$wanted_schema}} ) { 209 276 if ( $tables{$required} ) { 210 277 print "Table $required already exists... skipping...\n"; 211 278 } else { 212 279 print "Creating table $required... done\n"; 213 foreach my $sql ( @{ $create_sql->{$ SCHEMA_VERSION}->{$required} } ) {280 foreach my $sql ( @{ $create_sql->{$wanted_schema}->{$required} } ) { 214 281 $dbh->do($sql) or croak $dbh->errstr; 215 282 } … … 329 396 return $args{wanted_schema}; 330 397 } 331 332 # Args passed as list of connection details.333 return $_[1];334 398 } 335 399 -
wiki-toolkit/trunk/lib/Wiki/Toolkit/Setup/SQLite.pm
r438 r440 16 16 17 17 my $create_sql = { 18 8 => { 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 (8) 25 | ], 26 node => [ qq| 27 CREATE TABLE node ( 28 id integer NOT NULL PRIMARY KEY AUTOINCREMENT, 29 name varchar(200) NOT NULL DEFAULT '', 30 version integer NOT NULL default 0, 31 text mediumtext NOT NULL default '', 32 modified datetime default NULL 33 ) 34 | ], 35 content => [ qq| 36 CREATE TABLE content ( 37 node_id integer NOT NULL, 38 version integer NOT NULL default 0, 39 text mediumtext NOT NULL default '', 40 modified datetime default NULL, 41 comment mediumtext NOT NULL default '', 42 PRIMARY KEY (node_id, version) 43 ) 44 | ], 45 internal_links => [ qq| 46 CREATE TABLE internal_links ( 47 link_from varchar(200) NOT NULL default '', 48 link_to varchar(200) NOT NULL default '', 49 PRIMARY KEY (link_from, link_to) 50 ) 51 | ], 52 metadata => [ qq| 53 CREATE TABLE metadata ( 54 node_id integer NOT NULL, 55 version integer NOT NULL default 0, 56 metadata_type varchar(200) NOT NULL DEFAULT '', 57 metadata_value mediumtext NOT NULL DEFAULT '' 58 ) 59 | ] 60 }, 18 61 9 => { 19 62 schema_info => [ qq| … … 112 155 my $wanted_schema = _get_wanted_schema( @args ) || $SCHEMA_VERSION; 113 156 157 die "No schema information for requested schema version $wanted_schema\n" 158 unless $create_sql->{$wanted_schema}; 159 114 160 # Check whether tables exist, set them up if not. 115 my %tables = fetch_tables_listing($dbh );161 my %tables = fetch_tables_listing($dbh, $wanted_schema); 116 162 117 163 # Do we need to upgrade the schema? … … 131 177 132 178 # Grab new list of tables 133 %tables = fetch_tables_listing($dbh );179 %tables = fetch_tables_listing($dbh, $wanted_schema); 134 180 } 135 181 136 182 # Set up tables if not found 137 foreach my $required ( keys %{$create_sql->{$ SCHEMA_VERSION}} ) {183 foreach my $required ( keys %{$create_sql->{$wanted_schema}} ) { 138 184 if ( $tables{$required} ) { 139 185 print "Table $required already exists... skipping...\n"; 140 186 } else { 141 187 print "Creating table $required... done\n"; 142 foreach my $sql (@{$create_sql->{$ SCHEMA_VERSION}->{$required}} ) {188 foreach my $sql (@{$create_sql->{$wanted_schema}->{$required}} ) { 143 189 $dbh->do($sql) or croak $dbh->errstr; 144 190 } … … 158 204 sub fetch_tables_listing { 159 205 my $dbh = shift; 206 my $wanted_schema = shift; 160 207 161 208 # Check whether tables exist, set them up if not. 162 209 my $sql = "SELECT name FROM sqlite_master 163 210 WHERE type='table' AND name in (" 164 . join( ",", map { $dbh->quote($_) } keys %{$create_sql->{$ SCHEMA_VERSION}} ) . ")";211 . join( ",", map { $dbh->quote($_) } keys %{$create_sql->{$wanted_schema}} ) . ")"; 165 212 my $sth = $dbh->prepare($sql) or croak $dbh->errstr; 166 213 $sth->execute; … … 245 292 return $args{wanted_schema}; 246 293 } 247 248 # Args passed as list of connection details.249 return $_[1];250 294 } 251 295 -
wiki-toolkit/trunk/lib/Wiki/Toolkit/TestLib.pm
r438 r440 70 70 dbhost => $config{dbhost}, 71 71 }, 72 dsn => $dsn 72 73 }; 73 74 }
