From: Gordon Irving Date: Wed, 2 Dec 2009 22:52:40 +0000 (+0000) Subject: all ddl tests now pass X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=912e2d5a8dec3b433c42ef97e666008a4e128322;p=dbsrgits%2FDBIx-Class-Historic.git all ddl tests now pass --- diff --git a/lib/DBIx/Class/Admin.pm b/lib/DBIx/Class/Admin.pm index fcb2618..9e2f90d 100644 --- a/lib/DBIx/Class/Admin.pm +++ b/lib/DBIx/Class/Admin.pm @@ -87,7 +87,6 @@ sub _build_schema { $self->ensure_class_loaded($self->schema_class); $self->connect_info->[3]->{ignore_version} =1; - #warn Dumper ($self->connect_info(), $self->connect_info->[3], {ignore_version => 1 }); return $self->schema_class->connect(@{$self->connect_info()} ); # , $self->connect_info->[3], { ignore_version => 1} ); } @@ -143,7 +142,7 @@ has 'sql_type' => ( ); has version => ( - is => 'ro', + is => 'rw', isa => 'Str', ); @@ -153,13 +152,22 @@ has preversion => ( predicate => 'has_preversion', ); +has force => ( + is => 'rw', + isa => 'Bool', +); + +has '_confirm' => ( + is => 'ro', + isa => 'Bool', +); + sub create { my ($self, $sqlt_type, $sqlt_args) = @_; if ($self->has_preversion) { print "attempting to create diff file for ".$self->preversion."\n"; } my $schema = $self->schema(); -# warn "running with params sqlt_type = $sqlt_type, version = " .$schema->schema_version . " sql_dir = " . $self->sql_dir . " preversion = " . ($self->has_preversion ? $self->preversion : "" ). "\n"; # create the dir if does not exist $self->sql_dir->mkpath() if ( ! -d $self->sql_dir); @@ -171,22 +179,33 @@ sub upgrade { my $schema = $self->schema(); if (!$schema->get_db_version()) { # schema is unversioned - warn "could not determin current schema version, please either install or deploy"; + die "could not determin current schema version, please either install or deploy"; } else { $schema->upgrade(); } } sub install { - my ($self) = @_; + my ($self, $version) = @_; my $schema = $self->schema(); - if (!$schema->get_db_version()) { + $version ||= $self->version(); + if (!$schema->get_db_version() ) { # schema is unversioned - print "Going to install schema version"; - $schema->install($self->version); - } else { - warn "schema already has a version not installing, try upgrade instead"; + print "Going to install schema version\n"; + my $ret = $schema->install($version); + print "retun is $ret\n"; + } + elsif ($schema->get_db_version() and $self->force ) { + warn "forcing install may not be a good idea"; + if($self->confirm() ) { + # FIXME private api + warn $version; + $self->schema->_set_db_version({ version => $version}); + } + } + else { + die "schema already has a version not installing, try upgrade instead"; } } @@ -196,13 +215,10 @@ sub deploy { my $schema = $self->schema(); if (!$schema->get_db_version() ) { # schema is unversioned -# warn "going to deploy"; -# warn Dumper $schema->deployment_statements(); - $schema->deploy( $args, $self->sql_dir) or die "could not deploy schema"; } else { - warn "there already is a database with a version here, try upgrade instead"; + die "there already is a database with a version here, try upgrade instead"; } } @@ -303,8 +319,11 @@ sub output_data { } sub confirm { - print "Are you sure you want to do this? (type YES to confirm) "; - my $response = ; + my ($self) = @_; + print "Are you sure you want to do this? (type YES to confirm) "; + # mainly here for testing + return 1 if ($self->_confirm()); + my $response = ; return 1 if ($response=~/^YES/); return; } diff --git a/t/admin/02ddl.t b/t/admin/02ddl.t index 9c280e5..3a5bb9c 100644 --- a/t/admin/02ddl.t +++ b/t/admin/02ddl.t @@ -34,7 +34,7 @@ use ok 'DBIx::Class::Admin'; use DBICTest; -my $sql_dir = dir($Bin,"var","sql"); +my $sql_dir = dir($Bin,"..","var"); { # create the schema @@ -87,19 +87,52 @@ load 'DBICVersionNew'; $admin = DBIx::Class::Admin->new( schema_class => 'DBICVersion::Schema', - sql_dir => $sql_dir, + sql_dir => "t/var", connect_info => $schema->storage->connect_info(), ); -lives_ok { $admin->create($schema->storage->sqlt_type(), {add_drop_table=>0}); } 'Can create DBICVersionOrig sql in ' . $schema->storage->sqlt_type; +$admin->preversion("1.0"); +lives_ok { $admin->create($schema->storage->sqlt_type(), ); } 'Can create diff for ' . $schema->storage->sqlt_type; lives_ok {$admin->upgrade();} 'upgrade the schema'; -is($schema->get_db_version, $DBICVersion::Schema::VERSION, 'Schema deployed and versions match'); +is($schema->get_db_version, $DBICVersion::Schema::VERSION, 'Schema and db versions match'); + +} + +{ # install + +clean_dir($sql_dir); + +my $schema = DBICTest->init_schema( + no_deploy=>1, + no_populate=>1, + sqlite_use_file => 1, + ); +my $admin = DBIx::Class::Admin->new( + schema_class => 'DBICVersion::Schema', + sql_dir => $sql_dir, + connect_info => $schema->storage->connect_info(), + _confirm => 1, +); + +$admin->version("3.0"); +lives_ok { $admin->install(); } 'install schema version 3.0'; +is($admin->schema->get_db_version, "3.0", 'db thinks its version 3.0'); +dies_ok { $admin->install("4.0"); } 'cannot install to allready existing version'; +sleep 1; +$admin->force(1); +lives_ok { $admin->install("4.0"); } 'can force install to allready existing version'; +is($admin->schema->get_db_version, "4.0", 'db thinks its version 4.0'); +#clean_dir($sql_dir); } sub clean_dir { my ($dir) =@_; + $dir = $dir->resolve; + if ( ! -d $dir ) { + $dir->mkpath(); + } foreach my $file ($dir->children) { # skip any hidden files next if ($file =~ /^\./);