From: Luke Saunders Date: Fri, 1 Feb 2008 16:08:30 +0000 (+0000) Subject: DBIx::Class::Fixtures::Versioned created X-Git-Tag: v1.001002~51 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=9a9a783268273667dc1a9dd867a575aa927180c6;p=dbsrgits%2FDBIx-Class-Fixtures.git DBIx::Class::Fixtures::Versioned created --- diff --git a/lib/DBIx/Class/Fixtures.pm b/lib/DBIx/Class/Fixtures.pm index 954ca80..c4f5a5b 100644 --- a/lib/DBIx/Class/Fixtures.pm +++ b/lib/DBIx/Class/Fixtures.pm @@ -4,7 +4,6 @@ use strict; use warnings; use DBIx::Class::Exception; -use DBIx::Class::Fixtures::Schema; use Class::Accessor; use Path::Class qw(dir file); use Config::Any::JSON; @@ -18,7 +17,7 @@ use Data::Dumper; use base qw(Class::Accessor); -__PACKAGE__->mk_accessors(qw(config_dir _inherited_attributes debug)); +__PACKAGE__->mk_accessors(qw(config_dir _inherited_attributes debug schema_class )); =head1 VERSION @@ -311,9 +310,13 @@ sub _generate_schema { $self->msg("\ncreating schema"); # die 'must pass version param to generate_schema_from_ddl' unless $params->{version}; + my $schema_class = $self->schema_class || "DBIx::Class::Fixtures::SchemaVersioned"; + eval "require $schema_class"; + die $@ if $@; + my $pre_schema; my $connection_details = $params->{connection_details}; - unless( $pre_schema = DBIx::Class::Fixtures::Schema->connect(@{$connection_details}) ) { + unless( $pre_schema = $schema_class->connect(@{$connection_details}) ) { return DBIx::Class::Exception->throw('connection details not valid'); } my @tables = map { $pre_schema->source($_)->from }$pre_schema->sources; @@ -342,11 +345,7 @@ sub _generate_schema { # load schema object from our new DB $self->msg("- loading fresh DBIC object from DB"); - my $schema = DBIx::Class::Fixtures::Schema->connect(@{$connection_details}); - - # manually set the version then set DB version to it (upgrade) -# $Takkle::SchemaPopulate::VERSION = $params->{version}; -# $schema->upgrade(); # set version number + my $schema = $schema_class->connect(@{$connection_details}); return $schema; } @@ -362,7 +361,7 @@ sub populate { return DBIx::Class::Exception->throw($param . ' param not specified'); } } - my $fixture_dir = dir($params->{directory}); + my $fixture_dir = dir(delete $params->{directory}); unless (-e $fixture_dir) { return DBIx::Class::Exception->throw('fixture directory does not exist at ' . $fixture_dir); } @@ -370,7 +369,7 @@ sub populate { my $ddl_file; my $dbh; if ($params->{ddl} && $params->{connection_details}) { - $ddl_file = file($params->{ddl}); + $ddl_file = file(delete $params->{ddl}); unless (-e $ddl_file) { return DBIx::Class::Exception->throw('DDL does not exist at ' . $ddl_file); } @@ -383,7 +382,7 @@ sub populate { return DBIx::Class::Exception->throw('you must set the ddl and connection_details params'); } - my $schema = $self->_generate_schema({ ddl => $ddl_file, connection_details => $params->{connection_details} }); + my $schema = $self->_generate_schema({ ddl => $ddl_file, connection_details => delete $params->{connection_details}, %{$params} }); $self->msg("\nimporting fixtures"); my $tmp_fixture_dir = dir($fixture_dir, "-~populate~-" . $<); @@ -439,7 +438,9 @@ sub populate { sub msg { my $self = shift; my $subject = shift || return; - return unless $self->debug; + my $level = shift || 1; + + return unless $self->debug >= $level; if (ref $subject) { print Dumper($subject); } else { diff --git a/lib/DBIx/Class/Fixtures/Versioned.pm b/lib/DBIx/Class/Fixtures/Versioned.pm new file mode 100644 index 0000000..0142814 --- /dev/null +++ b/lib/DBIx/Class/Fixtures/Versioned.pm @@ -0,0 +1,61 @@ +package DBIx::Class::Fixtures::Versioned; + +use strict; +use warnings; + +use base qw/DBIx::Class::Fixtures/; +use Class::C3; + +=head1 VERSION + +Version 1.000 + +=cut + +our $VERSION = '1.000'; + +=head1 NAME + +=head1 SYNOPSIS + +=head1 DESCRIPTION + +=head1 AUTHOR + +=head1 CONTRIBUTORS + +=head1 METHODS + +=head2 new + +=cut + +sub populate { + my $self = shift; + my ($params) = @_; + + $self->schema_class("DBIx::Class::Fixtures::SchemaVersioned"); + unless ($params->{version}) { + return DBIx::Class::Exception->throw('You must pass a version to populate'); + } + + return $self->next::method(@_); +} + +sub _generate_schema { + my $self = shift; + my ($params) = @_; + + my $v = $self->schema_class; + # manually set the schema version + ${$v::VERSION} = $params->{version}; + + my $schema = $self->next::method(@_); + + # set the db version to the schema version + $schema->upgrade(); # set version number + + return $schema; +} + +1;