From: Luke Saunders Date: Tue, 23 Oct 2007 12:18:13 +0000 (+0000) Subject: fixed versioning test and checked db and schema versions are not equal before upgrading X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=1d48fcff9694c6801167356612181616004c1d4d;p=dbsrgits%2FDBIx-Class-Historic.git fixed versioning test and checked db and schema versions are not equal before upgrading --- diff --git a/Makefile.PL b/Makefile.PL index 67326b5..51a0096 100644 --- a/Makefile.PL +++ b/Makefile.PL @@ -22,7 +22,7 @@ requires 'Scope::Guard' => 0.03; # Perl 5.8.0 doesn't have utf8::is_utf8() requires 'Encode' => 0 if ($] <= 5.008000); -build_requires 'DBD::SQLite' => 1.13; +build_requires 'DBD::SQLite' => 1.14; build_requires 'Test::Builder' => 0.33; install_script 'script/dbicadmin'; diff --git a/lib/DBIx/Class/Schema/Versioned.pm b/lib/DBIx/Class/Schema/Versioned.pm index 7eeb110..da1afbc 100644 --- a/lib/DBIx/Class/Schema/Versioned.pm +++ b/lib/DBIx/Class/Schema/Versioned.pm @@ -148,12 +148,10 @@ sub upgrade my ($self) = @_; my $db_version = $self->get_db_version(); + my %driver_to_db_map = ( + 'mysql' => 'MySQL' + ); 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"; @@ -168,7 +166,7 @@ sub upgrade parser_args => { dbh => $self->storage->dbh } }); - $db_tr->producer($db); + $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); @@ -183,13 +181,13 @@ sub upgrade 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, + $db, $self->upgrade_directory, $self->schema_version, 'PRE', @@ -208,6 +206,11 @@ sub upgrade 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 { + if ($db_version eq $self->schema_version) { + print "Upgrade not necessary\n"; + return; + } + my $file = $self->ddl_filename( $self->storage->sqlt_type, $self->upgrade_directory, diff --git a/t/94versioning.t b/t/94versioning.t index b53fb59..ceae0e7 100644 --- a/t/94versioning.t +++ b/t/94versioning.t @@ -5,113 +5,55 @@ use Test::More; use File::Spec; use File::Copy; +#warn "$dsn $user $pass"; +my ($dsn, $user, $pass); + BEGIN { - eval "use DBD::SQLite; use SQL::Translator 0.08;"; + ($dsn, $user, $pass) = @ENV{map { "DBICTEST_MYSQL_${_}" } qw/DSN USER PASS/}; + + plan skip_all => 'Set $ENV{DBICTEST_MYSQL_DSN}, _USER and _PASS to run this test' + unless ($dsn && $user); + + + eval "use DBD::mysql; use SQL::Translator 0.08;"; plan $@ - ? ( skip_all => 'needs DBD::SQLite and SQL::Translator 0.08 for testing' ) - : ( tests => 6 ); + ? ( skip_all => 'needs DBD::mysql and SQL::Translator 0.08 for testing' ) + : ( tests => 9 ); } use lib qw(t/lib); use_ok('DBICVersionOrig'); -my $db_file = "t/var/versioning.db"; -unlink($db_file) if -e $db_file; -unlink($db_file . "-journal") if -e $db_file . "-journal"; -mkdir("t/var") unless -d "t/var"; -unlink('t/var/DBICVersion-Schema-1.0-SQLite.sql'); +my $schema_orig = DBICVersion::Schema->connect($dsn, $user, $pass); +eval { $schema_orig->storage->dbh->do('drop table SchemaVersions') }; -my $schema_orig = DBICVersion::Schema->connect( - "dbi:SQLite:$db_file", - undef, - undef, - { AutoCommit => 1 }, -); -# $schema->storage->ensure_connected(); +is($schema_orig->ddl_filename('MySQL', 't/var', '1.0'), File::Spec->catfile('t', 'var', 'DBICVersion-Schema-1.0-MySQL.sql'), 'Filename creation working'); +unlink('t/var/DBICVersion-Schema-1.0-MySQL.sql') if (-e 't/var/DBICVersion-Schema-1.0-MySQL.sql'); +$schema_orig->create_ddl_dir('MySQL', undef, 't/var'); -is($schema_orig->ddl_filename('SQLite', 't/var', '1.0'), File::Spec->catfile('t', 'var', 'DBICVersion-Schema-1.0-SQLite.sql'), 'Filename creation working'); -$schema_orig->create_ddl_dir('SQLite', undef, 't/var'); +ok(-f 't/var/DBICVersion-Schema-1.0-MySQL.sql', 'Created DDL file'); +$schema_orig->deploy({ add_drop_table => 1 }); +$schema_orig->upgrade(); -ok(-f 't/var/DBICVersion-Schema-1.0-SQLite.sql', 'Created DDL file'); -## do this here or let Versioned.pm do it? -# $schema->deploy(); - -my $tvrs = $schema_orig->resultset('Table'); +my $tvrs = $schema_orig->{vschema}->resultset('Table'); is($schema_orig->_source_exists($tvrs), 1, 'Created schema from DDL file'); eval "use DBICVersionNew"; -my $schema_new = DBICVersion::Schema->connect( - "dbi:SQLite:$db_file", - undef, - undef, - { AutoCommit => 1 }, -); - -## create new to pick up filedata for upgrade files we just made (on_connect) - -# { -# 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'); + unlink('t/var/DBICVersion-Schema-2.0-MySQL.sql'); + unlink('t/var/DBICVersion-Schema-1.0-2.0-MySQL.sql'); - my $schema_upgrade = DBICVersion::Schema->connect( - "dbi:SQLite:$db_file", - undef, - undef, - { AutoCommit => 1 }, - ); - - + my $schema_upgrade = DBICVersion::Schema->connect($dsn, $user, $pass); is($schema_upgrade->get_db_version(), '1.0', 'get_db_version ok'); + is($schema_upgrade->schema_version, '2.0', 'schema version ok'); + $schema_upgrade->create_ddl_dir('MySQL', '2.0', 't/var', '1.0'); + ok(-f 't/var/DBICVersion-Schema-1.0-2.0-MySQL.sql', 'Created DDL file'); + $schema_upgrade->upgrade(); + is($schema_upgrade->get_db_version(), '2.0', 'db version number upgraded'); 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'); -unlink('t/var/DBICVersion-Schema-2.0-SQLite.sql'); -unlink('t/var/DBICVersion-Schema-1.0-2.0-SQLite.sql'); -unlink(); diff --git a/t/lib/DBICVersionNew.pm b/t/lib/DBICVersionNew.pm index 5efacd7..2f6595c 100644 --- a/t/lib/DBICVersionNew.pm +++ b/t/lib/DBICVersionNew.pm @@ -24,7 +24,7 @@ __PACKAGE__->add_columns 'is_nullable' => 0, 'size' => '10' }, - 'NewsVersionName' => { + 'NewVersionName' => { 'data_type' => 'VARCHAR', 'is_auto_increment' => 0, 'default_value' => undef,