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=3984202823d6d043a226ebb5b63ad76d02ae28fa;hpb=56988b6c1e5a61ee6f9da6917e3ee581f117840f;p=dbsrgits%2FDBIx-Class.git diff --git a/lib/DBIx/Class/Schema/Versioned.pm b/lib/DBIx/Class/Schema/Versioned.pm index 3984202..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 @@ -150,13 +145,13 @@ The script above assumes that if the database is unversioned then it is empty and we can safely deploy the DDL to it. However things are not always so simple. if you want to initialise a pre-existing database where the DDL is not the same -as the DDL for your current schema version then you will need a diff which +as the DDL for your current schema version then you will need a diff which converts the database's DDL to the current DDL. The best way to do this is to get a dump of the database schema (without data) and save that in your SQL directory as version 0.000 (the filename must be as with -L) then create a diff using your create DDL +L) then create a diff using your create DDL script given above from version 0.000 to the current version. Then hand check -and if necessary edit the resulting diff to ensure that it will apply. Once you have +and if necessary edit the resulting diff to ensure that it will apply. Once you have done all that you can do this: if (!$schema->get_db_version()) { @@ -168,7 +163,7 @@ done all that you can do this: $schema->upgrade(); In the case of an unversioned database the above code will create the -dbix_class_schema_versions table and write version 0.000 to it, then +dbix_class_schema_versions table and write version 0.000 to it, then upgrade will then apply the diff we talked about creating in the previous paragraph and then you're good to go. @@ -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 @@ -258,7 +255,7 @@ sub deploy { =back -Virtual method that should be overriden to create an upgrade file. +Virtual method that should be overridden to create an upgrade file. This is useful in the case of upgrading across multiple versions to concatenate several files to create one upgrade file. @@ -279,7 +276,7 @@ sub create_upgrade_path { =back -Virtual method that should be overriden to return an ordered list +Virtual method that should be overridden to return an ordered list of schema versions. This is then used to produce a set of steps to upgrade through to achieve the required schema version. @@ -303,7 +300,7 @@ list of schema versions (if ordered_schema_versions returns nothing then it is assumed you can do the upgrade as a single step). It then iterates through the list of versions between the current db version and the schema version applying one update at a time until -all relvant updates are applied. +all relevant updates are applied. The individual update steps are performed by using L, which will apply the update and also @@ -399,7 +396,7 @@ sub upgrade_single_step } # strangely the first time this is called can - # differ to subsequent times. so we call it + # differ to subsequent times. so we call it # here to be sure. # XXX - just fix it $self->storage->sqlt_type; @@ -435,7 +432,7 @@ 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 sandwich your data upgrading. For example, first run all the B -commands, then migrate your data from old to new tables/formats, then +commands, then migrate your data from old to new tables/formats, then issue the DROP commands when you are finished. Will run the whole file as it is by default. =cut @@ -469,7 +466,7 @@ sub run_upgrade $self->_filedata([ grep { $_ !~ /$stm/i } @{$self->_filedata} ]); for (@statements) - { + { $self->storage->debugobj->query_start($_) if $self->storage->debug; $self->apply_statement($_); $self->storage->debugobj->query_end($_) if $self->storage->debug; @@ -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; @@ -544,7 +541,7 @@ warns if they are not the same or if the DB is unversioned. It also provides compatibility between the old versions table (SchemaVersions) and the new one (dbix_class_schema_versions). -To avoid the checks on connect, set the env var DBIC_NO_VERSION_CHECK or alternatively you can set the ignore_version attr in the forth argument like so: +To avoid the checks on connect, set the environment var DBIC_NO_VERSION_CHECK or alternatively you can set the ignore_version attr in the forth argument like so: my $schema = MyApp::Schema->connect( $dsn, @@ -558,30 +555,30 @@ To avoid the checks on connect, set the env var DBIC_NO_VERSION_CHECK or alterna sub connection { my $self = shift; $self->next::method(@_); - $self->_on_connect($_[3]); + $self->_on_connect(); return $self; } sub _on_connect { - my ($self, $args) = @_; + my ($self) = @_; - $args = {} unless $args; + 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(@{$self->storage->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(@{$self->storage->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); } } @@ -617,8 +614,9 @@ sub _create_db_to_schema_diff { return; } - $self->throw_exception($self->storage->_sqlt_version_error) - if (not $self->storage->_sqlt_version_ok); + unless (DBIx::Class::Optional::Dependencies->req_ok_for ('deploy')) { + $self->throw_exception("Unable to proceed without " . DBIx::Class::Optional::Dependencies->req_missing_for ('deploy') ); + } my $db_tr = SQL::Translator->new({ add_drop_table => 1, @@ -641,7 +639,7 @@ sub _create_db_to_schema_diff { $tr->parser->($tr, $$data); } - my $diff = SQL::Translator::Diff::schema_diff($db_tr->schema, $db, + 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 }); @@ -680,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, @@ -704,14 +702,17 @@ sub _read_sql_file { my $self = shift; my $file = shift || return; - my $fh; - open $fh, "<$file" or carp("Can't open upgrade file, $file ($!)"); - my @data = split(/\n/, join('', <$fh>)); - @data = grep(!/^--/, @data); - @data = split(/;/, join('', @data)); - close($fh); - @data = grep { $_ && $_ !~ /^-- / } @data; - @data = grep { $_ !~ /^(BEGIN|BEGIN TRANSACTION|COMMIT)/m } @data; + open my $fh, '<', $file or carp("Can't open upgrade file, $file ($!)"); + my @data = split /\n/, join '', <$fh>; + close $fh; + + @data = split /;/, + join '', + grep { $_ && + !/^--/ && + !/^(BEGIN|BEGIN TRANSACTION|COMMIT)/mi } + @data; + return \@data; } @@ -719,12 +720,12 @@ sub _source_exists { my ($self, $rs) = @_; - my $c = eval { - $rs->search({ 1, 0 })->count; + return try { + $rs->search(\'1=0')->cursor->next; + 1; + } catch { + 0; }; - return 0 if $@ || !defined $c; - - return 1; } 1;