__PACKAGE__->mk_classdata('_filedata');
__PACKAGE__->mk_classdata('upgrade_directory');
__PACKAGE__->mk_classdata('backup_directory');
+__PACKAGE__->mk_classdata('do_backup');
sub schema_version {
my ($self) = @_;
my $fh;
open $fh, "<$file" or warn("Can't open upgrade file, $file ($!)");
- my @data = split(/;\n/, join('', <$fh>));
+ my @data = split(/[;\n]/, join('', <$fh>));
close($fh);
@data = grep { $_ && $_ !~ /^-- / } @data;
@data = grep { $_ !~ /^(BEGIN TRANACTION|COMMIT)/m } @data;
],
as => ['maxinstall'],
})->first;
- $pversion = $vtable->search({ Installed => $psearch->get_column('maxinstall'),
+ my $pversion = $vtable->search({ Installed => $psearch->get_column('maxinstall'),
})->first;
$pversion = $pversion->Version if($pversion);
return $pversion;
{
my ($self) = @_;
- $self->backup();
- $self->do_upgrade();
+ $self->backup() if($self->do_backup);
+
+ $self->txn_do(sub {
+ $self->do_upgrade();
+ });
my $vtable = $self->{vschema}->resultset('Table');
$vtable->create({ Version => $self->schema_version,
Installed => strftime("%Y-%m-%d %H:%M:%S", gmtime())
});
+
}
sub do_upgrade
use warnings;
use Test::More;
use File::Spec;
+use File::Copy;
BEGIN {
eval "use DBD::SQLite; use SQL::Translator 0.08;";
{ AutoCommit => 1 },
);
-unlink('t/var/DBICVersion-Schema-2.0-SQLite.sql');
-unlink('t/var/DBICVersion-Schema-1.0-2.0-SQLite.sql');
-$schema_new->create_ddl_dir('SQLite', undef, 't/var', '1.0');
-ok(-f 't/var/DBICVersion-Schema-1.0-2.0-SQLite.sql', 'Created DDL upgrade file');
-
## create new to pick up filedata for upgrade files we just made (on_connect)
-my $schema_upgrade = DBICVersion::Schema->connect(
- "dbi:SQLite:$db_file",
- undef,
- undef,
- { AutoCommit => 1 },
-);
-## do this here or let Versioned.pm do it?
-$schema_upgrade->upgrade();
-$tvrs = $schema_upgrade->resultset('Table');
-is($schema_upgrade->_source_exists($tvrs), 1, 'Upgraded schema from DDL file');
+# {
+# unlink('t/var/DBICVersion-Schema-1.0-2.0-SQLite.sql');
+# copy('t/var/DBICVersion-Schema-1.0-2.0-SQLite-erroneous.sql', 't/var/DBICVersion-Schema-1.0-2.0-SQLite.sql');
+
+# my $schema_upgrade = DBICVersion::Schema->connect(
+# "dbi:SQLite:$db_file",
+# undef,
+# undef,
+# { AutoCommit => 1 },
+# );
+
+
+# is($schema_upgrade->get_db_version(), '1.0', 'get_db_version ok');
+
+# eval {
+# # this will die with errors
+# $schema_upgrade->upgrade();
+# };
+# isnt($@, '', 'dodgy upgrade dies');
+
+# eval {
+# my @results = $schema_upgrade->storage->dbh->do('select VersionName from TestVersion');
+# };
+# is($@, '', 'partial upgrade properly rolledback');
+# is($schema_upgrade->get_db_version(), '1.0', 'db version number not upgraded');
+# }
+
+{
+ unlink('t/var/DBICVersion-Schema-2.0-SQLite.sql');
+ unlink('t/var/DBICVersion-Schema-1.0-2.0-SQLite.sql');
+
+# $schema_new->create_ddl_dir('SQLite', undef, 't/var', '1.0');
+# ok(-f 't/var/DBICVersion-Schema-1.0-2.0-SQLite.sql', 'Created DDL upgrade file');
+
+ copy('t/var/DBICVersion-Schema-1.0-2.0-SQLite-proper.sql', 't/var/DBICVersion-Schema-1.0-2.0-SQLite.sql');
+
+ my $schema_upgrade = DBICVersion::Schema->connect(
+ "dbi:SQLite:$db_file",
+ undef,
+ undef,
+ { AutoCommit => 1 },
+ );
+
+
+ is($schema_upgrade->get_db_version(), '1.0', 'get_db_version ok');
+
+ eval {
+ # this should be okay
+ $schema_upgrade->upgrade();
+ };
+ is($@, '', 'proper upgrade okay');
+ eval {
+ $schema_upgrade->storage->dbh->do('select NewVersionName from TestVersion');
+ };
+ is($@, '', 'new column created');
+ is($schema_upgrade->get_db_version(), '2.0', 'db version number successfully upgraded');
+}
+exit;
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');