$self->ensure_class_loaded($self->schema_class);
$self->connect_info->[3]->{ignore_version} =1;
- #warn Dumper ($self->connect_info(), $self->connect_info->[3], {ignore_version => 1 });
return $self->schema_class->connect(@{$self->connect_info()} ); # , $self->connect_info->[3], { ignore_version => 1} );
}
);
has version => (
- is => 'ro',
+ is => 'rw',
isa => 'Str',
);
predicate => 'has_preversion',
);
+has force => (
+ is => 'rw',
+ isa => 'Bool',
+);
+
+has '_confirm' => (
+ is => 'ro',
+ isa => 'Bool',
+);
+
sub create {
my ($self, $sqlt_type, $sqlt_args) = @_;
if ($self->has_preversion) {
print "attempting to create diff file for ".$self->preversion."\n";
}
my $schema = $self->schema();
-# warn "running with params sqlt_type = $sqlt_type, version = " .$schema->schema_version . " sql_dir = " . $self->sql_dir . " preversion = " . ($self->has_preversion ? $self->preversion : "" ). "\n";
# create the dir if does not exist
$self->sql_dir->mkpath() if ( ! -d $self->sql_dir);
my $schema = $self->schema();
if (!$schema->get_db_version()) {
# schema is unversioned
- warn "could not determin current schema version, please either install or deploy";
+ die "could not determin current schema version, please either install or deploy";
} else {
$schema->upgrade();
}
}
sub install {
- my ($self) = @_;
+ my ($self, $version) = @_;
my $schema = $self->schema();
- if (!$schema->get_db_version()) {
+ $version ||= $self->version();
+ if (!$schema->get_db_version() ) {
# schema is unversioned
- print "Going to install schema version";
- $schema->install($self->version);
- } else {
- warn "schema already has a version not installing, try upgrade instead";
+ print "Going to install schema version\n";
+ my $ret = $schema->install($version);
+ print "retun is $ret\n";
+ }
+ elsif ($schema->get_db_version() and $self->force ) {
+ warn "forcing install may not be a good idea";
+ if($self->confirm() ) {
+ # FIXME private api
+ warn $version;
+ $self->schema->_set_db_version({ version => $version});
+ }
+ }
+ else {
+ die "schema already has a version not installing, try upgrade instead";
}
}
my $schema = $self->schema();
if (!$schema->get_db_version() ) {
# schema is unversioned
-# warn "going to deploy";
-# warn Dumper $schema->deployment_statements();
-
$schema->deploy( $args, $self->sql_dir)
or die "could not deploy schema";
} else {
- warn "there already is a database with a version here, try upgrade instead";
+ die "there already is a database with a version here, try upgrade instead";
}
}
}
sub confirm {
- print "Are you sure you want to do this? (type YES to confirm) ";
- my $response = <STDIN>;
+ my ($self) = @_;
+ print "Are you sure you want to do this? (type YES to confirm) ";
+ # mainly here for testing
+ return 1 if ($self->_confirm());
+ my $response = <STDIN>;
return 1 if ($response=~/^YES/);
return;
}
use DBICTest;
-my $sql_dir = dir($Bin,"var","sql");
+my $sql_dir = dir($Bin,"..","var");
{ # create the schema
$admin = DBIx::Class::Admin->new(
schema_class => 'DBICVersion::Schema',
- sql_dir => $sql_dir,
+ sql_dir => "t/var",
connect_info => $schema->storage->connect_info(),
);
-lives_ok { $admin->create($schema->storage->sqlt_type(), {add_drop_table=>0}); } 'Can create DBICVersionOrig sql in ' . $schema->storage->sqlt_type;
+$admin->preversion("1.0");
+lives_ok { $admin->create($schema->storage->sqlt_type(), ); } 'Can create diff for ' . $schema->storage->sqlt_type;
lives_ok {$admin->upgrade();} 'upgrade the schema';
-is($schema->get_db_version, $DBICVersion::Schema::VERSION, 'Schema deployed and versions match');
+is($schema->get_db_version, $DBICVersion::Schema::VERSION, 'Schema and db versions match');
+
+}
+
+{ # install
+
+clean_dir($sql_dir);
+
+my $schema = DBICTest->init_schema(
+ no_deploy=>1,
+ no_populate=>1,
+ sqlite_use_file => 1,
+ );
+my $admin = DBIx::Class::Admin->new(
+ schema_class => 'DBICVersion::Schema',
+ sql_dir => $sql_dir,
+ connect_info => $schema->storage->connect_info(),
+ _confirm => 1,
+);
+
+$admin->version("3.0");
+lives_ok { $admin->install(); } 'install schema version 3.0';
+is($admin->schema->get_db_version, "3.0", 'db thinks its version 3.0');
+dies_ok { $admin->install("4.0"); } 'cannot install to allready existing version';
+sleep 1;
+$admin->force(1);
+lives_ok { $admin->install("4.0"); } 'can force install to allready existing version';
+is($admin->schema->get_db_version, "4.0", 'db thinks its version 4.0');
+#clean_dir($sql_dir);
}
sub clean_dir {
my ($dir) =@_;
+ $dir = $dir->resolve;
+ if ( ! -d $dir ) {
+ $dir->mkpath();
+ }
foreach my $file ($dir->children) {
# skip any hidden files
next if ($file =~ /^\./);