X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FDBIx%2FClass%2FSchema%2FVersioned.pm;h=91e78aa0eeb2caa16edd3e1b758b54d9a4590688;hb=8a89a03cad87b903e938825ec45b6b2c48db53c5;hp=7e892d7250f57d08727bfc0f3edfc89118905c29;hpb=a6b67aca7181168a16b162f586a1790259fdb098;p=dbsrgits%2FDBIx-Class.git diff --git a/lib/DBIx/Class/Schema/Versioned.pm b/lib/DBIx/Class/Schema/Versioned.pm index 7e892d7..91e78aa 100644 --- a/lib/DBIx/Class/Schema/Versioned.pm +++ b/lib/DBIx/Class/Schema/Versioned.pm @@ -37,30 +37,29 @@ __PACKAGE__->register_class('Table', 'DBIx::Class::Version::Table'); # --------------------------------------------------------------------------- -package DBIx::Class::Versioning; +package DBIx::Class::Schema::Versioned; use strict; use warnings; use base 'DBIx::Class'; use POSIX 'strftime'; use Data::Dumper; -# use DBIx::Class::Version; __PACKAGE__->mk_classdata('_filedata'); __PACKAGE__->mk_classdata('upgrade_directory'); +__PACKAGE__->mk_classdata('backup_directory'); sub on_connect { my ($self) = @_; -# print "on_connect\n"; my $vschema = DBIx::Class::Version->connect(@{$self->storage->connect_info()}); my $vtable = $vschema->resultset('Table'); my $pversion; + if(!$self->exists($vtable)) { -# print "deploying.. \n"; - $vschema->storage->debug(1); -# print "Debugging is: ", $vschema->storage->debug, "\n"; +# $vschema->storage->debug(1); + $vschema->storage->ensure_connected(); $vschema->deploy(); $pversion = 0; } @@ -83,21 +82,22 @@ sub on_connect return 1; } - - $vtable->create({ Version => $self->VERSION, - Installed => strftime("%Y-%m-%d %H:%M:%S", gmtime()) - }); +## use IC::DT? if(!$pversion) { - warn "No previous version found, skipping upgrade\n"; + $vtable->create({ Version => $self->VERSION, + Installed => strftime("%Y-%m-%d %H:%M:%S", gmtime()) + }); + ## If we let the user do this, where does the Version table get updated? + warn "No previous version found, calling deploy to install this version.\n"; + $self->deploy(); return 1; } -# $self->create_upgrades($self->upgrade_directoy, $pversion, $self->VERSION); - - my $file = $self->ddl_filename($self->upgrade_directory, + my $file = $self->ddl_filename( $self->storage->sqlt_type, + $self->upgrade_directory, $self->VERSION ); if(!$file) @@ -106,42 +106,43 @@ sub on_connect return 1; } - $file =~ s/@{[ $self->VERSION ]}/"${pversion}-" . $self->VERSION/e; + $file = $self->ddl_filename( + $self->storage->sqlt_type, + $self->upgrade_directory, + $self->VERSION, + $pversion, + ); +# $file =~ s/@{[ $self->VERSION ]}/"${pversion}-" . $self->VERSION/e; if(!-f $file) { warn "Upgrade not possible, no upgrade file found ($file)\n"; return; } -# print "Found Upgrade file: $file\n"; + my $fh; open $fh, "<$file" or warn("Can't open upgrade file, $file ($!)"); my @data = split(/;\n/, join('', <$fh>)); close($fh); @data = grep { $_ && $_ !~ /^-- / } @data; @data = grep { $_ !~ /^(BEGIN TRANACTION|COMMIT)/m } @data; -# print "Commands: ", join("\n", @data), "\n"; + $self->_filedata(\@data); - $self->backup(); - $self->upgrade(); - -# X Create version table if not exists? -# Make backup -# Run create statements -# Run post-create callback -# Run alter/drop statement -# Run post-alter callback + ## Don't do this yet, do only on command? + ## If we do this later, where does the Version table get updated?? + warn "Versions out of sync. This is " . $self->VERSION . + ", your database contains version $pversion, please call upgrade on your Schema.\n"; +# $self->upgrade($pversion, $self->VERSION); } sub exists { my ($self, $rs) = @_; - eval { + my $c = eval { $rs->search({ 1, 0 })->count; }; - - return 0 if $@; + return 0 if $@ || !defined $c; return 1; } @@ -150,7 +151,7 @@ sub backup { my ($self) = @_; ## Make each ::DBI::Foo do this -# $self->storage->backup(); + $self->storage->backup($self->backup_directory()); } sub upgrade @@ -159,12 +160,20 @@ sub upgrade ## overridable sub, per default just run all the commands. + $self->backup(); + $self->run_upgrade(qr/create/i); $self->run_upgrade(qr/alter table .*? add/i); $self->run_upgrade(qr/alter table .*? (?!drop)/i); $self->run_upgrade(qr/alter table .*? drop/i); $self->run_upgrade(qr/drop/i); - $self->run_upgrade(qr//i); +# $self->run_upgrade(qr//i); + + my $vschema = DBIx::Class::Version->connect(@{$self->storage->connect_info()}); + my $vtable = $vschema->resultset('Table'); + $vtable->create({ Version => $self->VERSION, + Installed => strftime("%Y-%m-%d %H:%M:%S", gmtime()) + }); } @@ -186,6 +195,8 @@ sub run_upgrade return 1; } +1; + =head1 NAME DBIx::Class::Versioning - DBIx::Class::Schema plugin for Schema upgrades @@ -197,8 +208,9 @@ DBIx::Class::Versioning - DBIx::Class::Schema plugin for Schema upgrades # load Library::Schema::CD, Library::Schema::Book, Library::Schema::DVD __PACKAGE__->load_classes(qw/CD Book DVD/); - __PACKAGE__->load_components(qw/Versioning/); + __PACKAGE__->load_components(qw/+DBIx::Class::Schema::Versioned/); __PACKAGE__->upgrade_directory('/path/to/upgrades/'); + __PACKAGE__->backup_directory('/path/to/backups/'); sub backup { @@ -237,6 +249,14 @@ in L. Return a false value if there is no upgrade path between the two versions supplied. By default, every change in your VERSION is regarded as needing an upgrade. +The actual upgrade is called manually by calling C on your +schema object. Code is run at connect time to determine whether an +upgrade is needed, if so, a warning "Versions out of sync" is +produced. + +NB: At the moment, SQLite upgrading is rather spotty, as SQL::Translator::Diff +returns SQL statements that SQLite does not support. + =head1 METHODS @@ -247,6 +267,11 @@ allow you to make a backup of the database. Per default this method attempts to call C<< $self->storage->backup >>, to run the standard backup on each database type. +This method should return the name of the backup file, if appropriate. + +C is called from C, make sure you call it, if you write your +own method. + =head2 upgrade This is an overwritable method used to run your upgrade. The freeform method @@ -265,6 +290,14 @@ idea is that this method can be called any number of times from your C method, running whichever commands you specify via the regex in the parameter. +=head2 upgrade_directory + +Use this to set the directory your upgrade files are stored in. + +=head2 backup_directory + +Use this to set the directory you want your backups stored in. + =head1 AUTHOR Jess Robinson