From: Luke Saunders Date: Wed, 15 Aug 2007 22:29:57 +0000 (+0000) Subject: tests are a mess, but Versioned.pm should work now X-Git-Tag: v0.08240~545^2~31 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=f925f7cb11f47c9706c003fda8035be2956087fc;p=dbsrgits%2FDBIx-Class.git tests are a mess, but Versioned.pm should work now --- diff --git a/lib/DBIx/Class/Schema/Versioned.pm b/lib/DBIx/Class/Schema/Versioned.pm index 755d881..aa76fff 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) = @_; @@ -131,7 +132,7 @@ sub _on_connect my $fh; open $fh, "<$file" or warn("Can't open upgrade file, $file ($!)"); - my @data = split(/;\n/, join('', <$fh>)); + my @data = split(/[;\n]/, join('', <$fh>)); close($fh); @data = grep { $_ && $_ !~ /^-- / } @data; @data = grep { $_ !~ /^(BEGIN TRANACTION|COMMIT)/m } @data; @@ -156,7 +157,7 @@ sub get_db_version ], as => ['maxinstall'], })->first; - $pversion = $vtable->search({ Installed => $psearch->get_column('maxinstall'), + my $pversion = $vtable->search({ Installed => $psearch->get_column('maxinstall'), })->first; $pversion = $pversion->Version if($pversion); return $pversion; @@ -185,13 +186,17 @@ sub upgrade { my ($self) = @_; - $self->backup(); - $self->do_upgrade(); + $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 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');