X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FDBIx%2FClass%2FSchema%2FVersioned.pm;h=4e7b944de29ded6ef694eb85633d580dc4b19622;hb=4c633384a2afd15b77f6c5d46b9c7831feaa1272;hp=2021cf750d7605184b50a5977a8162f4ce91fb77;hpb=67e7b822a0f4105d8127538ae4f8cf4212df46f4;p=dbsrgits%2FDBIx-Class.git diff --git a/lib/DBIx/Class/Schema/Versioned.pm b/lib/DBIx/Class/Schema/Versioned.pm index 2021cf7..4e7b944 100644 --- a/lib/DBIx/Class/Schema/Versioned.pm +++ b/lib/DBIx/Class/Schema/Versioned.pm @@ -96,11 +96,6 @@ this will attempt to upgrade the database from its current version to the curren schema version using a diff from your I. If a suitable diff is not found then no upgrade is possible. -NB: At the moment, only SQLite and MySQL are supported. This is due to -spotty behaviour in the SQL::Translator producers, please help us by -enhancing them. Ask on the mailing list or IRC channel for details (community details -in L). - =head1 GETTING STARTED Firstly you need to setup your schema class as per the L, make sure @@ -182,6 +177,8 @@ use base 'DBIx::Class::Schema'; use Carp::Clan qw/^DBIx::Class/; use Time::HiRes qw/gettimeofday/; +use Try::Tiny; +use namespace::clean; __PACKAGE__->mk_classdata('_filedata'); __PACKAGE__->mk_classdata('upgrade_directory'); @@ -225,7 +222,7 @@ sub install # must be called on a fresh database if ($self->get_db_version()) { - carp 'Install not possible as versions table already exists in database'; + $self->throw_exception("A versioned schema has already been deployed, try upgrade instead.\n"); } # default to current version if none passed @@ -503,7 +500,7 @@ sub get_db_version my ($self, $rs) = @_; my $vtable = $self->{vschema}->resultset('Table'); - my $version = eval { + my $version = try { $vtable->search({}, { order_by => { -desc => 'installed' }, rows => 1 } ) ->get_column ('version') ->next; @@ -566,23 +563,22 @@ sub _on_connect { my ($self) = @_; - my $info = $self->storage->connect_info; - my $args = $info->[-1]; + my $conn_info = $self->storage->connect_info; + $self->{vschema} = DBIx::Class::Version->connect(@$conn_info); + my $conn_attrs = $self->{vschema}->storage->_dbic_connect_attributes || {}; - $self->{vschema} = DBIx::Class::Version->connect(@$info); my $vtable = $self->{vschema}->resultset('Table'); # useful when connecting from scripts etc - return if ($args->{ignore_version} || ($ENV{DBIC_NO_VERSION_CHECK} && !exists $args->{ignore_version})); + return if ($conn_attrs->{ignore_version} || ($ENV{DBIC_NO_VERSION_CHECK} && !exists $conn_attrs->{ignore_version})); # check for legacy versions table and move to new if exists - my $vschema_compat = DBIx::Class::VersionCompat->connect(@$info); unless ($self->_source_exists($vtable)) { - my $vtable_compat = $vschema_compat->resultset('TableCompat'); + my $vtable_compat = DBIx::Class::VersionCompat->connect(@$conn_info)->resultset('TableCompat'); if ($self->_source_exists($vtable_compat)) { $self->{vschema}->deploy; map { $vtable->create({ installed => $_->Installed, version => $_->Version }) } $vtable_compat->all; - $self->storage->dbh->do("DROP TABLE " . $vtable_compat->result_source->from); + $self->storage->_get_dbh->do("DROP TABLE " . $vtable_compat->result_source->from); } } @@ -682,13 +678,13 @@ sub _set_db_version { # This is necessary since there are legitimate cases when upgrades can happen # back to back within the same second. This breaks things since we relay on the # ability to sort by the 'installed' value. The logical choice of an autoinc - # is not possible, as it will break multiple legacy installations. Also it is + # is not possible, as it will break multiple legacy installations. Also it is # not possible to format the string sanely, as the column is a varchar(20). # The 'v' character is added to the front of the string, so that any version # formatted by this new function will sort _after_ any existing 200... strings. my @tm = gettimeofday(); my @dt = gmtime ($tm[0]); - my $o = $vtable->create({ + my $o = $vtable->create({ version => $version, installed => sprintf("v%04d%02d%02d_%02d%02d%02d.%03.0f", $dt[5] + 1900, @@ -724,16 +720,12 @@ sub _source_exists { my ($self, $rs) = @_; - my $c; - my $exception; - try { - $c = $rs->search({ 1, 0 })->count; + return try { + $rs->search(\'1=0')->cursor->next; + 1; } catch { - $exception=1; + 0; }; - return 0 if $exception || !defined $c; - - return 1; } 1;