From: Jess Robinson Date: Thu, 23 Nov 2006 23:33:42 +0000 (+0000) Subject: Add backup_directory, make tests cleanup X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=8795fefb31085d0e9d88c6d6dcf1d7a83e1d417a;p=dbsrgits%2FDBIx-Class-Historic.git Add backup_directory, make tests cleanup --- diff --git a/lib/DBIx/Class/Schema/Versioned.pm b/lib/DBIx/Class/Schema/Versioned.pm index 6ab73e4..91e78aa 100644 --- a/lib/DBIx/Class/Schema/Versioned.pm +++ b/lib/DBIx/Class/Schema/Versioned.pm @@ -47,6 +47,7 @@ use Data::Dumper; __PACKAGE__->mk_classdata('_filedata'); __PACKAGE__->mk_classdata('upgrade_directory'); +__PACKAGE__->mk_classdata('backup_directory'); sub on_connect { @@ -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 @@ -209,6 +210,7 @@ DBIx::Class::Versioning - DBIx::Class::Schema plugin for Schema upgrades __PACKAGE__->load_components(qw/+DBIx::Class::Schema::Versioned/); __PACKAGE__->upgrade_directory('/path/to/upgrades/'); + __PACKAGE__->backup_directory('/path/to/backups/'); sub backup { @@ -247,6 +249,11 @@ 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. @@ -283,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 diff --git a/lib/DBIx/Class/Storage/DBI/SQLite.pm b/lib/DBIx/Class/Storage/DBI/SQLite.pm index 1bbfd1d..02a3c51 100644 --- a/lib/DBIx/Class/Storage/DBI/SQLite.pm +++ b/lib/DBIx/Class/Storage/DBI/SQLite.pm @@ -15,7 +15,8 @@ sub _dbh_last_insert_id { sub backup { - my ($self) = @_; + my ($self, $dir) = @_; + $dir ||= './'; ## Where is the db file? my $dsn = $self->connect_info()->[0]; @@ -30,15 +31,18 @@ sub backup # print "Found database: $dbname\n"; # my $dbfile = file($dbname); - my ($vol, $dir, $file) = File::Spec->splitpath($dbname); + my ($vol, $dbdir, $file) = File::Spec->splitpath($dbname); # my $file = $dbfile->basename(); $file = strftime("%y%m%d%h%M%s", localtime()) . $file; $file = "B$file" while(-f $file); - - my $res = copy($dbname, $file); + + mkdir($dir) unless -f $dir; + my $backupfile = File::Spec->catfile($dir, $file); + + my $res = copy($dbname, $backupfile); $self->throw_exception("Backup failed! ($!)") if(!$res); - return $file; + return $backupfile; } 1; diff --git a/t/94versioning.t b/t/94versioning.t index 1c8026c..852d4fc 100644 --- a/t/94versioning.t +++ b/t/94versioning.t @@ -48,3 +48,10 @@ my $schema_upgrade = DBICVersion::Schema->connect("dbi:SQLite:$db_file"); $schema_upgrade->upgrade(); $tvrs = $schema_upgrade->resultset('Table'); is($schema_upgrade->exists($tvrs), 1, 'Upgraded schema from DDL file'); + +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 8718447..f92c3a5 100644 --- a/t/lib/DBICVersionNew.pm +++ b/t/lib/DBICVersionNew.pm @@ -37,10 +37,12 @@ our $VERSION = '2.0'; __PACKAGE__->register_class('Table', 'DBICVersion::Table'); __PACKAGE__->load_components('+DBIx::Class::Schema::Versioned'); +__PACKAGE__->upgrade_directory('t/var/'); +__PACKAGE__->backup_directory('t/var/backup/'); -sub upgrade_directory -{ - return 't/var/'; -} +#sub upgrade_directory +#{ +# return 't/var/'; +#} 1;