[Dbix-class] Altering sources (was "regression" introduced in DBIC r5298..)

Toby Corkindale toby.corkindale at strategicdata.com.au
Fri Jun 12 02:16:04 GMT 2009


Matt S Trout wrote:
> On Tue, Jun 09, 2009 at 11:29:53AM +1000, Toby Corkindale wrote:
>> Matt S Trout wrote:
>>> Basically, you can't add relationships after the fact - if you can't set up
>>> your relationships before the source is registered with the schema then
>>> you'll need to re-register it afterwards.
>>>
>>> Slightly annoying side effect of the prototype inheritance style DBIC uses
>>> for schema and sources - I'm aware it's a tad annoying but things break
>>> subtly without it.
>> Is the correct way to re-register it this?
>>
>> My::Schema->register_extra_source(
>>     Users => My::Schema->class('Users')->new->result_source_instance
>> );
>>
>> (That seems to work at least.. trying to do register_class/source() 
>> again causes an error about it already existing)
> 
> ITYM warning, and since you're doing something weird you probably -do-
> want to overwrite.
> 
> Possibly what we need is a patch to allow a "yes, I meant to do this"
> option to squelch the warning ...

I wondered about a patch that added a function to DBIx/Class/Schema.pm 
to alter the source, like so?
Although Peter suggested this sort of thing probably wasn't to be 
encouraged; I'll put it out there as an option though..

---
  lib/DBIx/Class/Schema.pm |   22 ++++++++++++++++++++++
  1 files changed, 22 insertions(+), 0 deletions(-)

diff --git a/lib/DBIx/Class/Schema.pm b/lib/DBIx/Class/Schema.pm
index 4b945cc..17b2a8e 100644
--- a/lib/DBIx/Class/Schema.pm
+++ b/lib/DBIx/Class/Schema.pm
@@ -1295,6 +1295,28 @@ sub _unregister_source {
      }
  }

+=head2 alter_source
+
+=over 4
+
+=item Arguments: $moniker
+
+=back
+
+As L</register_source> but should be used if the result class already
+has a source and you want to replace it with an altered one. (Eg. If 
you have
+adjusted its metadata (incl. relationships) after registering it the 
first time.)
+
+=cut
+
+sub alter_source {
+    my ($self, $moniker) = @_;
+    my $class = $self->class($moniker);
+    $schema->_unregister_source($moniker);
+    $schema->register_source(
+        $moniker => $class->new->result_source_instance
+    );
+}

  =head2 compose_connection (DEPRECATED)

-- 
1.6.3.2



More information about the DBIx-Class mailing list