From: Yuval Kogman Date: Tue, 20 Jan 2009 23:10:22 +0000 (+0000) Subject: Add sqlt_deploy_hook to Result Source X-Git-Tag: v0.08240~185 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=f89bb832611636e21c2f2fdde0dcda13b264442f;p=dbsrgits%2FDBIx-Class.git Add sqlt_deploy_hook to Result Source --- diff --git a/lib/DBIx/Class/ResultSource.pm b/lib/DBIx/Class/ResultSource.pm index 3946d7c..fd7da0f 100644 --- a/lib/DBIx/Class/ResultSource.pm +++ b/lib/DBIx/Class/ResultSource.pm @@ -13,7 +13,7 @@ use base qw/DBIx::Class/; __PACKAGE__->mk_group_accessors('simple' => qw/_ordered_columns _columns _primaries _unique_constraints name resultset_attributes schema from _relationships column_info_from_storage source_info - source_name/); + source_name sqlt_deploy_callback/); __PACKAGE__->mk_group_accessors('component_class' => qw/resultset_class result_class/); @@ -47,6 +47,7 @@ sub new { $new->{_relationships} = { %{$new->{_relationships}||{}} }; $new->{name} ||= "!!NAME NOT SET!!"; $new->{_columns_info_loaded} ||= 0; + $new->{sqlt_deploy_callback} ||= "default_sqlt_deploy_hook"; return $new; } @@ -1410,6 +1411,49 @@ should not be used. It will be removed before 1.0. __PACKAGE__->column_info_from_storage(1); +=cut + +=head2 sqlt_deploy_hook($sqlt_table) + +Triggers C. + +=cut + +sub sqlt_deploy_hook { + my $self = shift; + if ( my $hook = $self->sqlt_deploy_callback) { + $self->$hook(@_); + } +} + +=head2 default_sqlt_deploy_hook($table) + +Delegates to a an optional C method on the C. + +This will get passed the L object when you +deploy the schema via L or L. + +For an example of what you can do with this, see +L. + +=cut + +sub default_sqlt_deploy_hook { + my $self = shift; + + my $class = $self->result_class; + + if ($class and $class->can('sqlt_deploy_hook')) { + $class->sqlt_deploy_hook(@_); + } +} + +=head2 sqlt_deploy_callback + +An attribute which contains the callback to trigger on C. +Defaults to C. Can be a code reference or a method +name. + =head1 AUTHORS Matt S. Trout diff --git a/lib/SQL/Translator/Parser/DBIx/Class.pm b/lib/SQL/Translator/Parser/DBIx/Class.pm index 4a2af21..c43a4a6 100644 --- a/lib/SQL/Translator/Parser/DBIx/Class.pm +++ b/lib/SQL/Translator/Parser/DBIx/Class.pm @@ -214,9 +214,7 @@ sub parse { } } - if ($source->result_class->can('sqlt_deploy_hook')) { - $source->result_class->sqlt_deploy_hook($table); - } + $source->sqlt_deploy_hook($table) } if ($dbicschema->can('sqlt_deploy_hook')) { diff --git a/t/86sqlt.t b/t/86sqlt.t index ab76bbf..4b00fda 100644 --- a/t/86sqlt.t +++ b/t/86sqlt.t @@ -10,7 +10,7 @@ plan skip_all => 'SQL::Translator required' if $@; my $schema = DBICTest->init_schema; -plan tests => 131; +plan tests => 132; my $translator = SQL::Translator->new( parser_args => { @@ -26,6 +26,17 @@ my $translator = SQL::Translator->new( my $relinfo = $schema->source('Artist')->relationship_info ('cds'); local $relinfo->{attrs}{on_delete} = 'restrict'; + $schema->source('Track')->sqlt_deploy_callback(sub { + my ($self, $sqlt_table) = @_; + + if ($sqlt_table->schema->translator->producer_type =~ /SQLite$/ ) { + $sqlt_table->add_index( name => 'track_title', fields => ['title'] ) + or die $sqlt_table->error; + } + + $self->default_sqlt_deploy_hook($sqlt_table); + }); + $translator->parser('SQL::Translator::Parser::DBIx::Class'); $translator->producer('SQLite'); @@ -258,7 +269,12 @@ my %indexes = ( { 'fields' => ['name'] }, - ] + ], + track => [ + { + 'fields' => ['title'] + } + ], ); my $tschema = $translator->schema(); @@ -300,7 +316,6 @@ for my $expected_constraints (keys %unique_constraints) { for my $table_index (keys %indexes) { for my $expected_index ( @{ $indexes{$table_index} } ) { - ok ( get_index($table_index, $expected_index), "Got a matching index on $table_index table"); } }