From: Matt S Trout Date: Fri, 14 Sep 2007 20:56:27 +0000 (+0000) Subject: Merge 'trunk' into 'versioned_enhancements' X-Git-Tag: v0.08240~545^2~27 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=14ad95291d2a5fe439fdb19058f05a685bd0331e;hp=7a2c1380cfeb48e9a4fc2b5cfa422584035afb1e;p=dbsrgits%2FDBIx-Class.git Merge 'trunk' into 'versioned_enhancements' r11158@jules (orig r3684): jnapiorkowski | 2007-08-16 16:23:18 +0100 -- added some documentation to ->populate to warn people about the side effects of using wantarray versus void context. -- added some additional documentation to resultset->create r11194@jules (orig r3695): matthewt | 2007-08-21 19:24:53 +0100 r10460@jules (orig r3640): tomboh | 2007-08-01 12:27:38 +0100 Add an 'on_disconnect_do' argument to DBIx::Class::Storage::DBI::connect_info that, on disconnection, do what 'on_connect_do' does on connection. Currently, this only works if the code explicitly calls disconnect() on the Storage object. While I'm here, make both 'on_connect_do' and 'on_disconnect_do' accept code references as well as strings containing SQL statements. Finally, remove code to call compose_connection() from DBICTest.pm that never gets called any more. r11195@jules (orig r3696): matthewt | 2007-08-21 19:24:57 +0100 r11152@jules (orig r3678): tomboh | 2007-08-15 09:24:32 +0100 Restore code that I removed in revision 3640 that tests still need r11196@jules (orig r3697): matthewt | 2007-08-21 19:25:00 +0100 r11153@jules (orig r3679): tomboh | 2007-08-15 09:25:47 +0100 Let on_connect_do() and on_disconnect_do() take code references. Provide tests and documentation for this. r11197@jules (orig r3698): matthewt | 2007-08-21 19:25:33 +0100 r11199@jules (orig r3700): matthewt | 2007-08-21 19:50:00 +0100 arrayrefs for on_connect_do r11200@jules (orig r3701): matthewt | 2007-08-21 19:53:44 +0100 note on_connect_do changes r11201@jules (orig r3702): matthewt | 2007-08-21 20:17:22 +0100 oracle datetime inflator patch r11202@jules (orig r3703): matthewt | 2007-08-21 20:52:09 +0100 force CDBICompat deps for developers, fix tests to work with latest Class::Trigger r11217@jules (orig r3705): tomboh | 2007-08-22 11:28:58 +0100 Fix the behaviour of code refs within array refs for on_(dis)?connect_do and enhance tests to spot the previous mistake. r11218@jules (orig r3706): tomboh | 2007-08-22 11:57:14 +0100 Add myself to the list of contributors r11232@jules (orig r3709): gphat | 2007-08-24 18:11:46 +0100 Fix typos. r14025@jules (orig r3720): ilmari | 2007-09-04 17:44:41 +0100 Fix return value for DBIC::ResultSource::Table->table($table) r14026@jules (orig r3721): ash | 2007-09-04 20:14:02 +0100 Bump for New relase r14027@jules (orig r3722): semifor | 2007-09-05 04:49:46 +0100 r1185@titanic: mjm | 2007-09-04 20:11:07 -0700 Updated email address r14029@jules (orig r3723): castaway | 2007-09-06 21:07:52 +0100 Doc patch from wreis r14030@jules (orig r3724): castaway | 2007-09-06 22:35:49 +0100 Add more about accessors to doc r14031@jules (orig r3725): tomboh | 2007-09-07 17:54:00 +0100 Small POD fix r14032@jules (orig r3726): semifor | 2007-09-08 17:58:56 +0100 r1191@titanic: mjm | 2007-09-08 09:20:32 -0700 discard_changes is also "refresh from storage" r14035@jules (orig r3729): castaway | 2007-09-12 00:23:20 +0100 Much doch shuffling and more argument explanations (due to initself ;) --- diff --git a/lib/DBIx/Class/Schema/Versioned.pm b/lib/DBIx/Class/Schema/Versioned.pm index f5ea037..7eeb110 100644 --- a/lib/DBIx/Class/Schema/Versioned.pm +++ b/lib/DBIx/Class/Schema/Versioned.pm @@ -48,6 +48,7 @@ use Data::Dumper; __PACKAGE__->mk_classdata('_filedata'); __PACKAGE__->mk_classdata('upgrade_directory'); __PACKAGE__->mk_classdata('backup_directory'); +__PACKAGE__->mk_classdata('do_backup'); sub schema_version { my ($self) = @_; @@ -70,46 +71,19 @@ sub connection { sub _on_connect { my ($self) = @_; - my $vschema = DBIx::Class::Version->connect(@{$self->storage->connect_info()}); - my $vtable = $vschema->resultset('Table'); - my $pversion; + $self->{vschema} = DBIx::Class::Version->connect(@{$self->storage->connect_info()}); + + my $pversion = $self->get_db_version(); - if(!$self->_source_exists($vtable)) - { -# $vschema->storage->debug(1); - $vschema->storage->ensure_connected(); - $vschema->deploy(); - $pversion = 0; - } - else - { - my $psearch = $vtable->search(undef, - { select => [ - { 'max' => 'Installed' }, - ], - as => ['maxinstall'], - })->first; - $pversion = $vtable->search({ Installed => $psearch->get_column('maxinstall'), - })->first; - $pversion = $pversion->Version if($pversion); - } -# warn("Previous version: $pversion\n"); if($pversion eq $self->schema_version) { warn "This version is already installed\n"; return 1; } -## use IC::DT? - if(!$pversion) { - $vtable->create({ Version => $self->schema_version, - Installed => strftime("%Y-%m-%d %H:%M:%S", gmtime()) - }); - ## If we let the user do this, where does the Version table get updated? - warn "No previous version found, calling deploy to install this version.\n"; - $self->deploy(); + warn "Your DB is currently unversioned. Please call upgrade on your schema to sync the DB.\n"; return 1; } @@ -124,33 +98,30 @@ sub _on_connect return 1; } - $file = $self->ddl_filename( - $self->storage->sqlt_type, - $self->upgrade_directory, - $self->schema_version, - $pversion, - ); -# $file =~ s/@{[ $self->schema_version ]}/"${pversion}-" . $self->schema_version/e; - if(!-f $file) - { - warn "Upgrade not possible, no upgrade file found ($file)\n"; - return; - } - - my $fh; - open $fh, "<$file" or warn("Can't open upgrade file, $file ($!)"); - my @data = split(/;\n/, join('', <$fh>)); - close($fh); - @data = grep { $_ && $_ !~ /^-- / } @data; - @data = grep { $_ !~ /^(BEGIN TRANACTION|COMMIT)/m } @data; - - $self->_filedata(\@data); ## Don't do this yet, do only on command? ## If we do this later, where does the Version table get updated?? warn "Versions out of sync. This is " . $self->schema_version . ", your database contains version $pversion, please call upgrade on your Schema.\n"; -# $self->upgrade($pversion, $self->schema_version); +} + +sub get_db_version +{ + my ($self, $rs) = @_; + + my $vtable = $self->{vschema}->resultset('Table'); + return 0 unless ($self->_source_exists($vtable)); + + my $psearch = $vtable->search(undef, + { select => [ + { 'max' => 'Installed' }, + ], + as => ['maxinstall'], + })->first; + my $pversion = $vtable->search({ Installed => $psearch->get_column('maxinstall'), + })->first; + $pversion = $pversion->Version if($pversion); + return $pversion; } sub _source_exists @@ -175,26 +146,115 @@ sub backup sub upgrade { my ($self) = @_; + my $db_version = $self->get_db_version(); - ## overridable sub, per default just run all the commands. + if (!$db_version) { + my %driver_to_db_map = ( + 'mysql' => 'MySQL', + 'Pg' => 'PostgreSQL', + 'Oracle' => 'Oracle' + ); + my $db = $driver_to_db_map{$self->storage->dbh->{Driver}->{Name}}; + unless ($db) { + print "Sorry, this is an unsupported DB\n"; + return; + } + + require SQL::Translator; + require SQL::Translator::Diff; + my $db_tr = SQL::Translator->new({ + add_drop_table => 1, + parser => 'DBI', + parser_args => { dbh => $self->storage->dbh } + }); + + $db_tr->producer($db); + my $dbic_tr = SQL::Translator->new; + $dbic_tr->parser('SQL::Translator::Parser::DBIx::Class'); + $dbic_tr = $self->storage->configure_sqlt($dbic_tr, $db); + $dbic_tr->data($self); + $dbic_tr->producer($db); + + $db_tr->schema->name('db_schema'); + $dbic_tr->schema->name('dbic_schema'); + + # is this really necessary? + foreach my $tr ($db_tr, $dbic_tr) { + my $data = $tr->data; + $tr->parser->($tr, $$data); + } + + my $diff = SQL::Translator::Diff::schema_diff($db_tr->schema, $db, + $dbic_tr->schema, $db, + { ignore_constraint_names => 1, ignore_index_names => 1, caseopt => 1 }); + + my $filename = $self->ddl_filename( + $self->storage->sqlt_type, + $self->upgrade_directory, + $self->schema_version, + 'PRE', + ); + my $file; + if(!open($file, ">$filename")) + { + $self->throw_exception("Can't open $filename for writing ($!)"); + next; + } + print $file $diff; + close($file); + + # create versions table + $self->{vschema}->deploy; + + print "WARNING: There may be differences between your DB and your DBIC schema. Please review and if necessary run the SQL in $filename to sync your DB.\n"; + } else { + my $file = $self->ddl_filename( + $self->storage->sqlt_type, + $self->upgrade_directory, + $self->schema_version, + $db_version, + ); - $self->backup(); + if(!-f $file) + { + warn "Upgrade not possible, no upgrade file found ($file)\n"; + return; + } + + my $fh; + open $fh, "<$file" or warn("Can't open upgrade file, $file ($!)"); + my @data = split(/[;\n]/, join('', <$fh>)); + close($fh); + @data = grep { $_ && $_ !~ /^-- / } @data; + @data = grep { $_ !~ /^(BEGIN TRANACTION|COMMIT)/m } @data; + + $self->_filedata(\@data); + $self->backup() if($self->do_backup); + + $self->txn_do(sub { + $self->do_upgrade(); + }); + } + + my $vtable = $self->{vschema}->resultset('Table'); + $vtable->create({ Version => $self->schema_version, + Installed => strftime("%Y-%m-%d %H:%M:%S", gmtime()) + }); + +} +sub do_upgrade +{ + my ($self) = @_; + + ## overridable sub, per default just run all the commands. $self->run_upgrade(qr/create/i); $self->run_upgrade(qr/alter table .*? add/i); $self->run_upgrade(qr/alter table .*? (?!drop)/i); $self->run_upgrade(qr/alter table .*? drop/i); $self->run_upgrade(qr/drop/i); -# $self->run_upgrade(qr//i); - - my $vschema = DBIx::Class::Version->connect(@{$self->storage->connect_info()}); - my $vtable = $vschema->resultset('Table'); - $vtable->create({ Version => $self->schema_version, - Installed => strftime("%Y-%m-%d %H:%M:%S", gmtime()) - }); } - sub run_upgrade { my ($self, $stm) = @_; @@ -285,13 +345,15 @@ allow you to make a backup of the database. Per default this method attempts to call C<< $self->storage->backup >>, to run the standard backup on each database type. -This method should return the name of the backup file, if appropriate. - -C is called from C, make sure you call it, if you write your -own method. +This method should return the name of the backup file, if appropriate.. =head2 upgrade +This is the main upgrade method which calls the overridable do_upgrade and +also handles the backups and updating of the SchemaVersion table. + +=head2 do_upgrade + This is an overwritable method used to run your upgrade. The freeform method allows you to run your upgrade any way you please, you can call C any number of times to run the actual SQL commands, and in between you can diff --git a/lib/DBIx/Class/Storage/DBI.pm b/lib/DBIx/Class/Storage/DBI.pm index c0b17ad..dbddab3 100644 --- a/lib/DBIx/Class/Storage/DBI.pm +++ b/lib/DBIx/Class/Storage/DBI.pm @@ -1331,23 +1331,22 @@ sub create_ddl_dir if(-e $filename) { warn("$filename already exists, skipping $db"); - next; - } - - my $output = $sqlt->translate; - if(!$output) - { - warn("Failed to translate to $db, skipping. (" . $sqlt->error . ")"); - next; - } - if(!open($file, ">$filename")) - { - $self->throw_exception("Can't open $filename for writing ($!)"); + next unless ($preversion); + } else { + my $output = $sqlt->translate; + if(!$output) + { + warn("Failed to translate to $db, skipping. (" . $sqlt->error . ")"); next; - } - print $file $output; - close($file); - + } + if(!open($file, ">$filename")) + { + $self->throw_exception("Can't open $filename for writing ($!)"); + next; + } + print $file $output; + close($file); + } if($preversion) { require SQL::Translator::Diff; diff --git a/lib/SQL/Translator/Parser/DBIx/Class.pm b/lib/SQL/Translator/Parser/DBIx/Class.pm index e3f0860..7f7d66e 100644 --- a/lib/SQL/Translator/Parser/DBIx/Class.pm +++ b/lib/SQL/Translator/Parser/DBIx/Class.pm @@ -158,15 +158,24 @@ sub parse { ) { $created_FK_rels{$rel_table}->{$key_test} = 1; - $table->add_constraint( - type => 'foreign_key', - name => "fk_$keys[0]", - fields => \@keys, - reference_fields => \@refkeys, - reference_table => $rel_table, - on_delete => $on_delete, - on_update => $on_update - ); + if (scalar(@keys)) { + $table->add_constraint( + type => 'foreign_key', + name => "fk_$keys[0]", + fields => \@keys, + reference_fields => \@refkeys, + reference_table => $rel_table, + on_delete => $on_delete, + on_update => $on_update + ); + + my $index = $table->add_index( + name => $table->name . "_fk_$keys[0]", + fields => \@keys, + type => 'NORMAL', + ); + } + } } } diff --git a/t/94versioning.t b/t/94versioning.t index 7de9edd..b53fb59 100644 --- a/t/94versioning.t +++ b/t/94versioning.t @@ -3,6 +3,7 @@ use strict; use warnings; use Test::More; use File::Spec; +use File::Copy; BEGIN { eval "use DBD::SQLite; use SQL::Translator 0.08;"; @@ -47,24 +48,67 @@ my $schema_new = DBICVersion::Schema->connect( { AutoCommit => 1 }, ); -unlink('t/var/DBICVersion-Schema-2.0-SQLite.sql'); -unlink('t/var/DBICVersion-Schema-1.0-2.0-SQLite.sql'); -$schema_new->create_ddl_dir('SQLite', undef, 't/var', '1.0'); -ok(-f 't/var/DBICVersion-Schema-1.0-2.0-SQLite.sql', 'Created DDL upgrade file'); - ## create new to pick up filedata for upgrade files we just made (on_connect) -my $schema_upgrade = DBICVersion::Schema->connect( - "dbi:SQLite:$db_file", - undef, - undef, - { AutoCommit => 1 }, -); -## do this here or let Versioned.pm do it? -$schema_upgrade->upgrade(); -$tvrs = $schema_upgrade->resultset('Table'); -is($schema_upgrade->_source_exists($tvrs), 1, 'Upgraded schema from DDL file'); +# { +# unlink('t/var/DBICVersion-Schema-1.0-2.0-SQLite.sql'); +# copy('t/var/DBICVersion-Schema-1.0-2.0-SQLite-erroneous.sql', 't/var/DBICVersion-Schema-1.0-2.0-SQLite.sql'); + +# my $schema_upgrade = DBICVersion::Schema->connect( +# "dbi:SQLite:$db_file", +# undef, +# undef, +# { AutoCommit => 1 }, +# ); + + +# is($schema_upgrade->get_db_version(), '1.0', 'get_db_version ok'); + +# eval { +# # this will die with errors +# $schema_upgrade->upgrade(); +# }; +# isnt($@, '', 'dodgy upgrade dies'); + +# eval { +# my @results = $schema_upgrade->storage->dbh->do('select VersionName from TestVersion'); +# }; +# is($@, '', 'partial upgrade properly rolledback'); +# is($schema_upgrade->get_db_version(), '1.0', 'db version number not upgraded'); +# } + +{ + unlink('t/var/DBICVersion-Schema-2.0-SQLite.sql'); + unlink('t/var/DBICVersion-Schema-1.0-2.0-SQLite.sql'); + +# $schema_new->create_ddl_dir('SQLite', undef, 't/var', '1.0'); +# ok(-f 't/var/DBICVersion-Schema-1.0-2.0-SQLite.sql', 'Created DDL upgrade file'); + + copy('t/var/DBICVersion-Schema-1.0-2.0-SQLite-proper.sql', 't/var/DBICVersion-Schema-1.0-2.0-SQLite.sql'); + + my $schema_upgrade = DBICVersion::Schema->connect( + "dbi:SQLite:$db_file", + undef, + undef, + { AutoCommit => 1 }, + ); + + + is($schema_upgrade->get_db_version(), '1.0', 'get_db_version ok'); + + eval { + # this should be okay + $schema_upgrade->upgrade(); + }; + is($@, '', 'proper upgrade okay'); + eval { + $schema_upgrade->storage->dbh->do('select NewVersionName from TestVersion'); + }; + is($@, '', 'new column created'); + is($schema_upgrade->get_db_version(), '2.0', 'db version number successfully upgraded'); +} +exit; unlink($db_file) if -e $db_file; unlink($db_file . "-journal") if -e $db_file . "-journal"; unlink('t/var/DBICVersion-Schema-1.0-SQLite.sql'); diff --git a/t/lib/DBICVersionNew.pm b/t/lib/DBICVersionNew.pm index f92c3a5..5efacd7 100644 --- a/t/lib/DBICVersionNew.pm +++ b/t/lib/DBICVersionNew.pm @@ -21,9 +21,17 @@ __PACKAGE__->add_columns 'is_auto_increment' => 0, 'default_value' => undef, 'is_foreign_key' => 0, + 'is_nullable' => 0, + 'size' => '10' + }, + 'NewsVersionName' => { + 'data_type' => 'VARCHAR', + 'is_auto_increment' => 0, + 'default_value' => undef, + 'is_foreign_key' => 0, 'is_nullable' => 1, 'size' => '20' - }, + } ); __PACKAGE__->set_primary_key('Version');