__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/);
$new->{_relationships} = { %{$new->{_relationships}||{}} };
$new->{name} ||= "!!NAME NOT SET!!";
$new->{_columns_info_loaded} ||= 0;
+ $new->{sqlt_deploy_callback} ||= "default_sqlt_deploy_hook";
return $new;
}
__PACKAGE__->column_info_from_storage(1);
+=cut
+
+=head2 sqlt_deploy_hook($sqlt_table)
+
+Triggers C<sqlt_deploy_callback>.
+
+=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<sqlt_deploy_hook> method on the C<result_class>.
+
+This will get passed the L<SQL::Translator::Schema::Table> object when you
+deploy the schema via L</create_ddl_dir> or L</deploy>.
+
+For an example of what you can do with this, see
+L<DBIx::Class::Manual::Cookbook/Adding Indexes And Functions To Your SQL>.
+
+=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<sqlt_deploy_hook>.
+Defaults to C<default_sqlt_deploy_hook>. Can be a code reference or a method
+name.
+
=head1 AUTHORS
Matt S. Trout <mst@shadowcatsystems.co.uk>
my $schema = DBICTest->init_schema;
-plan tests => 131;
+plan tests => 132;
my $translator = SQL::Translator->new(
parser_args => {
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');
{
'fields' => ['name']
},
- ]
+ ],
+ track => [
+ {
+ 'fields' => ['title']
+ }
+ ],
);
my $tschema = $translator->schema();
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");
}
}