From: Ash Berlin Date: Sat, 24 Nov 2007 22:17:41 +0000 (+0000) Subject: Change diffing code to use $sqlt_schema. Sort tables in parser X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=b7e303a8b3f93ecfbb959b2c7504b2b1cc707cac;p=dbsrgits%2FDBIx-Class-Historic.git Change diffing code to use $sqlt_schema. Sort tables in parser --- diff --git a/lib/DBIx/Class/Schema/Versioned.pm b/lib/DBIx/Class/Schema/Versioned.pm index e5493ce..99e2e5a 100644 --- a/lib/DBIx/Class/Schema/Versioned.pm +++ b/lib/DBIx/Class/Schema/Versioned.pm @@ -6,16 +6,16 @@ use warnings; __PACKAGE__->load_components(qw/ Core/); __PACKAGE__->table('SchemaVersions'); -__PACKAGE__->add_columns - ( 'Version' => { - 'data_type' => 'VARCHAR', - 'is_auto_increment' => 0, - 'default_value' => undef, - 'is_foreign_key' => 0, - 'name' => 'Version', - 'is_nullable' => 0, - 'size' => '10' - }, +__PACKAGE__->add_columns( + 'Version' => { + 'data_type' => 'VARCHAR', + 'is_auto_increment' => 0, + 'default_value' => undef, + 'is_foreign_key' => 0, + 'name' => 'Version', + 'is_nullable' => 0, + 'size' => '10' + }, 'Installed' => { 'data_type' => 'VARCHAR', 'is_auto_increment' => 0, @@ -24,8 +24,8 @@ __PACKAGE__->add_columns 'name' => 'Installed', 'is_nullable' => 0, 'size' => '20' - }, - ); + }, +); __PACKAGE__->set_primary_key('Version'); package DBIx::Class::Version; @@ -143,6 +143,7 @@ sub backup $self->storage->backup($self->backup_directory()); } +# TODO: some of this needs to be merged with ->create_ddl_dir sub upgrade { my ($self) = @_; diff --git a/lib/DBIx/Class/Storage/DBI.pm b/lib/DBIx/Class/Storage/DBI.pm index 80fce38..261b267 100644 --- a/lib/DBIx/Class/Storage/DBI.pm +++ b/lib/DBIx/Class/Storage/DBI.pm @@ -1334,16 +1334,17 @@ sub create_ddl_dir if !$self->_check_sqlt_version; my $sqlt = SQL::Translator->new({ -# debug => 1, add_drop_table => 1, }); + + $sqlt->parser('SQL::Translator::Parser::DBIx::Class'); + my $sqlt_schema = $sqlt->translate({ data => $schema }) or die $sqlt->error; + foreach my $db (@$databases) { $sqlt->reset(); - $sqlt->parser('SQL::Translator::Parser::DBIx::Class'); -# $sqlt->parser_args({'DBIx::Class' => $schema); $sqlt = $self->configure_sqlt($sqlt, $db); - $sqlt->data($schema); + $sqlt->{schema} = $sqlt_schema; $sqlt->producer($db); my $file; @@ -1378,34 +1379,23 @@ sub create_ddl_dir warn("No previous schema file found ($prefilename)"); next; } - #### We need to reparse the SQLite file we just wrote, so that - ## Diff doesnt get all confoosed, and Diff is *very* confused. - ## FIXME: rip Diff to pieces! -# my $target_schema = $sqlt->schema; -# unless ( $target_schema->name ) { -# $target_schema->name( $filename ); -# } - my @input; - push @input, {file => $prefilename, parser => $db}; - push @input, {file => $filename, parser => $db}; - my ( $source_schema, $source_db, $target_schema, $target_db ) = map { - my $file = $_->{'file'}; - my $parser = $_->{'parser'}; + my $source_schema; + { my $t = SQL::Translator->new; + $t->parser_args(no_default_sizes => 1); $t->debug( 0 ); $t->trace( 0 ); - $t->parser( $parser ) or die $t->error; - my $out = $t->translate( $file ) or die $t->error; - my $schema = $t->schema; - unless ( $schema->name ) { - $schema->name( $file ); + $t->parser( $db ) or die $t->error; + my $out = $t->translate( $prefilename ) or die $t->error; + $source_schema = $t->schema; + unless ( $source_schema->name ) { + $source_schema->name( $prefilename ); } - ($schema, $parser); - } @input; + } my $diff = SQL::Translator::Diff::schema_diff($source_schema, $db, - $target_schema, $db, + $sqlt_schema, $db, {} ); my $difffile = $schema->ddl_filename($db, $dir, $version, $preversion); @@ -1563,9 +1553,9 @@ sub build_datetime_parser { my $_check_sqlt_message; # private sub _check_sqlt_version { return $_check_sqlt_version if defined $_check_sqlt_version; - eval 'use SQL::Translator 0.08'; - $_check_sqlt_message = $@ ? $@ : ''; - $_check_sqlt_version = $@ ? 0 : 1; + eval 'use SQL::Translator "0.08"'; + $_check_sqlt_message = $@ || ''; + $_check_sqlt_version = !$@; } sub _check_sqlt_message { diff --git a/lib/SQL/Translator/Parser/DBIx/Class.pm b/lib/SQL/Translator/Parser/DBIx/Class.pm index 1433a52..ec6b9a8 100644 --- a/lib/SQL/Translator/Parser/DBIx/Class.pm +++ b/lib/SQL/Translator/Parser/DBIx/Class.pm @@ -9,9 +9,8 @@ package # hide from PAUSE use strict; use warnings; -use vars qw($DEBUG $VERSION @EXPORT_OK); +use vars qw($DEBUG @EXPORT_OK); $DEBUG = 0 unless defined $DEBUG; -$VERSION = sprintf "%d.%02d", q$Revision 1.0$ =~ /(\d+)\.(\d+)/; use Exporter; use Data::Dumper; @@ -30,26 +29,25 @@ use base qw(Exporter); sub parse { my ($tr, $data) = @_; my $args = $tr->parser_args; - my $dbixschema = $args->{'DBIx::Schema'} || $data; - $dbixschema ||= $args->{'package'}; + my $dbicschema = $args->{'DBIx::Class::Schema'} || $args->{"DBIx::Schema"} ||$data; + $dbicschema ||= $args->{'package'}; my $limit_sources = $args->{'sources'}; - die 'No DBIx::Schema' unless ($dbixschema); - if (!ref $dbixschema) { - eval "use $dbixschema;"; - die "Can't load $dbixschema ($@)" if($@); + die 'No DBIx::Class::Schema' unless ($dbicschema); + if (!ref $dbicschema) { + eval "use $dbicschema;"; + die "Can't load $dbicschema ($@)" if($@); } my $schema = $tr->schema; my $table_no = 0; -# print Dumper($dbixschema->registered_classes); - - #foreach my $tableclass ($dbixschema->registered_classes) + $schema->name( ref($dbicschema) . " v" . ($dbicschema->VERSION || '1.x')) + unless ($schema->name); my %seen_tables; - my @monikers = $dbixschema->sources; + my @monikers = sort $dbicschema->sources; if ($limit_sources) { my $ref = ref $limit_sources || ''; die "'sources' parameter must be an array or hash ref" unless $ref eq 'ARRAY' || ref eq 'HASH'; @@ -67,8 +65,9 @@ sub parse { foreach my $moniker (sort @monikers) { - my $source = $dbixschema->source($moniker); + my $source = $dbicschema->source($moniker); + # Its possible to have multiple DBIC source using same table next if $seen_tables{$source->name}++; my $table = $schema->add_table( @@ -104,7 +103,7 @@ sub parse { ); } my %unique_constraints = $source->unique_constraints; - foreach my $uniq (keys %unique_constraints) { + foreach my $uniq (sort keys %unique_constraints) { if (!$source->compare_relationship_keys($unique_constraints{$uniq}, \@primary)) { $table->add_constraint( type => 'unique', @@ -113,6 +112,7 @@ sub parse { ); my $index = $table->add_index( + # TODO: Pick a better than that wont conflict name => $unique_constraints{$uniq}->[0], fields => $unique_constraints{$uniq}, type => 'NORMAL', @@ -197,8 +197,8 @@ sub parse { } } - if ($dbixschema->can('sqlt_deploy_hook')) { - $dbixschema->sqlt_deploy_hook($schema); + if ($dbicschema->can('sqlt_deploy_hook')) { + $dbicschema->sqlt_deploy_hook($schema); } return 1;